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