]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.maps/src/org/simantics/maps/eclipse/MapPainter.java
853982214d933a49cf60c2348215c06bb5b5dc0b
[simantics/district.git] / org.simantics.district.maps / src / org / simantics / maps / eclipse / MapPainter.java
1 /*******************************************************************************
2  * Copyright (c) 2012 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.maps.eclipse;
13
14 import java.awt.Color;
15 import java.awt.geom.AffineTransform;
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.maps.sg.MapNode;
23 import org.simantics.maps.sg.MapScaleNode;
24 import org.simantics.maps.sg.commands.MapCommands;
25 import org.simantics.scenegraph.g2d.G2DParentNode;
26 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
27 import org.simantics.scenegraph.g2d.events.command.CommandEvent;
28 import org.simantics.scenegraph.g2d.events.command.Commands;
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  * MapPainter is an ICanvasContext participant that uses the scene graph
37  * {@link MapNode} to draw tiled maps in the background of the canvas.
38  * 
39  * @author J-P Laine
40  * 
41  * @see MapNode
42  */
43 public class MapPainter extends AbstractCanvasParticipant {
44
45     /**
46      * Grid enabled status. Default value is True
47      */
48     public static final Key KEY_MAP_ENABLED  = new KeyOf(Boolean.class);
49
50     public static final Key KEY_MAP_BACKGROUND_COLOR  = new KeyOf(Object.class);
51
52     public static final double ZOOM_IN_LIMIT = 10000000.0;
53
54     public static final double ZOOM_OUT_LIMIT = 10.0;
55
56     IHintListener mapListener = new HintListenerAdapter() {
57         public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
58             ICanvasContext cc = getContext();
59             if (cc != null) {
60                 updateNode();
61                 cc.getContentContext().setDirty();
62             }
63         }
64     };
65
66     protected MapNode node = null;
67     protected MapScaleNode scaleNode = null;
68
69     private AffineTransform transform;
70
71     public MapPainter(AffineTransform transform) {
72         this.transform = transform;
73     }
74
75     @Override
76     public void addedToContext(ICanvasContext ctx) {
77         super.addedToContext(ctx);
78         getHintStack().addKeyHintListener(getThread(), KEY_MAP_ENABLED, mapListener);
79         getHintStack().addKeyHintListener(getThread(), KEY_MAP_BACKGROUND_COLOR, mapListener);
80     }
81
82     @Override
83     public void removedFromContext(ICanvasContext ctx) {
84         getHintStack().removeKeyHintListener(getThread(), KEY_MAP_ENABLED, mapListener);
85         getHintStack().removeKeyHintListener(getThread(), KEY_MAP_BACKGROUND_COLOR, mapListener);
86         super.removedFromContext(ctx);
87     }
88
89     @EventHandler(priority = 0)
90     public boolean handleKeyEvent(CommandEvent e) {
91         if (e.command.equals( Commands.MAP_ENABLE )) {
92             setEnabled(true);
93             updateNode();
94             setDirty();
95             return true;
96         } else if (e.command.equals( MapCommands.MAP_BACKGROUND_COLOR_CHANGE )) {
97             ICanvasContext context = (ICanvasContext) e.getContext();
98             Color s = context.getHintStack().getHint(MapCommands.KEY_MAP_BACKGROUND_COLOR);
99             setBackgroundColor(s);
100             setDirty();
101             return true;
102         } else if (e.command.equals( Commands.MAP_DISABLE )) {
103             setEnabled(false);
104             updateNode();
105             setDirty();
106             return true;
107         } else if (e.command.equals( Commands.MAP_TOGGLE )) {
108             setEnabled(!isMapEnabled());
109             updateNode();
110             setDirty();
111             return true;
112         } else if (e.command.equals( Commands.ENABLE_PAINTING )) {
113             enablePainting();
114             updateNode();
115             setDirty();
116             return true;
117         }
118         return false;
119     }
120
121     @SGInit
122     public void initSG(G2DParentNode parent) {
123         node = parent.addNode("map", MapNode.class);
124         node.setTransform(transform);
125         node.setEnabled(true);
126         node.setZIndex(Integer.MIN_VALUE + 999); // Just under the grid
127         
128         scaleNode = parent.addNode("mapScale", MapScaleNode.class);
129         scaleNode.setTransform(transform);
130         scaleNode.setEnabled(true);
131         scaleNode.setZIndex(Integer.MAX_VALUE - 999); // Just under the grid
132     }
133
134     @SGCleanup
135     public void cleanupSG() {
136         node.remove();
137     }
138
139     protected void updateNode() {
140         node.setEnabled(isPaintingEnabled());
141         node.setBackgroundColor(getBackgroundColor());
142     }
143
144     boolean isPaintingEnabled() {
145         boolean enabled = isMapEnabled();
146         Boolean globalDisable = getHint(Hints.KEY_DISABLE_PAINTING);
147         return enabled && !Boolean.TRUE.equals(globalDisable);
148     }
149
150     public boolean isMapEnabled() {
151         Boolean enabled = getHint(KEY_MAP_ENABLED);
152         return !Boolean.FALSE.equals(enabled);
153     }
154
155     public void setEnabled(boolean enabled) {
156         setHint(KEY_MAP_ENABLED, enabled);
157     }
158
159     private void enablePainting() {
160         setHint(Hints.KEY_DISABLE_PAINTING, false);
161     }
162     
163     private void setBackgroundColor(Color backgroundColor) {
164         setHint(KEY_MAP_BACKGROUND_COLOR, backgroundColor);
165     }
166     
167     private Color getBackgroundColor() {
168         return getHint(KEY_MAP_BACKGROUND_COLOR);
169     }
170 }