1 /*******************************************************************************
2 * Copyright (c) 2012 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.maps.eclipse;
14 import java.awt.Color;
15 import java.awt.geom.AffineTransform;
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;
36 * MapPainter is an ICanvasContext participant that uses the scene graph
37 * {@link MapNode} to draw tiled maps in the background of the canvas.
43 public class MapPainter extends AbstractCanvasParticipant {
46 * Grid enabled status. Default value is True
48 public static final Key KEY_MAP_ENABLED = new KeyOf(Boolean.class);
50 public static final Key KEY_MAP_BACKGROUND_COLOR = new KeyOf(Object.class);
52 public static final double ZOOM_IN_LIMIT = 10000000.0;
54 public static final double ZOOM_OUT_LIMIT = 10.0;
56 IHintListener mapListener = new HintListenerAdapter() {
57 public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
58 ICanvasContext cc = getContext();
61 cc.getContentContext().setDirty();
66 protected MapNode node = null;
67 protected MapScaleNode scaleNode = null;
69 private AffineTransform transform;
71 public MapPainter(AffineTransform transform) {
72 this.transform = transform;
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);
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);
89 @EventHandler(priority = 0)
90 public boolean handleKeyEvent(CommandEvent e) {
91 if (e.command.equals( Commands.MAP_ENABLE )) {
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);
102 } else if (e.command.equals( Commands.MAP_DISABLE )) {
107 } else if (e.command.equals( Commands.MAP_TOGGLE )) {
108 setEnabled(!isMapEnabled());
112 } else if (e.command.equals( Commands.ENABLE_PAINTING )) {
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
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
135 public void cleanupSG() {
139 protected void updateNode() {
140 node.setEnabled(isPaintingEnabled());
141 node.setBackgroundColor(getBackgroundColor());
144 boolean isPaintingEnabled() {
145 boolean enabled = isMapEnabled();
146 Boolean globalDisable = getHint(Hints.KEY_DISABLE_PAINTING);
147 return enabled && !Boolean.TRUE.equals(globalDisable);
150 public boolean isMapEnabled() {
151 Boolean enabled = getHint(KEY_MAP_ENABLED);
152 return !Boolean.FALSE.equals(enabled);
155 public void setEnabled(boolean enabled) {
156 setHint(KEY_MAP_ENABLED, enabled);
159 private void enablePainting() {
160 setHint(Hints.KEY_DISABLE_PAINTING, false);
163 private void setBackgroundColor(Color backgroundColor) {
164 setHint(KEY_MAP_BACKGROUND_COLOR, backgroundColor);
167 private Color getBackgroundColor() {
168 return getHint(KEY_MAP_BACKGROUND_COLOR);