some minor fixed:

* warn when trying to load visualized plugins when cooja is not visualized
* force calling doLoadConfig() from non-AWT thread
This commit is contained in:
fros4943 2008-09-29 13:02:15 +00:00
parent 1a1ddd0bd5
commit 1483a80a28

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.80 2008/09/18 14:46:24 fros4943 Exp $ * $Id: GUI.java,v 1.81 2008/09/29 13:02:15 fros4943 Exp $
*/ */
package se.sics.cooja; package se.sics.cooja;
@ -384,7 +384,7 @@ public class GUI extends Observable {
} }
} }
private Vector<File> getFileHistory() { public Vector<File> getFileHistory() {
Vector<File> history = new Vector<File>(); Vector<File> history = new Vector<File>();
// Fetch current history // Fetch current history
@ -397,7 +397,7 @@ public class GUI extends Observable {
return history; return history;
} }
private void addToFileHistory(File file) { public void addToFileHistory(File file) {
// Fetch current history // Fetch current history
String[] history = getExternalToolsSetting("SIMCFG_HISTORY", "").split(";"); String[] history = getExternalToolsSetting("SIMCFG_HISTORY", "").split(";");
@ -1740,7 +1740,7 @@ public class GUI extends Observable {
pluginClass.asSubclass(VisPlugin.class); pluginClass.asSubclass(VisPlugin.class);
// Cast succeded, plugin is visualizer plugin! // Cast succeded, plugin is visualizer plugin!
logger.fatal("Can't start visualizer plugin (no GUI): " + pluginClass); logger.warn("Can't start visualizer plugin (no GUI): " + pluginClass);
return null; return null;
} catch (ClassCastException e) { } catch (ClassCastException e) {
} }
@ -2131,7 +2131,7 @@ public class GUI extends Observable {
doRemoveSimulation(false); doRemoveSimulation(false);
// Check already selected file, or select file using filechooser // Check already selected file, or select file using filechooser
if (configFile != null) { if (configFile != null && !configFile.isDirectory()) {
if (!configFile.exists() || !configFile.canRead()) { if (!configFile.exists() || !configFile.canRead()) {
logger.fatal("No read access to file"); logger.fatal("No read access to file");
return; return;
@ -2141,12 +2141,16 @@ public class GUI extends Observable {
fc.setFileFilter(GUI.SAVED_SIMULATIONS_FILES); fc.setFileFilter(GUI.SAVED_SIMULATIONS_FILES);
if (configFile != null && configFile.isDirectory()) {
fc.setCurrentDirectory(configFile);
} else {
// Suggest file using history // Suggest file using history
Vector<File> history = getFileHistory(); Vector<File> history = getFileHistory();
if (history != null && history.size() > 0) { if (history != null && history.size() > 0) {
File suggestedFile = getFileHistory().firstElement(); File suggestedFile = getFileHistory().firstElement();
fc.setSelectedFile(suggestedFile); fc.setSelectedFile(suggestedFile);
} }
}
int returnVal = fc.showOpenDialog(GUI.getTopParentContainer()); int returnVal = fc.showOpenDialog(GUI.getTopParentContainer());
if (returnVal == JFileChooser.APPROVE_OPTION) { if (returnVal == JFileChooser.APPROVE_OPTION) {
@ -2169,8 +2173,9 @@ public class GUI extends Observable {
} }
} }
// Load simulation in separate thread, while showing progress monitor
final JDialog progressDialog; final JDialog progressDialog;
if (quick) {
if (GUI.getTopParentContainer() instanceof Window) { if (GUI.getTopParentContainer() instanceof Window) {
progressDialog = new JDialog((Window) GUI.getTopParentContainer(), "Loading", ModalityType.APPLICATION_MODAL); progressDialog = new JDialog((Window) GUI.getTopParentContainer(), "Loading", ModalityType.APPLICATION_MODAL);
} else if (GUI.getTopParentContainer() instanceof Frame) { } else if (GUI.getTopParentContainer() instanceof Frame) {
@ -2182,37 +2187,9 @@ public class GUI extends Observable {
progressDialog = new JDialog((Frame) null, "Loading", ModalityType.APPLICATION_MODAL); progressDialog = new JDialog((Frame) null, "Loading", ModalityType.APPLICATION_MODAL);
} }
final File fileToLoad = configFile; final Thread loadThread = Thread.currentThread();
final Thread loadThread = new Thread(new Runnable() { SwingUtilities.invokeLater(new Runnable() {
public void run() { public void run() {
Simulation newSim = null;
try {
newSim = loadSimulationConfig(fileToLoad, quick);
addToFileHistory(fileToLoad);
if (progressDialog != null && progressDialog.isDisplayable()) {
progressDialog.dispose();
}
} catch (UnsatisfiedLinkError e) {
showErrorDialog(GUI.getTopParentContainer(), "Simulation load error", e, false);
if (progressDialog != null && progressDialog.isDisplayable()) {
progressDialog.dispose();
}
newSim = null;
} catch (SimulationCreationException e) {
showErrorDialog(GUI.getTopParentContainer(), "Simulation load error", e, false);
if (progressDialog != null && progressDialog.isDisplayable()) {
progressDialog.dispose();
}
newSim = null;
}
if (newSim != null) {
myGUI.setSimulation(newSim);
}
}
});
JPanel progressPanel = new JPanel(new BorderLayout()); JPanel progressPanel = new JPanel(new BorderLayout());
JProgressBar progressBar; JProgressBar progressBar;
JButton button; JButton button;
@ -2221,6 +2198,7 @@ public class GUI extends Observable {
progressBar.setValue(0); progressBar.setValue(0);
progressBar.setIndeterminate(true); progressBar.setIndeterminate(true);
button = new JButton("Cancel"); button = new JButton("Cancel");
button.addActionListener(new ActionListener() { button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (loadThread != null && loadThread.isAlive()) { if (loadThread != null && loadThread.isAlive()) {
@ -2245,14 +2223,33 @@ public class GUI extends Observable {
progressDialog.getRootPane().setDefaultButton(button); progressDialog.getRootPane().setDefaultButton(button);
progressDialog.setLocationRelativeTo(GUI.getTopParentContainer()); progressDialog.setLocationRelativeTo(GUI.getTopParentContainer());
progressDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); progressDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
loadThread.start();
if (quick) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
progressDialog.setVisible(true); progressDialog.setVisible(true);
} }
}); });
} else {
progressDialog = null;
} }
// Load simulation in this thread, while showing progress monitor
final File fileToLoad = configFile;
Simulation newSim = null;
try {
newSim = loadSimulationConfig(fileToLoad, quick);
addToFileHistory(fileToLoad);
} catch (UnsatisfiedLinkError e) {
showErrorDialog(GUI.getTopParentContainer(), "Simulation load error", e, false);
} catch (SimulationCreationException e) {
showErrorDialog(GUI.getTopParentContainer(), "Simulation load error", e, false);
}
if (progressDialog != null && progressDialog.isDisplayable()) {
progressDialog.dispose();
}
if (newSim != null) {
myGUI.setSimulation(newSim);
}
return;
} }
/** /**
@ -2694,15 +2691,31 @@ public class GUI extends Observable {
} 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("confopen sim")) { } else if (e.getActionCommand().equals("confopen sim")) {
new Thread(new Runnable() {
public void run() {
myGUI.doLoadConfig(true, false, null); myGUI.doLoadConfig(true, false, null);
}
}).start();
} else if (e.getActionCommand().equals("confopen last sim")) { } else if (e.getActionCommand().equals("confopen last sim")) {
File file = (File) ((JMenuItem) e.getSource()).getClientProperty("file"); final File file = (File) ((JMenuItem) e.getSource()).getClientProperty("file");
new Thread(new Runnable() {
public void run() {
myGUI.doLoadConfig(true, false, file); myGUI.doLoadConfig(true, false, file);
}
}).start();
} else if (e.getActionCommand().equals("open sim")) { } else if (e.getActionCommand().equals("open sim")) {
new Thread(new Runnable() {
public void run() {
myGUI.doLoadConfig(true, true, null); myGUI.doLoadConfig(true, true, null);
}
}).start();
} else if (e.getActionCommand().equals("open last sim")) { } else if (e.getActionCommand().equals("open last sim")) {
File file = (File) ((JMenuItem) e.getSource()).getClientProperty("file"); final File file = (File) ((JMenuItem) e.getSource()).getClientProperty("file");
new Thread(new Runnable() {
public void run() {
myGUI.doLoadConfig(true, true, file); myGUI.doLoadConfig(true, true, file);
}
}).start();
} 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")) {
@ -3435,9 +3448,9 @@ public class GUI extends Observable {
// Read plugin class // Read plugin class
String pluginClassName = pluginElement.getText().trim(); String pluginClassName = pluginElement.getText().trim();
Class<? extends Plugin> visPluginClass = tryLoadClass(this, Class<? extends Plugin> pluginClass = tryLoadClass(this,
Plugin.class, pluginClassName); Plugin.class, pluginClassName);
if (visPluginClass == null) { if (pluginClass == null) {
logger.fatal("Could not load plugin class: " + pluginClassName); logger.fatal("Could not load plugin class: " + pluginClassName);
return false; return false;
} }
@ -3455,15 +3468,21 @@ public class GUI extends Observable {
} }
// Start plugin (before applying rest of config) // Start plugin (before applying rest of config)
Plugin startedPlugin = startPlugin(visPluginClass, this, simulation, Plugin startedPlugin = startPlugin(pluginClass, this, simulation, mote);
mote);
/* Ignore visualized plugins if Cooja not visualized */
try {
if (!visAvailable && startedPlugin == null && pluginClass.asSubclass(VisPlugin.class) != null) {
continue;
}
} catch (ClassCastException e) { }
// Apply plugin specific configuration // Apply plugin specific configuration
for (Element pluginSubElement : (List<Element>) pluginElement for (Element pluginSubElement : (List<Element>) pluginElement
.getChildren()) { .getChildren()) {
if (pluginSubElement.getName().equals("plugin_config")) { if (pluginSubElement.getName().equals("plugin_config")) {
startedPlugin.setConfigXML(pluginSubElement.getChildren(), startedPlugin.setConfigXML(pluginSubElement.getChildren(), visAvailable);
visAvailable);
} }
} }