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