-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
executable file
·387 lines (277 loc) · 7.9 KB
/
GUI.java
File metadata and controls
executable file
·387 lines (277 loc) · 7.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import javax.swing.JFileChooser;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.File;
import java.awt.BorderLayout;
import javax.swing.JToolBar;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class GUI {
JTextArea ta;
JTextArea initializationTa;
//JButton open;
JButton save;
JButton run;
JButton open;
JButton export;
JButton view;
JFrame window;
String assignmentText = null;
File file = null;
public GUI(String filename) {
this();
load(filename);
}
public GUI() {
window = new JFrame();
window.setTitle("IBScript");
window.setSize(500,500);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new BorderLayout());
ta = new JTextArea();
ta.setTabSize(2);
initializationTa = new JTextArea();
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.addTab("Code", new JScrollPane(ta));
tabbedPane.addTab("Initialization", new JScrollPane(initializationTa));
window.add(tabbedPane, BorderLayout.CENTER);
JToolBar tb = new JToolBar();
open = new JButton("Open");
open.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(file != null) save();
JFileChooser selector = new JFileChooser();
selector.setFileFilter(
new FileFilter() {
public boolean accept(File file) {
if(file.getName().endsWith(".ib") || file.getName().endsWith(".ibasg") || file.isDirectory()) {
return true;
} else {
return false;
}
}
public String getDescription() {
return "IBScript files";
}
}
);
selector.setCurrentDirectory(new File("."));
int fileRtn = selector.showOpenDialog(window);
if(fileRtn == JFileChooser.APPROVE_OPTION) {
file = selector.getSelectedFile();
}
load();
}
}
);
save = new JButton("Save");
save.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
save();
}
}
);
run = new JButton("Run");
run.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
save();
run();
}
}
);
export = new JButton("Export as Assignment");
export.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
File oldFile = file;
saveAs(".ibasg");
file = oldFile;
}
}
);
view = new JButton("View Assignment Details");
view.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(assignmentText != null) {
JOptionPane.showMessageDialog(window, assignmentText);
}
}
}
);
tb.add(open);
tb.add(save);
tb.add(run);
tb.add(export);
tb.add(view);
window.add(tb, BorderLayout.NORTH);
window.setVisible(true);
}
public static void main(String args[]) {
if(args.length < 1) {
new GUI();
} else {
new GUI(args[0]);
}
}
int evaluate(String ss, HashMap<String, Integer> variables) {
String s = ss.trim();
try {
int a = Integer.parseInt(s);
System.out.println(a);
return a;
} catch(NumberFormatException e) {
//return variables.get(s);
System.out.println(variables);
return 1;
}
}
public void run() {
JFrame f = new JFrame();
f.setSize(500,500);
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JTextArea out = new JTextArea();
out.setTabSize(2);
out.setEditable(false);
String s = ta.getText();
StringTokenizer stk = new StringTokenizer(s, "\n");
String execs[] = new String[stk.countTokens()];
f.add(new JScrollPane(out));
f.setVisible(true);
try {
Process proc;
if(System.getProperty("os.name").contains("Windows")) {
proc = Runtime.getRuntime().exec("ibscript.exe " + file.getCanonicalPath());
} else {
proc = Runtime.getRuntime().exec("./ibscript " + file.getCanonicalPath());
}
BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedReader err = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
String str = null;
Threader threader = new Threader(out, in, err);
(new Thread(threader)).start();
f.addWindowListener(
new WindowAdapter() {
public void windowClosed(WindowEvent e) {
threader.stopThreader();
}
}
);
} catch(IOException e) {
System.out.println(e);
}
}
public void load(String filename) {
file = new File(filename);
load();
}
private void load() {
ta.setText("");
window.setTitle(file.getName());
if(file.getName().contains(".ibasg")) {
try {
Scanner r = new Scanner(file);
while(r.hasNextLine()) {
String s = r.nextLine();
if(s.equals("//END ASSIGNMENT")) {
assignmentText = initializationTa.getText();
initializationTa.setText("");
} else {
initializationTa.append(s + "\n");
}
}
ta.setText("");
r.close();
file = new File(file.getName().replace(".ibasg", ".ib"));
} catch(IOException e) {
}
} else {
try {
Scanner r = new Scanner(file);
while(r.hasNextLine()) {
String s = r.nextLine();
if(s.equals("//END INITIALIZATION")) {
initializationTa.setText(ta.getText());
ta.setText("");
} else {
ta.append(s + "\n");
}
}
assignmentText = null;
r.close();
} catch(IOException e) {
JOptionPane.showMessageDialog(window, "Invalid IBScript file", "Error", JOptionPane.ERROR_MESSAGE);
}
}
}
public void saveAs(String extension) {
JFileChooser selector = new JFileChooser();
selector.setFileFilter(
new FileFilter() {
public boolean accept(File file) {
if(file.getName().endsWith(extension) || file.isDirectory()) {
return true;
} else {
return false;
}
}
public String getDescription() {
return "IBScript files";
}
}
);
selector.setCurrentDirectory(new File("."));
int fileRtn = selector.showSaveDialog(window);
if(fileRtn == JFileChooser.APPROVE_OPTION) {
file = selector.getSelectedFile();
save();
}
}
public void save() {
if(file == null) {
saveAs(".ib");
return;
}
if(file.getName().contains(".ibasg")) {
try {
PrintWriter pw = new PrintWriter(file);
String description = JOptionPane.showInputDialog(window, "Describe the assignment");
pw.println(description);
pw.println("//END ASSIGNMENT");
String s = initializationTa.getText();
pw.println(s);
pw.close();
} catch(IOException exc) {
}
} else {
try {
PrintWriter pw = new PrintWriter(file);
String s = initializationTa.getText();
pw.println(s);
pw.println("//END INITIALIZATION");
s = ta.getText();
pw.println(s);
pw.close();
} catch(IOException exc) {
}
}
}
}