simplified skin menu implementation, removed unused skin button code

This commit is contained in:
Fredrik Osterlind 2012-06-05 10:03:47 +02:00
parent c276f90247
commit 9c05689be0

View file

@ -37,7 +37,6 @@ import java.awt.Cursor;
import java.awt.Dimension; import java.awt.Dimension;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Graphics2D; import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.Point; import java.awt.Point;
import java.awt.Polygon; import java.awt.Polygon;
import java.awt.RenderingHints; import java.awt.RenderingHints;
@ -68,7 +67,6 @@ import java.util.Observable;
import java.util.Observer; import java.util.Observer;
import javax.swing.AbstractAction; import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem; import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu; import javax.swing.JMenu;
import javax.swing.JMenuBar; import javax.swing.JMenuBar;
@ -80,6 +78,8 @@ import javax.swing.JSeparator;
import javax.swing.MenuElement; import javax.swing.MenuElement;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.Timer; import javax.swing.Timer;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import javax.swing.plaf.basic.BasicInternalFrameUI; import javax.swing.plaf.basic.BasicInternalFrameUI;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@ -87,16 +87,16 @@ import org.jdom.Element;
import se.sics.cooja.ClassDescription; import se.sics.cooja.ClassDescription;
import se.sics.cooja.GUI; import se.sics.cooja.GUI;
import se.sics.cooja.GUI.MoteRelation;
import se.sics.cooja.HasQuickHelp; import se.sics.cooja.HasQuickHelp;
import se.sics.cooja.Mote; import se.sics.cooja.Mote;
import se.sics.cooja.MoteInterface; import se.sics.cooja.MoteInterface;
import se.sics.cooja.PluginType; import se.sics.cooja.PluginType;
import se.sics.cooja.RadioMedium; import se.sics.cooja.RadioMedium;
import se.sics.cooja.SimEventCentral.MoteCountListener;
import se.sics.cooja.Simulation; import se.sics.cooja.Simulation;
import se.sics.cooja.SupportedArguments; import se.sics.cooja.SupportedArguments;
import se.sics.cooja.VisPlugin; import se.sics.cooja.VisPlugin;
import se.sics.cooja.GUI.MoteRelation;
import se.sics.cooja.SimEventCentral.MoteCountListener;
import se.sics.cooja.interfaces.LED; import se.sics.cooja.interfaces.LED;
import se.sics.cooja.interfaces.Position; import se.sics.cooja.interfaces.Position;
import se.sics.cooja.interfaces.SerialPort; import se.sics.cooja.interfaces.SerialPort;
@ -160,7 +160,6 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
private Cursor moveCursor = new Cursor(Cursor.MOVE_CURSOR); private Cursor moveCursor = new Cursor(Cursor.MOVE_CURSOR);
/* Visualizers */ /* Visualizers */
private final JButton skinButton = new JButton("Select visualizer skins");
private static ArrayList<Class<? extends VisualizerSkin>> visualizerSkins = private static ArrayList<Class<? extends VisualizerSkin>> visualizerSkins =
new ArrayList<Class<? extends VisualizerSkin>>(); new ArrayList<Class<? extends VisualizerSkin>>();
static { static {
@ -222,10 +221,20 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
/* Menus */ /* Menus */
JMenuBar menuBar = new JMenuBar(); JMenuBar menuBar = new JMenuBar();
viewMenu = new JMenu("View"); viewMenu = new JMenu("View");
viewMenu.addMenuListener(new MenuListener() {
public void menuSelected(MenuEvent e) {
viewMenu.removeAll();
populateSkinMenu(viewMenu);
}
public void menuDeselected(MenuEvent e) {
}
public void menuCanceled(MenuEvent e) {
}
});
JMenu zoomMenu = new JMenu("Zoom"); JMenu zoomMenu = new JMenu("Zoom");
menuBar.add(viewMenu); menuBar.add(viewMenu);
menuBar.add(zoomMenu); menuBar.add(zoomMenu);
@ -280,20 +289,6 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
canvas.setBackground(Color.WHITE); canvas.setBackground(Color.WHITE);
viewportTransform = new AffineTransform(); viewportTransform = new AffineTransform();
/* Skin selector */
skinButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Point mouse = MouseInfo.getPointerInfo().getLocation();
JPopupMenu skinPopupMenu = new JPopupMenu();
populateSkinMenu(skinPopupMenu);
skinPopupMenu.setLocation(mouse);
skinPopupMenu.setInvoker(skinButton);
skinPopupMenu.setVisible(true);
}
});
/*this.add(BorderLayout.NORTH, skinButton);*/
this.add(BorderLayout.CENTER, canvas); this.add(BorderLayout.CENTER, canvas);
/* Observe simulation and mote positions */ /* Observe simulation and mote positions */
@ -524,7 +519,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
/* XXX HACK: here we set the position and size of the window when it appears on a blank simulation screen. */ /* XXX HACK: here we set the position and size of the window when it appears on a blank simulation screen. */
this.setLocation(1, 1); this.setLocation(1, 1);
this.setSize(400, 400); this.setSize(400, 400);
} }
private void generateAndActivateSkin(Class<? extends VisualizerSkin> skinClass) { private void generateAndActivateSkin(Class<? extends VisualizerSkin> skinClass) {
@ -550,9 +545,6 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
} catch (IllegalAccessException e1) { } catch (IllegalAccessException e1) {
e1.printStackTrace(); e1.printStackTrace();
} }
skinButton.setText("Select visualizer " +
"(" + currentSkins.size() + "/" + visualizerSkins.size() + ")");
repaint(); repaint();
} }
@ -572,9 +564,6 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
simulation.getGUI().tryLoadClass(this, VisualizerSkin.class, skin); simulation.getGUI().tryLoadClass(this, VisualizerSkin.class, skin);
generateAndActivateSkin(skinClass); generateAndActivateSkin(skinClass);
} }
populateSkinMenu(viewMenu);
} }
public VisualizerSkin[] getCurrentSkins() { public VisualizerSkin[] getCurrentSkins() {
@ -700,11 +689,15 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
menu.setVisible(true); menu.setVisible(true);
} }
private void populateSkinMenu(MenuElement skinMenu) { private void populateSkinMenu(MenuElement menu) {
JCheckBoxMenuItem item;
for (Class<? extends VisualizerSkin> skinClass: visualizerSkins) { for (Class<? extends VisualizerSkin> skinClass: visualizerSkins) {
/* Should skin be enabled in this simulation? */
if (!isSkinCompatible(skinClass)) {
continue;
}
String description = GUI.getDescriptionOf(skinClass); String description = GUI.getDescriptionOf(skinClass);
item = new JCheckBoxMenuItem(description, false); JCheckBoxMenuItem item = new JCheckBoxMenuItem(description, false);
item.putClientProperty("skinclass", skinClass); item.putClientProperty("skinclass", skinClass);
/* Select skin if active */ /* Select skin if active */
@ -749,23 +742,15 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
skinToDeactivate.setInactive(); skinToDeactivate.setInactive();
repaint(); repaint();
currentSkins.remove(skinToDeactivate); currentSkins.remove(skinToDeactivate);
skinButton.setText("Select visualizers " +
"(" + currentSkins.size() + "/" + visualizerSkins.size() + ")");
} }
} }
}); });
if (menu instanceof JMenu) {
/* Should skin be enabled in this simulation? */ ((JMenu)menu).add(item);
if (!isSkinCompatible(skinClass)) {
continue;
} }
if (menu instanceof JPopupMenu) {
if (skinMenu instanceof JMenu) { ((JPopupMenu)menu).add(item);
((JMenu)skinMenu).add(item);
}
if (skinMenu instanceof JPopupMenu) {
((JPopupMenu)skinMenu).add(item);
} }
} }
} }
@ -845,7 +830,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
newZoom, newZoom,
newZoom newZoom
); );
/*Position moved = transformPixelToPosition(zoomingPixel); /*Position moved = transformPixelToPosition(zoomingPixel);
viewportTransform.translate( viewportTransform.translate(
moved.getXCoordinate() - zoomingPosition.getXCoordinate(), moved.getXCoordinate() - zoomingPosition.getXCoordinate(),
@ -919,7 +904,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
} }
/* Restore cursor */ /* Restore cursor */
canvas.setCursor(Cursor.getDefaultCursor()); canvas.setCursor(Cursor.getDefaultCursor());
/* Move mote */ /* Move mote */
if (moveStartTime < 0 || System.currentTimeMillis() - moveStartTime > 300) { if (moveStartTime < 0 || System.currentTimeMillis() - moveStartTime > 300) {
@ -1296,7 +1281,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
} }
public boolean setConfigXML(Collection<Element> configXML, boolean visAvailable) { public boolean setConfigXML(Collection<Element> configXML, boolean visAvailable) {
loadedConfig = true; loadedConfig = true;
for (Element element : configXML) { for (Element element : configXML) {
if (element.getName().equals("skin")) { if (element.getName().equals("skin")) {
@ -1337,21 +1322,8 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
} else if (element.getName().equals("hidden")) { } else if (element.getName().equals("hidden")) {
BasicInternalFrameUI ui = (BasicInternalFrameUI) getUI(); BasicInternalFrameUI ui = (BasicInternalFrameUI) getUI();
ui.getNorthPane().setPreferredSize(new Dimension(0,0)); ui.getNorthPane().setPreferredSize(new Dimension(0,0));
skinButton.setVisible(false);
} }
} }
/*
* This is a hack: since the viewers are activated through an invokeLater
* mechanism (see above), we'll have to invoke the
* populateSkinMenu(viewMenu) later too. Lets just hope the invokation of
* populateSkinMenu() is done later than the activation of the viewers...
*/
SwingUtilities.invokeLater(new Runnable() {
public void run() {
populateSkinMenu(viewMenu);
}
});
return true; return true;
} }
@ -1503,11 +1475,9 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
ui.getNorthPane().getPreferredSize().height == 0) { ui.getNorthPane().getPreferredSize().height == 0) {
/* Restore window decorations */ /* Restore window decorations */
ui.getNorthPane().setPreferredSize(null); ui.getNorthPane().setPreferredSize(null);
visualizer.skinButton.setVisible(true);
} else { } else {
/* Hide window decorations */ /* Hide window decorations */
ui.getNorthPane().setPreferredSize(new Dimension(0,0)); ui.getNorthPane().setPreferredSize(new Dimension(0,0));
visualizer.skinButton.setVisible(false);
} }
visualizer.revalidate(); visualizer.revalidate();
SwingUtilities.invokeLater(new Runnable() { SwingUtilities.invokeLater(new Runnable() {