]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - support/sdk/java/net/tinyos/mviz/DMoteModel.java
Updated files.
[tinyos-2.x.git] / support / sdk / java / net / tinyos / mviz / DMoteModel.java
diff --git a/support/sdk/java/net/tinyos/mviz/DMoteModel.java b/support/sdk/java/net/tinyos/mviz/DMoteModel.java
new file mode 100644 (file)
index 0000000..d430818
--- /dev/null
@@ -0,0 +1,233 @@
+/*\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 DMoteModel \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 x, y, id;\r
+    protected float[] values;\r
+    protected Color[] colors;\r
+    protected int[] sizes;\r
+    \r
+    protected int SHAPE_SIZE_MAX = 100;\r
+    protected int COLOR_MAX = 230;\r
+    \r
+    \r
+       \r
+    public DMoteModel(int id, int x, int y, float[] values, DDocument root) {\r
+        this.root = root;\r
+        this.x = x;\r
+        this.y = y;\r
+       this.id = id;\r
+       \r
+        values = new float[root.sensed_motes.size()];\r
+        colors = new Color[root.sensed_motes.size()];\r
+        sizes = new int[root.sensed_motes.size()];\r
+        \r
+        for (int i=0; i<values.length; i++){\r
+            colors[i] = setColor(values[i]);\r
+        }\r
+        for (int i=0; i<values.length; i++){\r
+            sizes[i] = setShapeSize(values[i]);\r
+        }\r
+        \r
+        listeners = null;\r
+    }\r
+\r
+       \r
+    public DMoteModel(DDocument root, int id, String name){\r
+       \r
+    }\r
+       \r
+    public DMoteModel(int id, Random rand, DDocument root){\r
+        this.root = root;\r
+       this.id = id;\r
+               \r
+        x = 20 + rand.nextInt(root.canvas.getWidth() - 20);\r
+        y = 20 + rand.nextInt(root.canvas.getHeight()- 20);\r
+        \r
+        values = new float[root.sensed_motes.size()];\r
+       \r
+        colors = new Color[root.sensed_motes.size()];\r
+        sizes = new int[root.sensed_motes.size()];\r
+        \r
+        for (int i=0; i<root.sensed_motes.size(); i++){\r
+            values[i] = rand.nextFloat()*1000;     \r
+            colors[i] = setColor(values[i]);\r
+            sizes[i] = setShapeSize(values[i]);\r
+        }   \r
+        \r
+        listeners = null;\r
+    }\r
+    \r
+    public int getId() {\r
+       return id;\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
+    public int setShapeSize(float value){\r
+        return SHAPE_SIZE_MAX;\r
+        //return (int)(value/root.maxValues[root.selectedFieldIndex] * SHAPE_SIZE_MAX);\r
+    }    \r
+       \r
+    public float getValue(int index) {\r
+       if (values.length <= index) {\r
+           return 0;\r
+       }\r
+       else {\r
+           return(values[index]);\r
+       }\r
+    }\r
+\r
+    public boolean setMoteValue(String field, int value){\r
+       int index = root.sensed_motes.indexOf(field);\r
+       if (index < 0) return false;\r
+       colors[index] = setColor((float)value);\r
+       setValue(index, (float) value);\r
+       return true;\r
+    }\r
+\r
+    \r
+    public int getX() { return(x); }\r
+    public int getY() { return(y); }\r
+    public ImageIcon getIcon(){ return root.icon; }\r
+       \r
+    public void setValue(int index, float value){\r
+       values[index] = value;\r
+       fireChanges();\r
+    }    \r
+    public void applyDeltas(int dx, int dy) {        \r
+       x += dx;\r
+       y += dy;\r
+       fireChanges();\r
+    }\r
+    public Image getImage() {\r
+       return root.image;\r
+    }\r
+    public int getWidth(int index) {\r
+       return getIcon().getImage().getWidth(this.root);\r
+       //return sizes[index];\r
+    }  \r
+    public int getHeight(int index) {\r
+       return getIcon().getImage().getHeight(this.root);\r
+       //return sizes[index];\r
+    }  \r
+    public int getLeft(){\r
+       return getLocX() - getWidth(0)/2;\r
+    }\r
+    public int getTop(){\r
+       return getLocY() - getHeight(0)/2;\r
+    } \r
+    public int getLocX() {\r
+       return x;\r
+    }  \r
+    public int getLocY() {\r
+       return y;\r
+    }          \r
+    public Color getColor(int index) { \r
+       return colors[index]; \r
+    }\r
+       \r
+       \r
+    public void addListener(DMoteModelListener 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(DMoteModelListener 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
+           ((DMoteModelListener)(it.next())).shapeChanged(this, ANY);\r
+    }\r
+    public void requestRepaint(){\r
+       fireChanges();\r
+    }\r
+       \r
+    //=========================================================================/\r
+    public void move(int x, int y){\r
+        this.x = x;\r
+        this.y = y;\r
+       fireChanges();      \r
+    }\r
+\r
+    public boolean equals(Object o) {\r
+       if (o instanceof DMoteModel) {\r
+           DMoteModel dm = (DMoteModel)o;\r
+           if (dm.getId() == getId()) {\r
+               return true;\r
+           }\r
+       }\r
+       return false;\r
+    }\r
+}\r
+\r