-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathNodeBoxMenuBar.java
More file actions
575 lines (485 loc) · 17.7 KB
/
NodeBoxMenuBar.java
File metadata and controls
575 lines (485 loc) · 17.7 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
package nodebox.client;
import javax.swing.*;
import javax.swing.undo.UndoManager;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
/**
* The main menu bar for the NodeBox application.
*/
public class NodeBoxMenuBar extends JMenuBar {
private NodeBoxDocument document;
private boolean enabled;
private static ArrayList<JMenu> recentFileMenus = new ArrayList<JMenu>();
private static Preferences recentFilesPreferences = Preferences.userRoot().node("/nodebox/recent");
private static Logger logger = Logger.getLogger("nodebox.client.NodeBoxMenuBar");
private JMenuItem showConsoleItem;
private UndoManager undoManager;
private UndoAction undoAction;
private RedoAction redoAction;
public NodeBoxMenuBar() {
this(null);
}
public NodeBoxMenuBar(NodeBoxDocument document) {
this.document = document;
if (document != null)
this.undoManager = document.getUndoManager();
// File menu
JMenu fileMenu = new JMenu("File");
fileMenu.add(new NewAction());
fileMenu.add(new OpenAction());
JMenu recentFileMenu = new JMenu("Open Recent");
recentFileMenus.add(recentFileMenu);
buildRecentFileMenu();
fileMenu.add(recentFileMenu);
fileMenu.addSeparator();
fileMenu.add(new CloseAction());
fileMenu.add(new SaveAction());
fileMenu.add(new SaveAsAction());
fileMenu.add(new RevertAction());
fileMenu.addSeparator();
fileMenu.add(new ExportAction());
fileMenu.add(new ExportRangeAction());
fileMenu.add(new ExportMovieAction());
if (!PlatformUtils.onMac()) {
fileMenu.addSeparator();
fileMenu.add(new ExitAction());
}
add(fileMenu);
// Edit menu
JMenu editMenu = new JMenu("Edit");
editMenu.add(undoAction = new UndoAction());
editMenu.add(redoAction = new RedoAction());
editMenu.addSeparator();
editMenu.add(new CutAction());
editMenu.add(new CopyAction());
editMenu.add(new PasteAction());
editMenu.addSeparator();
editMenu.add(new DeleteAction());
if (!PlatformUtils.onMac()) {
editMenu.addSeparator();
editMenu.add(new PreferencesAction());
}
add(editMenu);
// Node menu
JMenu pythonMenu = new JMenu("Node");
pythonMenu.add(new NewNodeAction());
pythonMenu.add(new ReloadAction());
//pythonMenu.add(newLibraryAction);
add(pythonMenu);
// Window menu
JMenu windowMenu = new JMenu("Window");
windowMenu.add(new MinimizeAction());
windowMenu.add(new ZoomAction());
showConsoleItem = windowMenu.add(new JCheckBoxMenuItem(new ShowConsoleAction()));
setShowConsoleChecked(Application.getInstance().isConsoleOpened());
windowMenu.addSeparator();
windowMenu.add(new BringAllToFrontAction());
// TODO Add all active windows.
add(windowMenu);
// Help menu
JMenu helpMenu = new JMenu("Help");
helpMenu.add(new GettingStartedAction());
helpMenu.add(new HelpAndSupportAction());
helpMenu.addSeparator();
if (!PlatformUtils.onMac()) {
helpMenu.add(new AboutAction());
}
helpMenu.add(new CheckForUpdatesAction());
helpMenu.add(new NodeboxSiteAction());
add(helpMenu);
}
public void updateUndoRedoState() {
undoAction.update();
redoAction.update();
}
public NodeBoxDocument getDocument() {
return document;
}
public boolean hasDocument() {
return document != null;
}
public static void addRecentFile(File f) {
File canonicalFile;
try {
canonicalFile = f.getCanonicalFile();
} catch (IOException e) {
logger.log(Level.WARNING, "Could not get canonical file name", e);
return;
}
ArrayList<File> fileList = getRecentFiles();
// If the recent file was already in the list, remove it and add it to the top.
// If the list did not contain the file, the remove call does nothing.
fileList.remove(canonicalFile);
fileList.add(0, canonicalFile);
writeRecentFiles(fileList);
buildRecentFileMenu();
}
public static void writeRecentFiles(ArrayList<File> fileList) {
int i = 1;
for (File f : fileList) {
try {
recentFilesPreferences.put(String.valueOf(i), f.getCanonicalPath());
} catch (IOException e) {
logger.log(Level.WARNING, "Could not get canonical file name", e);
return;
}
i++;
if (i > 10) break;
}
try {
recentFilesPreferences.flush();
} catch (BackingStoreException e) {
logger.log(Level.WARNING, "Could not write recent files preferences", e);
}
}
public static ArrayList<File> getRecentFiles() {
ArrayList<File> fileList = new ArrayList<File>(10);
for (int i = 1; i <= 10; i++) {
File file = new File(recentFilesPreferences.get(String.valueOf(i), ""));
if (file.exists()) {
fileList.add(file);
}
}
return fileList;
}
private static void buildRecentFileMenu() {
for (JMenu recentFileMenu : recentFileMenus) {
recentFileMenu.removeAll();
for (File f : getRecentFiles()) {
recentFileMenu.add(new OpenRecentAction(f));
}
}
}
public void setShowConsoleChecked(boolean checked) {
showConsoleItem.getModel().setSelected(checked);
}
//// Actions ////
public abstract class AbstractDocumentAction extends AbstractAction {
@Override
public boolean isEnabled() {
return NodeBoxMenuBar.this.hasDocument() && super.isEnabled();
}
}
public static class NewAction extends AbstractAction {
public NewAction() {
putValue(NAME, "New");
putValue(ACCELERATOR_KEY, PlatformUtils.getKeyStroke(KeyEvent.VK_N));
}
public void actionPerformed(ActionEvent e) {
Application.getInstance().createNewDocument();
}
}
public class OpenAction extends AbstractAction {
public OpenAction() {
putValue(NAME, "Open...");
putValue(ACCELERATOR_KEY, PlatformUtils.getKeyStroke(KeyEvent.VK_O));
}
public void actionPerformed(ActionEvent e) {
File chosenFile = FileUtils.showOpenDialog(getDocument(), NodeBoxDocument.lastFilePath, "ndbx", "NodeBox Document");
if (chosenFile != null) {
Application.getInstance().openDocument(chosenFile);
}
}
}
public static class OpenRecentAction extends AbstractAction {
private File file;
public OpenRecentAction(File file) {
this.file = file;
putValue(NAME, file.getName());
}
public void actionPerformed(ActionEvent e) {
Application.getInstance().openDocument(file);
}
}
public class CloseAction extends AbstractDocumentAction {
public CloseAction() {
putValue(NAME, "Close");
putValue(ACCELERATOR_KEY, PlatformUtils.getKeyStroke(KeyEvent.VK_W));
}
public void actionPerformed(ActionEvent e) {
getDocument().close();
}
}
public class SaveAction extends AbstractDocumentAction {
public SaveAction() {
putValue(NAME, "Save");
putValue(ACCELERATOR_KEY, PlatformUtils.getKeyStroke(KeyEvent.VK_S));
}
public void actionPerformed(ActionEvent e) {
getDocument().save();
}
}
public class SaveAsAction extends AbstractDocumentAction {
public SaveAsAction() {
putValue(NAME, "Save As...");
putValue(ACCELERATOR_KEY, PlatformUtils.getKeyStroke(KeyEvent.VK_S, Event.SHIFT_MASK));
}
public void actionPerformed(ActionEvent e) {
getDocument().saveAs();
}
}
public class RevertAction extends AbstractDocumentAction {
public RevertAction() {
putValue(NAME, "Revert to Saved");
}
public void actionPerformed(ActionEvent e) {
getDocument().revert();
}
}
public class ExportAction extends AbstractDocumentAction {
public ExportAction() {
putValue(NAME, "Export...");
putValue(ACCELERATOR_KEY, PlatformUtils.getKeyStroke(KeyEvent.VK_E));
}
public void actionPerformed(ActionEvent e) {
getDocument().export();
}
}
public class ExportRangeAction extends AbstractDocumentAction {
public ExportRangeAction() {
putValue(NAME, "Export Range...");
putValue(ACCELERATOR_KEY, PlatformUtils.getKeyStroke(KeyEvent.VK_E, Event.SHIFT_MASK));
}
public void actionPerformed(ActionEvent e) {
getDocument().exportRange();
}
}
public class ExportMovieAction extends AbstractDocumentAction {
public ExportMovieAction() {
putValue(NAME, "Export Movie...");
}
public void actionPerformed(ActionEvent e) {
getDocument().exportMovie();
}
}
public static class ExitAction extends AbstractAction {
public ExitAction() {
putValue(NAME, "Exit");
}
public void actionPerformed(ActionEvent e) {
Application.getInstance().quit();
}
}
public class UndoAction extends AbstractDocumentAction {
private String undoText = UIManager.getString("AbstractUndoableEdit.undoText");
public UndoAction() {
putValue(NAME, undoText);
putValue(ACCELERATOR_KEY, PlatformUtils.getKeyStroke(KeyEvent.VK_Z));
setEnabled(false);
}
public void actionPerformed(ActionEvent e) {
Component c = getDocument().getFocusOwner();
if (c instanceof CodeArea) {
((CodeArea) c).undo();
} else {
getDocument().undo();
}
updateUndoRedoState();
}
public void update() {
Component c = getDocument().getFocusOwner();
if (c instanceof CodeArea) {
setEnabled(true);
} else {
if (undoManager != null && undoManager.canUndo()) {
setEnabled(true);
putValue(Action.NAME, undoManager.getUndoPresentationName());
} else {
setEnabled(false);
putValue(Action.NAME, undoText);
}
}
}
}
public class RedoAction extends AbstractDocumentAction {
private String redoText = UIManager.getString("AbstractUndoableEdit.redoText");
public RedoAction() {
putValue(NAME, redoText);
putValue(ACCELERATOR_KEY, PlatformUtils.getKeyStroke(KeyEvent.VK_Z, Event.SHIFT_MASK));
setEnabled(false);
}
public void actionPerformed(ActionEvent e) {
Component c = getDocument().getFocusOwner();
if (c instanceof CodeArea) {
((CodeArea) c).redo();
} else {
getDocument().redo();
}
updateUndoRedoState();
}
public void update() {
Component c = getDocument().getFocusOwner();
if (c instanceof CodeArea) {
setEnabled(true);
} else {
if (undoManager != null && undoManager.canRedo()) {
setEnabled(true);
putValue(Action.NAME, undoManager.getRedoPresentationName());
} else {
setEnabled(false);
putValue(Action.NAME, redoText);
}
}
}
}
public class CutAction extends AbstractDocumentAction {
public CutAction() {
putValue(NAME, "Cut");
putValue(ACCELERATOR_KEY, PlatformUtils.getKeyStroke(KeyEvent.VK_X));
}
public void actionPerformed(ActionEvent e) {
getDocument().cut();
}
}
public class CopyAction extends AbstractDocumentAction {
public CopyAction() {
putValue(NAME, "Copy");
putValue(ACCELERATOR_KEY, PlatformUtils.getKeyStroke(KeyEvent.VK_C));
}
public void actionPerformed(ActionEvent e) {
getDocument().copy();
}
}
public class PasteAction extends AbstractDocumentAction {
public PasteAction() {
putValue(NAME, "Paste");
putValue(ACCELERATOR_KEY, PlatformUtils.getKeyStroke(KeyEvent.VK_V));
}
public void actionPerformed(ActionEvent e) {
getDocument().paste();
}
}
public class DeleteAction extends AbstractDocumentAction {
public DeleteAction() {
putValue(NAME, "Delete");
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0));
}
public void actionPerformed(ActionEvent e) {
getDocument().deleteSelection();
}
}
public static class PreferencesAction extends AbstractAction {
public PreferencesAction() {
putValue(NAME, "Preferences");
}
public void actionPerformed(ActionEvent e) {
Application.getInstance().showPreferences();
}
}
public class NewNodeAction extends AbstractDocumentAction {
public NewNodeAction() {
putValue(NAME, "Create New Node...");
putValue(ACCELERATOR_KEY, PlatformUtils.getKeyStroke(KeyEvent.VK_N, Event.SHIFT_MASK));
}
public void actionPerformed(ActionEvent e) {
document.createNewNode();
}
}
public class ReloadAction extends AbstractDocumentAction {
public ReloadAction() {
putValue(NAME, "Reload");
putValue(ACCELERATOR_KEY, PlatformUtils.getKeyStroke(KeyEvent.VK_R));
}
public void actionPerformed(ActionEvent e) {
getDocument().reload();
}
}
// public class NewLibraryAction extends AbstractAction {
// public NewLibraryAction() {
// putValue(NAME, "New Library...");
// }
//
// public void actionPerformed(ActionEvent e) {
// String libraryName = JOptionPane.showInputDialog(NodeBoxDocument.this, "Enter the name for the new library", "Create New Library", JOptionPane.QUESTION_MESSAGE);
// if (libraryName == null || libraryName.trim().length() == 0) return;
// createNewLibrary(libraryName);
// }
// }
public class MinimizeAction extends AbstractDocumentAction {
public MinimizeAction() {
putValue(NAME, "Minimize");
putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_M, KeyEvent.META_MASK));
}
public void actionPerformed(ActionEvent e) {
getDocument().setState(Frame.ICONIFIED);
}
}
public class ZoomAction extends AbstractDocumentAction {
public ZoomAction() {
putValue(NAME, "Zoom");
}
public void actionPerformed(ActionEvent e) {
// TODO: Implement
Toolkit.getDefaultToolkit().beep();
}
}
public class ShowConsoleAction extends AbstractDocumentAction {
public ShowConsoleAction() {
putValue(NAME, "Show Console");
}
public void actionPerformed(ActionEvent e) {
Application instance = Application.getInstance();
if (instance.isConsoleOpened())
instance.hideConsole();
else
instance.showConsole();
}
}
public class BringAllToFrontAction extends AbstractDocumentAction {
public BringAllToFrontAction() {
putValue(NAME, "Bring All to Front");
}
public void actionPerformed(ActionEvent e) {
// TODO: Implement
Toolkit.getDefaultToolkit().beep();
}
}
public static class AboutAction extends AbstractAction {
public AboutAction() {
super("About");
}
public void actionPerformed(ActionEvent e) {
Application.getInstance().showAbout();
}
}
public static class NodeboxSiteAction extends AbstractAction {
public NodeboxSiteAction() {
putValue(NAME, "NodeBox Site");
}
public void actionPerformed(ActionEvent e) {
PlatformUtils.openURL("http://beta.nodebox.net/");
}
}
public static class GettingStartedAction extends AbstractAction {
public GettingStartedAction() {
super("Getting Started");
}
public void actionPerformed(ActionEvent e) {
PlatformUtils.openURL("http://beta.nodebox.net/documentation/getting-started/");
}
}
public static class HelpAndSupportAction extends AbstractAction {
public HelpAndSupportAction() {
super("Help and Support");
}
public void actionPerformed(ActionEvent e) {
PlatformUtils.openURL("http://beta.nodebox.net/documentation/");
}
}
public static class CheckForUpdatesAction extends AbstractAction {
public CheckForUpdatesAction() {
super("Check for Updates...");
}
public void actionPerformed(ActionEvent e) {
Application.getInstance().getUpdater().checkForUpdates();
}
}
}