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.Graphics;
import java.awt.Graphics2D;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.RenderingHints;
@ -68,7 +67,6 @@ import java.util.Observable;
import java.util.Observer;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
@ -80,6 +78,8 @@ import javax.swing.JSeparator;
import javax.swing.MenuElement;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;
import javax.swing.plaf.basic.BasicInternalFrameUI;
import org.apache.log4j.Logger;
@ -87,16 +87,16 @@ import org.jdom.Element;
import se.sics.cooja.ClassDescription;
import se.sics.cooja.GUI;
import se.sics.cooja.GUI.MoteRelation;
import se.sics.cooja.HasQuickHelp;
import se.sics.cooja.Mote;
import se.sics.cooja.MoteInterface;
import se.sics.cooja.PluginType;
import se.sics.cooja.RadioMedium;
import se.sics.cooja.SimEventCentral.MoteCountListener;
import se.sics.cooja.Simulation;
import se.sics.cooja.SupportedArguments;
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.Position;
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);
/* Visualizers */
private final JButton skinButton = new JButton("Select visualizer skins");
private static ArrayList<Class<? extends VisualizerSkin>> visualizerSkins =
new ArrayList<Class<? extends VisualizerSkin>>();
static {
@ -224,6 +223,16 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
JMenuBar menuBar = new JMenuBar();
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");
menuBar.add(viewMenu);
@ -280,20 +289,6 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
canvas.setBackground(Color.WHITE);
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);
/* Observe simulation and mote positions */
@ -550,9 +545,6 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
} catch (IllegalAccessException e1) {
e1.printStackTrace();
}
skinButton.setText("Select visualizer " +
"(" + currentSkins.size() + "/" + visualizerSkins.size() + ")");
repaint();
}
@ -572,9 +564,6 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
simulation.getGUI().tryLoadClass(this, VisualizerSkin.class, skin);
generateAndActivateSkin(skinClass);
}
populateSkinMenu(viewMenu);
}
public VisualizerSkin[] getCurrentSkins() {
@ -700,11 +689,15 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
menu.setVisible(true);
}
private void populateSkinMenu(MenuElement skinMenu) {
JCheckBoxMenuItem item;
private void populateSkinMenu(MenuElement menu) {
for (Class<? extends VisualizerSkin> skinClass: visualizerSkins) {
/* Should skin be enabled in this simulation? */
if (!isSkinCompatible(skinClass)) {
continue;
}
String description = GUI.getDescriptionOf(skinClass);
item = new JCheckBoxMenuItem(description, false);
JCheckBoxMenuItem item = new JCheckBoxMenuItem(description, false);
item.putClientProperty("skinclass", skinClass);
/* Select skin if active */
@ -749,23 +742,15 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
skinToDeactivate.setInactive();
repaint();
currentSkins.remove(skinToDeactivate);
skinButton.setText("Select visualizers " +
"(" + currentSkins.size() + "/" + visualizerSkins.size() + ")");
}
}
});
/* Should skin be enabled in this simulation? */
if (!isSkinCompatible(skinClass)) {
continue;
if (menu instanceof JMenu) {
((JMenu)menu).add(item);
}
if (skinMenu instanceof JMenu) {
((JMenu)skinMenu).add(item);
}
if (skinMenu instanceof JPopupMenu) {
((JPopupMenu)skinMenu).add(item);
if (menu instanceof JPopupMenu) {
((JPopupMenu)menu).add(item);
}
}
}
@ -1296,7 +1281,7 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
}
public boolean setConfigXML(Collection<Element> configXML, boolean visAvailable) {
loadedConfig = true;
loadedConfig = true;
for (Element element : configXML) {
if (element.getName().equals("skin")) {
@ -1337,21 +1322,8 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
} else if (element.getName().equals("hidden")) {
BasicInternalFrameUI ui = (BasicInternalFrameUI) getUI();
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;
}
@ -1503,11 +1475,9 @@ public class Visualizer extends VisPlugin implements HasQuickHelp {
ui.getNorthPane().getPreferredSize().height == 0) {
/* Restore window decorations */
ui.getNorthPane().setPreferredSize(null);
visualizer.skinButton.setVisible(true);
} else {
/* Hide window decorations */
ui.getNorthPane().setPreferredSize(new Dimension(0,0));
visualizer.skinButton.setVisible(false);
}
visualizer.revalidate();
SwingUtilities.invokeLater(new Runnable() {