added load/save-simulation config history support
This commit is contained in:
parent
d65b820ca2
commit
1e1e49f3ff
1 changed files with 155 additions and 23 deletions
|
@ -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.22 2007/03/22 11:14:27 fros4943 Exp $
|
* $Id: GUI.java,v 1.23 2007/03/22 13:59:33 fros4943 Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package se.sics.cooja;
|
package se.sics.cooja;
|
||||||
|
@ -155,6 +155,8 @@ public class GUI {
|
||||||
|
|
||||||
private JMenu menuPlugins, menuMoteTypeClasses, menuMoteTypes;
|
private JMenu menuPlugins, menuMoteTypeClasses, menuMoteTypes;
|
||||||
|
|
||||||
|
private JMenu menuOpenSimulation, menuQuickOpenSimulation;
|
||||||
|
|
||||||
private Vector<Class<? extends Plugin>> menuMotePluginClasses;
|
private Vector<Class<? extends Plugin>> menuMotePluginClasses;
|
||||||
|
|
||||||
private JDesktopPane myDesktopPane;
|
private JDesktopPane myDesktopPane;
|
||||||
|
@ -189,15 +191,15 @@ public class GUI {
|
||||||
mySimulation = null;
|
mySimulation = null;
|
||||||
myDesktopPane = desktop;
|
myDesktopPane = desktop;
|
||||||
|
|
||||||
|
// Load default and overwrite with user settings (if any)
|
||||||
|
loadExternalToolsDefaultSettings();
|
||||||
|
loadExternalToolsUserSettings();
|
||||||
|
|
||||||
// Add menu bar
|
// Add menu bar
|
||||||
if (frame != null) {
|
if (frame != null) {
|
||||||
frame.setJMenuBar(createMenuBar());
|
frame.setJMenuBar(createMenuBar());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load default and overwrite with user settings (if any)
|
|
||||||
loadExternalToolsDefaultSettings();
|
|
||||||
loadExternalToolsUserSettings();
|
|
||||||
|
|
||||||
// Register default user platforms
|
// Register default user platforms
|
||||||
String defaultUserPlatforms = getExternalToolsSetting(
|
String defaultUserPlatforms = getExternalToolsSetting(
|
||||||
"DEFAULT_USERPLATFORMS", null);
|
"DEFAULT_USERPLATFORMS", null);
|
||||||
|
@ -233,6 +235,88 @@ public class GUI {
|
||||||
return frame != null;
|
return frame != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Vector<File> getFileHistory() {
|
||||||
|
Vector<File> history = new Vector<File>();
|
||||||
|
|
||||||
|
// Fetch current history
|
||||||
|
String[] historyArray = getExternalToolsSetting("SIMCFG_HISTORY", "").split(";");
|
||||||
|
|
||||||
|
for (String file: historyArray) {
|
||||||
|
history.add(new File(file));
|
||||||
|
}
|
||||||
|
|
||||||
|
return history;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addToFileHistory(File file) {
|
||||||
|
// Fetch current history
|
||||||
|
String[] history = getExternalToolsSetting("SIMCFG_HISTORY", "").split(";");
|
||||||
|
|
||||||
|
// Create new history
|
||||||
|
String[] newHistory = null;
|
||||||
|
if (history == null || history.length <= 1 && history[0].equals("")) {
|
||||||
|
newHistory = new String[1];
|
||||||
|
} else {
|
||||||
|
newHistory = new String[Math.min(5, history.length+1)];
|
||||||
|
System.arraycopy(history, 0, newHistory, 1, newHistory.length-1);
|
||||||
|
}
|
||||||
|
newHistory[0] = file.getAbsolutePath();
|
||||||
|
|
||||||
|
// Abort if file added is equal to last file
|
||||||
|
if (history.length >= 1 &&
|
||||||
|
file.getAbsolutePath().equals(new File(history[0]).getAbsolutePath())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String newHistoryConfig = null;
|
||||||
|
for (String path: newHistory) {
|
||||||
|
if (newHistoryConfig == null)
|
||||||
|
newHistoryConfig = path;
|
||||||
|
else
|
||||||
|
newHistoryConfig += ";" + path;
|
||||||
|
}
|
||||||
|
|
||||||
|
setExternalToolsSetting("SIMCFG_HISTORY", newHistoryConfig);
|
||||||
|
saveExternalToolsUserSettings();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateOpenHistoryMenuItems() {
|
||||||
|
menuOpenSimulation.removeAll();
|
||||||
|
|
||||||
|
JMenuItem browseItem = new JMenuItem("Browse...");
|
||||||
|
browseItem.setActionCommand("open sim");
|
||||||
|
browseItem.addActionListener(guiEventHandler);
|
||||||
|
menuOpenSimulation.add(browseItem);
|
||||||
|
menuOpenSimulation.add(new JSeparator());
|
||||||
|
Vector<File> openFilesHistory = getFileHistory();
|
||||||
|
|
||||||
|
for (File file: openFilesHistory) {
|
||||||
|
JMenuItem lastItem = new JMenuItem(file.getName());
|
||||||
|
lastItem.setActionCommand("open last sim");
|
||||||
|
lastItem.putClientProperty("file", file);
|
||||||
|
lastItem.setToolTipText(file.getAbsolutePath());
|
||||||
|
lastItem.addActionListener(guiEventHandler);
|
||||||
|
menuOpenSimulation.add(lastItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
menuQuickOpenSimulation.removeAll();
|
||||||
|
|
||||||
|
browseItem = new JMenuItem("Browse...");
|
||||||
|
browseItem.setActionCommand("open sim quick");
|
||||||
|
browseItem.addActionListener(guiEventHandler);
|
||||||
|
menuQuickOpenSimulation.add(browseItem);
|
||||||
|
menuQuickOpenSimulation.add(new JSeparator());
|
||||||
|
|
||||||
|
for (File file: openFilesHistory) {
|
||||||
|
JMenuItem lastItem = new JMenuItem(file.getName());
|
||||||
|
lastItem.setActionCommand("open last sim quick");
|
||||||
|
lastItem.putClientProperty("file", file);
|
||||||
|
lastItem.setToolTipText(file.getAbsolutePath());
|
||||||
|
lastItem.addActionListener(guiEventHandler);
|
||||||
|
menuQuickOpenSimulation.add(lastItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private JMenuBar createMenuBar() {
|
private JMenuBar createMenuBar() {
|
||||||
JMenuBar menuBar = new JMenuBar();
|
JMenuBar menuBar = new JMenuBar();
|
||||||
JMenu menu;
|
JMenu menu;
|
||||||
|
@ -240,6 +324,16 @@ public class GUI {
|
||||||
|
|
||||||
// File menu
|
// File menu
|
||||||
menu = new JMenu("File");
|
menu = new JMenu("File");
|
||||||
|
menu.addMenuListener(new MenuListener() {
|
||||||
|
public void menuSelected(MenuEvent e) {
|
||||||
|
updateOpenHistoryMenuItems();
|
||||||
|
}
|
||||||
|
public void menuDeselected(MenuEvent e) {
|
||||||
|
}
|
||||||
|
|
||||||
|
public void menuCanceled(MenuEvent e) {
|
||||||
|
}
|
||||||
|
});
|
||||||
menu.setMnemonic(KeyEvent.VK_F);
|
menu.setMnemonic(KeyEvent.VK_F);
|
||||||
menuBar.add(menu);
|
menuBar.add(menu);
|
||||||
|
|
||||||
|
@ -257,17 +351,13 @@ public class GUI {
|
||||||
menuItem.addActionListener(guiEventHandler);
|
menuItem.addActionListener(guiEventHandler);
|
||||||
menu.add(menuItem);
|
menu.add(menuItem);
|
||||||
|
|
||||||
menuItem = new JMenuItem("Open simulation");
|
menuOpenSimulation = new JMenu("Open simulation");
|
||||||
menuItem.setMnemonic(KeyEvent.VK_O);
|
menuOpenSimulation.setMnemonic(KeyEvent.VK_O);
|
||||||
menuItem.setActionCommand("open sim");
|
menu.add(menuOpenSimulation);
|
||||||
menuItem.addActionListener(guiEventHandler);
|
|
||||||
menu.add(menuItem);
|
|
||||||
|
|
||||||
menuItem = new JMenuItem("Quick-open simulation");
|
menuQuickOpenSimulation = new JMenu("Quick-open simulation");
|
||||||
menuItem.setMnemonic(KeyEvent.VK_Q);
|
menuQuickOpenSimulation.setMnemonic(KeyEvent.VK_Q);
|
||||||
menuItem.setActionCommand("open sim quick");
|
menu.add(menuQuickOpenSimulation);
|
||||||
menuItem.addActionListener(guiEventHandler);
|
|
||||||
menu.add(menuItem);
|
|
||||||
|
|
||||||
menuItem = new JMenuItem("Save simulation");
|
menuItem = new JMenuItem("Save simulation");
|
||||||
menuItem.setMnemonic(KeyEvent.VK_S);
|
menuItem.setMnemonic(KeyEvent.VK_S);
|
||||||
|
@ -1601,10 +1691,11 @@ public class GUI {
|
||||||
/**
|
/**
|
||||||
* Load a simulation configuration file from disk
|
* Load a simulation configuration file from disk
|
||||||
*
|
*
|
||||||
* @param askForConfirmation
|
* @param askForConfirmation Ask for confirmation before removing any current simulation
|
||||||
* Should we ask for confirmation if a simulation is already active?
|
* @param quick Quick-load simulation
|
||||||
|
* @param configFile Configuration file to load, if null a dialog will appear
|
||||||
*/
|
*/
|
||||||
public void doLoadConfig(boolean askForConfirmation, boolean quick) {
|
public void doLoadConfig(boolean askForConfirmation, boolean quick, File configFile) {
|
||||||
|
|
||||||
if (CoreComm.hasLibraryBeenLoaded()) {
|
if (CoreComm.hasLibraryBeenLoaded()) {
|
||||||
JOptionPane
|
JOptionPane
|
||||||
|
@ -1630,10 +1721,36 @@ public class GUI {
|
||||||
|
|
||||||
doRemoveSimulation(false);
|
doRemoveSimulation(false);
|
||||||
|
|
||||||
|
if (configFile != null) {
|
||||||
|
if (configFile.exists() && configFile.canRead()) {
|
||||||
|
Simulation newSim = null;
|
||||||
|
try {
|
||||||
|
newSim = loadSimulationConfig(configFile, quick);
|
||||||
|
addToFileHistory(configFile);
|
||||||
|
} catch (UnsatisfiedLinkError e) {
|
||||||
|
logger.warn("Could not reopen libraries: " + e.getMessage());
|
||||||
|
newSim = null;
|
||||||
|
}
|
||||||
|
if (newSim != null) {
|
||||||
|
myGUI.setSimulation(newSim);
|
||||||
|
}
|
||||||
|
} else
|
||||||
|
logger.fatal("No read access to file");
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
JFileChooser fc = new JFileChooser();
|
JFileChooser fc = new JFileChooser();
|
||||||
|
|
||||||
fc.setFileFilter(GUI.SAVED_SIMULATIONS_FILES);
|
fc.setFileFilter(GUI.SAVED_SIMULATIONS_FILES);
|
||||||
|
|
||||||
|
// Suggest file using history
|
||||||
|
Vector<File> history = getFileHistory();
|
||||||
|
if (history != null && history.size() > 0) {
|
||||||
|
File suggestedFile = getFileHistory().firstElement();
|
||||||
|
fc.setSelectedFile(suggestedFile);
|
||||||
|
}
|
||||||
|
|
||||||
int returnVal = fc.showOpenDialog(frame);
|
int returnVal = fc.showOpenDialog(frame);
|
||||||
if (returnVal == JFileChooser.APPROVE_OPTION) {
|
if (returnVal == JFileChooser.APPROVE_OPTION) {
|
||||||
File loadFile = fc.getSelectedFile();
|
File loadFile = fc.getSelectedFile();
|
||||||
|
@ -1648,6 +1765,7 @@ public class GUI {
|
||||||
Simulation newSim = null;
|
Simulation newSim = null;
|
||||||
try {
|
try {
|
||||||
newSim = loadSimulationConfig(loadFile, quick);
|
newSim = loadSimulationConfig(loadFile, quick);
|
||||||
|
addToFileHistory(loadFile);
|
||||||
} catch (UnsatisfiedLinkError e) {
|
} catch (UnsatisfiedLinkError e) {
|
||||||
logger.warn("Could not reopen libraries: " + e.getMessage());
|
logger.warn("Could not reopen libraries: " + e.getMessage());
|
||||||
newSim = null;
|
newSim = null;
|
||||||
|
@ -1678,6 +1796,13 @@ public class GUI {
|
||||||
|
|
||||||
fc.setFileFilter(GUI.SAVED_SIMULATIONS_FILES);
|
fc.setFileFilter(GUI.SAVED_SIMULATIONS_FILES);
|
||||||
|
|
||||||
|
// Suggest file using history
|
||||||
|
Vector<File> history = getFileHistory();
|
||||||
|
if (history != null && history.size() > 0) {
|
||||||
|
File suggestedFile = getFileHistory().firstElement();
|
||||||
|
fc.setSelectedFile(suggestedFile);
|
||||||
|
}
|
||||||
|
|
||||||
int returnVal = fc.showSaveDialog(myDesktopPane);
|
int returnVal = fc.showSaveDialog(myDesktopPane);
|
||||||
if (returnVal == JFileChooser.APPROVE_OPTION) {
|
if (returnVal == JFileChooser.APPROVE_OPTION) {
|
||||||
File saveFile = fc.getSelectedFile();
|
File saveFile = fc.getSelectedFile();
|
||||||
|
@ -1703,9 +1828,10 @@ public class GUI {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!saveFile.exists() || saveFile.canWrite())
|
if (!saveFile.exists() || saveFile.canWrite()) {
|
||||||
saveSimulationConfig(saveFile);
|
saveSimulationConfig(saveFile);
|
||||||
else
|
addToFileHistory(saveFile);
|
||||||
|
} else
|
||||||
logger.fatal("No write access to file");
|
logger.fatal("No write access to file");
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
@ -1945,9 +2071,15 @@ public class GUI {
|
||||||
} else if (e.getActionCommand().equals("close sim")) {
|
} else if (e.getActionCommand().equals("close sim")) {
|
||||||
myGUI.doRemoveSimulation(true);
|
myGUI.doRemoveSimulation(true);
|
||||||
} else if (e.getActionCommand().equals("open sim")) {
|
} else if (e.getActionCommand().equals("open sim")) {
|
||||||
myGUI.doLoadConfig(true, false);
|
myGUI.doLoadConfig(true, false, null);
|
||||||
|
} else if (e.getActionCommand().equals("open last sim")) {
|
||||||
|
File file = (File) ((JMenuItem) e.getSource()).getClientProperty("file");
|
||||||
|
myGUI.doLoadConfig(true, false, file);
|
||||||
} else if (e.getActionCommand().equals("open sim quick")) {
|
} else if (e.getActionCommand().equals("open sim quick")) {
|
||||||
myGUI.doLoadConfig(true, true);
|
myGUI.doLoadConfig(true, true, null);
|
||||||
|
} else if (e.getActionCommand().equals("open last sim quick")) {
|
||||||
|
File file = (File) ((JMenuItem) e.getSource()).getClientProperty("file");
|
||||||
|
myGUI.doLoadConfig(true, true, file);
|
||||||
} else if (e.getActionCommand().equals("save sim")) {
|
} else if (e.getActionCommand().equals("save sim")) {
|
||||||
myGUI.doSaveConfig(true);
|
myGUI.doSaveConfig(true);
|
||||||
} else if (e.getActionCommand().equals("quit")) {
|
} else if (e.getActionCommand().equals("quit")) {
|
||||||
|
|
Loading…
Reference in a new issue