]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/g2d/nodes/BranchPointNode.java
G2DParentNode handles "undefined" child bounds separately
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / g2d / nodes / BranchPointNode.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.scenegraph.g2d.nodes;
13
14 import java.awt.BasicStroke;
15 import java.awt.Color;
16 import java.awt.Graphics2D;
17 import java.awt.Shape;
18 import java.awt.Stroke;
19 import java.awt.geom.AffineTransform;
20 import java.awt.geom.Ellipse2D;
21 import java.awt.geom.Line2D;
22 import java.awt.geom.Rectangle2D;
23
24 import org.simantics.scenegraph.g2d.G2DNode;
25 import org.simantics.scenegraph.utils.NodeUtil;
26
27 public class BranchPointNode extends G2DNode {
28     private static final long serialVersionUID = -5838113741617205901L;
29
30 //    public static final Shape SHAPE = new Ellipse2D.Double(-.5, -.5, 1, 1);
31 //    public static final Shape SHAPE2 = new Rectangle2D.Double(-.3, -.3, .6, .6);
32     public static final Shape SHAPE = new Ellipse2D.Double(-.5, -.5, 1, 1);
33     public static final Shape SHAPE2 = new Rectangle2D.Double(-.5, -.5, 1, 1);
34
35     public static final Stroke STROKE = new BasicStroke(0.1f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
36
37     Integer degree = 0;
38     // 0 == any, 1 == horizontal, 2 == vertical
39     Byte direction = 0;
40
41     @SyncField("degree")
42     public void setDegree(int degree) {
43         this.degree = degree;
44     }
45
46     @SyncField("direction")
47     public void setDirection(byte direction) {
48         this.direction = direction;
49     }
50
51     @Override
52     public void render(Graphics2D g) {
53         AffineTransform oldTransform = g.getTransform();
54         if (transform != null)
55             g.transform(transform);
56
57         try {
58             renderBranchpoint(g);
59         } finally {
60             g.setTransform(oldTransform);
61         }
62     }
63
64     private void renderBranchpoint(Graphics2D g) {
65         boolean selected = NodeUtil.isSelected(this, 2);
66         if (degree <= 2) {
67             if (selected) {
68                 g.setPaint(Color.GREEN);
69                 g.fill(SHAPE2);
70                 g.setPaint(Color.BLACK);
71                 g.setStroke(STROKE);
72                 g.draw(SHAPE2);
73             } else {
74                 //g.setPaint(new Color(128,128,128,64));
75                 g.setPaint(Color.LIGHT_GRAY);
76                 g.fill(SHAPE2);
77             }
78         } else {
79             if (selected) {
80                 g.setPaint(Color.GREEN);
81                 //g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
82                 g.fill(SHAPE);
83                 g.setPaint(Color.BLACK);
84                 g.setStroke(STROKE);
85                 g.draw(SHAPE);
86             } else {
87                 g.setPaint(Color.BLACK);
88                 //g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
89                 g.fill(SHAPE);
90             }
91         }
92
93         if (direction != 0) {
94             g.setStroke(STROKE);
95             g.setColor(degree <= 2 ? Color.BLACK : (selected ? Color.BLACK : Color.GREEN));
96             if (direction == 1) {
97                 // Horizontal
98                 g.draw(new Line2D.Double(-.5, 0, .5, 0));
99             } else if (direction == 2) {
100                 // Vertical
101                 g.draw(new Line2D.Double(0, -.5, 0, .5));
102             }
103         }
104     }
105
106     @Override
107     public String toString() {
108         return super.toString() + " [degree=" + degree + "]";
109     }
110
111     @Override
112     public Rectangle2D getBoundsInLocal() {
113         Shape shp = SHAPE;
114         if (degree <= 2) {
115             shp = SHAPE2;
116         }
117         return shp.getBounds2D();
118     }
119 }