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