From 9e67306eee8b071a5b574a7a0e988eba45df1e97 Mon Sep 17 00:00:00 2001 From: Fredrik Osterlind Date: Fri, 25 May 2012 15:40:26 +0200 Subject: [PATCH] cleaned up the mote type visualizer code --- .../se/sics/cooja/mspmote/MspMoteType.java | 13 ++- tools/cooja/java/se/sics/cooja/MoteType.java | 6 +- .../motes/AbstractApplicationMoteType.java | 54 +++-------- .../cooja/plugins/MoteTypeInformation.java | 95 +++++++++---------- 4 files changed, 70 insertions(+), 98 deletions(-) diff --git a/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/MspMoteType.java b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/MspMoteType.java index 1628e165e..a58972c61 100644 --- a/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/MspMoteType.java +++ b/tools/cooja/apps/mspsim/src/se/sics/cooja/mspmote/MspMoteType.java @@ -29,17 +29,19 @@ package se.sics.cooja.mspmote; -import java.awt.BorderLayout; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.Hashtable; + import javax.swing.Icon; +import javax.swing.JComponent; import javax.swing.JLabel; -import javax.swing.JPanel; + import org.apache.log4j.Logger; import org.jdom.Element; + import se.sics.cooja.ClassDescription; import se.sics.cooja.GUI; import se.sics.cooja.Mote; @@ -131,7 +133,7 @@ public abstract class MspMoteType implements MoteType { protected abstract MspMote createMote(Simulation simulation); @Override - public JPanel getTypeVisualizer() { + public JComponent getTypeVisualizer() { StringBuilder sb = new StringBuilder(); // Identifier sb.append("
Identifier") @@ -171,10 +173,7 @@ public abstract class MspMoteType implements MoteType { label.setIcon(moteTypeIcon); } } - - JPanel panel = new JPanel(new BorderLayout()); - panel.add(BorderLayout.CENTER, label); - return panel; + return label; } public abstract Icon getMoteTypeIcon(); diff --git a/tools/cooja/java/se/sics/cooja/MoteType.java b/tools/cooja/java/se/sics/cooja/MoteType.java index 5d65b88dd..c07772244 100644 --- a/tools/cooja/java/se/sics/cooja/MoteType.java +++ b/tools/cooja/java/se/sics/cooja/MoteType.java @@ -30,7 +30,9 @@ package se.sics.cooja; import java.awt.Container; import java.io.File; import java.util.Collection; -import javax.swing.JPanel; + +import javax.swing.JComponent; + import org.jdom.Element; import se.sics.cooja.contikimote.ContikiMoteType; @@ -146,7 +148,7 @@ public interface MoteType { * * @return Mote type visualizer */ - public JPanel getTypeVisualizer(); + public JComponent getTypeVisualizer(); /** * Returns this mote type's project configuration. diff --git a/tools/cooja/java/se/sics/cooja/motes/AbstractApplicationMoteType.java b/tools/cooja/java/se/sics/cooja/motes/AbstractApplicationMoteType.java index a58fcb82f..55375553f 100644 --- a/tools/cooja/java/se/sics/cooja/motes/AbstractApplicationMoteType.java +++ b/tools/cooja/java/se/sics/cooja/motes/AbstractApplicationMoteType.java @@ -23,21 +23,16 @@ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * $Id: AbstractApplicationMoteType.java,v 1.10 2010/02/18 11:13:21 joxe Exp $ */ package se.sics.cooja.motes; -import java.awt.BorderLayout; import java.awt.Container; -import java.awt.Dimension; import java.io.File; import java.util.ArrayList; import java.util.Collection; -import javax.swing.Box; -import javax.swing.BoxLayout; +import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JPanel; @@ -68,7 +63,8 @@ public abstract class AbstractApplicationMoteType implements MoteType { private String identifier = null; private String description = null; - private final Class[] moteInterfaceClasses = new Class[] { + @SuppressWarnings("unchecked") + private final Class[] moteInterfaceClasses = new Class[] { SimpleMoteID.class, Position.class, ApplicationSerialPort.class, @@ -88,7 +84,7 @@ public abstract class AbstractApplicationMoteType implements MoteType { this.description = "Application Mote Type #" + identifier; } - public boolean configureAndInit(Container parentContainer, Simulation simulation, boolean visAvailable) + public boolean configureAndInit(Container parentContainer, Simulation simulation, boolean visAvailable) throws MoteTypeCreationException { if (identifier == null) { /* Create unique identifier */ @@ -139,44 +135,24 @@ public abstract class AbstractApplicationMoteType implements MoteType { throw new RuntimeException("Can not change the mote interface classes"); } - public JPanel getTypeVisualizer() { - JPanel panel = new JPanel(); - JLabel label = new JLabel(); - JPanel smallPane; - - panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); - + public JComponent getTypeVisualizer() { + StringBuilder sb = new StringBuilder(); // Identifier - smallPane = new JPanel(new BorderLayout()); - label = new JLabel("Identifier"); - smallPane.add(BorderLayout.WEST, label); - label = new JLabel(identifier); - smallPane.add(BorderLayout.EAST, label); - panel.add(smallPane); + sb.append(""); // Description - smallPane = new JPanel(new BorderLayout()); - label = new JLabel("Description"); - smallPane.add(BorderLayout.WEST, label); - label = new JLabel(description); - smallPane.add(BorderLayout.EAST, label); - panel.add(smallPane); - - // Mote Interfaces - smallPane = new JPanel(new BorderLayout()); - label = new JLabel("Mote interfaces"); - smallPane.add(BorderLayout.WEST, label); - panel.add(smallPane); + sb.append(""); for (Class moteInterface : moteInterfaceClasses) { - smallPane = new JPanel(new BorderLayout()); - label = new JLabel(moteInterface.getSimpleName()); - smallPane.add(BorderLayout.EAST, label); - panel.add(smallPane); + sb.append(""); } - panel.add(Box.createRigidArea(new Dimension(0, 5))); - return panel; + JLabel label = new JLabel(sb.append("
Identifier") + .append(getIdentifier()).append("
Description") + .append(getDescription()).append("
Mote interface") + .append(moteInterface.getSimpleName()).append("
").toString()); + label.setVerticalTextPosition(JLabel.TOP); + return label; } public File getContikiSourceFile() { diff --git a/tools/cooja/java/se/sics/cooja/plugins/MoteTypeInformation.java b/tools/cooja/java/se/sics/cooja/plugins/MoteTypeInformation.java index 8768fe7ad..2751576e5 100644 --- a/tools/cooja/java/se/sics/cooja/plugins/MoteTypeInformation.java +++ b/tools/cooja/java/se/sics/cooja/plugins/MoteTypeInformation.java @@ -25,19 +25,28 @@ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. - * - * $Id: MoteTypeInformation.java,v 1.4 2008/02/11 14:37:17 fros4943 Exp $ */ package se.sics.cooja.plugins; -import java.awt.*; +import java.awt.BorderLayout; import java.util.Observable; import java.util.Observer; -import javax.swing.*; + +import javax.swing.BorderFactory; +import javax.swing.Box; +import javax.swing.JComponent; +import javax.swing.JLabel; +import javax.swing.JScrollPane; + import org.apache.log4j.Logger; -import se.sics.cooja.*; +import se.sics.cooja.ClassDescription; +import se.sics.cooja.GUI; +import se.sics.cooja.MoteType; +import se.sics.cooja.PluginType; +import se.sics.cooja.Simulation; +import se.sics.cooja.VisPlugin; /** * Shows a summary of all mote types. @@ -49,83 +58,69 @@ import se.sics.cooja.*; public class MoteTypeInformation extends VisPlugin { private static Logger logger = Logger.getLogger(MoteTypeInformation.class); - private static final long serialVersionUID = 1L; - - private Simulation mySimulation; - + private Simulation simulation; private Observer simObserver; + private int nrMotesTypes = -1; /** - * Create a new mote type information window. - * * @param simulation Simulation + * @param gui Cooja */ public MoteTypeInformation(Simulation simulation, GUI gui) { super("Mote Type Information", gui); - mySimulation = simulation; + this.simulation = simulation; this.getContentPane().add(BorderLayout.CENTER, new JScrollPane(createPanel(), JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED)); pack(); + setSize(Math.min(getWidth(), 600), Math.min(getHeight(), 600)); + nrMotesTypes = simulation.getMoteTypes().length; - mySimulation.addObserver(simObserver = new Observer() { + simulation.addObserver(simObserver = new Observer() { public void update(Observable obs, Object obj) { + if (MoteTypeInformation.this.simulation.getMoteTypes().length == nrMotesTypes) { + return; + } + nrMotesTypes = MoteTypeInformation.this.simulation.getMoteTypes().length; MoteTypeInformation.this.getContentPane().removeAll(); MoteTypeInformation.this.getContentPane().add(BorderLayout.CENTER, new JScrollPane(createPanel(), JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED)); - pack(); + revalidate(); + repaint(); } }); - - try { - setSelected(true); - } catch (java.beans.PropertyVetoException e) { - // Could not select - } - } - private JPanel createPanel() { - JLabel label; - JPanel smallPane; + private JComponent createPanel() { + Box box = Box.createVerticalBox(); + box.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); - JPanel panel = new JPanel(); - panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); - panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); + /* Mote types */ + for (MoteType moteType: simulation.getMoteTypes()) { + String moteTypeString = + GUI.getDescriptionOf(moteType) +": " + + "ID=" + moteType.getIdentifier() + + ", \"" + moteType.getDescription() + "\""; - // Visualize mote types - for (MoteType moteType: mySimulation.getMoteTypes()) { - smallPane = new JPanel(); - smallPane.setLayout(new BorderLayout()); - - label = new JLabel(GUI.getDescriptionOf(moteType) +": " + - "ID=" + moteType.getIdentifier() + - ", \"" + moteType.getDescription() + "\""); - label.setAlignmentX(JLabel.CENTER_ALIGNMENT); - smallPane.add(BorderLayout.NORTH, label); - - JPanel moteTypeVisualizer = moteType.getTypeVisualizer(); - if (moteTypeVisualizer != null) { - moteTypeVisualizer.setBorder(BorderFactory.createEtchedBorder()); - smallPane.add(BorderLayout.CENTER, moteTypeVisualizer); - } else { - smallPane.add(BorderLayout.CENTER, Box.createVerticalStrut(25)); + JComponent moteTypeVisualizer = moteType.getTypeVisualizer(); + if (moteTypeVisualizer == null) { + moteTypeVisualizer = new JLabel("[no information available]"); } - - panel.add(smallPane); - panel.add(Box.createRigidArea(new Dimension(0,20))); + moteTypeVisualizer.setAlignmentX(Box.LEFT_ALIGNMENT); + moteTypeVisualizer.setBorder(BorderFactory.createTitledBorder(moteTypeString)); + box.add(moteTypeVisualizer); + box.add(Box.createVerticalStrut(15)); } - - return panel; + return box; } public void closePlugin() { - mySimulation.deleteObserver(simObserver); + simulation.deleteObserver(simObserver); } }