-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathDrawingFrame.java
More file actions
1168 lines (1083 loc) · 40.2 KB
/
DrawingFrame.java
File metadata and controls
1168 lines (1083 loc) · 40.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
/*
* Open Source Physics software is free software as described near the bottom of this code file.
*
* For additional information and documentation on Open Source Physics please see:
* <http://www.opensourcephysics.org/>
*/
package org.opensourcephysics.display;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.HeadlessException;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.opensourcephysics.controls.OSPLog;
import org.opensourcephysics.controls.XML;
import org.opensourcephysics.controls.XMLControl;
import org.opensourcephysics.controls.XMLControlElement;
import org.opensourcephysics.controls.XMLProperty;
import org.opensourcephysics.controls.XMLTreeChooser;
import org.opensourcephysics.controls.XMLTreePanel;
import org.opensourcephysics.display.axes.DrawableAxes;
import org.opensourcephysics.tools.FontSizer;
import org.opensourcephysics.tools.Job;
import org.opensourcephysics.tools.LocalJob;
import org.opensourcephysics.tools.SnapshotTool;
import org.opensourcephysics.tools.Tool;
import org.opensourcephysics.tools.VideoTool;
/**
* Drawing Frame: a frame that contains a drawing panel.
*
* @author Wolfgang Christian
* @created July 16, 2004
* @version 1.1
*/
public class DrawingFrame extends OSPFrame implements ClipboardOwner {
// protected static String resourcesPath = "/org/opensourcephysics/resources/display/";
// protected static String defaultToolsFileName = "drawing_tools.xml";
protected JMenu fileMenu, editMenu;
protected JMenuItem copyItem, pasteItem, replaceItem;
protected DrawingPanel drawingPanel;
protected final static int MENU_SHORTCUT_KEY_MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
protected Window customInspector; // optional custom inspector for this frame
protected Tool reply;
/**
* DrawingFrame constructor that creates a default DrawingPanel.
*
* The default DrawingPanel is an InteractivePanel.
*/
public DrawingFrame() {
this(DisplayRes.getString("DrawingFrame.DefaultTitle"), new InteractivePanel()); //$NON-NLS-1$
}
/**
* DrawingFrame constructor specifying the DrawingPanel that will be placed
* in the center of the content pane.
*
* @param drawingPanel
*/
public DrawingFrame(DrawingPanel drawingPanel) {
this(DisplayRes.getString("DrawingFrame.DefaultTitle"), drawingPanel); //$NON-NLS-1$
}
/**
* DrawingFrame constructor specifying the title and the DrawingPanel that
* will be placed in the center of the content pane.
*
* @param title
* @param _drawingPanel
*/
public DrawingFrame(String title, DrawingPanel _drawingPanel) {
super(title);
// forces dependence on the DatasetTool class;
// add or remove the next line if you do not want the DatasetTool in your jar
// org.opensourcephysics.tools.DataTool.loadClass = true;
drawingPanel = _drawingPanel;
if(drawingPanel!=null) {
getContentPane().add(drawingPanel, BorderLayout.CENTER);
}
getContentPane().add(buttonPanel, BorderLayout.SOUTH); // buttons are added using addButton method.
pack();
if(!OSPRuntime.appletMode) {
createMenuBar();
}
// responds to data from the Data Tool
reply = new Tool() {
public void send(Job job, Tool replyTo) throws RemoteException {
XMLControlElement control = new XMLControlElement();
try {
control.readXML(job.getXML());
} catch(RemoteException ex) {}
ArrayList<?> datasets = drawingPanel.getObjectOfClass(Dataset.class);
Iterator<?> it = control.getObjects(Dataset.class).iterator();
while(it.hasNext()) {
Dataset newData = (Dataset) it.next();
int id = newData.getID();
for(int i = 0, n = datasets.size(); i<n; i++) {
if(((Dataset) datasets.get(i)).getID()==id) {
XMLControl xml = new XMLControlElement(newData); // convert the source to xml
Dataset.getLoader().loadObject(xml, datasets.get(i)); // copy the data to the destination
break;
}
}
}
drawingPanel.repaint();
}
};
}
/**
* Renders the drawing panel if the frame is showing and not iconified.
*/
public void render() {
if(isIconified()||!isShowing()) {
return;
}
if(drawingPanel!=null) {
drawingPanel.render();
} else {
repaint();
}
}
/**
* Invalidates image buffers if a drawing panel buffered.
*/
public void invalidateImage() {
if(drawingPanel!=null) {
drawingPanel.invalidateImage();
}
}
/**
* Gets the drawing panel.
*
* @return the drawingPanel
*/
public DrawingPanel getDrawingPanel() {
return drawingPanel;
}
/**
* Sets the label for the X (horizontal) axis.
*
* @param label the label
*/
public void setXLabel(String label) {
if(drawingPanel instanceof PlottingPanel) {
((PlottingPanel) drawingPanel).setXLabel(label);
}
}
/**
* Sets the label for the Y (vertical) axis.
*
* @param label the label
*/
public void setYLabel(String label) {
if(drawingPanel instanceof PlottingPanel) {
((PlottingPanel) drawingPanel).setYLabel(label);
}
}
/**
* Converts to polar coordinates.
*
* @param plotTitle String
* @param deltaR double
*/
public void setPolar(String plotTitle, double deltaR) {
if(drawingPanel instanceof PlottingPanel) {
((PlottingPanel) drawingPanel).setPolar(plotTitle, deltaR);
}
}
/**
* Converts to cartesian coordinates.
*
*
* @param xLabel String
* @param yLabel String
* @param plotTitle String
*/
public void setCartesian(String xLabel, String yLabel, String plotTitle) {
if(drawingPanel instanceof PlottingPanel) {
((PlottingPanel) drawingPanel).setCartesian(xLabel, yLabel, plotTitle);
}
}
/**
* Limits the xmin and xmax values during autoscaling so that the mininimum value
* will be no greater than the floor and the maximum value will be no
* smaller than the ceil.
*
* Setting a floor or ceil value to <code>Double.NaN<\code> will disable that limit.
*
* @param floor the xfloor value
* @param ceil the xceil value
*/
public void limitAutoscaleX(double floor, double ceil) {
drawingPanel.limitAutoscaleX(floor, ceil);
}
/**
* Limits ymin and ymax values during autoscaling so that the mininimum value
* will be no greater than the floor and the maximum value will be no
* smaller than the ceil.
*
* Setting a floor or ceil value to <code>Double.NaN<\code> will disable that limit.
*
* @param floor the yfloor value
* @param ceil the yceil value
*/
public void limitAutoscaleY(double floor, double ceil) {
drawingPanel.limitAutoscaleY(floor, ceil);
}
/**
* Autoscale the drawing panel's x axis using min and max values.
* from measurable objects.
* @param autoscale
*/
public void setAutoscaleX(boolean autoscale) {
if(drawingPanel!=null) {
drawingPanel.setAutoscaleX(autoscale);
}
}
/**
* Determines if the panel's x axis autoscale property is true.
* @return <code>true<\code> if autoscaled.
*/
public boolean isAutoscaleX() {
if(drawingPanel!=null) {
return drawingPanel.isAutoscaleX();
}
return false;
}
/**
* Autoscale the y axis using min and max values.
* from measurable objects.
* @param autoscale
*/
public void setAutoscaleY(boolean autoscale) {
if(drawingPanel!=null) {
drawingPanel.setAutoscaleY(autoscale);
}
}
/**
* Determines if the y axis autoscale property is true.
* @return <code>true<\code> if autoscaled.
*/
public boolean isAutoscaleY() {
if(drawingPanel!=null) {
return drawingPanel.isAutoscaleY();
}
return false;
}
/**
* Sets the aspect ratio for horizontal to vertical to unity when <code>true<\code>.
* @param isSquare boolean
*/
public void setSquareAspect(boolean isSquare) {
if(drawingPanel!=null) {
drawingPanel.setSquareAspect(isSquare);
}
}
/**
* Sets Cartesian axes to log scale.
*
* @param logX
* @param logY
*/
public void setLogScale(boolean logX, boolean logY) {
if((drawingPanel!=null)&&(drawingPanel instanceof PlottingPanel)) {
((PlottingPanel) drawingPanel).setLogScale(logX, logY);
}
}
/**
* Sets the scale using pixels per unit.
*
* @param enable boolean enable fixed pixels per unit
* @param xPixPerUnit double
* @param yPixPerUnit double
*/
public void setPixelsPerUnit(boolean enable, double xPixPerUnit, double yPixPerUnit) {
drawingPanel.setPixelsPerUnit(enable, xPixPerUnit, yPixPerUnit);
}
/**
* Sets the drawing panel's preferred scale.
* @param xmin
* @param xmax
* @param ymin
* @param ymax
*/
public void setPreferredMinMax(double xmin, double xmax, double ymin, double ymax) {
if(drawingPanel!=null) {
drawingPanel.setPreferredMinMax(xmin, xmax, ymin, ymax);
}
}
/**
* Sets the drawing panel's preferred scale in the vertical direction.
* @param ymin
* @param ymax
*/
public void setPreferredMinMaxY(double ymin, double ymax) {
if(drawingPanel!=null) {
drawingPanel.setPreferredMinMaxY(ymin, ymax);
}
}
/**
* Sets the drawing panel's preferred scale in the horizontal direction.
* @param xmin the minimum value
* @param xmax the maximum value
*/
public void setPreferredMinMaxX(double xmin, double xmax) {
if(drawingPanel!=null) {
drawingPanel.setPreferredMinMaxX(xmin, xmax);
}
}
/**
* Clears data and repaints the drawing panel within this frame.
*/
public void clearDataAndRepaint() {
clearData();
drawingPanel.repaint();
}
/**
* Clears Drawable objects added by the user from this frame.
*/
public void clearDrawables() {
drawingPanel.clear(); // removes all drawables
}
/**
* Adds a drawable object to the frame's drawing panel.
* @param drawable
*/
public synchronized void addDrawable(Drawable drawable) {
if(drawingPanel!=null) {
drawingPanel.addDrawable(drawable);
}
}
/**
* Replaces a Drawable object with another Drawable.
*
* @param oldDrawable Drawable
* @param newDrawable Drawable
*/
public synchronized void replaceDrawable(Drawable oldDrawable, Drawable newDrawable) {
if(drawingPanel!=null) {
drawingPanel.replaceDrawable(oldDrawable, newDrawable);
}
}
/**
* Removes a drawable object to the frame's drawing panel.
* @param drawable
*/
public synchronized void removeDrawable(Drawable drawable) {
if(drawingPanel!=null) {
drawingPanel.removeDrawable(drawable);
}
}
/**
* Shows a message in a yellow text box in the lower right hand corner.
*
* @param msg
*/
public void setMessage(String msg) {
drawingPanel.setMessage(msg);
}
/**
* Shows a message in a yellow text box at the given location.
*
* location 0=bottom left
* location 1=bottom right
* location 2=top right
* location 3=top left
*
* @param msg
* @param location
*/
public void setMessage(String msg, int location) {
drawingPanel.setMessage(msg, location);
}
/**
* Gets objects of a specific class from the drawing panel.
*
* Assignable subclasses are NOT returned. Interfaces CANNOT be specified.
* The same objects will be in the drawable list and the cloned list.
*
* @param c the class of the object
*
* @return the list
*/
public synchronized <T extends Drawable> ArrayList<T> getObjectOfClass(Class<T> c) {
if(drawingPanel!=null) {
return drawingPanel.getObjectOfClass(c);
}
return null;
}
/**
* Gets Drawable previously objects added by the user.
*
* @return the list
*/
public synchronized ArrayList<Drawable> getDrawables() {
if(drawingPanel!=null) {
return drawingPanel.getDrawables();
}
return new ArrayList<Drawable>(); // return an empty list
}
public DrawableAxes getAxes() {
if(drawingPanel instanceof PlottingPanel) {
return((PlottingPanel) drawingPanel).getAxes();
}
return null;
}
/**
* Gets Drawable objects added by the user of an assignable type. The list contains
* objects that are assignable from the class or interface.
*
* @param c the type of Drawable object
*
* @return the cloned list
*
* @see #getObjectOfClass(Class c)
*/
public synchronized <T extends Drawable> ArrayList<T> getDrawables(Class<T> c) {
if(drawingPanel!=null) {
return drawingPanel.getDrawables(c);
}
return new ArrayList<T>(); // return an empty list
}
/**
* Removes all objects of the given class from the drawable list.
*
* Assignable subclasses are NOT removed. Interfaces CANNOT be specified.
*
* @param c the class
*/
public synchronized <T extends Drawable> void removeObjectsOfClass(Class<T> c) {
drawingPanel.removeObjectsOfClass(c);
}
/**
* Sets the interactive mouse handler if the drawing panel is an interactive panel.
*
* Throws an invalid cast exception if the panel is not of the correct type.
*
* @param handler the mouse handler
*/
public void setInteractiveMouseHandler(InteractiveMouseHandler handler) {
((InteractivePanel) drawingPanel).setInteractiveMouseHandler(handler);
}
/**
* Adds the drawing panel to the the frame. The panel is added to the center
* of the frame's content pane.
*
* @param _drawingPanel
*/
public void setDrawingPanel(DrawingPanel _drawingPanel) {
if(drawingPanel!=null) { // remove the old drawing panel.
getContentPane().remove(drawingPanel);
}
drawingPanel = _drawingPanel;
if(drawingPanel!=null) {
getContentPane().add(drawingPanel, BorderLayout.CENTER);
}
}
/**
* Sets the interior background color for the current drawing panel.
* The interior of a PlottingaPanel is the area inside the axes where is displayed.
* The interior of a DrawingPanel is the entire panel.
*/
public void setInteriorBackground(Color color) {
if(drawingPanel instanceof PlottingPanel) {
((PlottingPanel) drawingPanel).getAxes().setInteriorBackground(color);
} else {
drawingPanel.setBackground(color);
}
}
/**
* This is a hack to fix a bug when the reload button is pressed in browsers
* running JDK 1.4.
*
* @param g
*/
public void paint(Graphics g) {
if(!OSPRuntime.appletMode) {
super.paint(g);
return;
}
try {
super.paint(g);
} catch(Exception ex) {
System.err.println("OSPFrame paint error: "+ex.toString()); //$NON-NLS-1$
System.err.println("Title: "+this.getTitle()); //$NON-NLS-1$
}
}
/**
* Enables the paste edit menu item.
* @param enable boolean
*/
public void setEnabledPaste(boolean enable) {
pasteItem.setEnabled(enable);
}
/**
* Pastes drawables found in the specified xml control.
*
* @param control the xml control
*/
protected void pasteAction(XMLControlElement control) {
// get Drawables using an xml tree chooser
XMLTreeChooser chooser = new XMLTreeChooser(DisplayRes.getString("DrawingFrame.SelectDrawables_chooser_title"), DisplayRes.getString("DrawingFrame.SelectDrawables_chooser_message"), this); //$NON-NLS-1$ //$NON-NLS-2$
java.util.List<XMLProperty> props = chooser.choose(control, Drawable.class);
if(!props.isEmpty()) {
Iterator<XMLProperty> it = props.iterator();
while(it.hasNext()) {
XMLControl prop = (XMLControl) it.next();
Drawable drawable = (Drawable) prop.loadObject(null);
addDrawable(drawable);
}
}
drawingPanel.repaint();
}
/**
* Enables the replace edit menu item.
* @param enable boolean
*/
public void setEnabledReplace(boolean enable) {
replaceItem.setEnabled(enable);
}
/**
* Replaces the drawables with the drawables found in the specified XML control.
* @param control XMLControlElement
*/
public void replaceAction(XMLControlElement control) {
clearDrawables();
pasteAction(control);
}
/**
* Copies objects found in the specified xml control.
*
* @param control the xml control
*/
protected void copyAction(XMLControlElement control) {
StringSelection data = new StringSelection(control.toXML());
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(data, this);
}
/**
* Implementation of ClipboardOwner interface.
*
* Override this method to receive notification that data copied to the clipboard has changed.
*
* @param clipboard Clipboard
* @param contents Transferable
*/
public void lostOwnership(Clipboard clipboard, Transferable contents) {}
/**
* Enables the copy edit menu item.
* @param enable boolean
*/
public void setEnabledCopy(boolean enable) {
copyItem.setEnabled(enable);
}
protected void refreshGUI() {
createMenuBar();
addMenuItems();
pack();
}
/**
* Adds Views menu items on the menu bar.
* Override this method to add custom menu items.
*/
protected void addMenuItems() {}
/**
* Creates a standard DrawingFrame menu bar and adds it to the frame.
*/
private void createMenuBar() {
JMenuBar menuBar = new JMenuBar();
fileMenu = new JMenu(DisplayRes.getString("DrawingFrame.File_menu_item")); //$NON-NLS-1$
JMenu printMenu = new JMenu(DisplayRes.getString("DrawingFrame.Print_menu_title")); //$NON-NLS-1$
JMenuItem printItem = new JMenuItem(DisplayRes.getString("DrawingFrame.Print_menu_item")); //$NON-NLS-1$
printMenu.add(printItem);
printItem.setAccelerator(KeyStroke.getKeyStroke('P', MENU_SHORTCUT_KEY_MASK));
printItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PrintUtils.printComponent(drawingPanel);
}
});
JMenuItem printFrameItem = new JMenuItem(DisplayRes.getString("DrawingFrame.PrintFrame_menu_item")); //$NON-NLS-1$
printMenu.add(printFrameItem);
printFrameItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PrintUtils.printComponent(DrawingFrame.this);
}
});
JMenuItem saveXMLItem = new JMenuItem(DisplayRes.getString("DrawingFrame.SaveXML_menu_item")); //$NON-NLS-1$
saveXMLItem.setAccelerator(KeyStroke.getKeyStroke('S', MENU_SHORTCUT_KEY_MASK));
saveXMLItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveXML();
}
});
//ExportTool menu item
/*
JMenuItem exportItem = new JMenuItem(DisplayRes.getString("DrawingFrame.Export_menu_item")); //$NON-NLS-1$
exportItem.setAccelerator(KeyStroke.getKeyStroke('E', MENU_SHORTCUT_KEY_MASK));
exportItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
ExportTool.getTool().send(new LocalJob(drawingPanel), null);
} catch(RemoteException ex) {}
}
});*/
// create export tool menu item if the tool exists in classpath
JMenuItem exportItem = new JMenuItem(DisplayRes.getString("DrawingFrame.Export_menu_item")); //$NON-NLS-1$
exportItem.setAccelerator(KeyStroke.getKeyStroke('E', MENU_SHORTCUT_KEY_MASK));
Class<?> exportTool = null;
if(OSPRuntime.loadExportTool) {
try {
exportTool = Class.forName("org.opensourcephysics.tools.ExportTool"); //$NON-NLS-1$
} catch(Exception ex) {
OSPRuntime.loadExportTool = false;
OSPLog.finest("Cannot instantiate data export tool class:\n"+ex.toString()); //$NON-NLS-1$
exportItem.setEnabled(false);
}
}
final Class<?> tool = exportTool;
exportItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Method m = tool.getMethod("getTool", (Class[]) null); //$NON-NLS-1$
Tool tool = (Tool) m.invoke(null, (Object[]) null);
tool.send(new LocalJob(drawingPanel), reply);
} catch(Exception ex) {}
}
});
//Save menu item
JMenu saveImage = new JMenu(DisplayRes.getString("DrawingFrame.SaveImage_menu_title")); //$NON-NLS-1$
JMenuItem epsMenuItem = new JMenuItem(DisplayRes.getString("DrawingFrame.EPS_menu_item")); //$NON-NLS-1$
JMenuItem jpegMenuItem = new JMenuItem(DisplayRes.getString("DrawingFrame.JPEG_menu_item")); //$NON-NLS-1$
JMenuItem pngMenuItem = new JMenuItem(DisplayRes.getString("DrawingFrame.PNG_menu_item")); //$NON-NLS-1$
saveImage.add(epsMenuItem);
saveImage.add(jpegMenuItem);
saveImage.add(pngMenuItem);
epsMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
GUIUtils.saveImage(drawingPanel, "eps", DrawingFrame.this); //$NON-NLS-1$
}
});
jpegMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
GUIUtils.saveImage(drawingPanel, "jpeg", DrawingFrame.this); //$NON-NLS-1$
}
});
pngMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
GUIUtils.saveImage(drawingPanel, "png", DrawingFrame.this); //$NON-NLS-1$
}
});
JMenuItem inspectItem = new JMenuItem(DisplayRes.getString("DrawingFrame.InspectMenuItem")); //$NON-NLS-1$
inspectItem.setAccelerator(KeyStroke.getKeyStroke('I', MENU_SHORTCUT_KEY_MASK));
inspectItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inspectXML(); // cannot use a static method here because of run-time binding
}
});
if(OSPRuntime.applet==null) {
fileMenu.add(printMenu);
fileMenu.add(saveXMLItem);
fileMenu.add(exportItem);
fileMenu.add(saveImage);
}
fileMenu.add(inspectItem);
menuBar.add(fileMenu);
// edit menu
editMenu = new JMenu(DisplayRes.getString("DrawingFrame.Edit_menu_title")); //$NON-NLS-1$
menuBar.add(editMenu);
copyItem = new JMenuItem(DisplayRes.getString("DrawingFrame.Copy_menu_item")); //$NON-NLS-1$
copyItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
XMLControlElement control = new XMLControlElement(DrawingFrame.this);
control.saveObject(null);
copyAction(control);
}
});
editMenu.add(copyItem);
pasteItem = new JMenuItem(DisplayRes.getString("DrawingFrame.Paste_menu_item")); //$NON-NLS-1$
pasteItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable data = clipboard.getContents(null);
XMLControlElement control = new XMLControlElement();
control.readXML((String) data.getTransferData(DataFlavor.stringFlavor));
pasteAction(control);
} catch(UnsupportedFlavorException ex) {}
catch(IOException ex) {}
catch(HeadlessException ex) {}
}
});
pasteItem.setEnabled(false); // not supported yet
editMenu.add(pasteItem);
replaceItem = new JMenuItem(DisplayRes.getString("DrawingFrame.Replace_menu_item")); //$NON-NLS-1$
replaceItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable data = clipboard.getContents(null);
XMLControlElement control = new XMLControlElement();
control.readXML((String) data.getTransferData(DataFlavor.stringFlavor));
replaceAction(control);
} catch(UnsupportedFlavorException ex) {}
catch(IOException ex) {}
catch(HeadlessException ex) {}
}
});
replaceItem.setEnabled(false); // not supported yet
editMenu.add(replaceItem);
setJMenuBar(menuBar);
// additonal menus
loadDisplayMenu();
loadToolsMenu();
//help menu
JMenu helpMenu = new JMenu(DisplayRes.getString("DrawingFrame.Help_menu_item")); //$NON-NLS-1$
menuBar.add(helpMenu);
JMenuItem aboutItem = new JMenuItem(DisplayRes.getString("DrawingFrame.AboutOSP_menu_item")); //$NON-NLS-1$
aboutItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
OSPRuntime.showAboutDialog(DrawingFrame.this);
}
});
helpMenu.add(aboutItem);
}
/**
* Adds a Display menu to the menu bar.
*/
protected JMenu loadDisplayMenu() {
JMenuBar menuBar = getJMenuBar();
if(menuBar==null) {
return null;
}
JMenu displayMenu = new JMenu(DisplayRes.getString("DrawingFrame.Display_menu_title")); //$NON-NLS-1$
menuBar.add(displayMenu);
JMenu fontMenu = new JMenu(DisplayRes.getString("DrawingFrame.Font_menu_title")); //$NON-NLS-1$
displayMenu.add(fontMenu);
JMenuItem sizeUpItem = new JMenuItem(DisplayRes.getString("DrawingFrame.IncreaseFontSize_menu_item")); //$NON-NLS-1$
sizeUpItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FontSizer.levelUp();
}
});
fontMenu.add(sizeUpItem);
final JMenuItem sizeDownItem = new JMenuItem(DisplayRes.getString("DrawingFrame.DecreaseFontSize_menu_item")); //$NON-NLS-1$
sizeDownItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FontSizer.levelDown();
}
});
fontMenu.add(sizeDownItem);
fontMenu.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
sizeDownItem.setEnabled(FontSizer.getLevel()>0);
}
});
JMenu aliasMenu = new JMenu(DisplayRes.getString("DrawingFrame.AntiAlias_menu_title")); //$NON-NLS-1$
displayMenu.add(aliasMenu);
final JCheckBoxMenuItem textAliasItem = new JCheckBoxMenuItem(DisplayRes.getString("DrawingFrame.Text_checkbox_label"), false); //$NON-NLS-1$
textAliasItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
drawingPanel.antialiasTextOn = textAliasItem.isSelected();
drawingPanel.repaint();
}
});
aliasMenu.add(textAliasItem);
final JCheckBoxMenuItem shapeAliasItem = new JCheckBoxMenuItem(DisplayRes.getString("DrawingFrame.Drawing_textbox_label"), false); //$NON-NLS-1$
shapeAliasItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
drawingPanel.antialiasShapeOn = shapeAliasItem.isSelected();
drawingPanel.repaint();
}
});
aliasMenu.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
textAliasItem.setSelected(drawingPanel.antialiasTextOn);
shapeAliasItem.setSelected(drawingPanel.antialiasShapeOn);
}
});
aliasMenu.add(shapeAliasItem);
menuBar.add(displayMenu);
return displayMenu;
}
/**
* Adds a Tools menu to the menu bar.
*/
protected JMenu loadToolsMenu() {
JMenuBar menuBar = getJMenuBar();
if(menuBar==null) {
return null;
}
// create Tools menu item
JMenu toolsMenu = new JMenu(DisplayRes.getString("DrawingFrame.Tools_menu_title")); //$NON-NLS-1$
menuBar.add(toolsMenu);
// create Data Tool menu item if the tool exists in the classpath
JMenuItem datasetItem = new JMenuItem(DisplayRes.getString("DrawingFrame.DatasetTool_menu_item")); //$NON-NLS-1$
toolsMenu.add(datasetItem);
Class<?> datasetToolClass = null;
if(OSPRuntime.loadDataTool) {
try {
datasetToolClass = Class.forName("org.opensourcephysics.tools.DataTool"); //$NON-NLS-1$
} catch(Exception ex) {
OSPLog.finest("Cannot instantiate data analysis tool class:\n"+ex.toString()); //$NON-NLS-1$
OSPRuntime.loadDataTool = false;
datasetItem.setEnabled(false);
}
}
final Class<?> finalDatasetToolClass = datasetToolClass; // class must be final for action listener
datasetItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Method m = finalDatasetToolClass.getMethod("getTool", (Class[]) null); //$NON-NLS-1$
Tool tool = (Tool) m.invoke(null, (Object[]) null);
tool.send(new LocalJob(drawingPanel), reply);
if(tool instanceof OSPFrame) {
((OSPFrame) tool).setKeepHidden(false);
}
((JFrame) tool).setVisible(true);
} catch(Exception ex) {}
}
});
// create Fourier Tool menu item if the tool exists in the classpath
JMenuItem fourierToolItem = new JMenuItem(DisplayRes.getString("DrawingFrame.FourierTool_menu_item")); //$NON-NLS-1$
toolsMenu.add(fourierToolItem);
Class<?> fourierToolClass = null;
if(OSPRuntime.loadFourierTool) {
try {
fourierToolClass = Class.forName("org.opensourcephysics.tools.FourierTool"); //$NON-NLS-1$
} catch(Exception ex) {
OSPLog.finest("Cannot instantiate Fourier analysis tool class:\n"+ex.toString()); //$NON-NLS-1$
OSPRuntime.loadFourierTool = false;
fourierToolItem.setEnabled(false);
}
}
final Class<?> finalFourierToolClass = fourierToolClass; // class must be final for action listener
fourierToolItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
Method m = finalFourierToolClass.getMethod("getTool", (Class[]) null); //$NON-NLS-1$
Tool tool = (Tool) m.invoke(null, (Object[]) null);
tool.send(new LocalJob(drawingPanel), reply);
if(tool instanceof OSPFrame) {
((OSPFrame) tool).setKeepHidden(false);
}
((JFrame) tool).setVisible(true);
} catch(Exception ex) {}
}
});
// create snapshot menu item
JMenuItem snapshotItem = new JMenuItem(DisplayRes.getString("DisplayPanel.Snapshot_menu_item")); //$NON-NLS-1$
if(OSPRuntime.applet==null) {
toolsMenu.add(snapshotItem);
}
snapshotItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
SnapshotTool tool = SnapshotTool.getTool();
if(drawingPanel!=null) {
tool.saveImage(null, drawingPanel);
} else {
tool.saveImage(null, getContentPane());
}
}
});
// create video capture menu item
JMenuItem videoItem = new JMenuItem(DisplayRes.getString("DrawingFrame.MenuItem.Capture")); //$NON-NLS-1$
if(OSPRuntime.applet==null) {
toolsMenu.add(videoItem);
}
Class<?> videoToolClass = null;
if(OSPRuntime.loadVideoTool) {
try {
videoToolClass = Class.forName("org.opensourcephysics.tools.VideoCaptureTool"); //$NON-NLS-1$
} catch(Exception ex) {
OSPRuntime.loadVideoTool = false;
OSPLog.finest("Cannot instantiate video capture tool class:\n"+ex.toString()); //$NON-NLS-1$
videoItem.setEnabled(false);
}
}
final Class<?> finalVideoToolClass = videoToolClass; // class must be final for action listener
videoItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(drawingPanel.getVideoTool()==null) {
try {
Method m = finalVideoToolClass.getMethod("getTool", (Class[]) null); //$NON-NLS-1$
Tool tool = (Tool) m.invoke(null, (Object[]) null); // tool is a VideoTool
drawingPanel.setVideoTool((VideoTool) tool);
((VideoTool) tool).setVisible(true);
((VideoTool) tool).clear();
} catch(Exception ex) {
OSPLog.finest("Cannot perform action to get video tool class:\n"+ex.toString()); //$NON-NLS-1$
}
} else {
drawingPanel.getVideoTool().setVisible(true);
}
}
});
return toolsMenu;
}
/**
* Sets a custom properties inspector window.
*