Load last opened simulation configuration file when reloading without simulation.

Added key shortcuts to last opened files.
This commit is contained in:
nifi 2009-05-27 23:23:41 +00:00
parent 0f924ed2dd
commit efbae10245

View file

@ -24,7 +24,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* *
* $Id: GUI.java,v 1.125 2009/04/20 16:07:32 fros4943 Exp $ * $Id: GUI.java,v 1.126 2009/05/27 23:23:41 nifi Exp $
*/ */
package se.sics.cooja; package se.sics.cooja;
@ -269,6 +269,7 @@ public class GUI extends Observable {
private JMenu menuPlugins, menuMoteTypeClasses, menuMoteTypes; private JMenu menuPlugins, menuMoteTypeClasses, menuMoteTypes;
private JMenu menuOpenSimulation, menuConfOpenSimulation; private JMenu menuOpenSimulation, menuConfOpenSimulation;
private boolean hasFileHistoryChanged;
private Vector<Class<? extends Plugin>> menuMotePluginClasses; private Vector<Class<? extends Plugin>> menuMotePluginClasses;
@ -494,16 +495,19 @@ public class GUI extends Observable {
} }
} }
public Vector<File> getFileHistory() { public File getLastOpenedFile() {
Vector<File> history = new Vector<File>();
// Fetch current history // Fetch current history
String[] historyArray = getExternalToolsSetting("SIMCFG_HISTORY", "").split(";"); String[] historyArray = getExternalToolsSetting("SIMCFG_HISTORY", "").split(";");
return historyArray.length > 0 ? new File(historyArray[0]) : null;
}
for (String file: historyArray) { public File[] getFileHistory() {
history.add(new File(file)); // Fetch current history
String[] historyArray = getExternalToolsSetting("SIMCFG_HISTORY", "").split(";");
File[] history = new File[historyArray.length];
for (int i = 0; i < historyArray.length; i++) {
history[i] = new File(historyArray[i]);
} }
return history; return history;
} }
@ -529,46 +533,48 @@ public class GUI extends Observable {
} }
setExternalToolsSetting("SIMCFG_HISTORY", newHistory.toString()); setExternalToolsSetting("SIMCFG_HISTORY", newHistory.toString());
saveExternalToolsUserSettings(); saveExternalToolsUserSettings();
hasFileHistoryChanged = true;
} }
private void updateOpenHistoryMenuItems() { private void updateOpenHistoryMenuItems() {
menuConfOpenSimulation.removeAll();
if (isVisualizedInApplet()) { if (isVisualizedInApplet()) {
return; return;
} }
if (!hasFileHistoryChanged) {
JMenuItem browseItem = new JMenuItem("Browse..."); // No need to update menu because file history has not changed
browseItem.setActionCommand("confopen sim"); return;
browseItem.addActionListener(guiEventHandler);
menuConfOpenSimulation.add(browseItem);
menuConfOpenSimulation.add(new JSeparator());
Vector<File> openFilesHistory = getFileHistory();
for (File file: openFilesHistory) {
JMenuItem lastItem = new JMenuItem(file.getName());
lastItem.setActionCommand("confopen last sim");
lastItem.putClientProperty("file", file);
lastItem.setToolTipText(file.getAbsolutePath());
lastItem.addActionListener(guiEventHandler);
menuConfOpenSimulation.add(lastItem);
} }
hasFileHistoryChanged = false;
menuOpenSimulation.removeAll(); File[] openFilesHistory = getFileHistory();
updateOpenHistoryMenuItems("confopen", menuConfOpenSimulation, openFilesHistory);
updateOpenHistoryMenuItems("open", menuOpenSimulation, openFilesHistory);
}
browseItem = new JMenuItem("Browse..."); private void updateOpenHistoryMenuItems(String type, JMenu menu, File[] openFilesHistory) {
browseItem.setActionCommand("open sim"); menu.removeAll();
JMenuItem browseItem = new JMenuItem("Browse...");
browseItem.setActionCommand(type + " sim");
browseItem.addActionListener(guiEventHandler); browseItem.addActionListener(guiEventHandler);
menuOpenSimulation.add(browseItem); menu.add(browseItem);
menuOpenSimulation.add(new JSeparator()); menu.add(new JSeparator());
String command = type + " last sim";
int index = 0;
JMenuItem lastItem;
for (File file: openFilesHistory) { for (File file: openFilesHistory) {
JMenuItem lastItem = new JMenuItem(file.getName()); if (index < 10) {
lastItem.setActionCommand("open last sim"); char mnemonic = (char) ('0' + (++index % 10));
lastItem = new JMenuItem(mnemonic + " " + file.getName());
lastItem.setMnemonic(mnemonic);
} else {
lastItem = new JMenuItem(file.getName());
}
lastItem.setActionCommand(command);
lastItem.putClientProperty("file", file); lastItem.putClientProperty("file", file);
lastItem.setToolTipText(file.getAbsolutePath()); lastItem.setToolTipText(file.getAbsolutePath());
lastItem.addActionListener(guiEventHandler); lastItem.addActionListener(guiEventHandler);
menuOpenSimulation.add(lastItem); menu.add(lastItem);
} }
} }
@ -603,36 +609,45 @@ public class GUI extends Observable {
menuItem = new JMenu("Reload simulation"); menuItem = new JMenu("Reload simulation");
menu.add(menuItem); menu.add(menuItem);
ActionListener reloadListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (getSimulation() == null) {
// Load last opened simulation configuration file when
// reloading without simulation (ask for a file if no file
// has been previously opened)
final File file = getLastOpenedFile();
new Thread(new Runnable() {
public void run() {
myGUI.doLoadConfig(true, true, file);
}
}).start();
return;
}
long seed = getSimulation().getRandomSeed();
if ("new random seed".equals(e.getActionCommand())) {
seed++;
}
reloadCurrentSimulation(
getSimulation().isRunning(),
seed);
}
};
JMenuItem menuItem2 = new JMenuItem("same random seed"); JMenuItem menuItem2 = new JMenuItem("same random seed");
menuItem2.setActionCommand("same random seed");
menuItem2.setMnemonic(KeyEvent.VK_R); menuItem2.setMnemonic(KeyEvent.VK_R);
menuItem2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, menuItem2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,
ActionEvent.CTRL_MASK)); ActionEvent.CTRL_MASK));
menuItem2.addActionListener(new ActionListener() { menuItem2.addActionListener(reloadListener);
public void actionPerformed(ActionEvent e) {
if (getSimulation() == null) {
return;
}
reloadCurrentSimulation(
getSimulation().isRunning(),
getSimulation().getRandomSeed());
}
});
menuItem.add(menuItem2); menuItem.add(menuItem2);
menuItem2 = new JMenuItem("new random seed"); menuItem2 = new JMenuItem("new random seed");
menuItem2.setActionCommand("new random seed");
menuItem2.setMnemonic(KeyEvent.VK_R); menuItem2.setMnemonic(KeyEvent.VK_R);
menuItem2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R, menuItem2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_R,
ActionEvent.CTRL_MASK | ActionEvent.SHIFT_MASK)); ActionEvent.CTRL_MASK | ActionEvent.SHIFT_MASK));
menuItem2.addActionListener(new ActionListener() { menuItem2.addActionListener(reloadListener);
public void actionPerformed(ActionEvent e) {
if (getSimulation() == null) {
return;
}
reloadCurrentSimulation(
getSimulation().isRunning(),
getSimulation().getRandomSeed()+1);
}
});
menuItem.add(menuItem2); menuItem.add(menuItem2);
menuItem = new JMenuItem("Close simulation"); menuItem = new JMenuItem("Close simulation");
@ -656,6 +671,7 @@ public class GUI extends Observable {
menuConfOpenSimulation.setEnabled(false); menuConfOpenSimulation.setEnabled(false);
menuConfOpenSimulation.setToolTipText("Not available in applet version"); menuConfOpenSimulation.setToolTipText("Not available in applet version");
} }
hasFileHistoryChanged = true;
menuItem = new JMenuItem("Save simulation"); menuItem = new JMenuItem("Save simulation");
menuItem.setMnemonic(KeyEvent.VK_S); menuItem.setMnemonic(KeyEvent.VK_S);
@ -1994,9 +2010,8 @@ public class GUI extends Observable {
fc.setCurrentDirectory(suggestedFile); fc.setCurrentDirectory(suggestedFile);
} else { } else {
/* Suggest file using file history */ /* Suggest file using file history */
Vector<File> history = getFileHistory(); File suggestedFile = getLastOpenedFile();
if (history != null && history.size() > 0) { if (suggestedFile != null) {
File suggestedFile = getFileHistory().firstElement();
fc.setSelectedFile(suggestedFile); fc.setSelectedFile(suggestedFile);
} }
} }
@ -2248,9 +2263,8 @@ public class GUI extends Observable {
fc.setFileFilter(GUI.SAVED_SIMULATIONS_FILES); fc.setFileFilter(GUI.SAVED_SIMULATIONS_FILES);
// Suggest file using history // Suggest file using history
Vector<File> history = getFileHistory(); File suggestedFile = getLastOpenedFile();
if (history != null && history.size() > 0) { if (suggestedFile != null) {
File suggestedFile = getFileHistory().firstElement();
fc.setSelectedFile(suggestedFile); fc.setSelectedFile(suggestedFile);
} }