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