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;
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("<html><table><tr><td>Identifier</td><td>")
@ -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();

View File

@ -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.

View File

@ -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<? extends MoteInterface>[] moteInterfaceClasses = new Class[] {
@SuppressWarnings("unchecked")
private final Class<? extends MoteInterface>[] 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("<html><table><tr><td>Identifier</td><td>")
.append(getIdentifier()).append("</td></tr>");
// 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("<tr><td>Description</td><td>")
.append(getDescription()).append("</td></tr>");
for (Class<? extends MoteInterface> moteInterface : moteInterfaceClasses) {
smallPane = new JPanel(new BorderLayout());
label = new JLabel(moteInterface.getSimpleName());
smallPane.add(BorderLayout.EAST, label);
panel.add(smallPane);
sb.append("<tr><td>Mote interface</td><td>")
.append(moteInterface.getSimpleName()).append("</td></tr>");
}
panel.add(Box.createRigidArea(new Dimension(0, 5)));
return panel;
JLabel label = new JLabel(sb.append("</table></html>").toString());
label.setVerticalTextPosition(JLabel.TOP);
return label;
}
public File getContikiSourceFile() {

View File

@ -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);
}
}