📄 directedgraphplugin.java
字号:
// $Id: DirectedGraphPlugin.java,v 1.8.4.1 2003/08/18 22:09:44 cssharp Exp $/* Author Kamin Whitehousewhile this component draws directed graphs, the following code is organized interms of neighborhoods, ie. all edges are grouped by the origin node,so that the user can see only edges from one node if he/she desires. */package net.tinyos.sim.plugins;import java.lang.*;import java.util.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import net.tinyos.message.*;import net.tinyos.sim.*;import net.tinyos.sim.event.*;import net.tinyos.matlab.*;public class DirectedGraphPlugin extends Plugin implements SimConst, ActionListener { private TextArea helpText= new TextArea( "The DirectedGraphPlugin allows the user to define multiple directed graphs in their TinyOS code \n" + "and have them automatically appear as arrows in TinyViz.\n\n"+ "There are two commands that can be used with this plugin:\n\n"+ " dbg(DBG_USR1, \"MyGraphName DIRECTED GRAPH: add edge %d \\n\", someOtherNode);\n"+ " dbg(DBG_USR1, \"MyGraphName DIRECTED GRAPH: remove edge %d \\n\", someOtherNode);\n\n"+ " dbg(DBG_USR1, \"MyGraphName DIRECTED GRAPH: clear\\n\");\n\n"+ "These will have the effect of adding or removing edges between someOtherNode\n" + "and TOS_LOCAL_ADDRESS. \"MyGraphName\" will automatically appear \n" + "in the plugin drop-down list and "+ "you can select it to see all the arrows added to that graph.\n\n" + "To see only the edges originating with a particular node, select the \"selected motes only\" box.\n\n" + "There are several parameters you can append to the \"add\" command:\n\n"+ "\"color: 0xRRGGBB\" will cause the edge to have a color (default blue).\n\n"+ "\"label: myLable\" will cause the arrow to have the word \"myLabel\" printed next to it.\n\n"+ "\"timeout: xxx\" will cause the edge to disappear after xxx milliseconds of simulated time. \n\n"+ "\"offset: xxx\" will cause the edge be displayed xxx pixels to the side (useful for multiple colored arrows). \n\n"+ "\"direction: forward | backward | both | none\" will cause the edge of that arrow to point either \n" + "towards someNodeID or towards TOS_LOCAL_ADDRESS or both or neither.\n\n" + "\"sourceNode: someOtherNodeID\" will cause the edge to originate from someOtherNodeID instead of TOS_LOCAL_ADDRESS.\n\n" + "EXAMPLE:\n\n"+"An example of an \"add\" command with all four options is:\n\n"+"dbg(DBG_USR1, \"MyGraphName DIRECTED GRAPH: add edge %d color: 0xFF0000 label: %d timeout: 500 direction: both sourceNode: 0\\n\", someNodeID, TOS_LOCAL_ADDRESS);\n\n"+"This command will cause the edge to be drawn in both directions between node 0\n" +"and node someNodeID in RED with the label having the current node's address ID,\n"+"and the edge will disappear after 500 milliseconds.\n"); private Hashtable neighborhoods = new Hashtable(); private JCheckBox cbSelectedOnly; private boolean selectedOnly = false;// JComboBox cb = new JComboBox(); JList graphList = new JList();// String drawingHood; Vector knownHoods = new Vector(); public void handleEvent(SimEvent event) { if (event instanceof AttributeEvent) { AttributeEvent ae = (AttributeEvent) event; if (ae.getType() == AttributeEvent.ATTRIBUTE_CHANGED) { if (ae.getOwner() instanceof MoteSimObject && ae.getAttribute() instanceof MoteCoordinateAttribute) { tv.getMotePanel().refresh(); } } } else if (event instanceof TossimInitEvent) { neighborhoods.clear(); knownHoods=new Vector(); graphList.setListData(knownHoods); pluginPanel.revalidate(); } else if (event instanceof DebugMsgEvent) { DebugMsgEvent dme = (DebugMsgEvent) event; if (dme.getMessage().indexOf("DIRECTED GRAPH:") != -1) { int nodeID = dme.getMoteID(),neighborID=-1, timeout=-1, offset=0, direction=Arrow.SIDE_LEAD; String label=null; Color color=Color.lightGray; StringTokenizer st = new StringTokenizer(dme.getMessage()); String hood = st.nextToken(); if (dme.getMessage().indexOf("clear") != -1) { clearAll(hood); return; } if(!knownHoods.contains(hood)){ knownHoods.add(hood); int length; int[] selected=null; if(knownHoods.size()>1){ selected = graphList.getSelectedIndices(); length=selected.length+1; } else length=1; int[] newSelected = new int[length]; for(int i=0;i<length-1;i++){ newSelected[i]=selected[i]; } newSelected[newSelected.length-1]=knownHoods.size()-1; graphList.setListData(knownHoods); graphList.setSelectedIndices(newSelected);// graphList.setSelectedIndex(knownHoods.size()-1); } st.nextToken(); st.nextToken(); st.nextToken(); st.nextToken(); neighborID =Integer.parseInt(st.nextToken()); while(st.hasMoreTokens()){ String paramName=st.nextToken(); if(paramName.equals("label:")){ label= st.nextToken(); } else if(paramName.equals("sourceNode:")){ nodeID= Integer.parseInt(st.nextToken()); } else if(paramName.equals("timeout:")){ timeout= (int)(tv.getTosTime()*1000) + Integer.parseInt(st.nextToken()); } else if(paramName.equals("offset:")){ offset= Integer.parseInt(st.nextToken());; } else if(paramName.equals("color:")){ try{ color=new Color(Integer.decode(st.nextToken()).intValue()); } catch(Exception e){} } else if(paramName.equals("direction:")){ String dir = st.nextToken(); if(dir.equals("forward")){ direction=Arrow.SIDE_LEAD; } else if(dir.equals("backward")){ direction=Arrow.SIDE_TRAIL; } else if(dir.equals("both")){ direction=Arrow.SIDE_BOTH; } else if(dir.equals("none")){ direction=Arrow.SIDE_NONE; } } } MoteSimObject mote1 = state.getMoteSimObject(nodeID); MoteSimObject mote2 = state.getMoteSimObject(neighborID); if(mote1==null || mote2==null) return; Neighborhood neighborhood; if (neighborhoods.containsKey(new Integer(nodeID))) neighborhood = (Neighborhood) neighborhoods.get(new Integer(nodeID)); else { neighborhood = new Neighborhood(nodeID); } //dbg("XX DIRECTED GRAPH: add edge XX") if (dme.getMessage().indexOf("add edge") != -1) { neighborhood.neighbors.add(new Neighbor(neighborID, hood, color, label, direction, timeout, offset)); neighborhoods.put(new Integer(nodeID),neighborhood); } //dbg("XX DIRECTED GRAPH: remove edge XX") else if (dme.getMessage().indexOf("remove edge") != -1) { while(neighborhood.neighbors.remove(new Neighbor(neighborID, hood))); neighborhoods.put(new Integer(nodeID),neighborhood); } tv.getMotePanel().refresh(); }// motePanel.refresh(); } } public void register() {/* JTextArea ta = new JTextArea(2, 50); ta.setFont(tv.defaultFont); ta.setEditable(false); ta.setBackground(Color.lightGray); ta.setLineWrap(true);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -