forked from processing/processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugTray.java
More file actions
1022 lines (848 loc) · 30.2 KB
/
DebugTray.java
File metadata and controls
1022 lines (848 loc) · 30.2 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; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2012-15 The Processing Foundation
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation, Inc.
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package processing.mode.java;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.table.TableColumn;
import javax.swing.tree.*;
import org.netbeans.swing.outline.*;
import com.sun.jdi.Value;
import processing.app.EditorButton;
import processing.app.Language;
import processing.app.Mode;
import processing.mode.java.debug.VariableNode;
public class DebugTray extends JFrame {
static public final int GAP = 13;
EditorButton continueButton;
EditorButton stepButton;
EditorButton breakpointButton;
// The tray will be placed at this amount from the top of the editor window,
// and extend to this amount from the bottom of the editor window.
final int VERTICAL_OFFSET = 64;
/// the root node (invisible)
protected DefaultMutableTreeNode rootNode;
/// node for Processing built-in variables
protected DefaultMutableTreeNode builtins;
/// data model for the tree column
protected DefaultTreeModel treeModel;
// private JScrollPane scrollPane;
protected Outline tree;
protected OutlineModel model;
protected List<DefaultMutableTreeNode> callStack;
/// current local variables
protected List<VariableNode> locals;
/// all fields of the current this-object
protected List<VariableNode> thisFields;
/// declared i.e. non-inherited fields of this
protected List<VariableNode> declaredThisFields;
protected JavaEditor editor;
// protected Debugger dbg;
/// list of expanded tree paths. (using list to maintain the order of expansion)
protected List<TreePath> expandedNodes = new ArrayList<TreePath>();
/// processing / "advanced" mode flag (currently not used)
protected boolean p5mode = true;
public DebugTray(final JavaEditor editor) {
setUndecorated(true);
this.editor = editor;
editor.addComponentListener(new EditorFollower());
//setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
Box box = Box.createVerticalBox();
box.add(createToolbar());
box.add(createScrollPane());
getContentPane().add(box);
pack();
/*
bgColor = mode.getColor("buttons.bgcolor");
statusFont = mode.getFont("buttons.status.font");
statusColor = mode.getColor("buttons.status.color");
// modeTitle = mode.getTitle().toUpperCase();
modeTitle = mode.getTitle();
modeTextFont = mode.getFont("mode.button.font");
modeButtonColor = mode.getColor("mode.button.color");
*/
}
Container createToolbar() {
final Mode mode = editor.getMode();
Box box = Box.createHorizontalBox();
continueButton =
new EditorButton(mode, "theme/debug/continue",
Language.text("toolbar.debug.continue")) {
@Override
public void actionPerformed(ActionEvent e) {
Logger.getLogger(DebugTray.class.getName()).log(Level.INFO, "Invoked 'Continue' toolbar button");
editor.debugger.continueDebug();
}
};
box.add(continueButton);
box.add(Box.createHorizontalStrut(GAP));
stepButton =
new EditorButton(mode, "theme/debug/step",
Language.text("toolbar.debug.step"),
Language.text("toolbar.debug.step_into")) {
@Override
public void actionPerformed(ActionEvent e) {
if (isShiftDown()) {
Logger.getLogger(DebugTray.class.getName()).log(Level.INFO, "Invoked 'Step Into' toolbar button");
editor.debugger.stepInto();
} else {
Logger.getLogger(DebugTray.class.getName()).log(Level.INFO, "Invoked 'Step' toolbar button");
editor.debugger.stepOver();
}
}
};
box.add(stepButton);
box.add(Box.createHorizontalStrut(GAP));
breakpointButton =
new EditorButton(mode, "theme/debug/breakpoint",
Language.text("toolbar.debug.toggle_breakpoints")) {
@Override
public void actionPerformed(ActionEvent e) {
Logger.getLogger(DebugTray.class.getName()).log(Level.INFO, "Invoked 'Toggle Breakpoint' toolbar button");
editor.debugger.toggleBreakpoint();
}
};
box.add(breakpointButton);
box.add(Box.createHorizontalStrut(GAP));
JLabel label = new JLabel();
box.add(label);
continueButton.setRolloverLabel(label);
stepButton.setRolloverLabel(label);
breakpointButton.setRolloverLabel(label);
// the rest is all gaps
box.add(Box.createHorizontalGlue());
box.setBorder(new EmptyBorder(GAP, GAP, GAP, GAP));
// prevent the toolbar from getting taller than its default
box.setMaximumSize(new Dimension(getMaximumSize().width, getPreferredSize().height));
return box;
}
Container createScrollPane() {
JScrollPane scrollPane = new JScrollPane();
tree = new Outline();
scrollPane.setViewportView(tree);
/*
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(scrollPane,
GroupLayout.DEFAULT_SIZE,
400, Short.MAX_VALUE)));
layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(scrollPane,
GroupLayout.Alignment.TRAILING,
GroupLayout.DEFAULT_SIZE,
300, Short.MAX_VALUE)));
pack();
*/
// setup Outline
rootNode = new DefaultMutableTreeNode("root");
builtins = new DefaultMutableTreeNode("Processing");
treeModel = new DefaultTreeModel(rootNode); // model for the tree column
model = DefaultOutlineModel.createOutlineModel(treeModel, new VariableRowModel(), true, "Name"); // model for all columns
ExpansionHandler expansionHandler = new ExpansionHandler();
model.getTreePathSupport().addTreeWillExpandListener(expansionHandler);
model.getTreePathSupport().addTreeExpansionListener(expansionHandler);
tree.setModel(model);
tree.setRootVisible(false);
tree.setRenderDataProvider(new OutlineRenderer());
tree.setColumnHidingAllowed(false); // disable visible columns button (shows by default when right scroll bar is visible)
tree.setAutoscrolls(false);
// set custom renderer and editor for value column, since we are using a custom class for values (VariableNode)
TableColumn valueColumn = tree.getColumnModel().getColumn(1);
valueColumn.setCellRenderer(new ValueCellRenderer());
valueColumn.setCellEditor(new ValueCellEditor());
//System.out.println("renderer: " + tree.getDefaultRenderer(String.class).getClass());
//System.out.println("editor: " + tree.getDefaultEditor(String.class).getClass());
callStack = new ArrayList<DefaultMutableTreeNode>();
locals = new ArrayList<VariableNode>();
thisFields = new ArrayList<VariableNode>();
declaredThisFields = new ArrayList<VariableNode>();
return scrollPane;
}
protected void activateContinue() {
}
protected void deactivateContinue() {
}
protected void activateStep() {
}
protected void deactivateStep() {
}
/** Keeps the debug window adjacent the editor at all times. */
class EditorFollower implements ComponentListener {
@Override
public void componentShown(ComponentEvent e) {
if (editor.isDebuggerEnabled()) {
// updateBounds();
setVisible(true);
}
}
@Override
public void componentHidden(ComponentEvent e) {
if (isVisible()) {
setVisible(false);
}
}
@Override
public void componentResized(ComponentEvent e) {
if (isVisible()) {
updateBounds();
}
}
@Override
public void componentMoved(ComponentEvent e) {
if (isVisible()) {
updateBounds();
}
}
}
private void updateBounds() {
setBounds(editor.getX() + editor.getWidth(),
editor.getY() + VERTICAL_OFFSET,
getPreferredSize().width,
editor.getHeight() - VERTICAL_OFFSET*2);
}
public void setVisible(boolean visible) {
if (visible) {
updateBounds();
}
super.setVisible(visible);
}
// @Override
// public void setTitle(String title) {
// super.setTitle(title + " | Variable Inspector");
// }
/**
* Model for a Outline Row (excluding the tree column). Column 0 is "Value".
* Column 1 is "Type". Handles setting and getting values. TODO: Maybe use a
* TableCellRenderer instead of this to also have a different icon based on
* expanded state. See:
* http://kickjava.com/src/org/netbeans/swing/outline/DefaultOutlineCellRenderer.java.htm
*/
protected class VariableRowModel implements RowModel {
final String[] columnNames = { "Value", "Type" };
final int[] editableTypes = {
VariableNode.TYPE_BOOLEAN,
VariableNode.TYPE_FLOAT,
VariableNode.TYPE_INTEGER,
VariableNode.TYPE_STRING,
VariableNode.TYPE_FLOAT,
VariableNode.TYPE_DOUBLE,
VariableNode.TYPE_LONG,
VariableNode.TYPE_SHORT,
VariableNode.TYPE_CHAR
};
@Override
public int getColumnCount() {
if (p5mode) {
return 1; // only show value in p5 mode
} else {
return 2;
}
}
@Override
public Object getValueFor(Object obj, int column) {
if (obj instanceof VariableNode) {
VariableNode var = (VariableNode) obj;
if (column == 0) {
// will be converted to an appropriate String by ValueCellRenderer
return var;
} else if (column == 1) {
return var.getTypeName();
}
}
return "";
}
@Override
public Class<?> getColumnClass(int column) {
if (column == 0) {
return VariableNode.class;
}
return String.class;
}
@Override
public boolean isCellEditable(Object o, int i) {
if (i == 0 && o instanceof VariableNode) {
VariableNode var = (VariableNode) o;
//System.out.println("type: " + var.getTypeName());
for (int type : editableTypes) {
if (var.getType() == type) {
return true;
}
}
}
return false;
}
@Override
public void setValueFor(Object o, int i, Object o1) {
VariableNode var = (VariableNode) o;
String stringValue = (String) o1;
Debugger dbg = editor.getDebugger();
Value value = null;
try {
switch (var.getType()) {
case VariableNode.TYPE_INTEGER:
value = dbg.vm().mirrorOf(Integer.parseInt(stringValue));
break;
case VariableNode.TYPE_BOOLEAN:
value = dbg.vm().mirrorOf(Boolean.parseBoolean(stringValue));
break;
case VariableNode.TYPE_FLOAT:
value = dbg.vm().mirrorOf(Float.parseFloat(stringValue));
break;
case VariableNode.TYPE_STRING:
value = dbg.vm().mirrorOf(stringValue);
break;
case VariableNode.TYPE_LONG:
value = dbg.vm().mirrorOf(Long.parseLong(stringValue));
break;
case VariableNode.TYPE_BYTE:
value = dbg.vm().mirrorOf(Byte.parseByte(stringValue));
break;
case VariableNode.TYPE_DOUBLE:
value = dbg.vm().mirrorOf(Double.parseDouble(stringValue));
break;
case VariableNode.TYPE_SHORT:
value = dbg.vm().mirrorOf(Short.parseShort(stringValue));
break;
case VariableNode.TYPE_CHAR:
// TODO: better char support
if (stringValue.length() > 0) {
value = dbg.vm().mirrorOf(stringValue.charAt(0));
}
break;
}
} catch (NumberFormatException ex) {
Logger.getLogger(VariableRowModel.class.getName()).log(Level.INFO, "invalid value entered for {0}: {1}", new Object[]{var.getName(), stringValue});
}
if (value != null) {
var.setValue(value);
Logger.getLogger(VariableRowModel.class.getName()).log(Level.INFO, "new value set: {0}", var.getStringValue());
}
}
@Override
public String getColumnName(int i) {
return columnNames[i];
}
}
/**
* Renderer for the tree portion of the outline component.
* Handles icons, text color and tool tips.
*/
protected class OutlineRenderer implements RenderDataProvider {
protected Icon[][] icons;
protected static final int ICON_SIZE = 16; // icon size (square, size=width=height)
public OutlineRenderer() {
// load icons
icons = loadIcons("theme/var-icons.gif");
}
/**
* Load multiple icons (horizotal) with multiple states (vertical) from
* a single file.
*
* @param fileName file path in the mode folder.
* @return a nested array (first index: icon, second index: state) or
* null if the file wasn't found.
*/
protected ImageIcon[][] loadIcons(String fileName) {
Mode mode = editor.getMode();
File file = mode.getContentFile(fileName);
if (!file.exists()) {
Logger.getLogger(OutlineRenderer.class.getName()).log(Level.SEVERE, "icon file not found: {0}", file.getAbsolutePath());
return null;
}
Image allIcons = mode.loadImage(fileName);
int cols = allIcons.getWidth(null) / ICON_SIZE;
int rows = allIcons.getHeight(null) / ICON_SIZE;
ImageIcon[][] iconImages = new ImageIcon[cols][rows];
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
//Image image = createImage(ICON_SIZE, ICON_SIZE);
Image image = new BufferedImage(ICON_SIZE, ICON_SIZE, BufferedImage.TYPE_INT_ARGB);
Graphics g = image.getGraphics();
g.drawImage(allIcons, -i * ICON_SIZE, -j * ICON_SIZE, null);
iconImages[i][j] = new ImageIcon(image);
}
}
return iconImages;
}
protected Icon getIcon(int type, int state) {
if (type < 0 || type > icons.length - 1) {
return null;
}
return icons[type][state];
}
protected VariableNode toVariableNode(Object o) {
if (o instanceof VariableNode) {
return (VariableNode) o;
} else {
return null;
}
}
protected Icon toGray(Icon icon) {
if (icon instanceof ImageIcon) {
Image grayImage = GrayFilter.createDisabledImage(((ImageIcon) icon).getImage());
return new ImageIcon(grayImage);
}
// Cannot convert
return icon;
}
@Override
public String getDisplayName(Object o) {
return o.toString(); // VariableNode.toString() returns name; (for sorting)
// VariableNode var = toVariableNode(o);
// if (var != null) {
// return var.getName();
// } else {
// return o.toString();
// }
}
@Override
public boolean isHtmlDisplayName(Object o) {
return false;
}
@Override
public Color getBackground(Object o) {
return null;
}
@Override
public Color getForeground(Object o) {
if (tree.isEnabled()) {
return null; // default
} else {
return Color.GRAY;
}
}
@Override
public String getTooltipText(Object o) {
VariableNode var = toVariableNode(o);
if (var != null) {
return var.getDescription();
} else {
return "";
}
}
@Override
public Icon getIcon(Object o) {
VariableNode var = toVariableNode(o);
if (var != null) {
if (tree.isEnabled()) {
return getIcon(var.getType(), 0);
} else {
return getIcon(var.getType(), 1);
}
} else {
if (o instanceof TreeNode) {
// TreeNode node = (TreeNode) o;
// AbstractLayoutCache layout = tree.getLayoutCache();
UIDefaults defaults = UIManager.getDefaults();
boolean isLeaf = model.isLeaf(o);
Icon icon;
if (isLeaf) {
icon = defaults.getIcon("Tree.leafIcon");
} else {
icon = defaults.getIcon("Tree.closedIcon");
}
if (!tree.isEnabled()) {
return toGray(icon);
}
return icon;
}
}
return null; // use standard icon
//UIManager.getIcon(o);
}
}
// TODO: could probably extend the simpler DefaultTableCellRenderer here
/**
* Renderer for the value column. Uses an italic font for null values and
* Object values ("instance of ..."). Uses a gray color when tree is not
* enabled.
*/
protected class ValueCellRenderer extends DefaultOutlineCellRenderer {
public ValueCellRenderer() {
super();
}
protected void setItalic(boolean on) {
if (on) {
setFont(new Font(getFont().getName(), Font.ITALIC, getFont().getSize()));
} else {
setFont(new Font(getFont().getName(), Font.PLAIN, getFont().getSize()));
}
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (!tree.isEnabled()) {
setForeground(Color.GRAY);
} else {
setForeground(Color.BLACK);
}
if (value instanceof VariableNode) {
VariableNode var = (VariableNode) value;
if (var.getValue() == null || var.getType() == VariableNode.TYPE_OBJECT) {
setItalic(true);
} else {
setItalic(false);
}
value = var.getStringValue();
}
setValue(value);
return c;
}
}
/**
* Editor for the value column. Will show an empty string when editing
* String values that are null.
*/
protected class ValueCellEditor extends DefaultCellEditor {
public ValueCellEditor() {
super(new JTextField());
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
if (!(value instanceof VariableNode)) {
return super.getTableCellEditorComponent(table, value, isSelected, row, column);
}
VariableNode var = (VariableNode) value;
if (var.getType() == VariableNode.TYPE_STRING && var.getValue() == null) {
return super.getTableCellEditorComponent(table, "", isSelected, row, column);
} else {
return super.getTableCellEditorComponent(table, var.getStringValue(), isSelected, row, column);
}
}
}
/**
* Handler for expanding and collapsing tree nodes.
* Implements lazy loading of tree data (on expand).
*/
protected class ExpansionHandler implements ExtTreeWillExpandListener, TreeExpansionListener {
@Override
public void treeWillExpand(TreeExpansionEvent tee) throws ExpandVetoException {
//System.out.println("will expand");
Object last = tee.getPath().getLastPathComponent();
if (!(last instanceof VariableNode)) {
return;
}
VariableNode var = (VariableNode) last;
var.removeAllChildren(); // TODO: should we only load it once?
var.addChildren(filterNodes(editor.getDebugger().getFields(var.getValue(), 0, true), new ThisFilter()));
}
@Override
public void treeWillCollapse(TreeExpansionEvent tee) throws ExpandVetoException {
//throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void treeExpanded(TreeExpansionEvent tee) {
//System.out.println("expanded: " + tee.getPath());
if (!expandedNodes.contains(tee.getPath())) {
expandedNodes.add(tee.getPath());
}
}
@Override
public void treeCollapsed(TreeExpansionEvent tee) {
//System.out.println("collapsed: " + tee.getPath());
// first remove all children of collapsed path
// this makes sure children do not appear before parents in the list.
// (children can't be expanded before their parents)
List<TreePath> removalList = new ArrayList<TreePath>();
for (TreePath path : expandedNodes) {
if (path.getParentPath().equals(tee.getPath())) {
removalList.add(path);
}
}
for (TreePath path : removalList) {
expandedNodes.remove(path);
}
// remove collapsed path
expandedNodes.remove(tee.getPath());
}
@Override
public void treeExpansionVetoed(TreeExpansionEvent tee, ExpandVetoException eve) {
//System.out.println("expansion vetoed");
// nop
}
}
protected static void run(final DebugTray vi) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
vi.setVisible(true);
}
});
}
public DefaultMutableTreeNode getRootNode() {
return rootNode;
}
/**
* Unlock the inspector window. Rebuild after this to avoid ... dots in the
* trees labels
*/
public void unlock() {
tree.setEnabled(true);
}
/**
* Lock the inspector window. Cancels open edits.
*/
public void lock() {
if (tree.getCellEditor() != null) {
//tree.getCellEditor().stopCellEditing(); // force quit open edit
tree.getCellEditor().cancelCellEditing(); // cancel an open edit
}
tree.setEnabled(false);
}
/**
* Reset the inspector windows data. Rebuild after this to make changes
* visible.
*/
public void reset() {
rootNode.removeAllChildren();
// clear local data for good measure (in case someone rebuilds)
callStack.clear();
locals.clear();
thisFields.clear();
declaredThisFields.clear();
expandedNodes.clear();
// update
treeModel.nodeStructureChanged(rootNode);
}
/**
* Update call stack data.
*
* @param nodes a list of nodes that represent the call stack.
* @param title the title to be used when labeling or otherwise grouping
* call stack data.
*/
public void updateCallStack(List<DefaultMutableTreeNode> nodes, String title) {
callStack = nodes;
}
/**
* Update locals data.
*
* @param nodes a list of {@link VariableNode} to be shown as local
* variables in the inspector.
* @param title the title to be used when labeling or otherwise grouping
* locals data.
*/
public void updateLocals(List<VariableNode> nodes, String title) {
locals = nodes;
}
/**
* Update this-fields data.
*
* @param nodes a list of {@link VariableNode}s to be shown as this-fields
* in the inspector.
* @param title the title to be used when labeling or otherwise grouping
* this-fields data.
*/
public void updateThisFields(List<VariableNode> nodes, String title) {
thisFields = nodes;
}
/**
* Update declared (non-inherited) this-fields data.
*
* @param nodes a list of {@link VariableNode}s to be shown as declared
* this-fields in the inspector.
* @param title the title to be used when labeling or otherwise grouping
* declared this-fields data.
*/
public void updateDeclaredThisFields(List<VariableNode> nodes, String title) {
declaredThisFields = nodes;
}
/**
* Rebuild the outline tree from current data. Uses the data provided by
* {@link #updateCallStack}, {@link #updateLocals}, {@link #updateThisFields}
* and {@link #updateDeclaredThisFields}
*/
public void rebuild() {
rootNode.removeAllChildren();
if (p5mode) {
// add all locals to root
addAllNodes(rootNode, locals);
// add non-inherited this fields
addAllNodes(rootNode, filterNodes(declaredThisFields, new LocalHidesThisFilter(locals, LocalHidesThisFilter.MODE_PREFIX)));
// add p5 builtins in a new folder
builtins.removeAllChildren();
addAllNodes(builtins, filterNodes(thisFields, new P5BuiltinsFilter()));
if (builtins.getChildCount() > 0) { // skip builtins in certain situations e.g. in pure java tabs.
rootNode.add(builtins);
}
// notify tree (using model) changed a node and its children
// http://stackoverflow.com/questions/2730851/how-to-update-jtree-elements
// needs to be done before expanding paths!
treeModel.nodeStructureChanged(rootNode);
// handle node expansions
for (TreePath path : expandedNodes) {
//System.out.println("re-expanding: " + path);
path = synthesizePath(path);
if (path != null) {
tree.expandPath(path);
} else {
//System.out.println("couldn't synthesize path");
}
}
// this expansion causes problems when sorted and stepping
//tree.expandPath(new TreePath(new Object[]{rootNode, builtins}));
} else {
// TODO: implement advanced mode here
}
}
/**
* Re-build a {@link TreePath} from a previous path using equals-checks
* starting at the root node. This is used to use paths from previous trees
* for use on the current tree.
* @param path the path to synthesize.
* @return the rebuilt path, usable on the current tree.
*/
protected TreePath synthesizePath(TreePath path) {
//System.out.println("synthesizing: " + path);
if (path.getPathCount() == 0 || !rootNode.equals(path.getPathComponent(0))) {
return null;
}
Object[] newPath = new Object[path.getPathCount()];
newPath[0] = rootNode;
TreeNode currentNode = rootNode;
for (int i = 0; i < path.getPathCount() - 1; i++) {
// get next node
for (int j = 0; j < currentNode.getChildCount(); j++) {
TreeNode nextNode = currentNode.getChildAt(j);
if (nextNode.equals(path.getPathComponent(i + 1))) {
currentNode = nextNode;
newPath[i + 1] = nextNode;
//System.out.println("found node " + (i+1) + ": " + nextNode);
break;
}
}
if (newPath[i + 1] == null) {
//System.out.println("didn't find node");
return null;
}
}
return new TreePath(newPath);
}
/**
* Filter a list of nodes using a {@link VariableNodeFilter}.
* @param nodes the list of nodes to filter.
* @param filter the filter to be used.
* @return the filtered list.
*/
protected List<VariableNode> filterNodes(List<VariableNode> nodes, VariableNodeFilter filter) {
List<VariableNode> filtered = new ArrayList<VariableNode>();
for (VariableNode node : nodes) {
if (filter.accept(node)) {
filtered.add(node);
}
}
return filtered;
}
protected void addAllNodes(DefaultMutableTreeNode root, List<? extends MutableTreeNode> nodes) {
for (MutableTreeNode node : nodes) {
root.add(node);
}
}
public interface VariableNodeFilter {
/** Check whether the filter accepts a {@link VariableNode}. */
public boolean accept(VariableNode var);
}
/**
* A {@link VariableNodeFilter} that accepts Processing built-in variable
* names.
*/
public class P5BuiltinsFilter implements VariableNodeFilter {
protected String[] p5Builtins = {
"focused",
"frameCount",
"frameRate",
"height",
"online",
"screen",
"width",
"mouseX",
"mouseY",
"pmouseX",
"pmouseY",
"key",
"keyCode",
"keyPressed"
};
@Override
public boolean accept(VariableNode var) {
return Arrays.asList(p5Builtins).contains(var.getName());
}
}
/**
* A {@link VariableNodeFilter} that rejects implicit this references.
* (Names starting with "this$")
*/
public class ThisFilter implements VariableNodeFilter {
@Override
public boolean accept(VariableNode var) {
return !var.getName().startsWith("this$");
}
}
/**
* A {@link VariableNodeFilter} that either rejects this-fields if hidden by
* a local, or prefixes its name with "this."
*/
public class LocalHidesThisFilter implements VariableNodeFilter {
// Reject a this-field if hidden by a local.
public static final int MODE_HIDE = 0; // don't show hidden this fields
// Prefix a this-fields name with "this." if hidden by a local.
public static final int MODE_PREFIX = 1;
protected List<VariableNode> locals;
protected int mode;
/**
* Construct a {@link LocalHidesThisFilter}.
* @param locals a list of locals to check against
* @param mode either {@link #MODE_HIDE} or {@link #MODE_PREFIX}
*/
public LocalHidesThisFilter(List<VariableNode> locals, int mode) {
this.locals = locals;