-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathNodeBoxDocument.java
More file actions
1350 lines (1171 loc) · 45.8 KB
/
NodeBoxDocument.java
File metadata and controls
1350 lines (1171 loc) · 45.8 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
package nodebox.client;
import nodebox.client.movie.Movie;
import nodebox.client.movie.VideoFormat;
import nodebox.handle.Handle;
import nodebox.handle.HandleDelegate;
import nodebox.node.*;
import javax.swing.*;
import javax.swing.undo.UndoManager;
import java.awt.*;
import java.awt.event.AWTEventListener;
import java.awt.event.MouseEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.geom.Area;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import static nodebox.base.Preconditions.checkArgument;
import static nodebox.base.Preconditions.checkNotNull;
/**
* A NodeBoxDocument manages a NodeLibrary.
*/
public class NodeBoxDocument extends JFrame implements WindowListener, ViewerEventListener, HandleDelegate {
private final static String WINDOW_MODIFIED = "windowModified";
public static String lastFilePath;
public static String lastExportPath;
private static NodeLibrary clipboardLibrary;
private int pasteCount = 0;
private File documentFile;
private boolean documentChanged;
private static Logger logger = Logger.getLogger("nodebox.client.NodeBoxDocument");
private AnimationTimer animationTimer;
private ArrayList<ParameterEditor> parameterEditors = new ArrayList<ParameterEditor>();
private boolean loaded = false;
private SpotlightPanel spotlightPanel;
private UndoManager undoManager = new UndoManager();
private boolean holdEdits = false;
private String lastEditType = null;
private Object lastEditObject = null;
private NodeLibrary nodeLibrary;
private Node activeNetwork;
private Node activeNode;
// GUI components
private final NodeBoxMenuBar menuBar;
private final AnimationBar animationBar;
private final AddressBar addressBar;
private final Viewer viewer;
private final EditorPane editorPane;
private final ParameterView parameterView;
private final NetworkView networkView;
private JSplitPane viewEditorSplit;
private JSplitPane parameterNetworkSplit;
private JSplitPane topSplit;
public static NodeBoxDocument getCurrentDocument() {
return Application.getInstance().getCurrentDocument();
}
public static NodeLibraryManager getManager() {
return Application.getInstance().getManager();
}
public static NodeLibrary getNodeClipboard() {
return clipboardLibrary;
}
public static void setNodeClipboard(NodeLibrary clipboardLibrary) {
NodeBoxDocument.clipboardLibrary = clipboardLibrary;
for (NodeBoxDocument doc : Application.getInstance().getDocuments())
doc.pasteCount = 0;
}
public NodeBoxDocument(NodeLibrary library) {
setNodeLibrary(library);
JPanel rootPanel = new JPanel(new BorderLayout());
ViewerPane viewerPane = new ViewerPane(this);
viewer = viewerPane.getViewer();
editorPane = new EditorPane(this);
ParameterPane parameterPane = new ParameterPane();
parameterPane.setEditMetadataListener(new ParameterPane.EditMetadataListener() {
public void onEditMetadata() {
if (activeNode == null) return;
JDialog editorDialog = new NodeAttributesDialog(NodeBoxDocument.this);
editorDialog.setSize(580, 751);
editorDialog.setLocationRelativeTo(NodeBoxDocument.this);
editorDialog.setVisible(true);
}
});
parameterView = parameterPane.getParameterView();
parameterView.setDocument(this); // TODO Remove this once parameter view is fully decoupled.
NetworkPane networkPane = new NetworkPane(this);
networkView = networkPane.getNetworkView();
networkView.setDelegate(new NetworkView.Delegate() {
public void activeNodeChanged(Node node) {
setActiveNode(node);
}
});
viewEditorSplit = new CustomSplitPane(JSplitPane.VERTICAL_SPLIT, viewerPane, editorPane);
parameterNetworkSplit = new CustomSplitPane(JSplitPane.VERTICAL_SPLIT, parameterPane, networkPane);
topSplit = new CustomSplitPane(JSplitPane.HORIZONTAL_SPLIT, viewEditorSplit, parameterNetworkSplit);
addressBar = new AddressBar();
addressBar.setOnPartClickListener(new AddressBar.OnPartClickListener() {
public void onPartClicked(Node n) {
setActiveNetwork(n);
}
});
rootPanel.add(addressBar, BorderLayout.NORTH);
rootPanel.add(topSplit, BorderLayout.CENTER);
// Animation properties.
animationTimer = new AnimationTimer(this);
animationBar = new AnimationBar(this);
rootPanel.add(animationBar, BorderLayout.SOUTH);
setContentPane(rootPanel);
setLocationByPlatform(true);
setSize(1100, 800);
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
addWindowListener(this);
updateTitle();
menuBar = new NodeBoxMenuBar(this);
setJMenuBar(menuBar);
loaded = true;
setActiveNetwork(library.getRootNode());
// setActiveNode is not called because it registers that the current node is already null.
// The parameter view is a special case since it does need to show something when the active node is null.
Node rootNode = nodeLibrary.getRootNode();
if (rootNode != null && rootNode.getRenderedChild() != null)
parameterView.setActiveNode(rootNode.getRenderedChild());
else
parameterView.setActiveNode(rootNode);
spotlightPanel = new SpotlightPanel(networkPane);
setGlassPane(spotlightPanel);
spotlightPanel.setVisible(true);
spotlightPanel.setOpaque(false);
}
public NodeBoxDocument(File file) throws RuntimeException {
this(NodeLibrary.load(file, Application.getInstance().getManager()));
lastFilePath = file.getParentFile().getAbsolutePath();
setDocumentFile(file);
spotlightPanel.hideSpotlightPanel();
}
//// Node Library management ////
public NodeLibrary getNodeLibrary() {
return nodeLibrary;
}
public void setNodeLibrary(NodeLibrary newLibrary) {
checkNotNull(newLibrary, "Node library cannot be null.");
boolean startingUp = this.nodeLibrary == null;
this.nodeLibrary = newLibrary;
if (!startingUp) {
setActiveNetwork(newLibrary.getRootNode());
}
}
//// Node operations ////
/**
* Create a node in the active network.
* This node is based on a prototype.
*
* @param prototype The prototype node.
* @param pt The initial node position.
*/
public void createNode(Node prototype, Point pt) {
startEdits("Create Node");
Node n = getActiveNetwork().create(prototype);
setNodePosition(n, new nodebox.graphics.Point(pt));
setRenderedNode(n);
setActiveNode(n);
stopEdits();
networkView.updateNodes();
networkView.setActiveNode(activeNode);
parameterView.setActiveNode(activeNode);
editorPane.setActiveNode(activeNode);
}
/**
* Change the node position of the given node.
*
* @param node the node to move
* @param point the point to move to
*/
public void setNodePosition(Node node, nodebox.graphics.Point point) {
// Note that we're passing in the parent network of the node.
// This means that all move changes to the parent network are grouped
// together under one edit, instead of for each node individually.
addEdit("Move Node", "moveNode", node.getParent());
node.setPosition(point);
networkView.updatePosition(node);
}
/**
* Change the node name.
*
* @param node The node to rename.
* @param name The new node name.
*/
public void setNodeName(Node node, String name) {
node.setName(name);
networkView.updateNodes();
// Renaming the node can have an effect on expressions, so recalculate the network.
render();
}
/**
* Set the node metadata to the given metadata.
* Note that this method is not called when the node position or name changes.
*
* @param node The node to change.
* @param metadata A map of metadata.
*/
public void setNodeMetadata(Node node, Object metadata) {
// TODO: Implement
// TODO: Make NodeAttributesEditor use this.
// Metadata changes could mean the icon has changed.
networkView.updateNodes();
if (node == activeNode) {
parameterView.updateAll();
// Updating the metadata could cause changes to a handle.
viewer.repaint();
}
render();
}
/**
* Change the rendered node to the given node
*
* @param node the node to set rendered
*/
public void setRenderedNode(Node node) {
addEdit("Set Rendered");
node.setRendered();
networkView.updateNodes();
networkView.setActiveNode(activeNode);
render();
}
public void setNodeExported(Node node, boolean exported) {
addEdit("Set Exported");
node.setExported(exported);
}
/**
* Remove the given node from the active network.
*
* @param node The node to remove.
*/
public void removeNode(Node node) {
addEdit("Remove Node");
removeNodeImpl(node);
networkView.updateAll();
render();
}
/**
* Remove the given nodes from the active network.
*
* @param nodes The node to remove.
*/
public void removeNodes(Iterable<Node> nodes) {
addEdit("Delete Nodes");
for (Node node : nodes) {
removeNodeImpl(node);
}
networkView.updateAll();
render();
}
/**
* Helper method used by removeNode and removeNodes to do the removal and update the parameter view, if needed.
*
* @param node The node to remove.
*/
private void removeNodeImpl(Node node) {
checkNotNull(node, "Node to remove cannot be null.");
checkArgument(node.getParent() == activeNetwork, "Node to remove is not in active network.");
getActiveNetwork().remove(node);
// If the removed node was the active one, reset the parameter view.
if (node == activeNode) {
setActiveNode(null);
}
}
/**
* Create a connection from the given output to the given input.
*
* @param output the output port
* @param input the input port
*/
public void connect(Port output, Port input) {
addEdit("Connect");
getActiveNetwork().connectChildren(input, output);
if (input.getNode() == activeNode) {
parameterView.updateConnectionPanel();
}
networkView.updateConnections();
render();
}
/**
* Changes the ordering of output connections by moving the given connection a specified number of positions.
* <p/>
* To move the specified connection up one position, set the deltaIndex to -1. To move a connection down, set
* the deltaIndex to 1.
* <p/>
* If the delta index is larger or smaller than the number of positions this connection can move, it will
* move the connection to the beginning or end. This will not result in an error.
*
* @param connection the connection to reorder
* @param deltaIndex the number of places to move.
* @param multi the connection should only be reordered among connections connected to the same input port (with cardinality MULTIPLE).
*/
public void reorderConnection(Connection connection, int deltaIndex, boolean multi) {
connection.getInput().getParentNode().reorderConnection(connection, deltaIndex, multi);
parameterView.updateConnectionPanel();
networkView.updateConnections();
render();
}
/**
* Remove the given connection from the network.
*
* @param connection the connection to remove
*/
public void disconnect(Connection connection) {
addEdit("Disconnect");
getActiveNetwork().disconnect(connection);
networkView.updateConnections();
if (connection.getInputNode() == activeNode) {
parameterView.updateConnectionPanel();
}
render();
}
/**
* Copy children of this network to the new parent.
*
* @param children the children to copy
* @param oldParent the old parent
* @param newParent the new parent
* @return the newly copied node
*/
public Collection<Node> copyChildren(Collection<Node> children, Node oldParent, Node newParent) {
return oldParent.copyChildren(children, newParent);
}
/**
* @param node the node on which to add the parameter
* @param parameterName the name of the new parameter
*/
public void addParameter(Node node, String parameterName) {
addEdit("Add Parameter");
Parameter parameter = node.addParameter(parameterName, Parameter.Type.FLOAT);
if (node == activeNode) {
parameterView.updateAll();
viewer.repaint();
}
}
/**
* @param node the node on which to remove the parameter
* @param parameterName the name of the parameter
*/
public void removeParameter(Node node, String parameterName) {
addEdit("Remove Parameter");
node.removeParameter(parameterName);
if (node == activeNode) {
parameterView.updateAll();
viewer.repaint();
}
}
public void addPort(Node node, String portName, Port.Cardinality cardinality) {
addEdit("Add Port");
Port port = node.addPort(portName, cardinality);
if (node == activeNode && port.getCardinality() == Port.Cardinality.MULTIPLE) {
parameterView.updateAll();
}
networkView.updateNodes();
}
/**
* Set the parameter to the given value.
*
* @param parameter the parameter to set
* @param value the new value
*/
public void setParameterValue(Parameter parameter, Object value) {
checkNotNull(parameter, "Parameter cannot be null.");
addEdit("Change Value", "changeValue", parameter);
parameter.set(value);
if (parameter.getNode() == nodeLibrary.getRootNode()) {
nodeLibrary.setVariable(parameter.getName(), parameter.asString());
}
parameterView.updateParameterValue(parameter, value);
// Setting a parameter might change enable expressions, and thus change the enabled state of a parameter row.
parameterView.updateEnabledState();
// Setting a parameter might change the enabled state of the handle.
viewer.setHandleEnabled(activeNode != null && activeNode.hasEnabledHandle());
if (parameter.getName().equals("_image"))
networkView.updateNodes();
render();
}
public void setParameterExpression(Parameter parameter, String expression) {
addEdit("Change Parameter Expression");
parameter.setExpression(expression);
parameterView.updateParameter(parameter);
render();
}
public void clearParameterExpression(Parameter parameter) {
addEdit("Clear Parameter Expression");
parameter.clearExpression();
parameterView.updateParameter(parameter);
render();
}
public void revertParameterToDefault(Parameter parameter) {
addEdit("Revert Parameter to Default");
parameter.revertToDefault();
parameterView.updateParameter(parameter);
render();
}
public void setParameterLabel(Parameter parameter, String label) {
addEdit("Set Parameter Label");
parameter.setLabel(label);
parameterView.updateParameter(parameter);
}
public void setParameterHelpText(Parameter parameter, String helpText) {
addEdit("Set Parameter Help Text");
parameter.setHelpText(helpText);
parameterView.updateParameter(parameter);
}
public void setParameterWidget(Parameter parameter, Parameter.Widget widget) {
addEdit("Set Parameter Widget");
parameter.setWidget(widget);
parameterView.updateParameter(parameter);
render();
}
public void setParameterEnableExpression(Parameter parameter, String enableExpression) {
addEdit("Set Parameter Enable Expression");
parameter.setEnableExpression(enableExpression);
parameterView.updateParameter(parameter);
render();
}
public void setParameterBoundingMethod(Parameter parameter, Parameter.BoundingMethod method) {
addEdit("Set Parameter Bounding Method");
parameter.setBoundingMethod(method);
parameterView.updateParameter(parameter);
render();
}
public void setParameterMinimumValue(Parameter parameter, Float minimumValue) {
addEdit("Set Parameter Minimum Value");
parameter.setMinimumValue(minimumValue);
parameterView.updateParameter(parameter);
render();
}
public void setParameterMaximumValue(Parameter parameter, Float maximumValue) {
addEdit("Set Parameter Maximum Value");
parameter.setMaximumValue(maximumValue);
parameterView.updateParameter(parameter);
render();
}
public void setParameterDisplayLevel(Parameter parameter, Parameter.DisplayLevel displayLevel) {
addEdit("Set Parameter Display Level");
parameter.setDisplayLevel(displayLevel);
parameterView.updateParameter(parameter);
}
public void addParameterMenuItem(Parameter parameter, String key, String label) {
addEdit("Add Parameter Menu Item");
parameter.addMenuItem(key, label);
parameterView.updateParameter(parameter);
render();
}
public void removeParameterMenuItem(Parameter parameter, Parameter.MenuItem item) {
addEdit("Remove Parameter Menu Item");
parameter.removeMenuItem(item);
parameterView.updateParameter(parameter);
render();
}
public void moveParameterMenuItemDown(Parameter parameter, int itemIndex) {
addEdit("Move Parameter Item Down");
parameter.moveMenuItemDown(itemIndex);
parameterView.updateParameter(parameter);
}
public void moveParameterMenuItemUp(Parameter parameter, int itemIndex) {
addEdit("Move Parameter Item Up");
parameter.moveMenuItemUp(itemIndex);
parameterView.updateParameter(parameter);
}
public void updateParameterMenuItem(Parameter parameter, int itemIndex, String key, String label) {
addEdit("Update Parameter Menu Item");
parameter.updateMenuItem(itemIndex, key, label);
parameterView.updateParameter(parameter);
}
//// Editor pane callbacks ////
public void codeEdited(String source) {
networkView.codeChanged(activeNode, true);
}
//// HandleDelegate implementation ////
// TODO Merge setParameterValue and setValue.
public void setValue(Node node, String parameterName, Object value) {
checkNotNull(node, "Node cannot be null");
Parameter parameter = node.getParameter(parameterName);
checkNotNull(parameter, "Parameter '" + parameterName + "' is not a parameter on node " + node);
setParameterValue(parameter, value);
}
public void silentSet(Node node, String parameterName, Object value) {
try {
Parameter parameter = node.getParameter(parameterName);
setParameterValue(parameter, value);
} catch (Exception ignored) {
}
}
// TODO Merge stopEditing and stopCombiningEdits.
public void stopEditing(Node node) {
stopEdits();
stopCombiningEdits();
}
public void updateHandle(Node node) {
if (viewer.getHandle() != null)
viewer.getHandle().update();
// TODO Make viewer repaint more fine-grained.
viewer.repaint();
}
//// Active network / node ////
/**
* Return the network that is currently "open": shown in the network view.
*
* @return The currently active network.
*/
public Node getActiveNetwork() {
return activeNetwork;
}
public String getActiveNetworkPath() {
if (activeNetwork == null) return "";
return activeNetwork.getAbsolutePath();
}
public void setActiveNetwork(Node activeNetwork) {
checkNotNull(activeNetwork, "Active network cannot be null.");
this.activeNetwork = activeNetwork;
if (activeNetwork.getRenderedChild() != null) {
setActiveNode(activeNetwork.getRenderedChild());
} else if (!activeNetwork.isEmpty()) {
setActiveNode(activeNetwork.getChildAt(0));
} else {
setActiveNode(null);
}
addressBar.setActiveNetwork(activeNetwork);
viewer.setHandleEnabled(activeNode != null && activeNode.hasEnabledHandle());
viewer.repaint();
networkView.setActiveNetwork(activeNetwork);
networkView.setActiveNode(activeNode);
render();
}
public void setActiveNetwork(String path) {
Node network = nodeLibrary.getNodeForPath(path);
setActiveNetwork(network);
}
/**
* Return the node that is currently focused:
* visible in the parameter view, and whose handles are displayed in the viewer.
*
* @return
*/
public Node getActiveNode() {
return activeNode;
}
public String getActiveNodePath() {
if (activeNode == null) return "";
return activeNode.getAbsolutePath();
}
/**
* Set the active node to the given node.
* <p/>
* The active node is the one whose parameters are displayed in the parameter pane,
* and whose handle is displayed in the viewer.
* <p/>
* This will also change the active network if necessary.
*
* @param node the node to change to.
*/
public void setActiveNode(Node node) {
stopCombiningEdits();
if (activeNode == node) return;
activeNode = node;
createHandleForActiveNode();
viewer.repaint();
parameterView.setActiveNode(activeNode == null ? nodeLibrary.getRootNode() : activeNode);
networkView.setActiveNode(activeNode);
editorPane.setActiveNode(activeNode);
}
private void createHandleForActiveNode() {
if (activeNode != null) {
Handle handle = null;
try {
handle = activeNode.createHandle();
// If the handle was created successfully, remove the messages.
editorPane.clearMessages();
} catch (Exception e) {
editorPane.setMessages(e.toString());
}
if (handle != null) {
handle.setHandleDelegate(this);
// TODO Remove this. Find out why the handle needs access to the viewer (only repaint?) and put that in the HandleDelegate.
handle.setViewer(viewer);
viewer.setHandleEnabled(activeNode.hasEnabledHandle());
}
viewer.setHandle(handle);
} else {
viewer.setHandle(null);
}
}
//// Animation ////
public float getFrame() {
return nodeLibrary.getFrame();
}
public void setFrame(float frame) {
nodeLibrary.setFrame(frame);
animationBar.updateFrame();
render();
}
public void nextFrame() {
setFrame(getFrame() + 1);
}
public void playAnimation() {
animationTimer.start();
}
public void stopAnimation() {
animationTimer.stop();
}
public void rewindAnimation() {
stopAnimation();
setFrame(1);
}
//// Rendering ////
/**
* Called when the active network will start rendering.
* Called on the Swing EDT so you can update the GUI.
*
* @param context The processing context.
*/
public void startRendering(ProcessingContext context) {
addressBar.setProgressVisible(true);
}
/**
* Called when the active network has finished rendering.
*
* @param context The processing context.
*/
public void finishedRendering(ProcessingContext context) {
addressBar.setProgressVisible(false);
editorPane.updateMessages(activeNode, context);
viewer.setOutputValue(activeNetwork.getOutputValue());
networkView.checkErrorAndRepaint();
// TODO I don't know if this is the best way to do this.
if (viewer.getHandle() != null)
viewer.getHandle().update();
}
private void render() {
if (!loaded) return;
if (!activeNetwork.isDirty()) return;
final ProcessingContext context = new ProcessingContext(activeNetwork);
startRendering(context);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// If meanwhile the node has been marked clean, ignore the event.
// This avoids double renders.
if (!activeNetwork.isDirty()) return;
try {
activeNetwork.update(context);
} catch (ProcessingError processingError) {
Logger.getLogger("NodeBoxDocument").log(Level.WARNING, "Error while processing", processingError);
} finally {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
finishedRendering(context);
}
});
}
}
});
}
public void setActiveNodeCode(Parameter codeParameter, String source) {
if (activeNode == null) return;
if (codeParameter == null) return;
NodeCode code = new PythonCode(source);
codeParameter.set(code);
if (codeParameter.getName().equals("_handle")) {
createHandleForActiveNode();
}
networkView.codeChanged(activeNode, false);
render();
}
//// Undo ////
/**
* Edits are no longer recorded until you call stopEdits. This allows you to batch edits.
*
* @param command the command name of the edit batch
*/
public void startEdits(String command) {
addEdit(command);
holdEdits = true;
}
/**
* Edits are recorded again.
*/
public void stopEdits() {
holdEdits = false;
}
/**
* Add an edit to the undo manager.
* <p/>
* Since we don't specify the edit type or name, further edits will not be staggered.
*
* @param command the command name.
*/
public void addEdit(String command) {
if (!holdEdits) {
markChanged();
undoManager.addEdit(new NodeLibraryUndoableEdit(this, command));
menuBar.updateUndoRedoState();
stopCombiningEdits();
}
}
/**
* Add an edit to the undo manager.
*
* @param command the command name.
* @param type the type of edit
* @param object the edited object. This will be compared using ==.
*/
public void addEdit(String command, String type, Object object) {
spotlightPanel.hideSpotlightPanel();
if (!holdEdits) {
markChanged();
if (lastEditType != null && lastEditType.equals(type) && lastEditObject == object) {
// If the last edit type and last edit id are the same,
// we combine the two edits into one.
// Since we've already saved the last state, we don't need to do anything.
} else {
addEdit(command);
lastEditType = type;
lastEditObject = object;
}
}
}
/**
* Normally edits of the same type and object are combined into one.
* Calling this method will ensure that you create a new edit.
* <p/>
* Use this method e.g. for breaking apart overzealous edit grouping.
*/
public void stopCombiningEdits() {
// We just reset the last edit type and object so that addEdit will be forced to create a new edit.
lastEditType = null;
lastEditObject = null;
}
public UndoManager getUndoManager() {
return undoManager;
}
public void undo() {
if (!undoManager.canUndo()) return;
undoManager.undo();
menuBar.updateUndoRedoState();
}
public void redo() {
if (!undoManager.canRedo()) return;
undoManager.redo();
menuBar.updateUndoRedoState();
}
//// Parameter editor actions ////
public void addParameterEditor(ParameterEditor editor) {
if (parameterEditors.contains(editor)) return;
parameterEditors.add(editor);
}
public void removeParameterEditor(ParameterEditor editor) {
parameterEditors.remove(editor);
}
//// Code editor actions ////
public void fireCodeChanged(Node node, boolean changed) {
networkView.codeChanged(node, changed);
}
//// Document actions ////
public File getDocumentFile() {
return documentFile;
}
public void setDocumentFile(File documentFile) {
this.documentFile = documentFile;
updateTitle();
}
public boolean isChanged() {
return documentChanged;
}
public boolean close() {
stopAnimation();
if (shouldClose()) {
//renderThread.shutdown();
Application.getInstance().getManager().remove(nodeLibrary);
Application.getInstance().removeDocument(NodeBoxDocument.this);
for (ParameterEditor editor : parameterEditors) {
editor.dispose();
}
dispose();
// On Mac the application does not close if the last window is closed.
if (!PlatformUtils.onMac()) {
// If there are no more documents, exit the application.
if (Application.getInstance().getDocumentCount() == 0) {
System.exit(0);
}
}
return true;
} else {
return false;
}
}
private boolean shouldClose() {
if (isChanged()) {
SaveDialog sd = new SaveDialog();
int retVal = sd.show(this);
if (retVal == JOptionPane.YES_OPTION) {
return save();
} else if (retVal == JOptionPane.NO_OPTION) {
return true;
} else if (retVal == JOptionPane.CANCEL_OPTION) {
return false;
}
}
return true;
}
public boolean save() {
if (documentFile == null) {
return saveAs();
} else {
boolean saved = saveToFile(documentFile);
if (saved)
NodeBoxMenuBar.addRecentFile(documentFile);
return saved;
}
}
public boolean saveAs() {
File chosenFile = FileUtils.showSaveDialog(this, lastFilePath, "ndbx", "NodeBox File");
if (chosenFile != null) {
if (!chosenFile.getAbsolutePath().endsWith(".ndbx")) {
chosenFile = new File(chosenFile.getAbsolutePath() + ".ndbx");
}
lastFilePath = chosenFile.getParentFile().getAbsolutePath();
setDocumentFile(chosenFile);
boolean saved = saveToFile(documentFile);
if (saved)
NodeBoxMenuBar.addRecentFile(documentFile);
return saved;
}
return false;
}
public void revert() {
// TODO: Implement revert
JOptionPane.showMessageDialog(this, "Revert is not implemented yet.", "NodeBox", JOptionPane.ERROR_MESSAGE);
}
private boolean saveToFile(File file) {
try {
nodeLibrary.store(file);
} catch (IOException e) {
JOptionPane.showMessageDialog(this, "An error occurred while saving the file.", "NodeBox", JOptionPane.ERROR_MESSAGE);
logger.log(Level.SEVERE, "An error occurred while saving the file.", e);
return false;
}
documentChanged = false;
updateTitle();
return true;
}
private boolean exportToFile(File file, ImageFormat format) {
return exportToFile(file, activeNetwork, format);
}
private boolean exportToFile(File file, Node exportNetwork, ImageFormat format) {
file = format.ensureFileExtension(file);
if (exportNetwork == null) return false;
Object outputValue = exportNetwork.getOutputValue();
if (outputValue instanceof nodebox.graphics.Canvas) {
nodebox.graphics.Canvas c = (nodebox.graphics.Canvas) outputValue;
c.save(file);
return true;