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