1package com.gams.examples.interrupt;
3import java.awt.ComponentOrientation;
4import java.awt.Dimension;
6import java.awt.GridBagConstraints;
7import java.awt.GridBagLayout;
9import java.awt.event.ActionEvent;
10import java.awt.event.ActionListener;
12import java.io.IOException;
13import java.io.OutputStream;
14import java.io.PrintStream;
16import javax.swing.JButton;
17import javax.swing.JFrame;
18import javax.swing.JLabel;
19import javax.swing.JScrollPane;
20import javax.swing.JTextArea;
21import javax.swing.SwingUtilities;
35public class InterruptGUI extends JFrame implements ActionListener {
36 private final GridBagConstraints constraints;
37 private final JLabel headsLabel;
38 private final JTextArea logTextArea;
39 private final JScrollPane logScrollPanel;
40 private final JButton runButton, stopButton, exitButton;
44 private final PrintStream printStream;
45 private Worker worker;
47 public static void main(String[] args) {
48 SwingUtilities.invokeLater(
new Runnable() {
56 super(
"GAMS Java API - Interrupt GUI Example");
57 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
59 getContentPane().setLayout(
new GridBagLayout());
60 getContentPane().setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
62 constraints =
new GridBagConstraints();
63 constraints.insets =
new Insets(5, 10, 5, 10);
64 constraints.anchor = GridBagConstraints.NORTHWEST;
65 constraints.weightx = 1;
68 headsLabel =
new JLabel(
"GAMS Log -- running [circpack] model from GAMS Model Library");
69 constraints.fill = GridBagConstraints.HORIZONTAL;
70 constraints.gridx = 0;
71 constraints.gridy = 0;
72 getContentPane().add(headsLabel, constraints);
75 logTextArea =
new JTextArea(40, 140);
76 logTextArea.setLineWrap(
true);
77 logTextArea.setWrapStyleWord(
true);
78 logTextArea.setEditable(
false);
79 Font font =
new Font(
"Monospaced", Font.PLAIN, 12 );
80 logTextArea.setFont( font );
83 printStream =
new PrintStream(
new LogOutputStream(logTextArea));
84 System.setOut(printStream);
85 System.setErr(printStream);
87 logScrollPanel =
new JScrollPane(logTextArea);
88 logScrollPanel.setMinimumSize(
new Dimension(800, 500));
89 logScrollPanel.setAutoscrolls(
true);
90 constraints.fill = GridBagConstraints.HORIZONTAL;
91 constraints.gridx = 0;
92 constraints.gridy = 1;
93 constraints.gridwidth = 4;
94 getContentPane().add(logScrollPanel, constraints);
97 runButton = makeButton(
"Run");
98 constraints.fill = GridBagConstraints.HORIZONTAL;
99 constraints.gridx = 0;
100 constraints.gridy = 2;
101 constraints.gridwidth = 1;
102 getContentPane().add(runButton, constraints);
104 stopButton = makeButton(
"Stop");
105 stopButton.setEnabled(
false);
106 constraints.fill = GridBagConstraints.HORIZONTAL;
107 constraints.gridx = 2;
108 constraints.gridy = 2;
109 constraints.gridwidth = 1;
110 getContentPane().add(stopButton, constraints);
112 exitButton = makeButton(
"Exit");
113 constraints.fill = GridBagConstraints.HORIZONTAL;
114 constraints.gridx = 3;
115 constraints.gridy = 2;
116 constraints.gridwidth = 1;
117 getContentPane().add(exitButton, constraints);
121 File workingDirectory =
new File(System.getProperty(
"user.dir"),
"InterruptGUI");
122 workingDirectory.mkdir();
136 private JButton makeButton(String caption) {
137 JButton b =
new JButton(caption);
138 b.setActionCommand(caption);
139 b.addActionListener(
this);
140 getContentPane().add(b, constraints);
145 public void actionPerformed(ActionEvent e) {
147 if (
"Run" == e.getActionCommand()) {
148 runButton.setEnabled(
false);
149 stopButton.setEnabled(
true);
150 if ((worker ==
null) || (worker.getState()==Thread.State.TERMINATED))
151 worker =
new Worker(job, opt, printStream,
this);
153 }
else if (
"Stop" == e.getActionCommand()) {
154 runButton.setEnabled(
true);
155 stopButton.setEnabled(
false);
157 }
else if (
"Exit" == e.getActionCommand()) {
158 System.out.println(
"Closing Interrupt GUI Example...");
159 if (worker !=
null) {
160 if (!worker.interrupted || (worker.getState() != Thread.State.TERMINATED))
167 private void reportJobTerminated() {
168 runButton.setEnabled(
true);
169 stopButton.setEnabled(
false);
173 static class Worker
extends Thread {
197 job.
run(option, output);
198 }
catch(Exception e) {
201 caller.reportJobTerminated();
206 public void interrupt() {
207 if (getState() != Thread.State.TERMINATED)
214class LogOutputStream
extends OutputStream {
215 private JTextArea textArea;
217 public LogOutputStream(JTextArea textArea) {
218 this.textArea = textArea;
222 public void write(
int b)
throws IOException {
223 textArea.append(String.valueOf((
char)b));
224 textArea.setCaretPosition(textArea.getDocument().getLength());
void setAllModelTypes(String value)
void setWorkingDirectory(String directory)
GAMSJob addJobFromGamsLib(String modelName)
This small example demonstrates how to run a GAMS model in a graphical user interface.
Provides package namespace for Java interface and examples to General Algebraic Model System (GAMS).