isVisualized handling both visualized in frame and in applet.

This commit is contained in:
fros4943 2008-02-12 15:20:56 +00:00
parent fe11ea6c8f
commit 3b2a19c1ae

View file

@ -24,12 +24,13 @@
* (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.68 2008/02/11 14:04:51 fros4943 Exp $ * $Id: GUI.java,v 1.69 2008/02/12 15:20:56 fros4943 Exp $
*/ */
package se.sics.cooja; package se.sics.cooja;
import java.awt.*; import java.awt.*;
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.*;
@ -133,10 +134,9 @@ public class GUI {
} }
}; };
/** private static JFrame frame = null;
* Main frame for current GUI. Null when COOJA is run without visualizer!
*/ private static JApplet applet = null;
public static JFrame frame;
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
@ -276,7 +276,7 @@ public class GUI {
logger.fatal("Error when loading project directories: " + e.getMessage()); logger.fatal("Error when loading project directories: " + e.getMessage());
e.printStackTrace(); e.printStackTrace();
if (myDesktopPane != null) { if (myDesktopPane != null) {
JOptionPane.showMessageDialog(frame, JOptionPane.showMessageDialog(GUI.getTopParentContainer(),
"Loading project directories failed.\nStack trace printed to console.", "Loading project directories failed.\nStack trace printed to console.",
"Error", JOptionPane.ERROR_MESSAGE); "Error", JOptionPane.ERROR_MESSAGE);
} }
@ -317,25 +317,62 @@ public class GUI {
/** /**
* @return True if simulator is visualized * @return True if simulator is visualized
*/ */
public boolean isVisualized() { public static boolean isVisualized() {
return isVisualizedInFrame() || isVisualizedInApplet();
}
public static Container getTopParentContainer() {
if (isVisualizedInFrame()) {
return frame;
}
if (isVisualizedInApplet()) {
/* Find parent frame for applet */
Container container = applet;
while((container = container.getParent()) != null){
if (container instanceof Frame) {
return container;
}
if (container instanceof Dialog) {
return container;
}
if (container instanceof Window) {
return container;
}
}
logger.fatal("Returning null top owner container");
}
return null;
}
public static boolean isVisualizedInFrame() {
return frame != null; return frame != null;
} }
public static boolean isVisualizedInApplet() {
return applet != null;
}
/** /**
* EXPERIMENTAL!
* Tries to create/remove simulator visualizer. * Tries to create/remove simulator visualizer.
* *
* @param visualized Visualized * @param visualized Visualized
*/ */
public void setVisualized(boolean visualized) { public void setVisualizedInFrame(boolean visualized) {
if (!isVisualized() && visualized) { if (visualized) {
if (!isVisualizedInFrame()) {
configureFrame(myGUI, false); configureFrame(myGUI, false);
}
} else { } else {
if (frame != null) {
frame.setVisible(false); frame.setVisible(false);
frame.dispose(); frame.dispose();
frame = null; frame = null;
} }
} }
}
private Vector<File> getFileHistory() { private Vector<File> getFileHistory() {
Vector<File> history = new Vector<File>(); Vector<File> history = new Vector<File>();
@ -694,7 +731,6 @@ public class GUI {
frame.addWindowListener(gui.guiEventHandler); frame.addWindowListener(gui.guiEventHandler);
// Restore last frame size and position // Restore last frame size and position
if (frame != null) {
int framePosX = Integer.parseInt(getExternalToolsSetting("FRAME_POS_X", "-1")); int framePosX = Integer.parseInt(getExternalToolsSetting("FRAME_POS_X", "-1"));
int framePosY = Integer.parseInt(getExternalToolsSetting("FRAME_POS_Y", "-1")); int framePosY = Integer.parseInt(getExternalToolsSetting("FRAME_POS_Y", "-1"));
int frameWidth = Integer.parseInt(getExternalToolsSetting("FRAME_WIDTH", "-1")); int frameWidth = Integer.parseInt(getExternalToolsSetting("FRAME_WIDTH", "-1"));
@ -712,7 +748,6 @@ public class GUI {
frame.setExtendedState(JFrame.MAXIMIZED_BOTH); frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
} }
} }
}
// Display the window. // Display the window.
frame.setVisible(true); frame.setVisible(true);
@ -726,6 +761,33 @@ public class GUI {
} }
} }
private static void configureApplet(final GUI gui, boolean createSimDialog) {
applet = CoojaApplet.applet;
// Make sure we have nice window decorations.
try {
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception e) {
}
// Add menu bar
JMenuBar menuBar = gui.createMenuBar();
applet.setJMenuBar(menuBar);
JComponent newContentPane = gui.getDesktopPane();
newContentPane.setOpaque(true);
applet.setContentPane(newContentPane);
applet.setSize(700, 700);
if (createSimDialog) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
gui.doCreateSimulation(true);
}
});
}
}
/** /**
* @return Current desktop pane (simulator visualizer) * @return Current desktop pane (simulator visualizer)
*/ */
@ -1884,8 +1946,7 @@ public class GUI {
boolean moteTypeOK = false; boolean moteTypeOK = false;
try { try {
newMoteType = moteTypeClass.newInstance(); newMoteType = moteTypeClass.newInstance();
moteTypeOK = newMoteType.configureAndInit(frame, mySimulation, moteTypeOK = newMoteType.configureAndInit(GUI.getTopParentContainer(), mySimulation, isVisualized());
isVisualized());
} catch (InstantiationException e) { } catch (InstantiationException e) {
logger.fatal("Exception when creating mote type: " + e); logger.fatal("Exception when creating mote type: " + e);
return; return;
@ -1916,7 +1977,7 @@ public class GUI {
String s1 = "Remove"; String s1 = "Remove";
String s2 = "Cancel"; String s2 = "Cancel";
Object[] options = { s1, s2 }; Object[] options = { s1, s2 };
int n = JOptionPane.showOptionDialog(frame, int n = JOptionPane.showOptionDialog(GUI.getTopParentContainer(),
"You have an active simulation.\nDo you want to remove it?", "You have an active simulation.\nDo you want to remove it?",
"Remove current simulation?", JOptionPane.YES_NO_OPTION, "Remove current simulation?", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, s1); JOptionPane.QUESTION_MESSAGE, null, options, s1);
@ -1948,9 +2009,11 @@ public class GUI {
} }
// Reset frame title // Reset frame title
if (isVisualizedInFrame()) {
frame.setTitle("COOJA Simulator"); frame.setTitle("COOJA Simulator");
} }
} }
}
/** /**
* Load a simulation configuration file from disk * Load a simulation configuration file from disk
@ -1962,9 +2025,7 @@ public class GUI {
public void doLoadConfig(boolean askForConfirmation, final boolean quick, File configFile) { public void doLoadConfig(boolean askForConfirmation, final boolean quick, File configFile) {
if (CoreComm.hasLibraryBeenLoaded()) { if (CoreComm.hasLibraryBeenLoaded()) {
JOptionPane JOptionPane.showMessageDialog(GUI.getTopParentContainer(),
.showMessageDialog(
frame,
"Shared libraries has already been loaded.\nYou need to restart the simulator!", "Shared libraries has already been loaded.\nYou need to restart the simulator!",
"Can't load simulation", JOptionPane.ERROR_MESSAGE); "Can't load simulation", JOptionPane.ERROR_MESSAGE);
return; return;
@ -1974,7 +2035,7 @@ public class GUI {
String s1 = "Remove"; String s1 = "Remove";
String s2 = "Cancel"; String s2 = "Cancel";
Object[] options = { s1, s2 }; Object[] options = { s1, s2 };
int n = JOptionPane.showOptionDialog(frame, int n = JOptionPane.showOptionDialog(GUI.getTopParentContainer(),
"You have an active simulation.\nDo you want to remove it?", "You have an active simulation.\nDo you want to remove it?",
"Remove current simulation?", JOptionPane.YES_NO_OPTION, "Remove current simulation?", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, s1); JOptionPane.QUESTION_MESSAGE, null, options, s1);
@ -2003,7 +2064,7 @@ public class GUI {
fc.setSelectedFile(suggestedFile); fc.setSelectedFile(suggestedFile);
} }
int returnVal = fc.showOpenDialog(frame); int returnVal = fc.showOpenDialog(GUI.getTopParentContainer());
if (returnVal == JFileChooser.APPROVE_OPTION) { if (returnVal == JFileChooser.APPROVE_OPTION) {
configFile = fc.getSelectedFile(); configFile = fc.getSelectedFile();
@ -2025,7 +2086,18 @@ public class GUI {
} }
// Load simulation in separate thread, while showing progress monitor // Load simulation in separate thread, while showing progress monitor
final JDialog progressDialog = new JDialog(frame, "Loading", true); 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);
}
final File fileToLoad = configFile; final File fileToLoad = configFile;
final Thread loadThread = new Thread(new Runnable() { final Thread loadThread = new Thread(new Runnable() {
public void run() { public void run() {
@ -2037,14 +2109,14 @@ public class GUI {
progressDialog.dispose(); progressDialog.dispose();
} }
} catch (UnsatisfiedLinkError e) { } catch (UnsatisfiedLinkError e) {
showErrorDialog(frame, "Simulation load error", e, false); showErrorDialog(GUI.getTopParentContainer(), "Simulation load error", e, false);
if (progressDialog != null && progressDialog.isDisplayable()) { if (progressDialog != null && progressDialog.isDisplayable()) {
progressDialog.dispose(); progressDialog.dispose();
} }
newSim = null; newSim = null;
} catch (SimulationCreationException e) { } catch (SimulationCreationException e) {
showErrorDialog(frame, "Simulation load error", e, false); showErrorDialog(GUI.getTopParentContainer(), "Simulation load error", e, false);
if (progressDialog != null && progressDialog.isDisplayable()) { if (progressDialog != null && progressDialog.isDisplayable()) {
progressDialog.dispose(); progressDialog.dispose();
@ -2087,7 +2159,7 @@ public class GUI {
progressDialog.pack(); progressDialog.pack();
progressDialog.getRootPane().setDefaultButton(button); progressDialog.getRootPane().setDefaultButton(button);
progressDialog.setLocationRelativeTo(frame); progressDialog.setLocationRelativeTo(GUI.getTopParentContainer());
progressDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); progressDialog.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
loadThread.start(); loadThread.start();
if (quick) { if (quick) {
@ -2275,7 +2347,7 @@ public class GUI {
Object[] options = { s1, s2 }; Object[] options = { s1, s2 };
int n = JOptionPane int n = JOptionPane
.showOptionDialog( .showOptionDialog(
frame, GUI.getTopParentContainer(),
"A file with the same name already exists.\nDo you want to remove it?", "A file with the same name already exists.\nDo you want to remove it?",
"Overwrite existing file?", JOptionPane.YES_NO_OPTION, "Overwrite existing file?", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, s1); JOptionPane.QUESTION_MESSAGE, null, options, s1);
@ -2305,7 +2377,7 @@ public class GUI {
if (mySimulation != null) { if (mySimulation != null) {
mySimulation.stopSimulation(); mySimulation.stopSimulation();
Vector<Mote> newMotes = AddMoteDialog.showDialog(frame, mySimulation, Vector<Mote> newMotes = AddMoteDialog.showDialog(getTopParentContainer(), mySimulation,
moteType); moteType);
if (newMotes != null) { if (newMotes != null) {
for (Mote newMote : newMotes) { for (Mote newMote : newMotes) {
@ -2329,7 +2401,7 @@ public class GUI {
String s1 = "Remove"; String s1 = "Remove";
String s2 = "Cancel"; String s2 = "Cancel";
Object[] options = { s1, s2 }; Object[] options = { s1, s2 };
int n = JOptionPane.showOptionDialog(frame, int n = JOptionPane.showOptionDialog(GUI.getTopParentContainer(),
"You have an active simulation.\nDo you want to remove it?", "You have an active simulation.\nDo you want to remove it?",
"Remove current simulation?", JOptionPane.YES_NO_OPTION, "Remove current simulation?", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, s1); JOptionPane.QUESTION_MESSAGE, null, options, s1);
@ -2341,7 +2413,7 @@ public class GUI {
// Create new simulation // Create new simulation
doRemoveSimulation(false); doRemoveSimulation(false);
Simulation newSim = new Simulation(this); Simulation newSim = new Simulation(this);
boolean createdOK = CreateSimDialog.showDialog(frame, newSim); boolean createdOK = CreateSimDialog.showDialog(GUI.getTopParentContainer(), newSim);
if (createdOK) { if (createdOK) {
myGUI.setSimulation(newSim); myGUI.setSimulation(newSim);
} }
@ -2358,7 +2430,8 @@ public class GUI {
String s1 = "Quit"; String s1 = "Quit";
String s2 = "Cancel"; String s2 = "Cancel";
Object[] options = { s1, s2 }; Object[] options = { s1, s2 };
int n = JOptionPane.showOptionDialog(frame, "Sure you want to quit?", int n = JOptionPane.showOptionDialog(GUI.getTopParentContainer(),
"Sure you want to quit?",
"Close COOJA Simulator", JOptionPane.YES_NO_OPTION, "Close COOJA Simulator", JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE, null, options, s1); JOptionPane.QUESTION_MESSAGE, null, options, s1);
if (n != JOptionPane.YES_OPTION) { if (n != JOptionPane.YES_OPTION) {
@ -2373,13 +2446,13 @@ public class GUI {
} }
// Restore last frame size and position // Restore last frame size and position
if (frame != null) { if (isVisualizedInFrame()) {
setExternalToolsSetting("FRAME_POS_X", "" + frame.getLocationOnScreen().x); setExternalToolsSetting("FRAME_POS_X", "" + frame.getLocationOnScreen().x);
setExternalToolsSetting("FRAME_POS_Y", "" + frame.getLocationOnScreen().y); setExternalToolsSetting("FRAME_POS_Y", "" + frame.getLocationOnScreen().y);
setExternalToolsSetting("FRAME_WIDTH", "" + frame.getWidth()); setExternalToolsSetting("FRAME_WIDTH", "" + frame.getWidth());
setExternalToolsSetting("FRAME_HEIGHT", "" + frame.getHeight()); setExternalToolsSetting("FRAME_HEIGHT", "" + frame.getHeight());
saveExternalToolsUserSettings();
} }
saveExternalToolsUserSettings();
System.exit(0); System.exit(0);
} }
@ -2593,7 +2666,7 @@ public class GUI {
myGUI.doAddMotes((MoteType) ((JMenuItem) e.getSource()) myGUI.doAddMotes((MoteType) ((JMenuItem) e.getSource())
.getClientProperty("motetype")); .getClientProperty("motetype"));
} else if (e.getActionCommand().equals("edit paths")) { } else if (e.getActionCommand().equals("edit paths")) {
ExternalToolsDialog.showDialog(frame); ExternalToolsDialog.showDialog(GUI.getTopParentContainer());
} else if (e.getActionCommand().equals("close plugins")) { } else if (e.getActionCommand().equals("close plugins")) {
Object[] plugins = startedPlugins.toArray(); Object[] plugins = startedPlugins.toArray();
for (Object plugin : plugins) { for (Object plugin : plugins) {
@ -2610,8 +2683,8 @@ public class GUI {
} }
} }
} else if (e.getActionCommand().equals("manage projects")) { } else if (e.getActionCommand().equals("manage projects")) {
Vector<File> newProjects = ProjectDirectoriesDialog.showDialog(frame, Vector<File> newProjects = ProjectDirectoriesDialog.showDialog(
currentProjectDirs, null); GUI.getTopParentContainer(), currentProjectDirs, null);
if (newProjects != null) { if (newProjects != null) {
currentProjectDirs = newProjects; currentProjectDirs = newProjects;
try { try {
@ -2620,7 +2693,7 @@ public class GUI {
logger.fatal("Error when loading projects: " + e2.getMessage()); logger.fatal("Error when loading projects: " + e2.getMessage());
e2.printStackTrace(); e2.printStackTrace();
if (myGUI.isVisualized()) { if (myGUI.isVisualized()) {
JOptionPane.showMessageDialog(frame, JOptionPane.showMessageDialog(GUI.getTopParentContainer(),
"Error when loading projects.\nStack trace printed to console.", "Error when loading projects.\nStack trace printed to console.",
"Error", JOptionPane.ERROR_MESSAGE); "Error", JOptionPane.ERROR_MESSAGE);
} }
@ -2674,6 +2747,7 @@ public class GUI {
return projectDirClassLoader.loadClass(className).asSubclass( return projectDirClassLoader.loadClass(className).asSubclass(
classType); classType);
} }
} catch (NoClassDefFoundError e) {
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException e) {
} catch (UnsupportedClassVersionError e) { } catch (UnsupportedClassVersionError e) {
} }
@ -2751,6 +2825,7 @@ public class GUI {
} }
URL[] urlsArray = urls.toArray(new URL[urls.size()]); URL[] urlsArray = urls.toArray(new URL[urls.size()]);
/* TODO Load from webserver if applet */
return new URLClassLoader(urlsArray, parent); return new URLClassLoader(urlsArray, parent);
} }
@ -2934,9 +3009,21 @@ public class GUI {
} }
}); });
} else if (args.length > 0 && args[0].startsWith("-applet")) {
// Applet start-up
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
JDesktopPane desktop = new JDesktopPane();
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
GUI gui = new GUI(desktop);
configureApplet(gui, false);
}
});
} else { } else {
// Regular start-up // Frame start-up
javax.swing.SwingUtilities.invokeLater(new Runnable() { javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() { public void run() {
JDesktopPane desktop = new JDesktopPane(); JDesktopPane desktop = new JDesktopPane();