forked from OpenIntegrationEngine/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwingGui.java
More file actions
3717 lines (3383 loc) · 110 KB
/
SwingGui.java
File metadata and controls
3717 lines (3383 loc) · 110 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: java; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.javascript.tools.debugger;
import java.awt.AWTEvent;
import java.awt.ActiveEvent;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Event;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.MenuComponent;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.Reader;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.EventObject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JToolBar;
import javax.swing.JTree;
import javax.swing.JViewport;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;
import javax.swing.event.TreeModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Segment;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreePath;
import org.mozilla.javascript.Kit;
import org.mozilla.javascript.SecurityUtilities;
import org.mozilla.javascript.tools.debugger.treetable.JTreeTable;
import org.mozilla.javascript.tools.debugger.treetable.TreeTableModel;
import org.mozilla.javascript.tools.debugger.treetable.TreeTableModelAdapter;
import org.mozilla.javascript.tools.shell.ConsoleTextArea;
/**
* GUI for the Rhino debugger.
*/
public class SwingGui extends JFrame implements GuiCallback {
/**
* Serializable magic number.
*/
private static final long serialVersionUID = -8217029773456711621L;
/**
* The debugger.
*/
Dim dim;
/**
* The action to run when the 'Exit' menu item is chosen or the
* frame is closed.
*/
protected Runnable exitAction;
/**
* The {@link JDesktopPane} that holds the script windows.
*/
private JDesktopPane desk;
/**
* The {@link JPanel} that shows information about the context.
*/
protected ContextWindow context;
/**
* The menu bar.
*/
private Menubar menubar;
/**
* The tool bar.
*/
private JToolBar toolBar;
/**
* The console that displays I/O from the script.
*/
private JSInternalConsole console;
/**
* The {@link JSplitPane} that separates {@link #desk} from
* {@link org.mozilla.javascript.Context}.
*/
private JSplitPane split1;
/**
* The status bar.
*/
protected JLabel statusBar;
/**
* Hash table of internal frame names to the internal frames themselves.
*/
private final Map<String,JFrame> toplevels =
Collections.synchronizedMap(new HashMap<String,JFrame>());
/**
* Hash table of script URLs to their internal frames.
*/
private final Map<String,FileWindow> fileWindows =
Collections.synchronizedMap(new TreeMap<String,FileWindow>());
/**
* The {@link FileWindow} that last had the focus.
*/
private FileWindow currentWindow;
/**
* File choose dialog for loading a script.
*/
JFileChooser dlg;
/**
* The AWT EventQueue. Used for manually pumping AWT events from
* {@link #dispatchNextGuiEvent()}.
*/
private EventQueue awtEventQueue;
/**
* Creates a new SwingGui.
*/
public SwingGui(Dim dim, String title) {
super(title);
this.dim = dim;
init();
dim.setGuiCallback(this);
}
/**
* Returns the Menubar of this debugger frame.
*/
public Menubar getMenubar() {
return menubar;
}
/**
* Sets the {@link Runnable} that will be run when the "Exit" menu
* item is chosen.
*/
public void setExitAction(Runnable r) {
exitAction = r;
}
/**
* Returns the debugger console component.
*/
public JSInternalConsole getConsole() {
return console;
}
/**
* Sets the visibility of the debugger GUI.
*/
@Override
public void setVisible(boolean b) {
super.setVisible(b);
if (b) {
// this needs to be done after the window is visible
console.consoleTextArea.requestFocus();
context.split.setDividerLocation(0.5);
try {
console.setMaximum(true);
console.setSelected(true);
console.show();
console.consoleTextArea.requestFocus();
} catch (Exception exc) {
}
}
}
/**
* Records a new internal frame.
*/
void addTopLevel(String key, JFrame frame) {
if (frame != this) {
toplevels.put(key, frame);
}
}
/**
* Constructs the debugger GUI.
*/
private void init() {
menubar = new Menubar(this);
setJMenuBar(menubar);
toolBar = new JToolBar();
JButton button;
JButton breakButton, goButton, stepIntoButton,
stepOverButton, stepOutButton;
String [] toolTips = {"Break (Pause)",
"Go (F5)",
"Step Into (F11)",
"Step Over (F7)",
"Step Out (F8)"};
int count = 0;
button = breakButton = new JButton("Break");
button.setToolTipText("Break");
button.setActionCommand("Break");
button.addActionListener(menubar);
button.setEnabled(true);
button.setToolTipText(toolTips[count++]);
button = goButton = new JButton("Go");
button.setToolTipText("Go");
button.setActionCommand("Go");
button.addActionListener(menubar);
button.setEnabled(false);
button.setToolTipText(toolTips[count++]);
button = stepIntoButton = new JButton("Step Into");
button.setToolTipText("Step Into");
button.setActionCommand("Step Into");
button.addActionListener(menubar);
button.setEnabled(false);
button.setToolTipText(toolTips[count++]);
button = stepOverButton = new JButton("Step Over");
button.setToolTipText("Step Over");
button.setActionCommand("Step Over");
button.setEnabled(false);
button.addActionListener(menubar);
button.setToolTipText(toolTips[count++]);
button = stepOutButton = new JButton("Step Out");
button.setToolTipText("Step Out");
button.setActionCommand("Step Out");
button.setEnabled(false);
button.addActionListener(menubar);
button.setToolTipText(toolTips[count++]);
Dimension dim = stepOverButton.getPreferredSize();
breakButton.setPreferredSize(dim);
breakButton.setMinimumSize(dim);
breakButton.setMaximumSize(dim);
breakButton.setSize(dim);
goButton.setPreferredSize(dim);
goButton.setMinimumSize(dim);
goButton.setMaximumSize(dim);
stepIntoButton.setPreferredSize(dim);
stepIntoButton.setMinimumSize(dim);
stepIntoButton.setMaximumSize(dim);
stepOverButton.setPreferredSize(dim);
stepOverButton.setMinimumSize(dim);
stepOverButton.setMaximumSize(dim);
stepOutButton.setPreferredSize(dim);
stepOutButton.setMinimumSize(dim);
stepOutButton.setMaximumSize(dim);
toolBar.add(breakButton);
toolBar.add(goButton);
toolBar.add(stepIntoButton);
toolBar.add(stepOverButton);
toolBar.add(stepOutButton);
JPanel contentPane = new JPanel();
contentPane.setLayout(new BorderLayout());
getContentPane().add(toolBar, BorderLayout.NORTH);
getContentPane().add(contentPane, BorderLayout.CENTER);
desk = new JDesktopPane();
desk.setPreferredSize(new Dimension(600, 300));
desk.setMinimumSize(new Dimension(150, 50));
desk.add(console = new JSInternalConsole("JavaScript Console"));
context = new ContextWindow(this);
context.setPreferredSize(new Dimension(600, 120));
context.setMinimumSize(new Dimension(50, 50));
split1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, desk,
context);
split1.setOneTouchExpandable(true);
SwingGui.setResizeWeight(split1, 0.66);
contentPane.add(split1, BorderLayout.CENTER);
statusBar = new JLabel();
statusBar.setText("Thread: ");
contentPane.add(statusBar, BorderLayout.SOUTH);
dlg = new JFileChooser();
javax.swing.filechooser.FileFilter filter =
new javax.swing.filechooser.FileFilter() {
@Override
public boolean accept(File f) {
if (f.isDirectory()) {
return true;
}
String n = f.getName();
int i = n.lastIndexOf('.');
if (i > 0 && i < n.length() -1) {
String ext = n.substring(i + 1).toLowerCase();
if (ext.equals("js")) {
return true;
}
}
return false;
}
@Override
public String getDescription() {
return "JavaScript Files (*.js)";
}
};
dlg.addChoosableFileFilter(filter);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
exit();
}
});
}
/**
* Runs the {@link #exitAction}.
*/
protected void exit() {
if (exitAction != null) {
SwingUtilities.invokeLater(exitAction);
}
dim.setReturnValue(Dim.EXIT);
}
/**
* Returns the {@link FileWindow} for the given URL.
*/
FileWindow getFileWindow(String url) {
if (url == null || url.equals("<stdin>")) {
return null;
}
return fileWindows.get(url);
}
/**
* Returns a short version of the given URL.
*/
static String getShortName(String url) {
int lastSlash = url.lastIndexOf('/');
if (lastSlash < 0) {
lastSlash = url.lastIndexOf('\\');
}
String shortName = url;
if (lastSlash >= 0 && lastSlash + 1 < url.length()) {
shortName = url.substring(lastSlash + 1);
}
return shortName;
}
/**
* Closes the given {@link FileWindow}.
*/
void removeWindow(FileWindow w) {
fileWindows.remove(w.getUrl());
JMenu windowMenu = getWindowMenu();
int count = windowMenu.getItemCount();
JMenuItem lastItem = windowMenu.getItem(count -1);
String name = getShortName(w.getUrl());
for (int i = 5; i < count; i++) {
JMenuItem item = windowMenu.getItem(i);
if (item == null) continue; // separator
String text = item.getText();
//1 D:\foo.js
//2 D:\bar.js
int pos = text.indexOf(' ');
if (text.substring(pos + 1).equals(name)) {
windowMenu.remove(item);
// Cascade [0]
// Tile [1]
// ------- [2]
// Console [3]
// ------- [4]
if (count == 6) {
// remove the final separator
windowMenu.remove(4);
} else {
int j = i - 4;
for (;i < count -1; i++) {
JMenuItem thisItem = windowMenu.getItem(i);
if (thisItem != null) {
//1 D:\foo.js
//2 D:\bar.js
text = thisItem.getText();
if (text.equals("More Windows...")) {
break;
}
pos = text.indexOf(' ');
thisItem.setText((char)('0' + j) + " " +
text.substring(pos + 1));
thisItem.setMnemonic('0' + j);
j++;
}
}
if (count - 6 == 0 && lastItem != item) {
if (lastItem.getText().equals("More Windows...")) {
windowMenu.remove(lastItem);
}
}
}
break;
}
}
windowMenu.revalidate();
}
/**
* Shows the line at which execution in the given stack frame just stopped.
*/
void showStopLine(Dim.StackFrame frame) {
String sourceName = frame.getUrl();
if (sourceName == null || sourceName.equals("<stdin>")) {
if (console.isVisible()) {
console.show();
}
} else {
showFileWindow(sourceName, -1);
int lineNumber = frame.getLineNumber();
FileWindow w = getFileWindow(sourceName);
if (w != null) {
setFilePosition(w, lineNumber);
}
}
}
/**
* Shows a {@link FileWindow} for the given source, creating it
* if it doesn't exist yet. if <code>lineNumber</code> is greater
* than -1, it indicates the line number to select and display.
* @param sourceUrl the source URL
* @param lineNumber the line number to select, or -1
*/
protected void showFileWindow(String sourceUrl, int lineNumber) {
FileWindow w;
if (sourceUrl != null) {
w = getFileWindow(sourceUrl);
}
else {
JInternalFrame f = getSelectedFrame();
if (f != null && f instanceof FileWindow) {
w = (FileWindow) f;
}
else {
w = currentWindow;
}
}
if (w == null && sourceUrl != null) {
Dim.SourceInfo si = dim.sourceInfo(sourceUrl);
createFileWindow(si, -1);
w = getFileWindow(sourceUrl);
}
if (w == null) {
return;
}
if (lineNumber > -1) {
int start = w.getPosition(lineNumber-1);
int end = w.getPosition(lineNumber)-1;
if (start <= 0) {
return;
}
w.textArea.select(start);
w.textArea.setCaretPosition(start);
w.textArea.moveCaretPosition(end);
}
try {
if (w.isIcon()) {
w.setIcon(false);
}
w.setVisible(true);
w.moveToFront();
w.setSelected(true);
requestFocus();
w.requestFocus();
w.textArea.requestFocus();
} catch (Exception exc) {
}
}
/**
* Creates and shows a new {@link FileWindow} for the given source.
*/
protected void createFileWindow(Dim.SourceInfo sourceInfo, int line) {
boolean activate = true;
String url = sourceInfo.url();
FileWindow w = new FileWindow(this, sourceInfo);
fileWindows.put(url, w);
if (line != -1) {
if (currentWindow != null) {
currentWindow.setPosition(-1);
}
try {
w.setPosition(w.textArea.getLineStartOffset(line-1));
} catch (BadLocationException exc) {
try {
w.setPosition(w.textArea.getLineStartOffset(0));
} catch (BadLocationException ee) {
w.setPosition(-1);
}
}
}
desk.add(w);
if (line != -1) {
currentWindow = w;
}
menubar.addFile(url);
w.setVisible(true);
if (activate) {
try {
w.setMaximum(true);
w.setSelected(true);
w.moveToFront();
} catch (Exception exc) {
}
}
}
/**
* Update the source text for <code>sourceInfo</code>. This returns true
* if a {@link FileWindow} for the given source exists and could be updated.
* Otherwise, this does nothing and returns false.
* @param sourceInfo the source info
* @return true if a {@link FileWindow} for the given source exists
* and could be updated, false otherwise.
*/
protected boolean updateFileWindow(Dim.SourceInfo sourceInfo) {
String fileName = sourceInfo.url();
FileWindow w = getFileWindow(fileName);
if (w != null) {
w.updateText(sourceInfo);
w.show();
return true;
}
return false;
}
/**
* Moves the current position in the given {@link FileWindow} to the
* given line.
*/
private void setFilePosition(FileWindow w, int line) {
boolean activate = true;
JTextArea ta = w.textArea;
try {
if (line == -1) {
w.setPosition(-1);
if (currentWindow == w) {
currentWindow = null;
}
} else {
int loc = ta.getLineStartOffset(line-1);
if (currentWindow != null && currentWindow != w) {
currentWindow.setPosition(-1);
}
w.setPosition(loc);
currentWindow = w;
}
} catch (BadLocationException exc) {
// fix me
}
if (activate) {
if (w.isIcon()) {
desk.getDesktopManager().deiconifyFrame(w);
}
desk.getDesktopManager().activateFrame(w);
try {
w.show();
w.toFront(); // required for correct frame layering (JDK 1.4.1)
w.setSelected(true);
} catch (Exception exc) {
}
}
}
/**
* Handles script interruption.
*/
void enterInterruptImpl(Dim.StackFrame lastFrame,
String threadTitle, String alertMessage) {
statusBar.setText("Thread: " + threadTitle);
showStopLine(lastFrame);
if (alertMessage != null) {
MessageDialogWrapper.showMessageDialog(this,
alertMessage,
"Exception in Script",
JOptionPane.ERROR_MESSAGE);
}
updateEnabled(true);
Dim.ContextData contextData = lastFrame.contextData();
JComboBox<String> ctx = context.context;
List<String> toolTips = context.toolTips;
context.disableUpdate();
int frameCount = contextData.frameCount();
ctx.removeAllItems();
// workaround for JDK 1.4 bug that caches selected value even after
// removeAllItems() is called
ctx.setSelectedItem(null);
toolTips.clear();
for (int i = 0; i < frameCount; i++) {
Dim.StackFrame frame = contextData.getFrame(i);
String url = frame.getUrl();
int lineNumber = frame.getLineNumber();
String shortName = url;
if (url.length() > 20) {
shortName = "..." + url.substring(url.length() - 17);
}
String location = "\"" + shortName + "\", line " + lineNumber;
ctx.insertItemAt(location, i);
location = "\"" + url + "\", line " + lineNumber;
toolTips.add(location);
}
context.enableUpdate();
ctx.setSelectedIndex(0);
ctx.setMinimumSize(new Dimension(50, ctx.getMinimumSize().height));
}
/**
* Returns the 'Window' menu.
*/
private JMenu getWindowMenu() {
return menubar.getMenu(3);
}
/**
* Displays a {@link JFileChooser} and returns the selected filename.
*/
private String chooseFile(String title) {
dlg.setDialogTitle(title);
File CWD = null;
String dir = SecurityUtilities.getSystemProperty("user.dir");
if (dir != null) {
CWD = new File(dir);
}
if (CWD != null) {
dlg.setCurrentDirectory(CWD);
}
int returnVal = dlg.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
String result = dlg.getSelectedFile().getCanonicalPath();
CWD = dlg.getSelectedFile().getParentFile();
Properties props = System.getProperties();
props.put("user.dir", CWD.getPath());
System.setProperties(props);
return result;
} catch (IOException ignored) {
} catch (SecurityException ignored) {
}
}
return null;
}
/**
* Returns the current selected internal frame.
*/
private JInternalFrame getSelectedFrame() {
JInternalFrame[] frames = desk.getAllFrames();
for (int i = 0; i < frames.length; i++) {
if (frames[i].isShowing()) {
return frames[i];
}
}
return frames[frames.length - 1];
}
/**
* Enables or disables the menu and tool bars with respect to the
* state of script execution.
*/
protected void updateEnabled(boolean interrupted) {
((Menubar)getJMenuBar()).updateEnabled(interrupted);
for (int ci = 0, cc = toolBar.getComponentCount(); ci < cc; ci++) {
boolean enableButton;
if (ci == 0) {
// Break
enableButton = !interrupted;
} else {
enableButton = interrupted;
}
toolBar.getComponent(ci).setEnabled(enableButton);
}
if (interrupted) {
toolBar.setEnabled(true);
// raise the debugger window
int state = getExtendedState();
if (state == Frame.ICONIFIED) {
setExtendedState(Frame.NORMAL);
}
toFront();
context.setEnabled(true);
} else {
if (currentWindow != null) currentWindow.setPosition(-1);
context.setEnabled(false);
}
}
/**
* Calls {@link JSplitPane#setResizeWeight} via reflection.
* For compatibility, since JDK < 1.3 does not have this method.
*/
static void setResizeWeight(JSplitPane pane, double weight) {
try {
Method m = JSplitPane.class.getMethod("setResizeWeight",
new Class[]{double.class});
m.invoke(pane, new Object[]{weight});
} catch (NoSuchMethodException exc) {
} catch (IllegalAccessException exc) {
} catch (java.lang.reflect.InvocationTargetException exc) {
}
}
/**
* Reads the file with the given name and returns its contents as a String.
*/
private String readFile(String fileName) {
String text;
try {
try (Reader r = new FileReader(fileName)) {
text = Kit.readReader(r);
}
} catch (IOException ex) {
MessageDialogWrapper.showMessageDialog(this,
ex.getMessage(),
"Error reading "+fileName,
JOptionPane.ERROR_MESSAGE);
text = null;
}
return text;
}
// GuiCallback
/**
* Called when the source text for a script has been updated.
*/
@Override
public void updateSourceText(Dim.SourceInfo sourceInfo) {
RunProxy proxy = new RunProxy(this, RunProxy.UPDATE_SOURCE_TEXT);
proxy.sourceInfo = sourceInfo;
SwingUtilities.invokeLater(proxy);
}
/**
* Called when the interrupt loop has been entered.
*/
@Override
public void enterInterrupt(Dim.StackFrame lastFrame,
String threadTitle,
String alertMessage) {
if (SwingUtilities.isEventDispatchThread()) {
enterInterruptImpl(lastFrame, threadTitle, alertMessage);
} else {
RunProxy proxy = new RunProxy(this, RunProxy.ENTER_INTERRUPT);
proxy.lastFrame = lastFrame;
proxy.threadTitle = threadTitle;
proxy.alertMessage = alertMessage;
SwingUtilities.invokeLater(proxy);
}
}
/**
* Returns whether the current thread is the GUI event thread.
*/
@Override
public boolean isGuiEventThread() {
return SwingUtilities.isEventDispatchThread();
}
/**
* Processes the next GUI event.
*/
@Override
public void dispatchNextGuiEvent() throws InterruptedException {
EventQueue queue = awtEventQueue;
if (queue == null) {
queue = Toolkit.getDefaultToolkit().getSystemEventQueue();
awtEventQueue = queue;
}
AWTEvent event = queue.getNextEvent();
if (event instanceof ActiveEvent) {
((ActiveEvent)event).dispatch();
} else {
Object source = event.getSource();
if (source instanceof Component) {
Component comp = (Component)source;
comp.dispatchEvent(event);
} else if (source instanceof MenuComponent) {
((MenuComponent)source).dispatchEvent(event);
}
}
}
// ActionListener
/**
* Performs an action from the menu or toolbar.
*/
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
int returnValue = -1;
if (cmd.equals("Cut") || cmd.equals("Copy") || cmd.equals("Paste")) {
JInternalFrame f = getSelectedFrame();
if (f != null && f instanceof ActionListener) {
((ActionListener)f).actionPerformed(e);
}
} else if (cmd.equals("Step Over")) {
returnValue = Dim.STEP_OVER;
} else if (cmd.equals("Step Into")) {
returnValue = Dim.STEP_INTO;
} else if (cmd.equals("Step Out")) {
returnValue = Dim.STEP_OUT;
} else if (cmd.equals("Go")) {
returnValue = Dim.GO;
} else if (cmd.equals("Break")) {
dim.setBreak();
} else if (cmd.equals("Exit")) {
exit();
} else if (cmd.equals("Open")) {
String fileName = chooseFile("Select a file to compile");
if (fileName != null) {
String text = readFile(fileName);
if (text != null) {
RunProxy proxy = new RunProxy(this, RunProxy.OPEN_FILE);
proxy.fileName = fileName;
proxy.text = text;
new Thread(proxy).start();
}
}
} else if (cmd.equals("Load")) {
String fileName = chooseFile("Select a file to execute");
if (fileName != null) {
String text = readFile(fileName);
if (text != null) {
RunProxy proxy = new RunProxy(this, RunProxy.LOAD_FILE);
proxy.fileName = fileName;
proxy.text = text;
new Thread(proxy).start();
}
}
} else if (cmd.equals("More Windows...")) {
MoreWindows dlg = new MoreWindows(this, fileWindows,
"Window", "Files");
dlg.showDialog(this);
} else if (cmd.equals("Console")) {
if (console.isIcon()) {
desk.getDesktopManager().deiconifyFrame(console);
}
console.show();
desk.getDesktopManager().activateFrame(console);
console.consoleTextArea.requestFocus();
} else if (cmd.equals("Cut")) {
} else if (cmd.equals("Copy")) {
} else if (cmd.equals("Paste")) {
} else if (cmd.equals("Go to function...")) {
FindFunction dlg = new FindFunction(this, "Go to function",
"Function");
dlg.showDialog(this);
} else if (cmd.equals("Go to line...")) {
final String s = (String) JOptionPane.showInputDialog(
this,
"Line number",
"Go to line...",
JOptionPane.QUESTION_MESSAGE,
null,
null,
null);
if (s == null || s.trim().length() == 0) {
return;
}
try {
final int line = Integer.parseInt(s);
showFileWindow(null, line);
}
catch (final NumberFormatException nfe) {
// ignore
}
} else if (cmd.equals("Tile")) {
JInternalFrame[] frames = desk.getAllFrames();
int count = frames.length;
int rows, cols;
rows = cols = (int)Math.sqrt(count);
if (rows*cols < count) {
cols++;
if (rows * cols < count) {
rows++;
}
}
Dimension size = desk.getSize();
int w = size.width/cols;
int h = size.height/rows;
int x = 0;
int y = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
int index = (i*cols) + j;
if (index >= frames.length) {
break;
}
JInternalFrame f = frames[index];
try {
f.setIcon(false);
f.setMaximum(false);
} catch (Exception exc) {
}
desk.getDesktopManager().setBoundsForFrame(f, x, y,
w, h);
x += w;
}
y += h;
x = 0;
}
} else if (cmd.equals("Cascade")) {
JInternalFrame[] frames = desk.getAllFrames();
int count = frames.length;
int x, y, w, h;