cleaned up the mote type visualizer code

This commit is contained in:
Fredrik Osterlind 2012-05-25 15:40:26 +02:00
parent ad56ac80ee
commit 9e67306eee
4 changed files with 70 additions and 98 deletions

View File

@ -29,17 +29,19 @@
package se.sics.cooja.mspmote; package se.sics.cooja.mspmote;
import java.awt.BorderLayout;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Hashtable; import java.util.Hashtable;
import javax.swing.Icon; import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JPanel;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import org.jdom.Element; 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.Mote; import se.sics.cooja.Mote;
@ -131,7 +133,7 @@ public abstract class MspMoteType implements MoteType {
protected abstract MspMote createMote(Simulation simulation); protected abstract MspMote createMote(Simulation simulation);
@Override @Override
public JPanel getTypeVisualizer() { public JComponent getTypeVisualizer() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
// Identifier // Identifier
sb.append("<html><table><tr><td>Identifier</td><td>") sb.append("<html><table><tr><td>Identifier</td><td>")
@ -171,10 +173,7 @@ public abstract class MspMoteType implements MoteType {
label.setIcon(moteTypeIcon); label.setIcon(moteTypeIcon);
} }
} }
return label;
JPanel panel = new JPanel(new BorderLayout());
panel.add(BorderLayout.CENTER, label);
return panel;
} }
public abstract Icon getMoteTypeIcon(); public abstract Icon getMoteTypeIcon();

View File

@ -30,7 +30,9 @@ package se.sics.cooja;
import java.awt.Container; import java.awt.Container;
import java.io.File; import java.io.File;
import java.util.Collection; import java.util.Collection;
import javax.swing.JPanel;
import javax.swing.JComponent;
import org.jdom.Element; import org.jdom.Element;
import se.sics.cooja.contikimote.ContikiMoteType; import se.sics.cooja.contikimote.ContikiMoteType;
@ -146,7 +148,7 @@ public interface MoteType {
* *
* @return Mote type visualizer * @return Mote type visualizer
*/ */
public JPanel getTypeVisualizer(); public JComponent getTypeVisualizer();
/** /**
* Returns this mote type's project configuration. * Returns this mote type's project configuration.

View File

@ -23,21 +23,16 @@
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * 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 * (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: AbstractApplicationMoteType.java,v 1.10 2010/02/18 11:13:21 joxe Exp $
*/ */
package se.sics.cooja.motes; package se.sics.cooja.motes;
import java.awt.BorderLayout;
import java.awt.Container; import java.awt.Container;
import java.awt.Dimension;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import javax.swing.Box; import javax.swing.JComponent;
import javax.swing.BoxLayout;
import javax.swing.JLabel; import javax.swing.JLabel;
import javax.swing.JPanel; import javax.swing.JPanel;
@ -68,7 +63,8 @@ public abstract class AbstractApplicationMoteType implements MoteType {
private String identifier = null; private String identifier = null;
private String description = null; private String description = null;
private final Class<? extends MoteInterface>[] moteInterfaceClasses = new Class[] { @SuppressWarnings("unchecked")
private final Class<? extends MoteInterface>[] moteInterfaceClasses = new Class[] {
SimpleMoteID.class, SimpleMoteID.class,
Position.class, Position.class,
ApplicationSerialPort.class, ApplicationSerialPort.class,
@ -88,7 +84,7 @@ public abstract class AbstractApplicationMoteType implements MoteType {
this.description = "Application Mote Type #" + identifier; 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 { throws MoteTypeCreationException {
if (identifier == null) { if (identifier == null) {
/* Create unique identifier */ /* Create unique identifier */
@ -139,44 +135,24 @@ public abstract class AbstractApplicationMoteType implements MoteType {
throw new RuntimeException("Can not change the mote interface classes"); throw new RuntimeException("Can not change the mote interface classes");
} }
public JPanel getTypeVisualizer() { public JComponent getTypeVisualizer() {
JPanel panel = new JPanel(); StringBuilder sb = new StringBuilder();
JLabel label = new JLabel();
JPanel smallPane;
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
// Identifier // Identifier
smallPane = new JPanel(new BorderLayout()); sb.append("<html><table><tr><td>Identifier</td><td>")
label = new JLabel("Identifier"); .append(getIdentifier()).append("</td></tr>");
smallPane.add(BorderLayout.WEST, label);
label = new JLabel(identifier);
smallPane.add(BorderLayout.EAST, label);
panel.add(smallPane);
// Description // Description
smallPane = new JPanel(new BorderLayout()); sb.append("<tr><td>Description</td><td>")
label = new JLabel("Description"); .append(getDescription()).append("</td></tr>");
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);
for (Class<? extends MoteInterface> moteInterface : moteInterfaceClasses) { for (Class<? extends MoteInterface> moteInterface : moteInterfaceClasses) {
smallPane = new JPanel(new BorderLayout()); sb.append("<tr><td>Mote interface</td><td>")
label = new JLabel(moteInterface.getSimpleName()); .append(moteInterface.getSimpleName()).append("</td></tr>");
smallPane.add(BorderLayout.EAST, label);
panel.add(smallPane);
} }
panel.add(Box.createRigidArea(new Dimension(0, 5))); JLabel label = new JLabel(sb.append("</table></html>").toString());
return panel; label.setVerticalTextPosition(JLabel.TOP);
return label;
} }
public File getContikiSourceFile() { public File getContikiSourceFile() {

View File

@ -25,19 +25,28 @@
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * 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 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
*
* $Id: MoteTypeInformation.java,v 1.4 2008/02/11 14:37:17 fros4943 Exp $
*/ */
package se.sics.cooja.plugins; package se.sics.cooja.plugins;
import java.awt.*; import java.awt.BorderLayout;
import java.util.Observable; import java.util.Observable;
import java.util.Observer; 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 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. * Shows a summary of all mote types.
@ -49,83 +58,69 @@ import se.sics.cooja.*;
public class MoteTypeInformation extends VisPlugin { public class MoteTypeInformation extends VisPlugin {
private static Logger logger = Logger.getLogger(MoteTypeInformation.class); private static Logger logger = Logger.getLogger(MoteTypeInformation.class);
private static final long serialVersionUID = 1L; private Simulation simulation;
private Simulation mySimulation;
private Observer simObserver; private Observer simObserver;
private int nrMotesTypes = -1;
/** /**
* Create a new mote type information window.
*
* @param simulation Simulation * @param simulation Simulation
* @param gui Cooja
*/ */
public MoteTypeInformation(Simulation simulation, GUI gui) { public MoteTypeInformation(Simulation simulation, GUI gui) {
super("Mote Type Information", gui); super("Mote Type Information", gui);
mySimulation = simulation; this.simulation = simulation;
this.getContentPane().add(BorderLayout.CENTER, this.getContentPane().add(BorderLayout.CENTER,
new JScrollPane(createPanel(), new JScrollPane(createPanel(),
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED)); JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
pack(); 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) { 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().removeAll();
MoteTypeInformation.this.getContentPane().add(BorderLayout.CENTER, MoteTypeInformation.this.getContentPane().add(BorderLayout.CENTER,
new JScrollPane(createPanel(), new JScrollPane(createPanel(),
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED)); JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));
pack(); revalidate();
repaint();
} }
}); });
try {
setSelected(true);
} catch (java.beans.PropertyVetoException e) {
// Could not select
}
} }
private JPanel createPanel() { private JComponent createPanel() {
JLabel label; Box box = Box.createVerticalBox();
JPanel smallPane; box.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
JPanel panel = new JPanel(); /* Mote types */
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); for (MoteType moteType: simulation.getMoteTypes()) {
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); String moteTypeString =
GUI.getDescriptionOf(moteType) +": " +
"ID=" + moteType.getIdentifier() +
", \"" + moteType.getDescription() + "\"";
// Visualize mote types JComponent moteTypeVisualizer = moteType.getTypeVisualizer();
for (MoteType moteType: mySimulation.getMoteTypes()) { if (moteTypeVisualizer == null) {
smallPane = new JPanel(); moteTypeVisualizer = new JLabel("[no information available]");
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));
} }
moteTypeVisualizer.setAlignmentX(Box.LEFT_ALIGNMENT);
panel.add(smallPane); moteTypeVisualizer.setBorder(BorderFactory.createTitledBorder(moteTypeString));
panel.add(Box.createRigidArea(new Dimension(0,20))); box.add(moteTypeVisualizer);
box.add(Box.createVerticalStrut(15));
} }
return box;
return panel;
} }
public void closePlugin() { public void closePlugin() {
mySimulation.deleteObserver(simObserver); simulation.deleteObserver(simObserver);
} }
} }