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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.g2d.participant;
\r
14 import java.awt.BasicStroke;
\r
15 import java.awt.Color;
\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
37 * GridPainter draws grid lines through the scene graph.
\r
39 * This interactor has no dependencies to other participants.
\r
41 * @author Toni Kalajainen
\r
45 public class GridPainter extends AbstractCanvasParticipant {
\r
48 * Grid enabled status. Default value is True
\r
50 public static final Key KEY_GRID_ENABLED = new KeyOf(Boolean.class, "GRID_ENABLED");
\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
58 public static final Key KEY_GRID_SIZE = new KeyOf(Double.class, "GRID_SIZE");
\r
60 private static final double DEFAULT_GRID_SIZE = 1.0;
\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
68 IHintListener gridListener = new HintListenerAdapter() {
\r
69 public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
\r
70 ICanvasContext cc = getContext();
\r
73 cc.getContentContext().setDirty();
\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
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
98 @EventHandler(priority = 0)
\r
99 public boolean handleKeyEvent(CommandEvent e) {
\r
100 if (e.command.equals( Commands.GRID_ENABLE )) {
\r
105 } else if (e.command.equals( Commands.GRID_DISABLE )) {
\r
110 } else if (e.command.equals( Commands.GRID_TOGGLE )) {
\r
111 setEnabled(!isGridEnabled());
\r
119 protected GridNode node = null;
\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
130 public void cleanupSG() {
\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
141 boolean isPaintingEnabled()
\r
143 boolean enabled = isGridEnabled();
\r
144 Boolean globalDisable = getHint(Hints.KEY_DISABLE_PAINTING);
\r
145 return enabled && !Boolean.TRUE.equals(globalDisable);
\r
148 public boolean isGridEnabled()
\r
150 Boolean enabled = getHint(KEY_GRID_ENABLED);
\r
151 return !Boolean.FALSE.equals(enabled);
\r
154 public void setEnabled(boolean enabled)
\r
156 setHint(KEY_GRID_ENABLED, enabled);
\r
159 public Color getGridColor()
\r
161 return getHint(Hints.KEY_GRID_COLOR);
\r
165 * Convenience method
\r
170 public void setGridColor(int r, int g, int b)
\r
172 setGridColor(new Color(r, g, b));
\r
176 * Convenience method
\r
179 public void setGridColor(Color c)
\r
181 setHint(Hints.KEY_GRID_COLOR, c);
\r
184 public double getGridSize()
\r
186 Double d = getHint(KEY_GRID_SIZE);
\r
187 return d == null ? DEFAULT_GRID_SIZE : d.doubleValue();
\r
190 public void setGridSize(double gridSize)
\r
192 setHint(KEY_GRID_SIZE, gridSize);
\r