]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/participant/GridPainter.java
Testing SonarQube with Simantics Platform SDK
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / participant / GridPainter.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g2d.participant;\r
13 \r
14 import java.awt.BasicStroke;\r
15 import java.awt.Color;\r
16 \r
17 import org.simantics.g2d.canvas.Hints;\r
18 import org.simantics.g2d.canvas.ICanvasContext;\r
19 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;\r
20 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGCleanup;\r
21 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;\r
22 import org.simantics.g2d.diagram.DiagramHints;\r
23 import org.simantics.scenegraph.g2d.G2DParentNode;\r
24 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
25 import org.simantics.scenegraph.g2d.events.command.CommandEvent;\r
26 import org.simantics.scenegraph.g2d.events.command.Commands;\r
27 import org.simantics.scenegraph.g2d.nodes.GridNode;\r
28 import org.simantics.scenegraph.g2d.snap.ISnapAdvisor;\r
29 import org.simantics.utils.datastructures.hints.HintListenerAdapter;\r
30 import org.simantics.utils.datastructures.hints.IHintListener;\r
31 import org.simantics.utils.datastructures.hints.IHintObservable;\r
32 import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
33 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
34 \r
35 \r
36 /**\r
37  * GridPainter draws grid lines through the scene graph.\r
38  *\r
39  * This interactor has no dependencies to other participants.\r
40  *\r
41  * @author Toni Kalajainen\r
42  * \r
43  * @see GridNode\r
44  */\r
45 public class GridPainter extends AbstractCanvasParticipant {\r
46 \r
47     /**\r
48      * Grid enabled status. Default value is True\r
49      */\r
50     public static final Key         KEY_GRID_ENABLED  = new KeyOf(Boolean.class, "GRID_ENABLED");\r
51 \r
52     /**\r
53      * Specifies the absolute diagram grid size, which is also the snapping grid\r
54      * size. This painter will use {@link GridNode} to draw the grid in a\r
55      * resolution that is no less than this value. The value is expected to be\r
56      * defined in millimeters which is the standard diagram unit.\r
57      */\r
58     public static final Key         KEY_GRID_SIZE     = new KeyOf(Double.class, "GRID_SIZE");\r
59 \r
60     private static final double     DEFAULT_GRID_SIZE = 1.0;\r
61 \r
62     public static final BasicStroke GRID_LINE_STROKE =\r
63         new BasicStroke(0.1f,\r
64                 BasicStroke.CAP_SQUARE,\r
65                 BasicStroke.CAP_SQUARE,\r
66                 10.0f, null, 0.0f);\r
67 \r
68     IHintListener gridListener = new HintListenerAdapter() {\r
69         public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {\r
70             ICanvasContext cc = getContext();\r
71             if (cc != null) {\r
72                 updateNode();\r
73                 cc.getContentContext().setDirty();\r
74             }\r
75         }\r
76     };\r
77 \r
78     @Override\r
79     public void addedToContext(ICanvasContext ctx) {\r
80         super.addedToContext(ctx);\r
81         getHintStack().addKeyHintListener(getThread(), KEY_GRID_ENABLED, gridListener);\r
82         getHintStack().addKeyHintListener(getThread(), KEY_GRID_SIZE, gridListener);\r
83         getHintStack().addKeyHintListener(getThread(), Hints.KEY_GRID_COLOR, gridListener);\r
84         getHintStack().addKeyHintListener(getThread(), Hints.KEY_DISABLE_PAINTING, gridListener);\r
85         getHintStack().addKeyHintListener(getThread(), DiagramHints.SNAP_ADVISOR, gridListener);\r
86     }\r
87 \r
88     @Override\r
89     public void removedFromContext(ICanvasContext ctx) {\r
90         getHintStack().removeKeyHintListener(getThread(), KEY_GRID_ENABLED, gridListener);\r
91         getHintStack().removeKeyHintListener(getThread(), KEY_GRID_SIZE, gridListener);\r
92         getHintStack().removeKeyHintListener(getThread(), Hints.KEY_GRID_COLOR, gridListener);\r
93         getHintStack().removeKeyHintListener(getThread(), Hints.KEY_DISABLE_PAINTING, gridListener);\r
94         getHintStack().removeKeyHintListener(getThread(), DiagramHints.SNAP_ADVISOR, gridListener);\r
95         super.removedFromContext(ctx);\r
96     }\r
97 \r
98     @EventHandler(priority = 0)\r
99     public boolean handleKeyEvent(CommandEvent e) {\r
100         if (e.command.equals( Commands.GRID_ENABLE )) {\r
101             setEnabled(true);\r
102             updateNode();\r
103             setDirty();\r
104             return true;\r
105         } else if (e.command.equals( Commands.GRID_DISABLE )) {\r
106             setEnabled(false);\r
107             updateNode();\r
108             setDirty();\r
109             return true;\r
110         } else if (e.command.equals( Commands.GRID_TOGGLE )) {\r
111             setEnabled(!isGridEnabled());\r
112             updateNode();\r
113             setDirty();\r
114             return true;\r
115         }\r
116         return false;\r
117     }\r
118 \r
119     protected GridNode node = null;\r
120 \r
121     @SGInit\r
122     public void initSG(G2DParentNode parent) {\r
123         node = parent.addNode(GridNode.GRID_NODE_ID, GridNode.class);\r
124         node.setLookupId(GridNode.GRID_NODE_ID);\r
125         node.setZIndex(Integer.MIN_VALUE + 1000);\r
126         updateNode();\r
127     }\r
128 \r
129     @SGCleanup\r
130     public void cleanupSG() {\r
131         node.remove();\r
132     }\r
133 \r
134     protected void updateNode() {\r
135         node.setEnabled(isPaintingEnabled());\r
136         node.setGridColor(getGridColor());\r
137         node.setGridSize(getGridSize());\r
138         node.setSnapAdvisor( (ISnapAdvisor) getHint(DiagramHints.SNAP_ADVISOR) );\r
139     }\r
140 \r
141     boolean isPaintingEnabled()\r
142     {\r
143         boolean enabled = isGridEnabled();\r
144         Boolean globalDisable = getHint(Hints.KEY_DISABLE_PAINTING);\r
145         return enabled && !Boolean.TRUE.equals(globalDisable);\r
146     }\r
147 \r
148     public boolean isGridEnabled()\r
149     {\r
150         Boolean enabled = getHint(KEY_GRID_ENABLED);\r
151         return !Boolean.FALSE.equals(enabled);\r
152     }\r
153 \r
154     public void setEnabled(boolean enabled)\r
155     {\r
156         setHint(KEY_GRID_ENABLED, enabled);\r
157     }\r
158 \r
159     public Color getGridColor()\r
160     {\r
161         return getHint(Hints.KEY_GRID_COLOR);\r
162     }\r
163 \r
164     /**\r
165      * Convenience method\r
166      * @param r\r
167      * @param g\r
168      * @param b\r
169      */\r
170     public void setGridColor(int r, int g, int b)\r
171     {\r
172         setGridColor(new Color(r, g, b));\r
173     }\r
174 \r
175     /**\r
176      * Convenience method\r
177      * @param c\r
178      */\r
179     public void setGridColor(Color c)\r
180     {\r
181         setHint(Hints.KEY_GRID_COLOR, c);\r
182     }\r
183 \r
184     public double getGridSize()\r
185     {\r
186         Double d = getHint(KEY_GRID_SIZE);\r
187         return d == null ? DEFAULT_GRID_SIZE : d.doubleValue();\r
188     }\r
189 \r
190     public void setGridSize(double gridSize)\r
191     {\r
192         setHint(KEY_GRID_SIZE, gridSize);\r
193     }\r
194 \r
195 }\r