]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.debug.graphical/src/org/simantics/debug/graphical/model/Node.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.debug.graphical / src / org / simantics / debug / graphical / model / Node.java
1 package org.simantics.debug.graphical.model;\r
2 \r
3 import java.awt.Color;\r
4 import java.awt.Font;\r
5 import java.awt.Graphics2D;\r
6 import java.awt.font.FontRenderContext;\r
7 import java.awt.geom.AffineTransform;\r
8 import java.awt.geom.Point2D;\r
9 import java.awt.geom.Rectangle2D;\r
10 \r
11 public class Node {\r
12     public static final Font FONT = new Font("Arial", Font.PLAIN, 12);\r
13     public static final FontRenderContext FRC = \r
14             new FontRenderContext(new AffineTransform(), true, true);\r
15     public static final Rectangle2D MAX_BOUNDS = FONT.getMaxCharBounds(FRC);\r
16     public static final double FONT_HEIGHT = MAX_BOUNDS.getHeight();\r
17     \r
18     public static double PADDING = 3.0;\r
19     \r
20     NodeData data;       \r
21     Content content;\r
22     \r
23     double x;\r
24     double y;\r
25     \r
26     double radiusX;\r
27     double radiusY;\r
28     \r
29     // for layouter\r
30     public double forceX, forceY;\r
31     \r
32     public Node(NodeData data) {\r
33         this.data = data;\r
34     }\r
35 \r
36     public void setContent(Content content) {\r
37         this.content = content;\r
38         this.radiusX = content.radiusX;\r
39         this.radiusY = content.radiusY;\r
40     }\r
41     \r
42     public NodeData getData() {\r
43         return data;\r
44     }\r
45     \r
46     public void setPos(double x, double y) {\r
47         this.x = x;\r
48         this.y = y;\r
49     }\r
50     \r
51     public void render(Graphics2D g) {\r
52         Rectangle2D rect = new Rectangle2D.Double(x-radiusX, y-radiusY, 2*radiusX, 2*radiusY);\r
53         g.setColor(new Color(200, 200, 255));\r
54         g.fill(rect);\r
55         g.setColor(Color.BLACK);\r
56         \r
57         if(content != null)\r
58             content.render(g, x, y);\r
59         \r
60         g.draw(rect);\r
61     }\r
62     \r
63     public Point2D clipLineFromCenter(double targetX, double targetY) {\r
64         targetX -= x;\r
65         targetY -= y;\r
66         if(radiusX * Math.abs(targetY) < radiusY * Math.abs(targetX)) {\r
67             return new Point2D.Double(\r
68                     x+Math.signum(targetX)*radiusX, \r
69                     y+targetY * radiusX / Math.abs(targetX));\r
70         }\r
71         else {\r
72             return new Point2D.Double(                     \r
73                     x+targetX * radiusY / Math.abs(targetY),\r
74                     y+Math.signum(targetY)*radiusY);\r
75         }\r
76     }\r
77     \r
78     public boolean pick(double px, double py) {\r
79         return Math.abs(px - x) <= radiusX && Math.abs(py - y) <= radiusY; \r
80     }\r
81 \r
82     public double getX() {\r
83         return x;\r
84     }\r
85     \r
86     public double getY() {\r
87         return y;\r
88     }\r
89     \r
90     public double getMinX() {\r
91         return x - radiusX;\r
92     }\r
93     \r
94     public double getMinY() {\r
95         return y - radiusY;\r
96     }\r
97     \r
98     public double getMaxX() {\r
99         return x + radiusX;\r
100     }\r
101     \r
102     public double getMaxY() {\r
103         return y + radiusY;\r
104     }\r
105 }\r