-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathControlFrame.java
More file actions
473 lines (439 loc) · 17.8 KB
/
ControlFrame.java
File metadata and controls
473 lines (439 loc) · 17.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
/*
* 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.controls;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.opensourcephysics.display.DisplayRes;
import org.opensourcephysics.display.GUIUtils;
import org.opensourcephysics.display.OSPFrame;
import org.opensourcephysics.display.OSPRuntime;
import org.opensourcephysics.display.PrintUtils;
import org.opensourcephysics.tools.FontSizer;
import org.opensourcephysics.tools.ToolsRes;
/**
* A frame with menu items for saving and loading control parameters
*
* @author Wolfgang Christian
* @version 1.0
*/
abstract public class ControlFrame extends OSPFrame implements Control {
final static int MENU_SHORTCUT_KEY_MASK = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
protected Object model; // the object that will be controlled
protected JMenuItem[] languageItems;
protected JMenu languageMenu;
protected JMenu fileMenu;
protected JMenu editMenu;
protected JMenu displayMenu;
protected JMenuItem readItem, clearItem, printFrameItem, saveFrameAsEPSItem;
protected JMenuItem saveAsItem;
protected JMenuItem copyItem;
protected JMenuItem inspectItem;
protected JMenuItem logToFileItem;
protected JMenuItem sizeUpItem;
protected JMenuItem sizeDownItem;
protected OSPApplication ospApp;
protected XMLControlElement xmlDefault;
protected ControlFrame(String title) {
super(title);
createMenuBar();
setName("controlFrame"); //$NON-NLS-1$
}
private void createMenuBar() {
JMenuBar menuBar = new JMenuBar();
if(!OSPRuntime.appletMode) {
setJMenuBar(menuBar);
}
fileMenu = new JMenu(ControlsRes.getString("ControlFrame.File")); //$NON-NLS-1$
editMenu = new JMenu(ControlsRes.getString("ControlFrame.Edit")); //$NON-NLS-1$
menuBar.add(fileMenu);
menuBar.add(editMenu);
readItem = new JMenuItem(ControlsRes.getString("ControlFrame.Load_XML")); //$NON-NLS-1$
saveAsItem = new JMenuItem(ControlsRes.getString("ControlFrame.Save_XML")); //$NON-NLS-1$
inspectItem = new JMenuItem(ControlsRes.getString("ControlFrame.Inspect_XML")); //$NON-NLS-1$
clearItem = new JMenuItem(ControlsRes.getString("ControlFrame.Clear_XML")); //$NON-NLS-1$
clearItem.setEnabled(false);
copyItem = new JMenuItem(ControlsRes.getString("ControlFrame.Copy")); //$NON-NLS-1$
printFrameItem = new JMenuItem(DisplayRes.getString("DrawingFrame.PrintFrame_menu_item")); //$NON-NLS-1$
saveFrameAsEPSItem = new JMenuItem(DisplayRes.getString("DrawingFrame.SaveFrameAsEPS_menu_item")); //$NON-NLS-1$
JMenu printMenu = new JMenu(DisplayRes.getString("DrawingFrame.Print_menu_title")); //$NON-NLS-1$
if(OSPRuntime.applet==null) {
fileMenu.add(readItem);
}
if(OSPRuntime.applet==null) {
fileMenu.add(saveAsItem);
}
fileMenu.add(inspectItem);
fileMenu.add(clearItem);
if(OSPRuntime.applet==null) {
fileMenu.add(printMenu);
}
printMenu.add(printFrameItem);
printMenu.add(saveFrameAsEPSItem);
editMenu.add(copyItem);
copyItem.setAccelerator(KeyStroke.getKeyStroke('C', MENU_SHORTCUT_KEY_MASK));
copyItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
copy();
}
});
saveAsItem.setAccelerator(KeyStroke.getKeyStroke('S', MENU_SHORTCUT_KEY_MASK));
saveAsItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
saveXML();
}
});
inspectItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inspectXML(); // cannot use a static method here because of run-time binding
}
});
readItem.setAccelerator(KeyStroke.getKeyStroke('L', MENU_SHORTCUT_KEY_MASK));
readItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// readParameters(); // cannot use a static method here because of run-time binding
loadXML((String) null);
}
});
clearItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if((xmlDefault==null)||(model==null)) {
return;
}
xmlDefault = null;
clearItem.setEnabled(false);
if(model instanceof Calculation) {
((Calculation) model).resetCalculation();
((Calculation) model).calculate();
} else if(model instanceof Animation) {
((Animation) model).stopAnimation();
((Animation) model).resetAnimation();
((Animation) model).initializeAnimation();
}
GUIUtils.repaintOSPFrames();
}
});
// print item
printFrameItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PrintUtils.printComponent(ControlFrame.this);
}
});
// save as EPS item
saveFrameAsEPSItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
PrintUtils.saveComponentAsEPS(ControlFrame.this);
} catch(IOException ex) {}
}
});
// display menu
loadDisplayMenu();
// help menu
JMenu helpMenu = new JMenu(ControlsRes.getString("ControlFrame.Help")); //$NON-NLS-1$
menuBar.add(helpMenu);
JMenuItem aboutItem = new JMenuItem(ControlsRes.getString("ControlFrame.About")); //$NON-NLS-1$
aboutItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
OSPRuntime.showAboutDialog(ControlFrame.this);
}
});
helpMenu.add(aboutItem);
JMenuItem sysItem = new JMenuItem(ControlsRes.getString("ControlFrame.System")); //$NON-NLS-1$
sysItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ControlUtils.showSystemProperties(true);
}
});
helpMenu.add(sysItem);
JMenuItem showItem = new JMenuItem(ControlsRes.getString("ControlFrame.Display_All_Frames")); //$NON-NLS-1$
showItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
org.opensourcephysics.display.GUIUtils.showDrawingAndTableFrames();
}
});
helpMenu.add(showItem);
helpMenu.addSeparator();
JMenuItem logItem = new JMenuItem(ControlsRes.getString("ControlFrame.Message_Log")); //$NON-NLS-1$
logItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
OSPLog.showLog();
}
});
if(OSPRuntime.applet==null) {
helpMenu.add(logItem);
}
logToFileItem = new JCheckBoxMenuItem(ControlsRes.getString("ControlFrame.Log_to_File")); //$NON-NLS-1$
logToFileItem.setSelected(false);
logToFileItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JCheckBoxMenuItem item = (JCheckBoxMenuItem) e.getSource();
if(OSPRuntime.appletMode||(OSPRuntime.applet!=null)) {
return; // applets cannot log to file
}
OSPLog.getOSPLog().setLogToFile(item.isSelected());
}
});
helpMenu.add(logToFileItem);
validate();
}
/**
* Adds a Display menu to the menu bar. Overrides OSPFrame method.
*
* @return the display menu
*/
protected JMenu loadDisplayMenu() {
JMenuBar menuBar = getJMenuBar();
if(menuBar==null) {
return null;
}
displayMenu = super.loadDisplayMenu();
if(displayMenu==null) {
displayMenu = new JMenu();
displayMenu.setText(ControlsRes.getString("ControlFrame.Display")); //$NON-NLS-1$
menuBar.add(displayMenu);
}
// language menu
languageMenu = new JMenu();
languageMenu.setText(ControlsRes.getString("ControlFrame.Language")); //$NON-NLS-1$
Action languageAction = new AbstractAction() {
public void actionPerformed(ActionEvent e) {
String language = e.getActionCommand();
OSPLog.finest("setting language to "+language); //$NON-NLS-1$
Locale[] locales = OSPRuntime.getInstalledLocales();
for(int i = 0; i<locales.length; i++) {
if(language.equals(locales[i].getDisplayName())) {
ToolsRes.setLocale(locales[i]);
return;
}
}
}
};
Locale[] locales = OSPRuntime.getInstalledLocales();
ButtonGroup languageGroup = new ButtonGroup();
languageItems = new JMenuItem[locales.length];
for(int i = 0; i<locales.length; i++) {
languageItems[i] = new JRadioButtonMenuItem(locales[i].getDisplayName(locales[i]));
languageItems[i].setActionCommand(locales[i].getDisplayName());
languageItems[i].addActionListener(languageAction);
languageMenu.add(languageItems[i]);
languageGroup.add(languageItems[i]);
}
for(int i = 0; i<locales.length; i++) {
if(locales[i].getLanguage().equals(ToolsRes.getLanguage())) {
languageItems[i].setSelected(true);
}
}
if(OSPRuntime.isAuthorMode()||!OSPRuntime.isLauncherMode()) {
displayMenu.add(languageMenu); // add the menu if program is stand-alone or if user is authoring.
}
//font menu
JMenu fontMenu = new JMenu(DisplayRes.getString("DrawingFrame.Font_menu_title")); //$NON-NLS-1$
displayMenu.add(fontMenu);
JMenuItem sizeUpItem = new JMenuItem(ControlsRes.getString("ControlFrame.Increase_Font_Size")); //$NON-NLS-1$
sizeUpItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
FontSizer.levelUp();
}
});
fontMenu.add(sizeUpItem);
final JMenuItem sizeDownItem = new JMenuItem(ControlsRes.getString("ControlFrame.Decrease_Font_Size")); //$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);
}
});
return displayMenu;
}
/**
* Refreshes the user interface in response to display changes such as Language.
*/
protected void refreshGUI() {
super.refreshGUI();
createMenuBar();
}
/** Saves a file containing the control parameters to the disk. */
public void save() {
ControlUtils.saveToFile(this, ControlFrame.this);
}
/** Loads a file containing the control parameters from the disk. */
public void readParameters() {
ControlUtils.loadParameters(this, ControlFrame.this);
}
/** Copies the data in the table to the system clipboard */
public void copy() {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
StringSelection stringSelection = new StringSelection(this.toString());
clipboard.setContents(stringSelection, stringSelection);
}
public void saveXML() {
JFileChooser chooser = OSPRuntime.getChooser();
if(chooser==null) {
return;
}
String oldTitle = chooser.getDialogTitle();
chooser.setDialogTitle(ControlsRes.getString("ControlFrame.Save_XML_Data")); //$NON-NLS-1$
int result = chooser.showSaveDialog(null);
chooser.setDialogTitle(oldTitle);
if(result==JFileChooser.APPROVE_OPTION) {
File file = chooser.getSelectedFile();
// check to see if file already exists
if(file.exists()) {
int selected = JOptionPane.showConfirmDialog(null, ControlsRes.getString("ControlFrame.Replace_existing")+file.getName() //$NON-NLS-1$
+ControlsRes.getString("ControlFrame.question_mark"), ControlsRes.getString("ControlFrame.Replace_File"), //$NON-NLS-1$ //$NON-NLS-2$
JOptionPane.YES_NO_CANCEL_OPTION);
if(selected!=JOptionPane.YES_OPTION) {
return;
}
}
OSPRuntime.chooserDir = chooser.getCurrentDirectory().toString();
String fileName = file.getAbsolutePath();
// String fileName = XML.getRelativePath(file.getAbsolutePath());
if((fileName==null)||fileName.trim().equals("")) { //$NON-NLS-1$
return;
}
int i = fileName.toLowerCase().lastIndexOf(".xml"); //$NON-NLS-1$
if(i!=fileName.length()-4) {
fileName += ".xml"; //$NON-NLS-1$
}
XMLControl xml = new XMLControlElement(getOSPApp());
xml.write(fileName);
}
}
public void loadXML(String[] args) {
if(args!=null) {
for(int i = 0; i<args.length; i++) {
loadXML(args[i]);
}
}
}
/**
* Loads xml data into the model.
*
* @param xml
* @param compatibleModel true if model is known to be compatible with the app
*/
public void loadXML(XMLControlElement xml, boolean compatibleModel) {
if(xml==null) {
OSPLog.finer("XML data not found in ControlFrame loadXML method."); //$NON-NLS-1$
return;
}
// load xml into app if xml object class is an OSPApplication
// if(xml.getObjectClass().isAssignableFrom(OSPApplication.class)) {
if(OSPApplication.class.isAssignableFrom(xml.getObjectClass())) {
ospApp = getOSPApp();
ospApp.compatibleModel = compatibleModel;
xml.loadObject(getOSPApp());
ospApp.compatibleModel = false;
if(model.getClass()==ospApp.getLoadedModelClass()) { // classes match so we have a new default
xmlDefault = xml;
clearItem.setEnabled(true);
} else if(ospApp.getLoadedModelClass()!=null) { // a model was imported; create xml from current model
xmlDefault = new XMLControlElement(getOSPApp());
clearItem.setEnabled(true);
} else { // model is null
xmlDefault = null;
clearItem.setEnabled(false);
JOptionPane.showMessageDialog(this, "Model specified in file not found.", //$NON-NLS-1$
"Data not loaded.", JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$
}
} else {
JOptionPane.showMessageDialog(this, "Data for: "+xml.getObjectClass()+".", //$NON-NLS-1$ //$NON-NLS-2$
"OSP Application data not found.", JOptionPane.WARNING_MESSAGE); //$NON-NLS-1$
}
}
public void loadXML(String fileName) {
if((fileName==null)||fileName.trim().equals("")) { //$NON-NLS-1$
loadXML();
return;
}
loadXML(new XMLControlElement(fileName), false);
}
public void loadXML() {
JFileChooser chooser = OSPRuntime.getChooser();
if(chooser==null) {
return;
}
String oldTitle = chooser.getDialogTitle();
chooser.setDialogTitle(ControlsRes.getString("ControlFrame.Load_XML_Data")); //$NON-NLS-1$
int result = chooser.showOpenDialog(null);
chooser.setDialogTitle(oldTitle);
if(result==JFileChooser.APPROVE_OPTION) {
OSPRuntime.chooserDir = chooser.getCurrentDirectory().toString();
String fileName = chooser.getSelectedFile().getAbsolutePath();
loadXML(new XMLControlElement(fileName), false);
}
}
public void inspectXML() {
// display a TreePanel in a modal dialog
XMLControl xml = new XMLControlElement(getOSPApp());
XMLTreePanel treePanel = new XMLTreePanel(xml);
JDialog dialog = new JDialog((java.awt.Frame) null, true);
dialog.setContentPane(treePanel);
dialog.setSize(new Dimension(600, 300));
dialog.setVisible(true);
}
/**
* Gets the OSP Application that is controlled by this frame.
* @return
*/
public OSPApplication getOSPApp() {
if(ospApp==null) {
ospApp = new OSPApplication(this, model);
}
return ospApp;
}
}
/*
* Open Source Physics software is free software; you can redistribute
* it and/or modify it under the terms of the GNU General Public License (GPL) as
* published by the Free Software Foundation; either version 2 of the License,
* or(at your option) any later version.
*
* Code that uses any portion of the code in the org.opensourcephysics package
* or any subpackage (subdirectory) of this package must must also be be released
* under the GNU GPL license.
*
* This software 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; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307 USA
* or view the license online at http://www.gnu.org/copyleft/gpl.html
*
* Copyright (c) 2007 The Open Source Physics project
* http://www.opensourcephysics.org
*/