]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - support/sdk/java/net/tinyos/mviz/DLayer.java
Updated files.
[tinyos-2.x.git] / support / sdk / java / net / tinyos / mviz / DLayer.java
diff --git a/support/sdk/java/net/tinyos/mviz/DLayer.java b/support/sdk/java/net/tinyos/mviz/DLayer.java
new file mode 100644 (file)
index 0000000..d094168
--- /dev/null
@@ -0,0 +1,365 @@
+/*\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
+// DDocument.java\r
+\r
+import java.awt.*;\r
+\r
+import javax.imageio.ImageIO;\r
+import javax.swing.*;\r
+\r
+import java.util.*;\r
+import java.awt.event.*;\r
+import java.io.*;\r
+\r
+import javax.swing.*;\r
+import javax.swing.border.Border;\r
+import javax.swing.border.LineBorder;\r
+import javax.swing.table.*;\r
+\r
+import java.awt.image.*;\r
+\r
+\r
+// Standard imports for XML\r
+import javax.xml.parsers.*;\r
+import org.xml.sax.*;\r
+import org.w3c.dom.*;\r
+\r
+\r
+\r
+\r
+public class DLayer extends JPanel implements ActionListener{\r
+       \r
+    public static final int MOTE = 0;\r
+    public static final int LINK = 1;\r
+    public static final int FIELD = 2;\r
+    private static final Color[] COLORS = {\r
+       new Color(231, 220, 206),\r
+       new Color(250, 210, 99),\r
+       new Color(209, 230, 179)\r
+    };\r
+       \r
+    private int type;\r
+    protected int index;\r
+    protected int zIndex;\r
+    protected int z_index = 0;\r
+    private ArrayList layer = new ArrayList();\r
+       \r
+    private JLabel label;\r
+    private JCheckBox check;\r
+    private String[][] DISPLAYS = { {"circle", "img", "txt"}, {"line", "line+label", "label"}, {"color 256", "color 1024", "color 4096", "color 16384"}};\r
+    private JComboBox displays;\r
+       \r
+    private ArrayList models;\r
+    private ArrayList linkModels;\r
+    private JButton up;\r
+    private JButton down;\r
+       \r
+    protected int paintMode = 0;\r
+    // Values chosen for COLOR so that readings can be right shifted\r
+    // that many bits to be in range 0-255\r
+    static public final int COLOR_256 = 0;\r
+    static public final int OVAL = 1;\r
+    static public final int COLOR_1024 = 2;\r
+    static public final int IMG = 3;\r
+    static public final int COLOR_4096 = 4;\r
+    static public final int TXT_MOTE = 5;\r
+    static public final int COLOR_16384 = 6;\r
+    static public final int LINE = 7;\r
+    static public final int LABEL = 8;\r
+    static public final int LINE_LABEL = 9;\r
+    \r
+    protected DNavigate navigator;\r
+       \r
+    private String name;\r
+    private DDocument parent;\r
+       \r
+    public DLayer(int zIndex, int index, String label, int type, DDocument parent, ArrayList models, DNavigate navigator){\r
+       this.parent = parent;\r
+       this.type = type;\r
+       this.models = models;\r
+       this.zIndex = zIndex;\r
+       this.index = index;\r
+       this.navigator = navigator;\r
+       this.name = label;\r
+       if (type == MOTE) {\r
+           this.paintMode = OVAL;\r
+       }\r
+       else if (type == LINK) {\r
+           this.paintMode = LINE;\r
+       }\r
+\r
+       \r
+       SpringLayout layout = new SpringLayout();\r
+       setLayout(layout);\r
+       setMaximumSize(new Dimension(350, 25));\r
+       setPreferredSize(new Dimension(350, 25));\r
+       setSize(new Dimension(350, 25));\r
+       setDoubleBuffered(true);\r
+       setBackground(COLORS[type]);\r
+       setBorder(new LineBorder(new Color(155, 155, 155)));\r
+               \r
+       check = new JCheckBox();\r
+       check.setSize(35, 25);\r
+       check.setMaximumSize(new Dimension(35, 25));\r
+       check.setMinimumSize(new Dimension(35, 25));\r
+       check.setPreferredSize(new Dimension(35, 25));\r
+       \r
+       up = new JButton("^");\r
+       up.setFont(new Font("Times", Font.PLAIN, 9));\r
+       up.setSize(25, 25);\r
+       up.setMaximumSize(new Dimension(25, 25));\r
+       up.setMinimumSize(new Dimension(25, 25));\r
+       up.setPreferredSize(new Dimension(25, 25));\r
+       up.setMargin(new Insets(2, 2, 2, 2));\r
+\r
+       down = new JButton("v");\r
+       down.setFont(new Font("Times", Font.PLAIN, 8));\r
+       down.setSize(25, 25);\r
+       down.setMaximumSize(new Dimension(25, 25));\r
+       down.setMinimumSize(new Dimension(25, 25));\r
+       down.setPreferredSize(new Dimension(25, 25));\r
+       down.setMargin(new Insets(2, 2, 2, 2));\r
+\r
+       this.label = new JLabel(" " + label, JLabel.LEFT);\r
+       this.label.setSize(125, 25);\r
+       this.label.setMaximumSize(new Dimension(125, 25));\r
+       this.label.setMinimumSize(new Dimension(125, 25));\r
+       this.label.setPreferredSize(new Dimension(125, 25));\r
+       switch (type) {\r
+       case MOTE:\r
+           this.label.setBackground(new Color(255, 200, 200));\r
+           break;\r
+       case FIELD:\r
+           this.label.setBackground(new Color(200, 255, 200));\r
+           break;\r
+       case LINK:\r
+           this.label.setBackground(new Color(200, 200, 255));\r
+           break;\r
+       default:\r
+           // do nothing\r
+       }\r
+       \r
+       displays = new JComboBox(DISPLAYS[type]);\r
+       displays.setSize(100, 25);\r
+       //displays.setMaximumSize(new Dimension(125, 25));\r
+       displays.setMinimumSize(new Dimension(125, 25));\r
+       displays.setPreferredSize(new Dimension(125, 25));\r
+       \r
+       \r
+       check.addActionListener(this);\r
+       up.addActionListener(this);\r
+       down.addActionListener(this);\r
+       displays.addActionListener(this);\r
+\r
+       layout.putConstraint(SpringLayout.WEST, this, 0, SpringLayout.WEST, down);\r
+       layout.putConstraint(SpringLayout.EAST, check, 0, SpringLayout.WEST, down);\r
+       layout.putConstraint(SpringLayout.EAST, down, 0, SpringLayout.WEST, up);\r
+       layout.putConstraint(SpringLayout.EAST, up, 0, SpringLayout.WEST, this.label);\r
+       layout.putConstraint(SpringLayout.EAST, this.label, 0, SpringLayout.WEST, displays);\r
+       layout.putConstraint(SpringLayout.EAST, displays, 0, SpringLayout.EAST, this);\r
+\r
+       \r
+       add(check);\r
+       add(down);\r
+       add(up);\r
+       add(this.label);\r
+       add(displays);\r
+\r
+       \r
+               \r
+    }\r
+       \r
+    public boolean isFieldSelected(){\r
+       return (type==FIELD && check.isSelected());\r
+    }\r
+       \r
+    public void actionPerformed(ActionEvent e) {\r
+       if (e.getSource() == check) {\r
+           if (check.isSelected()){\r
+               parent.selectedFieldIndex = index;\r
+               //repaintLayer(g);\r
+               //System.out.println("redraw index " +zIndex +" on layer");\r
+           } else if(type==FIELD){\r
+               //System.out.println("clear");\r
+               //parent.canvas.repaint();\r
+               //repaintLayer(g);\r
+           } else {\r
+               //repaintLayer(g);\r
+           }\r
+       } else if (e.getSource() == up){\r
+           parent.navigator.moveLayerUp(this.zIndex);\r
+       } else if (e.getSource() == down){\r
+           parent.navigator.moveLayerDown(this.zIndex);\r
+       } else if (e.getSource() == displays){\r
+           String selected = (String)displays.getSelectedItem();\r
+           if (selected.equals("circle")){\r
+               paintMode = OVAL;\r
+           } else if (selected.equals("img")){\r
+               paintMode = IMG;                        \r
+           } else if (selected.equals("txt")){\r
+               paintMode = TXT_MOTE;                   \r
+           } else if (selected.equals("color 256")) {\r
+               paintMode = COLOR_256;\r
+           } else if (selected.equals("color 1024")) {\r
+               paintMode = COLOR_1024;\r
+           } else if (selected.equals("color 4096")) {\r
+               paintMode = COLOR_4096;\r
+           } else if (selected.equals("color 16384")) {\r
+               paintMode = COLOR_16384;\r
+           } else if (selected.equals("line")) {\r
+               paintMode = LINE;\r
+           } else if (selected.equals("label")) {\r
+               paintMode = LABEL;\r
+           } else if (selected.equals("line+label")) {\r
+               paintMode = LINE_LABEL;\r
+           }\r
+       }\r
+       //System.out.println("Repainting parent?");\r
+       //parent.repaint();\r
+    }\r
+\r
+    public void init(){\r
+       if (type==LINK){\r
+           //addLinks(true);\r
+       } else {\r
+           addMotes(true);\r
+       }\r
+    }\r
+\r
+    public String toString() {\r
+       return "Layer " +  name + " " + type;\r
+    }\r
+    \r
+       \r
+    // private void addLinks(boolean paint){\r
+    //                 Iterator it = models.iterator();\r
+    //                 while(it.hasNext()){\r
+    //                         DLink mm = (DLink) it.next();\r
+    //                         //canvas.add(mm);\r
+    //                         if (paint) mm.repaint();\r
+    //                 }       \r
+    //     }\r
+       \r
+    protected void addMote(DMoteModel model, boolean paint){\r
+       DShape mote = new DMote(model, this.parent, this);\r
+       layer.add(mote);\r
+    }\r
+       \r
+    private void addMotes(boolean paint){\r
+       Iterator it = models.iterator();\r
+        while(it.hasNext()){\r
+           addMote((DMoteModel) it.next(), paint);\r
+       }           \r
+    }\r
+    \r
+       \r
+    public void updateIndex(int index, boolean repaint){\r
+       zIndex = index;\r
+       z_index = (navigator.totalLayers - zIndex)*100;\r
+       //if (repaint) redrawLayer();\r
+       //parent.canvas.setLayer(d.canvas, length - i);\r
+    }\r
+\r
+    public void paintScreenBefore(Graphics g) \r
+    {\r
+\r
+        Dimension d = parent.canvas.getSize();\r
+        int x = 0;\r
+        int y = 0;\r
+        int xstep = (int)(d.width / 40);\r
+       int ystep = (int)(d.height / 40);  \r
+\r
+        for(;x < d.width; x += xstep){\r
+            for(y = 0;y < d.height; y += ystep){\r
+                double val = 0;\r
+                double sum = 0;\r
+                double total = 0;\r
+                double min = 10000000;\r
+                Iterator it = models.iterator();\r
+                while(it.hasNext()){\r
+                    DMoteModel m = (DMoteModel) it.next();\r
+                    double dist = distance(x, y, m.x, m.y);   \r
+                    if(true){ //121\r
+                        if(dist < min) min = dist;\r
+                        val += ((double)(((int)m.getValue(index)) >> paintMode ))  / dist /dist;\r
+                        sum += (1/dist/dist);\r
+                    }\r
+                }\r
+                int reading = (int)(val / sum);\r
+               //System.out.println("Reading: " + reading);\r
+                if (reading > 255)\r
+                    reading = 255;\r
+                g.setColor(new Color(reading, reading, reading));\r
+               //System.out.println("Filling "  + x + "+" + step + " " + y + "+" + step + " with " + g.getColor());\r
+                g.fillRect(x, y, xstep, ystep);\r
+            }\r
+        }\r
+\r
+       \r
+    }\r
+\r
+    public double distance(int x, int y, int x1, int y1){\r
+        return Math.sqrt( (x-x1)*(x-x1)+(y-y1)*(y-y1));\r
+    }\r
+\r
+    protected void repaintLayer(Graphics g){\r
+       if (check.isSelected()){\r
+           //System.out.println("Repaint layer " + name);\r
+           if  (type==FIELD){\r
+               paintScreenBefore(g);\r
+           } else if (type == LINK) {\r
+               Iterator it = models.iterator();\r
+               //System.out.print("Draw links: ");\r
+               while (it.hasNext()) {\r
+                   DLinkModel model = (DLinkModel)it.next();\r
+                   DLink lnk = new DLink(model, parent, this);\r
+                   lnk.paintShape(g);\r
+                   //System.out.print("+");\r
+               }\r
+               //System.out.println();\r
+           }\r
+           else if (type == MOTE) {\r
+               Iterator it = models.iterator();\r
+               //System.out.print("Draw motes: ");\r
+               while (it.hasNext()){\r
+                   DMoteModel model = (DMoteModel)it.next();\r
+                   DShape m = new DMote(model, parent, this);\r
+                   m.paintShape(g);\r
+                   //System.out.print("+");\r
+               }\r
+               //System.out.println();\r
+           }\r
+       }       \r
+    }\r
+}\r