-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathExamplesFrame.java
More file actions
410 lines (351 loc) · 14.2 KB
/
ExamplesFrame.java
File metadata and controls
410 lines (351 loc) · 14.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
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2013-19 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 as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package processing.app.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import processing.app.Base;
import processing.app.Language;
import processing.app.Library;
import processing.app.Mode;
import processing.app.Platform;
import processing.app.Preferences;
import processing.app.SketchReference;
import processing.app.contrib.Contribution;
import processing.app.contrib.ContributionManager;
import processing.app.contrib.ContributionType;
import processing.app.contrib.ExamplesContribution;
import processing.core.PApplet;
import processing.data.StringDict;
public class ExamplesFrame extends JFrame {
protected Base base;
protected Mode mode;
protected File examplesContribFolder;
public ExamplesFrame(final Base base, final Mode mode) {
super(Language.interpolate("examples.title", mode.getTitle()));
this.base = base;
this.mode = mode;
// Get path to the contributed examples compatible with this mode
examplesContribFolder = Base.getSketchbookExamplesFolder();
Toolkit.setIcon(this);
Toolkit.registerWindowCloseKeys(getRootPane(), new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
JPanel examplesPanel = new JPanel();
examplesPanel.setLayout(new BorderLayout());
examplesPanel.setBackground(Color.WHITE);
final JPanel openExamplesManagerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
JButton addExamplesButton = new JButton(Language.text("examples.add_examples"));
openExamplesManagerPanel.add(addExamplesButton);
openExamplesManagerPanel.setOpaque(false);
Border lineBorder = BorderFactory.createMatteBorder(0, 0, 1, 0, Color.LIGHT_GRAY);
Border paddingBorder = BorderFactory.createEmptyBorder(3, 5, 1, 4);
openExamplesManagerPanel.setBorder(BorderFactory.createCompoundBorder(lineBorder, paddingBorder));
openExamplesManagerPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
openExamplesManagerPanel.setCursor(new Cursor(Cursor.HAND_CURSOR));
addExamplesButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
ContributionManager.openExamples();
}
});
final JTree tree = new JTree(buildTree());
tree.setOpaque(true);
tree.setAlignmentX(Component.LEFT_ALIGNMENT);
tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
tree.setShowsRootHandles(true);
// expand the root
tree.expandRow(0);
// now hide the root
tree.setRootVisible(false);
// After 2.0a7, no longer expanding each of the categories at Casey's
// request. He felt that the window was too complicated too quickly.
// for (int row = tree.getRowCount()-1; row >= 0; --row) {
// tree.expandRow(row);
// }
tree.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
DefaultMutableTreeNode node =
(DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
int selRow = tree.getRowForLocation(e.getX(), e.getY());
//TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
//if (node != null && node.isLeaf() && node.getPath().equals(selPath)) {
if (node != null && node.isLeaf() && selRow != -1) {
SketchReference sketch = (SketchReference) node.getUserObject();
base.handleOpen(sketch.getPath());
}
}
}
});
tree.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) { // doesn't fire keyTyped()
setVisible(false);
}
}
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_ENTER) {
DefaultMutableTreeNode node =
(DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if (node != null && node.isLeaf()) {
SketchReference sketch = (SketchReference) node.getUserObject();
base.handleOpen(sketch.getPath());
}
}
}
});
tree.addTreeExpansionListener(new TreeExpansionListener() {
@Override
public void treeExpanded(TreeExpansionEvent event) {
updateExpanded(tree);
}
@Override
public void treeCollapsed(TreeExpansionEvent event) {
updateExpanded(tree);
}
});
tree.setBorder(new EmptyBorder(0, 5, 5, 5));
if (Platform.isMacOS()) {
tree.setToggleClickCount(2);
} else {
tree.setToggleClickCount(1);
}
// Special cell renderer that takes the UI zoom into account
tree.setCellRenderer(new ZoomTreeCellRenderer(mode));
JScrollPane treePane = new JScrollPane(tree);
treePane.setPreferredSize(Toolkit.zoom(250, 300));
treePane.setBorder(new EmptyBorder(Toolkit.zoom(2), 0, 0, 0));
treePane.setOpaque(true);
treePane.setBackground(Color.WHITE);
treePane.setAlignmentX(Component.LEFT_ALIGNMENT);
examplesPanel.add(openExamplesManagerPanel,BorderLayout.PAGE_START);
examplesPanel.add(treePane, BorderLayout.CENTER);
getContentPane().add(examplesPanel);
pack();
restoreExpanded(tree);
}
public void setVisible() {
// Space for the editor plus a li'l gap
int roughWidth = getWidth() + 20;
Point p = null;
// If no window open, or the editor is at the edge of the screen
Editor editor = base.getActiveEditor();
if (editor == null ||
(p = editor.getLocation()).x < roughWidth) {
// Center the window on the screen
setLocationRelativeTo(null);
} else {
// Open the window relative to the editor
setLocation(p.x - roughWidth, p.y);
}
setVisible(true);
}
protected void updateExpanded(JTree tree) {
Enumeration en = tree.getExpandedDescendants(new TreePath(tree.getModel().getRoot()));
//en.nextElement(); // skip the root "Examples" node
StringBuilder s = new StringBuilder();
while (en.hasMoreElements()) {
//System.out.println(en.nextElement());
TreePath tp = (TreePath) en.nextElement();
Object[] path = tp.getPath();
for (Object o : path) {
DefaultMutableTreeNode p = (DefaultMutableTreeNode) o;
String name = (String) p.getUserObject();
//System.out.print(p.getUserObject().getClass().getName() + ":" + p.getUserObject() + " -> ");
//System.out.print(name + " -> ");
s.append(name);
s.append(File.separatorChar);
}
//System.out.println();
s.setCharAt(s.length() - 1, File.pathSeparatorChar);
}
s.setLength(s.length() - 1); // nix that last separator
String pref = "examples." + getClass().getName() + ".visible";
Preferences.set(pref, s.toString());
Preferences.save();
// System.out.println(s);
// System.out.println();
}
protected void restoreExpanded(JTree tree) {
String pref = "examples." + getClass().getName() + ".visible";
String value = Preferences.get(pref);
if (value != null) {
String[] paths = PApplet.split(value, File.pathSeparator);
for (String path : paths) {
// System.out.println("trying to expand " + path);
String[] items = PApplet.split(path, File.separator);
DefaultMutableTreeNode[] nodes = new DefaultMutableTreeNode[items.length];
expandTree(tree, null, items, nodes, 0);
}
}
}
void expandTree(JTree tree, Object object, String[] items,
DefaultMutableTreeNode[] nodes, int index) {
// if (object == null) {
// object = model.getRoot();
// }
TreeModel model = tree.getModel();
if (index == 0) {
nodes[0] = (DefaultMutableTreeNode) model.getRoot();
expandTree(tree, nodes[0], items, nodes, 1);
} else if (index < items.length) {
// String item = items[0];
// TreeModel model = object.getModel();
// System.out.println(object.getClass().getName());
DefaultMutableTreeNode node = (DefaultMutableTreeNode) object;
int count = model.getChildCount(node);
// System.out.println("child count is " + count);
for (int i = 0; i < count; i++) {
DefaultMutableTreeNode child = (DefaultMutableTreeNode) model.getChild(node, i);
if (items[index].equals(child.getUserObject())) {
nodes[index] = child;
expandTree(tree, child, items, nodes, index+1);
}
}
} else { // last one
// PApplet.println(nodes);
tree.expandPath(new TreePath(nodes));
}
}
protected DefaultMutableTreeNode buildTree() {
DefaultMutableTreeNode root = new DefaultMutableTreeNode(); //"Examples");
try {
// Get the list of Mode-specific examples, in the order the Mode wants
// to present them (i.e. Basics, then Topics, then Demos...)
File[] examples = mode.getExampleCategoryFolders();
for (File subFolder : examples) {
DefaultMutableTreeNode subNode = new DefaultMutableTreeNode(subFolder.getName());
if (base.addSketches(subNode, subFolder, true)) {
root.add(subNode);
}
}
DefaultMutableTreeNode foundationLibraries =
new DefaultMutableTreeNode(Language.text("examples.core_libraries"));
// Get examples for core libraries
for (Library lib : mode.coreLibraries) {
if (lib.hasExamples()) {
DefaultMutableTreeNode libNode = new DefaultMutableTreeNode(lib.getName());
if (base.addSketches(libNode, lib.getExamplesFolder(), true)) {
foundationLibraries.add(libNode);
}
}
}
if (foundationLibraries.getChildCount() > 0) {
root.add(foundationLibraries);
}
// Get examples for third party libraries
DefaultMutableTreeNode contributedLibExamples = new
DefaultMutableTreeNode(Language.text("examples.libraries"));
for (Library lib : mode.contribLibraries) {
if (lib.hasExamples()) {
DefaultMutableTreeNode libNode =
new DefaultMutableTreeNode(lib.getName());
base.addSketches(libNode, lib.getExamplesFolder(), true);
contributedLibExamples.add(libNode);
}
}
if (contributedLibExamples.getChildCount() > 0) {
root.add(contributedLibExamples);
}
} catch (IOException e) {
e.printStackTrace();
}
DefaultMutableTreeNode contributedExamplesNode = buildContribTree();
if (contributedExamplesNode.getChildCount() > 0) {
root.add(contributedExamplesNode);
}
return root;
}
protected DefaultMutableTreeNode buildContribTree() {
DefaultMutableTreeNode contribExamplesNode =
new DefaultMutableTreeNode(Language.text("examples.contributed"));
try {
File[] subfolders =
ContributionType.EXAMPLES.listCandidates(examplesContribFolder);
if (subfolders != null) {
for (File sub : subfolders) {
StringDict props =
Contribution.loadProperties(sub, ContributionType.EXAMPLES);
if (props != null) {
if (ExamplesContribution.isCompatible(base, props)) {
DefaultMutableTreeNode subNode =
new DefaultMutableTreeNode(props.get("name"));
if (base.addSketches(subNode, sub, true)) {
contribExamplesNode.add(subNode);
// TODO there has to be a simpler way of handling this along
// with addSketches() as well [fry 150811]
int exampleNodeNumber = -1;
// The contrib may have other items besides the examples folder
for (int i = 0; i < subNode.getChildCount(); i++) {
if (subNode.getChildAt(i).toString().equals("examples")) {
exampleNodeNumber = i;
}
}
if (exampleNodeNumber != -1) {
TreeNode exampleNode = subNode.getChildAt(exampleNodeNumber);
subNode.remove(exampleNodeNumber);
int count = exampleNode.getChildCount();
for (int j = 0; j < count; j++) {
subNode.add((DefaultMutableTreeNode) exampleNode.getChildAt(0));
}
}
// if (subNode.getChildCount() != 1) {
// System.err.println("more children than expected when one is enough");
// }
// TreeNode exampleNode = subNode.getChildAt(0);
// subNode.add((DefaultMutableTreeNode) exampleNode.getChildAt(0));
}
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return contribExamplesNode;
}
}