📄 moteplugin.java
字号:
// $Id: MotePlugin.java,v 1.13.4.4 2003/09/10 03:41:52 mdwelsh Exp $/* tab:2 * * * "Copyright (c) 2000 and The Regents of the University * of California. All rights reserved. * * Permission to use, copy, modify, and distribute this software and * its documentation for any purpose, without fee, and without written * agreement is hereby granted, provided that the above copyright * notice and the following two paragraphs appear in all copies of * this software. * * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL * DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS * DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, * UPDATES, ENHANCEMENTS, OR MODIFICATIONS." * * Authors: Nelson Lee * Date: December 11 2002 * Desc: Default Mote Plugin * Those who want to implement their own MotePlugin should * create a class that extends MotePlugin! * *//** * @author Nelson Lee */package net.tinyos.sim;import java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.io.*;import net.tinyos.sim.event.*;public class MotePlugin extends Plugin implements SimConst { private static final boolean DEBUG = false; public final Color BASIC_COLOR = Color.black; public final Color SELECTED_COLOR = new Color(150, 150, 255); public final Color BASIC_COLOR_OFF = Color.lightGray; public final Color SELECTED_COLOR_OFF = SELECTED_COLOR; private static final double GRID_RANDOM_DEVIATION = 5; public static final int LAYOUT_RANDOM = 0; public static final int LAYOUT_GRID = 1; public static final int LAYOUT_GRID_RANDOM = 2; public static final int LAYOUT_FILE = 3; private int layout = LAYOUT_RANDOM; private SimState state; private MotePanel motePanel; private SimComm simComm; private Random rand = new Random(); private SpatialReader spatialReader; // SimObjectGenerators MotePlugin registers MoteSimObjectGenerator moteSimObjectGenerator; public void initialize(TinyViz tv, JPanel pluginPanel) { super.initialize(tv, pluginPanel); this.motePanel = tv.getMotePanel(); this.moteSimObjectGenerator = new MoteSimObjectGenerator(tv); this.state = tv.getSimState(); this.simComm = tv.getSimComm(); } public void register() { state.addSOGenerator(moteSimObjectGenerator); JButton bPower = tv.getMenuBar().addIconButton("On/off", "ui/power.gif"); bPower.addActionListener(new bPowerListener()); // Add layout menu ButtonGroup group = new ButtonGroup(); menuListener ml = new menuListener(); JMenu layoutMenu = new JMenu("Layout"); JRadioButtonMenuItem layoutRandom = new JRadioButtonMenuItem("Random"); group.add(layoutRandom); layoutRandom.setFont(tv.defaultFont); layoutRandom.addActionListener(ml); layoutRandom.setSelected(true); JRadioButtonMenuItem layoutGrid = new JRadioButtonMenuItem("Grid"); group.add(layoutGrid); layoutGrid.setFont(tv.defaultFont); layoutGrid.addActionListener(ml); JRadioButtonMenuItem layoutGridRandom = new JRadioButtonMenuItem("Grid + Random"); group.add(layoutGridRandom); layoutGridRandom.setFont(tv.defaultFont); layoutGridRandom.addActionListener(ml); JMenuItem layoutFileLoad = new JMenuItem("File Load"); layoutFileLoad.addActionListener(ml); layoutFileLoad.setFont(tv.defaultFont); JMenuItem layoutFileSave = new JMenuItem("File Save"); layoutFileSave.addActionListener(ml); layoutFileSave.setFont(tv.defaultFont); layoutMenu.add(layoutRandom); layoutMenu.add(layoutGrid); layoutMenu.add(layoutGridRandom); layoutMenu.add(layoutFileLoad); layoutMenu.add(layoutFileSave); tv.getMenuBar().addMenu(layoutMenu); } public void deregister() { state.removeSOGenerator(moteSimObjectGenerator); } public void handleEvent (SimEvent event) { if (DEBUG) System.err.println("MOTEPLUGIN: handling "+event); if (event instanceof OptionSetEvent) { OptionSetEvent ose = (OptionSetEvent)event; if (!ose.name.equals("layout")) return; if (ose.value.equals("grid")) { this.layout = MotePlugin.LAYOUT_GRID; } else if (ose.value.equals("random")) { this.layout = MotePlugin.LAYOUT_RANDOM; } else if (ose.value.equals("gridrandom")) { this.layout = MotePlugin.LAYOUT_GRID_RANDOM; } else if (ose.value.startsWith("file ")) { int oldlayout = this.layout; this.layout = MotePlugin.LAYOUT_FILE; try { loadLocationFile(new File(ose.value.substring(5))); } catch (Exception e) { System.err.println("Can't load location file '"+ose.value.substring(5)+"': "+e); this.layout = oldlayout; } } else { System.err.println("Bad value for layout option: "+ose.value); } doLayout(); motePanel.refresh(); } else if (event instanceof SimObjectEvent) { SimObjectEvent soEvent = (SimObjectEvent)event; SimObject simObject = soEvent.getSimObject(); switch (soEvent.getType()) { case (SimObjectEvent.OBJECT_ADDED): if (simObject instanceof MoteSimObject) { // XXX MDW: Assume motes aren't added on the fly //if (layout == LAYOUT_GRID || layout == LAYOUT_GRID_RANDOM) // doLayout(); //motePanel.refresh(); } break; case (SimObjectEvent.OBJECT_REMOVED): if (simObject instanceof MoteSimObject) { if (layout == LAYOUT_GRID || layout == LAYOUT_GRID_RANDOM) doLayout(); motePanel.refresh(); } break; } } else if (event instanceof TossimInitEvent) { TossimInitEvent tiEvent = (TossimInitEvent)event; int numMotes = tiEvent.get_numMotes(); for (int i = 0; i < numMotes; i++) { moteSimObjectGenerator.create(i); } if (layout != LAYOUT_RANDOM) doLayout(); motePanel.refresh(); } else if (event instanceof TossimEvent) { TossimEvent tosEvent = (TossimEvent)event; moteSimObjectGenerator.create(tosEvent.getMoteID()); if (tosEvent instanceof DebugMsgEvent) { DebugMsgEvent dmEvent = (DebugMsgEvent)tosEvent; boolean attributeModified = false; // change the led values accordingly, based on if the debug message has "LEDS:" in it String data = dmEvent.getMessage(); StringTokenizer st = new StringTokenizer(data, " "); if (st.countTokens() == 3) { if (st.nextToken().compareTo("LEDS:") == 0) { MoteSimObject mote = state.getMoteSimObject(dmEvent.getMoteID()); MoteLedsAttribute ledAttrib = (MoteLedsAttribute) (mote.getAttribute("net.tinyos.sim.MoteLedsAttribute")); String color = st.nextToken(); if (color.compareTo("Red") == 0) { if (st.nextToken().compareTo("off.") == 0) { ledAttrib.setRedOff(); attributeModified = true; } else { ledAttrib.setRedOn(); attributeModified = true; } } else if (color.compareTo("Yellow") == 0) { if (st.nextToken().compareTo("off.") == 0) { ledAttrib.setYellowOff(); attributeModified = true; } else { ledAttrib.setYellowOn(); attributeModified = true; } } else if (color.compareTo("Green") == 0) { if (st.nextToken().compareTo("off.") == 0) { ledAttrib.setGreenOff(); attributeModified = true; } else { ledAttrib.setGreenOn(); attributeModified = true; } } if (attributeModified) eventBus.addEvent(new AttributeEvent(ATTRIBUTE_CHANGED, mote, ledAttrib)); } motePanel.refresh(); } } //System.err.println("MotePlugin handleEvent done."); } } public void draw(Graphics graphics) { //System.err.println("MotePlugin draw called."); //System.out.println("drawing on graphics " + graphics); Iterator it = state.getMoteSimObjects().iterator(); graphics.setFont(tv.smallFont); while (it.hasNext()) { MoteSimObject moteSimObject = (MoteSimObject)it.next(); if (!moteSimObject.isVisible()) { continue; } if (moteSimObject.getPower()) { if (moteSimObject.isSelected()) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -