motesMenu.setMnemonic(KeyEvent.VK_M);
toolsMenu.setMnemonic(KeyEvent.VK_T);
helpMenu.setMnemonic(KeyEvent.VK_H);
/* File menu */
fileMenu.addMenuListener(new MenuListener() {
public void menuSelected(MenuEvent e) {
updateGUIComponentState();
updateOpenHistoryMenuItems();
}
public void menuDeselected(MenuEvent e) {
}
public void menuCanceled(MenuEvent e) {
}
});
fileMenu.add(new JMenuItem(newSimulationAction));
menuOpenSimulation = new JMenu("Open simulation");
menuOpenSimulation.setMnemonic(KeyEvent.VK_O);
fileMenu.add(menuOpenSimulation);
if (isVisualizedInApplet()) {
menuOpenSimulation.setEnabled(false);
menuOpenSimulation.setToolTipText("Not available in applet version");
}
fileMenu.add(new JMenuItem(closeSimulationAction));
hasFileHistoryChanged = true;
fileMenu.add(new JMenuItem(saveSimulationAction));
fileMenu.add(new JMenuItem(exportExecutableJARAction));
/* menu.addSeparator();*/
/* menu.add(new JMenuItem(closePluginsAction));*/
fileMenu.addSeparator();
fileMenu.add(new JMenuItem(exitCoojaAction));
/* Simulation menu */
simulationMenu.addMenuListener(new MenuListener() {
public void menuSelected(MenuEvent e) {
updateGUIComponentState();
}
public void menuDeselected(MenuEvent e) {
}
public void menuCanceled(MenuEvent e) {
}
});
simulationMenu.add(new JMenuItem(startStopSimulationAction));
JMenuItem reloadSimulationMenuItem = new JMenu("Reload simulation");
reloadSimulationMenuItem.add(new JMenuItem(reloadSimulationAction));
reloadSimulationMenuItem.add(new JMenuItem(reloadRandomSimulationAction));
simulationMenu.add(reloadSimulationMenuItem);
GUIAction guiAction = new StartPluginGUIAction("Control panel...");
menuItem = new JMenuItem(guiAction);
guiActions.add(guiAction);
menuItem.setMnemonic(KeyEvent.VK_C);
menuItem.putClientProperty("class", SimControl.class);
simulationMenu.add(menuItem);
guiAction = new StartPluginGUIAction("Simulation...");
menuItem = new JMenuItem(guiAction);
guiActions.add(guiAction);
menuItem.setMnemonic(KeyEvent.VK_I);
menuItem.putClientProperty("class", SimInformation.class);
simulationMenu.add(menuItem);
// Mote type menu
motesMenu.addMenuListener(new MenuListener() {
public void menuSelected(MenuEvent e) {
updateGUIComponentState();
}
public void menuDeselected(MenuEvent e) {
}
public void menuCanceled(MenuEvent e) {
}
});
// Mote type classes sub menu
menuMoteTypeClasses = new JMenu("Create new mote type");
menuMoteTypeClasses.setMnemonic(KeyEvent.VK_C);
menuMoteTypeClasses.addMenuListener(new MenuListener() {
public void menuSelected(MenuEvent e) {
// Clear menu
menuMoteTypeClasses.removeAll();
// Recreate menu items
JMenuItem menuItem;
for (Class<? extends MoteType> moteTypeClass : moteTypeClasses) {
/* Sort mote types according to abstraction level */
String abstractionLevelDescription = GUI.getAbstractionLevelDescriptionOf(moteTypeClass);
if(abstractionLevelDescription == null) {
abstractionLevelDescription = "[unknown cross-level]";
}
/* Check if abstraction description already exists */
JSeparator abstractionLevelSeparator = null;
for (Component component: menuMoteTypeClasses.getMenuComponents()) {
if (component == null || !(component instanceof JSeparator)) {
continue;
}
JSeparator existing = (JSeparator) component;
if (abstractionLevelDescription.equals(existing.getToolTipText())) {
abstractionLevelSeparator = existing;
break;
}
}
if (abstractionLevelSeparator == null) {
abstractionLevelSeparator = new JSeparator();
abstractionLevelSeparator.setToolTipText(abstractionLevelDescription);
menuMoteTypeClasses.add(abstractionLevelSeparator);
}
String description = GUI.getDescriptionOf(moteTypeClass);
menuItem = new JMenuItem(description + "...");
menuItem.setActionCommand("create mote type");
menuItem.putClientProperty("class", moteTypeClass);
/* menuItem.setToolTipText(abstractionLevelDescription);*/
menuItem.addActionListener(guiEventHandler);
if (isVisualizedInApplet() && moteTypeClass.equals(ContikiMoteType.class)) {
menuItem.setEnabled(false);
menuItem.setToolTipText("Not available in applet version");
}
/* Add new item directly after cross level separator */
for (int i=0; i < menuMoteTypeClasses.getMenuComponentCount(); i++) {
if (menuMoteTypeClasses.getMenuComponent(i) == abstractionLevelSeparator) {
menuMoteTypeClasses.add(menuItem, i+1);
break;
}
}
}
}
public void menuDeselected(MenuEvent e) {
}
public void menuCanceled(MenuEvent e) {
}
});
// Mote menu
motesMenu.addMenuListener(new MenuListener() {
public void menuSelected(MenuEvent e) {
updateGUIComponentState();
}
public void menuDeselected(MenuEvent e) {
}
public void menuCanceled(MenuEvent e) {
}
});
// Mote types sub menu
menuMoteTypes = new JMenu("Add motes");
menuMoteTypes.setMnemonic(KeyEvent.VK_A);
menuMoteTypes.addMenuListener(new MenuListener() {
public void menuSelected(MenuEvent e) {
// Clear menu
menuMoteTypes.removeAll();
if (mySimulation != null) {
// Recreate menu items
JMenuItem menuItem;
for (MoteType moteType : mySimulation.getMoteTypes()) {
menuItem = new JMenuItem(moteType.getDescription());
menuItem.setActionCommand("add motes");
menuItem.setToolTipText(getDescriptionOf(moteType.getClass()));
menuItem.putClientProperty("motetype", moteType);
menuItem.addActionListener(guiEventHandler);
menuMoteTypes.add(menuItem);
}
if(mySimulation.getMoteTypes().length > 0) {
menuMoteTypes.add(new JSeparator());
}
}
menuMoteTypes.add(menuMoteTypeClasses);
}
public void menuDeselected(MenuEvent e) {
}
public void menuCanceled(MenuEvent e) {
}
});
motesMenu.add(menuMoteTypes);
guiAction = new StartPluginGUIAction("Mote types...");
menuItem = new JMenuItem(guiAction);
guiActions.add(guiAction);
menuItem.putClientProperty("class", MoteTypeInformation.class);
motesMenu.add(menuItem);
motesMenu.add(new JMenuItem(removeAllMotesAction));
/* Tools menu */
toolsMenu.addMenuListener(new MenuListener() {
private ActionListener menuItemListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object pluginClass = ((JMenuItem)e.getSource()).getClientProperty("class");
Object mote = ((JMenuItem)e.getSource()).getClientProperty("mote");
tryStartPlugin((Class<? extends Plugin>) pluginClass, myGUI, getSimulation(), (Mote)mote);
}
};
private JMenuItem createMenuItem(Class<? extends Plugin> newPluginClass, int pluginType) {
String description = getDescriptionOf(newPluginClass);
JMenuItem menuItem = new JMenuItem(description + "...");
menuItem.putClientProperty("class", newPluginClass);
menuItem.addActionListener(menuItemListener);
String tooltip = "<html><pre>";
if (pluginType == PluginType.COOJA_PLUGIN || pluginType == PluginType.COOJA_STANDARD_PLUGIN) {
tooltip += "Cooja plugin: ";
} else if (pluginType == PluginType.SIM_PLUGIN || pluginType == PluginType.SIM_STANDARD_PLUGIN) {
tooltip += "Simulation plugin: ";
if (getSimulation() == null) {
menuItem.setEnabled(false);
}
} else if (pluginType == PluginType.MOTE_PLUGIN) {
tooltip += "Mote plugin: ";
}
tooltip += description + " (" + newPluginClass.getName() + ")";
/* Check if simulation plugin depends on any particular radio medium */
if (pluginType == PluginType.SIM_PLUGIN || pluginType == PluginType.SIM_STANDARD_PLUGIN) {
if (newPluginClass.getAnnotation(SupportedArguments.class) != null) {
boolean active = false;
Class<? extends RadioMedium>[] radioMediums = newPluginClass.getAnnotation(SupportedArguments.class).radioMediums();
for (Class<? extends Object> o: radioMediums) {
if (o.isAssignableFrom(getSimulation().getRadioMedium().getClass())) {
active = true;
break;
}
}
if (!active) {
menuItem.setVisible(false);
}
}
}
/* Check if plugin was imported by a extension directory */
File project =
getProjectConfig().getUserProjectDefining(GUI.class, "PLUGINS", newPluginClass.getName());
if (project != null) {
tooltip += "\nLoaded by extension: " + project.getPath();
}
tooltip += "</html>";
/*menuItem.setToolTipText(tooltip);*/
return menuItem;
}
public void menuSelected(MenuEvent e) {
/* Populate tools menu */
toolsMenu.removeAll();
/* Cooja plugins */
boolean hasCoojaPlugins = false;
for (Class<? extends Plugin> pluginClass: pluginClasses) {
int pluginType = pluginClass.getAnnotation(PluginType.class).value();
if (pluginType != PluginType.COOJA_PLUGIN && pluginType != PluginType.COOJA_STANDARD_PLUGIN) {
continue;
}
toolsMenu.add(createMenuItem(pluginClass, pluginType));
hasCoojaPlugins = true;
}
/* Simulation plugins */
boolean hasSimPlugins = false;
for (Class<? extends Plugin> pluginClass: pluginClasses) {
if (pluginClass.equals(SimControl.class)) {
continue; /* ignore */
}
if (pluginClass.equals(SimInformation.class)) {
continue; /* ignore */
}
if (pluginClass.equals(MoteTypeInformation.class)) {
continue; /* ignore */
}
int pluginType = pluginClass.getAnnotation(PluginType.class).value();
if (pluginType != PluginType.SIM_PLUGIN && pluginType != PluginType.SIM_STANDARD_PLUGIN) {
continue;
}
if (hasCoojaPlugins) {
hasCoojaPlugins = false;
toolsMenu.addSeparator();
}
toolsMenu.add(createMenuItem(pluginClass, pluginType));
hasSimPlugins = true;
}
for (Class<? extends Plugin> pluginClass: pluginClasses) {
int pluginType = pluginClass.getAnnotation(PluginType.class).value();
if (pluginType != PluginType.MOTE_PLUGIN) {
continue;
}
if (hasSimPlugins) {
hasSimPlugins = false;
toolsMenu.addSeparator();
}
toolsMenu.add(createMotePluginsSubmenu(pluginClass));
}
}
public void menuDeselected(MenuEvent e) {
}
public void menuCanceled(MenuEvent e) {
}
});
// Settings menu
settingsMenu.addMenuListener(new MenuListener() {
public void menuSelected(MenuEvent e) {
updateGUIComponentState();
}
public void menuDeselected(MenuEvent e) {
}