]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.maps/src/org/simantics/maps/eclipse/MapPainter.java
Enabling/Disabling map painting
[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.geom.AffineTransform;
15
16 import org.simantics.g2d.canvas.Hints;
17 import org.simantics.g2d.canvas.ICanvasContext;
18 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
19 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGCleanup;
20 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;
21 import org.simantics.maps.sg.MapNode;
22 import org.simantics.maps.sg.MapScaleNode;
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.utils.datastructures.hints.HintListenerAdapter;
28 import org.simantics.utils.datastructures.hints.IHintListener;
29 import org.simantics.utils.datastructures.hints.IHintObservable;
30 import org.simantics.utils.datastructures.hints.IHintContext.Key;
31 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
32
33 /**
34  * MapPainter is an ICanvasContext participant that uses the scene graph
35  * {@link MapNode} to draw tiled maps in the background of the canvas.
36  * 
37  * @author J-P Laine
38  * 
39  * @see MapNode
40  */
41 public class MapPainter extends AbstractCanvasParticipant {
42
43     /**
44      * Grid enabled status. Default value is True
45      */
46     public static final Key KEY_MAP_ENABLED  = new KeyOf(Boolean.class);
47
48     public static final double ZOOM_IN_LIMIT = 10000000.0;
49
50     public static final double ZOOM_OUT_LIMIT = 10.0;
51
52     IHintListener mapListener = new HintListenerAdapter() {
53         public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
54             ICanvasContext cc = getContext();
55             if (cc != null) {
56                 updateNode();
57                 cc.getContentContext().setDirty();
58             }
59         }
60     };
61
62     protected MapNode node = null;
63     protected MapScaleNode scaleNode = null;
64
65     private AffineTransform transform;
66
67     public MapPainter(AffineTransform transform) {
68         this.transform = transform;
69     }
70
71     @Override
72     public void addedToContext(ICanvasContext ctx) {
73         super.addedToContext(ctx);
74         getHintStack().addKeyHintListener(getThread(), KEY_MAP_ENABLED, mapListener);
75     }
76
77     @Override
78     public void removedFromContext(ICanvasContext ctx) {
79         getHintStack().removeKeyHintListener(getThread(), KEY_MAP_ENABLED, mapListener);
80         super.removedFromContext(ctx);
81     }
82
83     @EventHandler(priority = 0)
84     public boolean handleKeyEvent(CommandEvent e) {
85         if (e.command.equals( Commands.MAP_ENABLE )) {
86             setEnabled(true);
87             updateNode();
88             setDirty();
89             return true;
90         } else if (e.command.equals( Commands.MAP_DISABLE )) {
91             setEnabled(false);
92             updateNode();
93             setDirty();
94             return true;
95         } else if (e.command.equals( Commands.MAP_TOGGLE )) {
96             setEnabled(!isMapEnabled());
97             updateNode();
98             setDirty();
99             return true;
100         } else if (e.command.equals( Commands.ENABLE_PAINTING )) {
101             enablePainting();
102             updateNode();
103             setDirty();
104             return true;
105         }
106         return false;
107     }
108
109     @SGInit
110     public void initSG(G2DParentNode parent) {
111         node = parent.addNode("map", MapNode.class);
112         node.setTransform(transform);
113         node.setEnabled(true);
114         node.setZIndex(Integer.MIN_VALUE + 999); // Just under the grid
115         
116         scaleNode = parent.addNode("mapScale", MapScaleNode.class);
117         scaleNode.setTransform(transform);
118         scaleNode.setEnabled(true);
119         scaleNode.setZIndex(Integer.MAX_VALUE - 999); // Just under the grid
120     }
121
122     @SGCleanup
123     public void cleanupSG() {
124         node.remove();
125     }
126
127     protected void updateNode() {
128         node.setEnabled(isPaintingEnabled());
129     }
130
131     boolean isPaintingEnabled() {
132         boolean enabled = isMapEnabled();
133         Boolean globalDisable = getHint(Hints.KEY_DISABLE_PAINTING);
134         return enabled && !Boolean.TRUE.equals(globalDisable);
135     }
136
137     public boolean isMapEnabled() {
138         Boolean enabled = getHint(KEY_MAP_ENABLED);
139         return !Boolean.FALSE.equals(enabled);
140     }
141
142     public void setEnabled(boolean enabled) {
143         setHint(KEY_MAP_ENABLED, enabled);
144     }
145
146     private void enablePainting() {
147         setHint(Hints.KEY_DISABLE_PAINTING, false);
148     }
149 }