mirror of
https://github.com/oliverschmidt/contiki.git
synced 2025-01-03 11:30:53 +00:00
introduced wrapper class for executing swing code in event dispatcher thread.
not used by all parts of the code yet
This commit is contained in:
parent
118aa4cd4e
commit
553b576e98
@ -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.89 2008/10/29 13:31:02 fros4943 Exp $
|
* $Id: GUI.java,v 1.90 2008/11/04 14:32:32 fros4943 Exp $
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package se.sics.cooja;
|
package se.sics.cooja;
|
||||||
@ -34,6 +34,7 @@ import java.awt.Dialog.ModalityType;
|
|||||||
import java.awt.event.*;
|
import java.awt.event.*;
|
||||||
import java.beans.PropertyVetoException;
|
import java.beans.PropertyVetoException;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLClassLoader;
|
import java.net.URLClassLoader;
|
||||||
@ -258,6 +259,16 @@ public class GUI extends Observable {
|
|||||||
setExternalToolsSetting("PATH_CONTIKI", specifiedContikiPath);
|
setExternalToolsSetting("PATH_CONTIKI", specifiedContikiPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Debugging - Break on repaints outside EDT */
|
||||||
|
/*RepaintManager.setCurrentManager(new RepaintManager() {
|
||||||
|
public void addDirtyRegion(JComponent comp, int a, int b, int c, int d) {
|
||||||
|
if(!java.awt.EventQueue.isDispatchThread()) {
|
||||||
|
throw new RuntimeException("Repainting outside EDT");
|
||||||
|
}
|
||||||
|
super.addDirtyRegion(comp, a, b, c, d);
|
||||||
|
}
|
||||||
|
});*/
|
||||||
|
|
||||||
// Register default project directories
|
// Register default project directories
|
||||||
String defaultProjectDirs = getExternalToolsSetting(
|
String defaultProjectDirs = getExternalToolsSetting(
|
||||||
"DEFAULT_PROJECTDIRS", null);
|
"DEFAULT_PROJECTDIRS", null);
|
||||||
@ -849,7 +860,6 @@ public class GUI extends Observable {
|
|||||||
} catch (IllegalAccessException e) {
|
} catch (IllegalAccessException e) {
|
||||||
logger.warn("LookAndFeel: " + e);
|
logger.warn("LookAndFeel: " + e);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1613,36 +1623,42 @@ public class GUI extends Observable {
|
|||||||
* @param plugin
|
* @param plugin
|
||||||
* Internal frame to add
|
* Internal frame to add
|
||||||
*/
|
*/
|
||||||
public void showPlugin(VisPlugin plugin) {
|
public void showPlugin(final VisPlugin plugin) {
|
||||||
int nrFrames = myDesktopPane.getAllFrames().length;
|
new RunnableInEDT<Boolean>() {
|
||||||
myDesktopPane.add(plugin);
|
public Boolean work() {
|
||||||
|
int nrFrames = myDesktopPane.getAllFrames().length;
|
||||||
|
myDesktopPane.add(plugin);
|
||||||
|
|
||||||
// Set standard size if not specified by plugin itself
|
// Set standard size if not specified by plugin itself
|
||||||
if (plugin.getWidth() <= 0 || plugin.getHeight() <= 0) {
|
if (plugin.getWidth() <= 0 || plugin.getHeight() <= 0) {
|
||||||
plugin.setSize(FRAME_STANDARD_WIDTH, FRAME_STANDARD_HEIGHT);
|
plugin.setSize(FRAME_STANDARD_WIDTH, FRAME_STANDARD_HEIGHT);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set location if not already visible
|
// Set location if not already visible
|
||||||
if (plugin.getLocation().x <= 0 && plugin.getLocation().y <= 0) {
|
if (plugin.getLocation().x <= 0 && plugin.getLocation().y <= 0) {
|
||||||
plugin.setLocation(
|
plugin.setLocation(
|
||||||
nrFrames * FRAME_NEW_OFFSET,
|
nrFrames * FRAME_NEW_OFFSET,
|
||||||
nrFrames * FRAME_NEW_OFFSET);
|
nrFrames * FRAME_NEW_OFFSET);
|
||||||
}
|
}
|
||||||
|
|
||||||
plugin.setVisible(true);
|
plugin.setVisible(true);
|
||||||
|
|
||||||
// Deselect all other plugins before selecting the new one
|
// Deselect all other plugins before selecting the new one
|
||||||
try {
|
try {
|
||||||
for (JInternalFrame existingPlugin : myDesktopPane.getAllFrames()) {
|
for (JInternalFrame existingPlugin : myDesktopPane.getAllFrames()) {
|
||||||
existingPlugin.setSelected(false);
|
existingPlugin.setSelected(false);
|
||||||
|
}
|
||||||
|
plugin.setSelected(true);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mote plugin to front
|
||||||
|
myDesktopPane.moveToFront(plugin);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
plugin.setSelected(true);
|
}.invokeAndWait();
|
||||||
} catch (Exception e) {
|
|
||||||
// Ignore
|
|
||||||
}
|
|
||||||
|
|
||||||
// Mote plugin to front
|
|
||||||
myDesktopPane.moveToFront(plugin);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1680,29 +1696,26 @@ public class GUI extends Observable {
|
|||||||
* If plugin is the last one, ask user if we should remove current
|
* If plugin is the last one, ask user if we should remove current
|
||||||
* simulation also?
|
* simulation also?
|
||||||
*/
|
*/
|
||||||
public void removePlugin(Plugin plugin, boolean askUser) {
|
public void removePlugin(final Plugin plugin, final boolean askUser) {
|
||||||
// Clear any allocated resources and remove plugin
|
new RunnableInEDT<Boolean>() {
|
||||||
plugin.closePlugin();
|
public Boolean work() {
|
||||||
startedPlugins.remove(plugin);
|
/* Free resources */
|
||||||
|
plugin.closePlugin();
|
||||||
|
startedPlugins.remove(plugin);
|
||||||
|
|
||||||
// Dispose plugin if it has visualizer
|
/* Dispose visualized components */
|
||||||
if (plugin instanceof VisPlugin) {
|
if (plugin instanceof VisPlugin) {
|
||||||
((VisPlugin) plugin).dispose();
|
((VisPlugin) plugin).dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (getSimulation() != null && askUser && startedPlugins.isEmpty()) {
|
/* (OPTIONAL) Remove simulation if all plugins are closed */
|
||||||
String s1 = "Remove";
|
if (getSimulation() != null && askUser && startedPlugins.isEmpty()) {
|
||||||
String s2 = "Cancel";
|
doRemoveSimulation(true);
|
||||||
Object[] options = { s1, s2 };
|
}
|
||||||
int n = JOptionPane.showOptionDialog(frame,
|
|
||||||
"You have an active simulation.\nDo you want to remove it?",
|
return true;
|
||||||
"Remove current simulation?", JOptionPane.YES_NO_OPTION,
|
|
||||||
JOptionPane.QUESTION_MESSAGE, null, options, s1);
|
|
||||||
if (n != JOptionPane.YES_OPTION) {
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
doRemoveSimulation(false);
|
}.invokeAndWait();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1718,8 +1731,8 @@ public class GUI extends Observable {
|
|||||||
* Mote passed as argument to mote plugins
|
* Mote passed as argument to mote plugins
|
||||||
* @return Start plugin if any
|
* @return Start plugin if any
|
||||||
*/
|
*/
|
||||||
public Plugin startPlugin(Class<? extends Plugin> pluginClass, GUI gui,
|
public Plugin startPlugin(final Class<? extends Plugin> pluginClass,
|
||||||
Simulation simulation, Mote mote) {
|
final GUI gui, final Simulation simulation, final Mote mote) {
|
||||||
|
|
||||||
// Check that plugin class is registered
|
// Check that plugin class is registered
|
||||||
if (!pluginClasses.contains(pluginClass)) {
|
if (!pluginClasses.contains(pluginClass)) {
|
||||||
@ -1740,47 +1753,52 @@ public class GUI extends Observable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Construct plugin depending on plugin type
|
// Construct plugin depending on plugin type
|
||||||
Plugin newPlugin = null;
|
Plugin newPlugin = new RunnableInEDT<Plugin>() {
|
||||||
int pluginType = pluginClass.getAnnotation(PluginType.class).value();
|
public Plugin work() {
|
||||||
|
int pluginType = pluginClass.getAnnotation(PluginType.class).value();
|
||||||
|
Plugin plugin = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (pluginType == PluginType.MOTE_PLUGIN) {
|
if (pluginType == PluginType.MOTE_PLUGIN) {
|
||||||
if (mote == null) {
|
if (mote == null) {
|
||||||
logger.fatal("Can't start mote plugin (no mote selected)");
|
logger.fatal("Can't start mote plugin (no mote selected)");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
plugin = pluginClass.getConstructor(
|
||||||
|
new Class[] { Mote.class, Simulation.class, GUI.class })
|
||||||
|
.newInstance(mote, simulation, gui);
|
||||||
|
|
||||||
|
// Tag plugin with mote
|
||||||
|
plugin.tagWithObject(mote);
|
||||||
|
} else if (pluginType == PluginType.SIM_PLUGIN
|
||||||
|
|| pluginType == PluginType.SIM_STANDARD_PLUGIN) {
|
||||||
|
if (simulation == null) {
|
||||||
|
logger.fatal("Can't start simulation plugin (no simulation)");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
plugin = pluginClass.getConstructor(
|
||||||
|
new Class[] { Simulation.class, GUI.class }).newInstance(
|
||||||
|
simulation, gui);
|
||||||
|
} else if (pluginType == PluginType.COOJA_PLUGIN
|
||||||
|
|| pluginType == PluginType.COOJA_STANDARD_PLUGIN) {
|
||||||
|
if (gui == null) {
|
||||||
|
logger.fatal("Can't start COOJA plugin (no GUI)");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
plugin = pluginClass.getConstructor(new Class[] { GUI.class }).newInstance(gui);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.fatal("Exception thrown when starting plugin: " + e);
|
||||||
|
e.printStackTrace();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
newPlugin = pluginClass.getConstructor(
|
return plugin;
|
||||||
new Class[] { Mote.class, Simulation.class, GUI.class })
|
|
||||||
.newInstance(mote, simulation, gui);
|
|
||||||
|
|
||||||
// Tag plugin with mote
|
|
||||||
newPlugin.tagWithObject(mote);
|
|
||||||
} else if (pluginType == PluginType.SIM_PLUGIN
|
|
||||||
|| pluginType == PluginType.SIM_STANDARD_PLUGIN) {
|
|
||||||
if (simulation == null) {
|
|
||||||
logger.fatal("Can't start simulation plugin (no simulation)");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
newPlugin = pluginClass.getConstructor(
|
|
||||||
new Class[] { Simulation.class, GUI.class }).newInstance(
|
|
||||||
simulation, gui);
|
|
||||||
} else if (pluginType == PluginType.COOJA_PLUGIN
|
|
||||||
|| pluginType == PluginType.COOJA_STANDARD_PLUGIN) {
|
|
||||||
if (gui == null) {
|
|
||||||
logger.fatal("Can't start COOJA plugin (no GUI)");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
newPlugin = pluginClass.getConstructor(new Class[] { GUI.class })
|
|
||||||
.newInstance(gui);
|
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
}.invokeAndWait();
|
||||||
logger.fatal("Exception thrown when starting plugin: " + e);
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (newPlugin == null) {
|
if (newPlugin == null) {
|
||||||
return null;
|
return null;
|
||||||
@ -1871,14 +1889,14 @@ public class GUI extends Observable {
|
|||||||
* Should this plugin be added to the dedicated plugins menubar?
|
* Should this plugin be added to the dedicated plugins menubar?
|
||||||
* @return True if this plugin was registered ok, false otherwise
|
* @return True if this plugin was registered ok, false otherwise
|
||||||
*/
|
*/
|
||||||
private boolean registerPlugin(Class<? extends Plugin> newPluginClass,
|
private boolean registerPlugin(final Class<? extends Plugin> newPluginClass,
|
||||||
boolean addToMenu) {
|
boolean addToMenu) {
|
||||||
|
|
||||||
// Get description annotation (if any)
|
// Get description annotation (if any)
|
||||||
String description = getDescriptionOf(newPluginClass);
|
final String description = getDescriptionOf(newPluginClass);
|
||||||
|
|
||||||
// Get plugin type annotation (required)
|
// Get plugin type annotation (required)
|
||||||
int pluginType;
|
final int pluginType;
|
||||||
if (newPluginClass.isAnnotationPresent(PluginType.class)) {
|
if (newPluginClass.isAnnotationPresent(PluginType.class)) {
|
||||||
pluginType = newPluginClass.getAnnotation(PluginType.class).value();
|
pluginType = newPluginClass.getAnnotation(PluginType.class).value();
|
||||||
} else {
|
} else {
|
||||||
@ -1892,37 +1910,39 @@ public class GUI extends Observable {
|
|||||||
Simulation.class, GUI.class });
|
Simulation.class, GUI.class });
|
||||||
} else if (pluginType == PluginType.SIM_PLUGIN
|
} else if (pluginType == PluginType.SIM_PLUGIN
|
||||||
|| pluginType == PluginType.SIM_STANDARD_PLUGIN) {
|
|| pluginType == PluginType.SIM_STANDARD_PLUGIN) {
|
||||||
newPluginClass
|
newPluginClass.getConstructor(new Class[] { Simulation.class, GUI.class });
|
||||||
.getConstructor(new Class[] { Simulation.class, GUI.class });
|
|
||||||
} else if (pluginType == PluginType.COOJA_PLUGIN
|
} else if (pluginType == PluginType.COOJA_PLUGIN
|
||||||
|| pluginType == PluginType.COOJA_STANDARD_PLUGIN) {
|
|| pluginType == PluginType.COOJA_STANDARD_PLUGIN) {
|
||||||
newPluginClass.getConstructor(new Class[] { GUI.class });
|
newPluginClass.getConstructor(new Class[] { GUI.class });
|
||||||
} else {
|
} else {
|
||||||
logger.fatal("Could not find valid plugin type annotation in class "
|
logger.fatal("Could not find valid plugin type annotation in class " + newPluginClass);
|
||||||
+ newPluginClass);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
} catch (NoSuchMethodException e) {
|
} catch (NoSuchMethodException e) {
|
||||||
logger.fatal("Could not find valid constructor in class "
|
logger.fatal("Could not find valid constructor in class " + newPluginClass + ": " + e);
|
||||||
+ newPluginClass + ": " + e);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (addToMenu && menuPlugins != null) {
|
if (addToMenu && menuPlugins != null) {
|
||||||
// Create 'start plugin'-menu item
|
new RunnableInEDT<Boolean>() {
|
||||||
JMenuItem menuItem = new JMenuItem(description);
|
public Boolean work() {
|
||||||
menuItem.setActionCommand("start plugin");
|
// Create 'start plugin'-menu item
|
||||||
menuItem.putClientProperty("class", newPluginClass);
|
JMenuItem menuItem = new JMenuItem(description);
|
||||||
menuItem.addActionListener(guiEventHandler);
|
menuItem.setActionCommand("start plugin");
|
||||||
|
menuItem.putClientProperty("class", newPluginClass);
|
||||||
|
menuItem.addActionListener(guiEventHandler);
|
||||||
|
|
||||||
menuPlugins.add(menuItem);
|
menuPlugins.add(menuItem);
|
||||||
|
|
||||||
if (pluginType == PluginType.MOTE_PLUGIN) {
|
if (pluginType == PluginType.MOTE_PLUGIN) {
|
||||||
// Disable previous menu item and add new item to mote plugins menu
|
// Disable previous menu item and add new item to mote plugins menu
|
||||||
menuItem.setEnabled(false);
|
menuItem.setEnabled(false);
|
||||||
menuItem.setToolTipText("Mote plugin");
|
menuItem.setToolTipText("Mote plugin");
|
||||||
menuMotePluginClasses.add(newPluginClass);
|
menuMotePluginClasses.add(newPluginClass);
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}.invokeAndWait();
|
||||||
}
|
}
|
||||||
|
|
||||||
pluginClasses.add(newPluginClass);
|
pluginClasses.add(newPluginClass);
|
||||||
@ -2043,53 +2063,65 @@ public class GUI extends Observable {
|
|||||||
*
|
*
|
||||||
* @param askForConfirmation
|
* @param askForConfirmation
|
||||||
* Should we ask for confirmation if a simulation is already active?
|
* Should we ask for confirmation if a simulation is already active?
|
||||||
|
* @return True if no simulation exists when method returns
|
||||||
*/
|
*/
|
||||||
public void doRemoveSimulation(boolean askForConfirmation) {
|
public boolean doRemoveSimulation(boolean askForConfirmation) {
|
||||||
|
|
||||||
if (mySimulation != null) {
|
if (mySimulation == null) {
|
||||||
if (askForConfirmation) {
|
return true;
|
||||||
String s1 = "Remove";
|
|
||||||
String s2 = "Cancel";
|
|
||||||
Object[] options = { s1, s2 };
|
|
||||||
int n = JOptionPane.showOptionDialog(GUI.getTopParentContainer(),
|
|
||||||
"You have an active simulation.\nDo you want to remove it?",
|
|
||||||
"Remove current simulation?", JOptionPane.YES_NO_OPTION,
|
|
||||||
JOptionPane.QUESTION_MESSAGE, null, options, s1);
|
|
||||||
if (n != JOptionPane.YES_OPTION) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close all started non-GUI plugins
|
|
||||||
for (Object startedPlugin : startedPlugins.toArray()) {
|
|
||||||
int pluginType = startedPlugin.getClass().getAnnotation(
|
|
||||||
PluginType.class).value();
|
|
||||||
if (pluginType != PluginType.COOJA_PLUGIN
|
|
||||||
&& pluginType != PluginType.COOJA_STANDARD_PLUGIN) {
|
|
||||||
removePlugin((Plugin) startedPlugin, false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete simulation
|
|
||||||
mySimulation.deleteObservers();
|
|
||||||
mySimulation.stopSimulation();
|
|
||||||
mySimulation = null;
|
|
||||||
|
|
||||||
// Unregister temporary plugin classes
|
|
||||||
Enumeration<Class<? extends Plugin>> pluginClasses = pluginClassesTemporary
|
|
||||||
.elements();
|
|
||||||
while (pluginClasses.hasMoreElements()) {
|
|
||||||
unregisterPlugin(pluginClasses.nextElement());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reset frame title
|
|
||||||
if (isVisualizedInFrame()) {
|
|
||||||
frame.setTitle("COOJA Simulator");
|
|
||||||
}
|
|
||||||
|
|
||||||
setChanged();
|
|
||||||
notifyObservers();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (askForConfirmation) {
|
||||||
|
boolean ok = new RunnableInEDT<Boolean>() {
|
||||||
|
public Boolean work() {
|
||||||
|
String s1 = "Remove";
|
||||||
|
String s2 = "Cancel";
|
||||||
|
Object[] options = { s1, s2 };
|
||||||
|
int n = JOptionPane.showOptionDialog(GUI.getTopParentContainer(),
|
||||||
|
"You have an active simulation.\nDo you want to remove it?",
|
||||||
|
"Remove current simulation?", JOptionPane.YES_NO_OPTION,
|
||||||
|
JOptionPane.QUESTION_MESSAGE, null, options, s1);
|
||||||
|
if (n != JOptionPane.YES_OPTION) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}.invokeAndWait();
|
||||||
|
|
||||||
|
if (!ok) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close all started non-GUI plugins
|
||||||
|
for (Object startedPlugin : startedPlugins.toArray()) {
|
||||||
|
int pluginType = startedPlugin.getClass().getAnnotation(PluginType.class).value();
|
||||||
|
if (pluginType != PluginType.COOJA_PLUGIN
|
||||||
|
&& pluginType != PluginType.COOJA_STANDARD_PLUGIN) {
|
||||||
|
removePlugin((Plugin) startedPlugin, false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete simulation
|
||||||
|
mySimulation.deleteObservers();
|
||||||
|
mySimulation.stopSimulation();
|
||||||
|
mySimulation = null;
|
||||||
|
|
||||||
|
// Unregister temporary plugin classes
|
||||||
|
Enumeration<Class<? extends Plugin>> pluginClasses = pluginClassesTemporary.elements();
|
||||||
|
while (pluginClasses.hasMoreElements()) {
|
||||||
|
unregisterPlugin(pluginClasses.nextElement());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset frame title
|
||||||
|
if (isVisualizedInFrame()) {
|
||||||
|
frame.setTitle("COOJA Simulator");
|
||||||
|
}
|
||||||
|
|
||||||
|
setChanged();
|
||||||
|
notifyObservers();
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2104,67 +2136,59 @@ public class GUI extends Observable {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*if (CoreComm.hasLibraryBeenLoaded()) {
|
/* Remove current simulation */
|
||||||
JOptionPane.showMessageDialog(GUI.getTopParentContainer(),
|
if (!doRemoveSimulation(true)) {
|
||||||
"Shared libraries has already been loaded.\nYou need to restart the simulator!",
|
|
||||||
"Can't load simulation", JOptionPane.ERROR_MESSAGE);
|
|
||||||
return;
|
return;
|
||||||
}*/
|
|
||||||
|
|
||||||
if (askForConfirmation && mySimulation != null) {
|
|
||||||
String s1 = "Remove";
|
|
||||||
String s2 = "Cancel";
|
|
||||||
Object[] options = { s1, s2 };
|
|
||||||
int n = JOptionPane.showOptionDialog(GUI.getTopParentContainer(),
|
|
||||||
"You have an active simulation.\nDo you want to remove it?",
|
|
||||||
"Remove current simulation?", JOptionPane.YES_NO_OPTION,
|
|
||||||
JOptionPane.QUESTION_MESSAGE, null, options, s1);
|
|
||||||
if (n != JOptionPane.YES_OPTION) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
doRemoveSimulation(false);
|
/* Use provided configuration, or open File Chooser */
|
||||||
|
|
||||||
// Check already selected file, or select file using filechooser
|
|
||||||
if (configFile != null && !configFile.isDirectory()) {
|
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;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
JFileChooser fc = new JFileChooser();
|
final File suggestedFile = configFile;
|
||||||
|
configFile = new RunnableInEDT<File>() {
|
||||||
|
public File work() {
|
||||||
|
JFileChooser fc = new JFileChooser();
|
||||||
|
|
||||||
fc.setFileFilter(GUI.SAVED_SIMULATIONS_FILES);
|
fc.setFileFilter(GUI.SAVED_SIMULATIONS_FILES);
|
||||||
|
|
||||||
if (configFile != null && configFile.isDirectory()) {
|
if (suggestedFile != null && suggestedFile.isDirectory()) {
|
||||||
fc.setCurrentDirectory(configFile);
|
fc.setCurrentDirectory(suggestedFile);
|
||||||
} else {
|
} else {
|
||||||
// Suggest file using history
|
/* Suggest file using file 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());
|
||||||
|
if (returnVal != JFileChooser.APPROVE_OPTION) {
|
||||||
|
logger.info("Load command cancelled by user...");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
File file = fc.getSelectedFile();
|
||||||
|
|
||||||
|
if (!file.exists()) {
|
||||||
|
/* Try default file extension */
|
||||||
|
file = new File(file.getParent(), file.getName() + SAVED_SIMULATIONS_FILES);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!file.exists() || !file.canRead()) {
|
||||||
|
logger.fatal("No read access to file");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return file;
|
||||||
}
|
}
|
||||||
}
|
}.invokeAndWait();
|
||||||
|
|
||||||
int returnVal = fc.showOpenDialog(GUI.getTopParentContainer());
|
if (configFile == null) {
|
||||||
if (returnVal == JFileChooser.APPROVE_OPTION) {
|
|
||||||
configFile = fc.getSelectedFile();
|
|
||||||
|
|
||||||
// Try adding extension if not founds
|
|
||||||
if (!configFile.exists()) {
|
|
||||||
configFile = new File(configFile.getParent(), configFile.getName()
|
|
||||||
+ SAVED_SIMULATIONS_FILES);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!configFile.exists() || !configFile.canRead()) {
|
|
||||||
logger.fatal("No read access to file");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
logger.info("Load command cancelled by user...");
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2172,20 +2196,23 @@ public class GUI extends Observable {
|
|||||||
final JDialog progressDialog;
|
final JDialog progressDialog;
|
||||||
|
|
||||||
if (quick) {
|
if (quick) {
|
||||||
if (GUI.getTopParentContainer() instanceof Window) {
|
|
||||||
progressDialog = new JDialog((Window) GUI.getTopParentContainer(), "Loading", ModalityType.APPLICATION_MODAL);
|
|
||||||
} else if (GUI.getTopParentContainer() instanceof Frame) {
|
|
||||||
progressDialog = new JDialog((Frame) GUI.getTopParentContainer(), "Loading", ModalityType.APPLICATION_MODAL);
|
|
||||||
} else if (GUI.getTopParentContainer() instanceof Dialog) {
|
|
||||||
progressDialog = new JDialog((Dialog) GUI.getTopParentContainer(), "Loading", ModalityType.APPLICATION_MODAL);
|
|
||||||
} else {
|
|
||||||
logger.warn("No parent container");
|
|
||||||
progressDialog = new JDialog((Frame) null, "Loading", ModalityType.APPLICATION_MODAL);
|
|
||||||
}
|
|
||||||
|
|
||||||
final Thread loadThread = Thread.currentThread();
|
final Thread loadThread = Thread.currentThread();
|
||||||
SwingUtilities.invokeLater(new Runnable() {
|
|
||||||
public void run() {
|
progressDialog = new RunnableInEDT<JDialog>() {
|
||||||
|
public JDialog work() {
|
||||||
|
final JDialog progressDialog;
|
||||||
|
|
||||||
|
if (GUI.getTopParentContainer() instanceof Window) {
|
||||||
|
progressDialog = new JDialog((Window) GUI.getTopParentContainer(), "Loading", ModalityType.APPLICATION_MODAL);
|
||||||
|
} else if (GUI.getTopParentContainer() instanceof Frame) {
|
||||||
|
progressDialog = new JDialog((Frame) GUI.getTopParentContainer(), "Loading", ModalityType.APPLICATION_MODAL);
|
||||||
|
} else if (GUI.getTopParentContainer() instanceof Dialog) {
|
||||||
|
progressDialog = new JDialog((Dialog) GUI.getTopParentContainer(), "Loading", ModalityType.APPLICATION_MODAL);
|
||||||
|
} else {
|
||||||
|
logger.warn("No parent container");
|
||||||
|
progressDialog = new JDialog((Frame) null, "Loading", ModalityType.APPLICATION_MODAL);
|
||||||
|
}
|
||||||
|
|
||||||
JPanel progressPanel = new JPanel(new BorderLayout());
|
JPanel progressPanel = new JPanel(new BorderLayout());
|
||||||
JProgressBar progressBar;
|
JProgressBar progressBar;
|
||||||
JButton button;
|
JButton button;
|
||||||
@ -2197,11 +2224,11 @@ public class GUI extends Observable {
|
|||||||
|
|
||||||
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.isAlive()) {
|
||||||
loadThread.interrupt();
|
loadThread.interrupt();
|
||||||
doRemoveSimulation(false);
|
doRemoveSimulation(false);
|
||||||
}
|
}
|
||||||
if (progressDialog != null && progressDialog.isDisplayable()) {
|
if (progressDialog.isDisplayable()) {
|
||||||
progressDialog.dispose();
|
progressDialog.dispose();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2219,9 +2246,16 @@ 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);
|
||||||
progressDialog.setVisible(true);
|
|
||||||
|
java.awt.EventQueue.invokeLater(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
progressDialog.setVisible(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return progressDialog;
|
||||||
}
|
}
|
||||||
});
|
}.invokeAndWait();
|
||||||
} else {
|
} else {
|
||||||
progressDialog = null;
|
progressDialog = null;
|
||||||
}
|
}
|
||||||
@ -2426,21 +2460,12 @@ public class GUI extends Observable {
|
|||||||
* Should we ask for confirmation if a simulation is already active?
|
* Should we ask for confirmation if a simulation is already active?
|
||||||
*/
|
*/
|
||||||
public void doCreateSimulation(boolean askForConfirmation) {
|
public void doCreateSimulation(boolean askForConfirmation) {
|
||||||
if (askForConfirmation && mySimulation != null) {
|
/* Remove current simulation */
|
||||||
String s1 = "Remove";
|
if (!doRemoveSimulation(askForConfirmation)) {
|
||||||
String s2 = "Cancel";
|
return;
|
||||||
Object[] options = { s1, s2 };
|
|
||||||
int n = JOptionPane.showOptionDialog(GUI.getTopParentContainer(),
|
|
||||||
"You have an active simulation.\nDo you want to remove it?",
|
|
||||||
"Remove current simulation?", JOptionPane.YES_NO_OPTION,
|
|
||||||
JOptionPane.QUESTION_MESSAGE, null, options, s1);
|
|
||||||
if (n != JOptionPane.YES_OPTION) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create new simulation
|
// Create new simulation
|
||||||
doRemoveSimulation(false);
|
|
||||||
Simulation newSim = new Simulation(this);
|
Simulation newSim = new Simulation(this);
|
||||||
boolean createdOK = CreateSimDialog.showDialog(GUI.getTopParentContainer(), newSim);
|
boolean createdOK = CreateSimDialog.showDialog(GUI.getTopParentContainer(), newSim);
|
||||||
if (createdOK) {
|
if (createdOK) {
|
||||||
@ -3256,7 +3281,7 @@ public class GUI extends Observable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Create old to new identifier mappings */
|
/* Create old to new identifier mappings */
|
||||||
Enumeration existingIdentifiers = moteTypeIDMappings.keys();
|
Enumeration<Object> existingIdentifiers = moteTypeIDMappings.keys();
|
||||||
while (existingIdentifiers.hasMoreElements()) {
|
while (existingIdentifiers.hasMoreElements()) {
|
||||||
String existingIdentifier = (String) existingIdentifiers.nextElement();
|
String existingIdentifier = (String) existingIdentifiers.nextElement();
|
||||||
Collection<MoteType> existingMoteTypes = null;
|
Collection<MoteType> existingMoteTypes = null;
|
||||||
@ -3449,7 +3474,7 @@ public class GUI extends Observable {
|
|||||||
public boolean setPluginsConfigXML(Collection<Element> configXML,
|
public boolean setPluginsConfigXML(Collection<Element> configXML,
|
||||||
Simulation simulation, boolean visAvailable) {
|
Simulation simulation, boolean visAvailable) {
|
||||||
|
|
||||||
for (Element pluginElement : configXML.toArray(new Element[0])) {
|
for (final Element pluginElement : configXML.toArray(new Element[0])) {
|
||||||
if (pluginElement.getName().equals("plugin")) {
|
if (pluginElement.getName().equals("plugin")) {
|
||||||
|
|
||||||
// Read plugin class
|
// Read plugin class
|
||||||
@ -3463,8 +3488,7 @@ public class GUI extends Observable {
|
|||||||
|
|
||||||
// Parse plugin mote argument (if any)
|
// Parse plugin mote argument (if any)
|
||||||
Mote mote = null;
|
Mote mote = null;
|
||||||
for (Element pluginSubElement : (List<Element>) pluginElement
|
for (Element pluginSubElement : (List<Element>) pluginElement.getChildren()) {
|
||||||
.getChildren()) {
|
|
||||||
if (pluginSubElement.getName().equals("mote_arg")) {
|
if (pluginSubElement.getName().equals("mote_arg")) {
|
||||||
int moteNr = Integer.parseInt(pluginSubElement.getText());
|
int moteNr = Integer.parseInt(pluginSubElement.getText());
|
||||||
if (moteNr >= 0 && moteNr < simulation.getMotesCount()) {
|
if (moteNr >= 0 && moteNr < simulation.getMotesCount()) {
|
||||||
@ -3485,8 +3509,7 @@ public class GUI extends Observable {
|
|||||||
|
|
||||||
|
|
||||||
// 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(), visAvailable);
|
startedPlugin.setConfigXML(pluginSubElement.getChildren(), visAvailable);
|
||||||
}
|
}
|
||||||
@ -3494,56 +3517,61 @@ public class GUI extends Observable {
|
|||||||
|
|
||||||
// If plugin is visualizer plugin, parse visualization arguments
|
// If plugin is visualizer plugin, parse visualization arguments
|
||||||
if (startedPlugin instanceof VisPlugin) {
|
if (startedPlugin instanceof VisPlugin) {
|
||||||
Dimension size = new Dimension(100, 100);
|
final VisPlugin startedVisPlugin = (VisPlugin) startedPlugin;
|
||||||
Point location = new Point(100, 100);
|
new RunnableInEDT<Boolean>() {
|
||||||
VisPlugin startedVisPlugin = (VisPlugin) startedPlugin;
|
public Boolean work() {
|
||||||
|
Dimension size = new Dimension(100, 100);
|
||||||
|
Point location = new Point(100, 100);
|
||||||
|
|
||||||
for (Element pluginSubElement : (List<Element>) pluginElement
|
for (Element pluginSubElement : (List<Element>) pluginElement.getChildren()) {
|
||||||
.getChildren()) {
|
|
||||||
|
|
||||||
if (pluginSubElement.getName().equals("width") && size != null) {
|
if (pluginSubElement.getName().equals("width") && size != null) {
|
||||||
size.width = Integer.parseInt(pluginSubElement.getText());
|
size.width = Integer.parseInt(pluginSubElement.getText());
|
||||||
startedVisPlugin.setSize(size);
|
startedVisPlugin.setSize(size);
|
||||||
} else if (pluginSubElement.getName().equals("height")) {
|
} else if (pluginSubElement.getName().equals("height")) {
|
||||||
size.height = Integer.parseInt(pluginSubElement.getText());
|
size.height = Integer.parseInt(pluginSubElement.getText());
|
||||||
startedVisPlugin.setSize(size);
|
startedVisPlugin.setSize(size);
|
||||||
} else if (pluginSubElement.getName().equals("z")) {
|
} else if (pluginSubElement.getName().equals("z")) {
|
||||||
int zOrder = Integer.parseInt(pluginSubElement.getText());
|
int zOrder = Integer.parseInt(pluginSubElement.getText());
|
||||||
// Save z order as temporary client property
|
// Save z order as temporary client property
|
||||||
startedVisPlugin.putClientProperty("zorder", zOrder);
|
startedVisPlugin.putClientProperty("zorder", zOrder);
|
||||||
} else if (pluginSubElement.getName().equals("location_x")) {
|
} else if (pluginSubElement.getName().equals("location_x")) {
|
||||||
location.x = Integer.parseInt(pluginSubElement.getText());
|
location.x = Integer.parseInt(pluginSubElement.getText());
|
||||||
startedVisPlugin.setLocation(location);
|
startedVisPlugin.setLocation(location);
|
||||||
} else if (pluginSubElement.getName().equals("location_y")) {
|
} else if (pluginSubElement.getName().equals("location_y")) {
|
||||||
location.y = Integer.parseInt(pluginSubElement.getText());
|
location.y = Integer.parseInt(pluginSubElement.getText());
|
||||||
startedVisPlugin.setLocation(location);
|
startedVisPlugin.setLocation(location);
|
||||||
} else if (pluginSubElement.getName().equals("minimized")) {
|
} else if (pluginSubElement.getName().equals("minimized")) {
|
||||||
try {
|
try {
|
||||||
startedVisPlugin.setIcon(Boolean.parseBoolean(pluginSubElement
|
startedVisPlugin.setIcon(Boolean.parseBoolean(pluginSubElement.getText()));
|
||||||
.getText()));
|
} catch (PropertyVetoException e) {
|
||||||
} catch (PropertyVetoException e) {
|
// Ignoring
|
||||||
// Ignoring
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For all started visplugins, check if they have a zorder property
|
||||||
|
try {
|
||||||
|
for (JInternalFrame plugin : getDesktopPane().getAllFrames()) {
|
||||||
|
if (plugin.getClientProperty("zorder") != null) {
|
||||||
|
getDesktopPane().setComponentZOrder(plugin,
|
||||||
|
((Integer) plugin.getClientProperty("zorder")).intValue());
|
||||||
|
plugin.putClientProperty("zorder", null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
// Ignore errors
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}.invokeAndWait();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// For all started visplugins, check if they have a zorder property
|
|
||||||
try {
|
|
||||||
for (JInternalFrame plugin : getDesktopPane().getAllFrames()) {
|
|
||||||
if (plugin.getClientProperty("zorder") != null) {
|
|
||||||
getDesktopPane().setComponentZOrder(plugin,
|
|
||||||
((Integer) plugin.getClientProperty("zorder")).intValue());
|
|
||||||
plugin.putClientProperty("zorder", null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
// Ignore errors
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3577,10 +3605,14 @@ public class GUI extends Observable {
|
|||||||
* @param exception
|
* @param exception
|
||||||
* Exception causing window to be shown
|
* Exception causing window to be shown
|
||||||
* @param retryAvailable
|
* @param retryAvailable
|
||||||
* If true, a retry option is available
|
* If true, a retry option is presented
|
||||||
|
* @return Retry failed operation
|
||||||
*/
|
*/
|
||||||
public static boolean showErrorDialog(Component parentComponent,
|
public static boolean showErrorDialog(final Component parentComponent,
|
||||||
final String title, Throwable exception, boolean retryAvailable) {
|
final String title, final Throwable exception, final boolean retryAvailable) {
|
||||||
|
|
||||||
|
return new RunnableInEDT<Boolean>() {
|
||||||
|
public Boolean work() {
|
||||||
|
|
||||||
MessageList compilationOutput = null;
|
MessageList compilationOutput = null;
|
||||||
MessageList stackTrace = null;
|
MessageList stackTrace = null;
|
||||||
@ -3756,6 +3788,54 @@ public class GUI extends Observable {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
}.invokeAndWait();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs work method in event dispatcher thread.
|
||||||
|
* Worker method returns a value.
|
||||||
|
*
|
||||||
|
* @author Fredrik Österlind
|
||||||
|
*/
|
||||||
|
public static abstract class RunnableInEDT<T> {
|
||||||
|
private T val;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Work method to be implemented.
|
||||||
|
*
|
||||||
|
* @return Return value
|
||||||
|
*/
|
||||||
|
public abstract T work();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Runs worker method in event dispatcher thread.
|
||||||
|
*
|
||||||
|
* @see #work()
|
||||||
|
* @return Worker method return value
|
||||||
|
*/
|
||||||
|
public T invokeAndWait() {
|
||||||
|
if(java.awt.EventQueue.isDispatchThread()) {
|
||||||
|
return RunnableInEDT.this.work();
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
java.awt.EventQueue.invokeAndWait(new Runnable() {
|
||||||
|
public void run() {
|
||||||
|
val = RunnableInEDT.this.work();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (InvocationTargetException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
return val;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user