]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - support/sdk/java/net/tinyos/mviz/DLinkModel.java
Updated files.
[tinyos-2.x.git] / support / sdk / java / net / tinyos / mviz / DLinkModel.java
diff --git a/support/sdk/java/net/tinyos/mviz/DLinkModel.java b/support/sdk/java/net/tinyos/mviz/DLinkModel.java
new file mode 100644 (file)
index 0000000..560d78a
--- /dev/null
@@ -0,0 +1,162 @@
+/*\r
+ * Copyright (c) 2006 Stanford University.\r
+ * All rights reserved.\r
+ *\r
+ * Redistribution and use in source and binary forms, with or without\r
+ * modification, are permitted provided that the following conditions\r
+ * are met:\r
+ * - Redistributions of source code must retain the above copyright\r
+ *   notice, this list of conditions and the following disclaimer.\r
+ * - Redistributions in binary form must reproduce the above copyright\r
+ *   notice, this list of conditions and the following disclaimer in the\r
+ *   documentation and/or other materials provided with the\r
+ *   distribution.\r
+ * - Neither the name of the Stanford University nor the names of\r
+ *   its contributors may be used to endorse or promote products derived\r
+ *   from this software without specific prior written permission.\r
+ *\r
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS\r
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL STANFORD\r
+ * UNIVERSITY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\r
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\r
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\r
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\r
+ * OF THE POSSIBILITY OF SUCH DAMAGE.\r
+ */\r
+\r
+package net.tinyos.mviz;\r
+\r
+// DShapeModel.java\r
+/*\r
+ Store the data state for a single shape:\r
+  type, two points, color\r
+ Supports DShapeModelListeners.\r
+*/\r
+import java.awt.*;\r
+\r
+import javax.swing.*;\r
+import java.util.*;\r
+import java.awt.event.*;\r
+import java.io.*;\r
+\r
+\r
+class DLinkModel \r
+extends Object \r
+implements Serializable {\r
+\r
+       public static final int VALUE = 0;\r
+       public static final int MOTION = 1;\r
+       public static final int ANY = 1;\r
+       \r
+       \r
+    public DDocument root;\r
+    transient private ArrayList listeners;\r
+    \r
+    protected int x12, y12;\r
+    protected int[] values;\r
+    \r
+    DMoteModel m1;\r
+    DMoteModel m2;\r
+    \r
+    protected int COLOR_MAX = 230;\r
+       \r
+    public DLinkModel(DMoteModel m1, DMoteModel m2, Random rand, DDocument root){\r
+        this.root = root;\r
+        this.m1 = m1;\r
+        this.m2 = m2;\r
+        \r
+        x12 = getMiddle(m1.x, m2.x);\r
+        y12 = getMiddle(m1.y, m2.y);\r
+        \r
+        values = new int[root.sensed_links.size()];\r
+        \r
+        for (int i=0; i<root.sensed_links.size(); i++){\r
+            values[i] = 0;\r
+        }   \r
+        \r
+\r
+        //.listeners = null;\r
+    }\r
+\r
+    protected void setLinkValue(String name, int value) {\r
+       int index = root.sensed_links.indexOf(name);\r
+       if (index < 0) return;\r
+       values[index] = value;\r
+       //System.out.println("Setting link value " + index + " to " + value);\r
+    }\r
+    private int getMiddle(int x1, int x2){\r
+       return (x1 + x2)/2;\r
+    }\r
+    \r
+    public Color setColor(float value){\r
+        int color = (int)(value)%COLOR_MAX;\r
+        return new Color(color+15, color, color+25);\r
+    } \r
+       \r
+       public int getValue() { return(values[root.selectedLinkIndex]); }\r
+       public int getValue(int index) {\r
+           //System.out.println("Gettting link value " + index);\r
+           return(values[index]);\r
+       }\r
+    public int getValue(String name) {\r
+       int index = root.sensed_links.indexOf(name);\r
+       if (index < 0) return 0 ;\r
+       return values[index];\r
+    }\r
+       public int getTop() { return(Math.min(m1.y, m2.y)); }           \r
+       public int getBottom() { return(Math.max(m1.y, m2.y)); }                \r
+       public int getLeft() { return(Math.min(m1.x, m2.x)); }          \r
+       public int getRight() { return(Math.max(m1.x, m2.x)); }\r
+       \r
+       public int getWidth() { return(Math.abs(m1.x - m2.x)); }\r
+       public int getHeight() { return(Math.abs(m1.y - m2.y)); }\r
+\r
+    protected Color getColor() {\r
+       return Color.RED;\r
+    }\r
+       public void setValue(int value){\r
+           values[root.selectedLinkIndex] = value;\r
+           fireChanges();\r
+       }\r
+\r
+    \r
+       public int getLocX() {\r
+           return x12;\r
+       }       \r
+       public int getLocY() {\r
+           return y12;\r
+       }               \r
+       \r
+       \r
+       public void addListener(DLinkModelListener listener) {\r
+           if (listeners == null) listeners = new ArrayList();\r
+           Iterator it = listeners.iterator();\r
+               while (it.hasNext()) { if (it.next() == listener) return; };            \r
+               listeners.add(listener);            \r
+       }\r
+\r
+       public void removeListener(DLinkModelListener listener) {\r
+           if (listeners == null) return;          \r
+           Iterator it = listeners.iterator();\r
+               while (it.hasNext()) {\r
+                   if (it.next() == listener){\r
+                       it.remove();\r
+                       return;\r
+                   }           \r
+               }                       \r
+       }\r
+       //=========================================================================/\r
+       protected void fireChanges(){\r
+           if (listeners==null) return;\r
+           Iterator it = listeners.iterator();\r
+               while (it.hasNext()) \r
+                  ((DLinkModelListener)(it.next())).shapeChanged(this, ANY);\r
+       }\r
+       \r
+}\r
+\r