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.geom.AffineTransform;
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;
34 * MapPainter is an ICanvasContext participant that uses the scene graph
35 * {@link MapNode} to draw tiled maps in the background of the canvas.
41 public class MapPainter extends AbstractCanvasParticipant {
44 * Grid enabled status. Default value is True
46 public static final Key KEY_MAP_ENABLED = new KeyOf(Boolean.class);
48 public static final double ZOOM_IN_LIMIT = 10000000.0;
50 public static final double ZOOM_OUT_LIMIT = 10.0;
52 IHintListener mapListener = new HintListenerAdapter() {
53 public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
54 ICanvasContext cc = getContext();
57 cc.getContentContext().setDirty();
62 protected MapNode node = null;
63 protected MapScaleNode scaleNode = null;
65 private AffineTransform transform;
67 public MapPainter(AffineTransform transform) {
68 this.transform = transform;
72 public void addedToContext(ICanvasContext ctx) {
73 super.addedToContext(ctx);
74 getHintStack().addKeyHintListener(getThread(), KEY_MAP_ENABLED, mapListener);
78 public void removedFromContext(ICanvasContext ctx) {
79 getHintStack().removeKeyHintListener(getThread(), KEY_MAP_ENABLED, mapListener);
80 super.removedFromContext(ctx);
83 @EventHandler(priority = 0)
84 public boolean handleKeyEvent(CommandEvent e) {
85 if (e.command.equals( Commands.MAP_ENABLE )) {
90 } else if (e.command.equals( Commands.MAP_DISABLE )) {
95 } else if (e.command.equals( Commands.MAP_TOGGLE )) {
96 setEnabled(!isMapEnabled());
105 public void initSG(G2DParentNode parent) {
106 node = parent.addNode("map", MapNode.class);
107 node.setTransform(transform);
108 node.setEnabled(true);
109 node.setZIndex(Integer.MIN_VALUE + 999); // Just under the grid
111 scaleNode = parent.addNode("mapScale", MapScaleNode.class);
112 scaleNode.setTransform(transform);
113 scaleNode.setEnabled(true);
114 scaleNode.setZIndex(Integer.MAX_VALUE - 999); // Just under the grid
118 public void cleanupSG() {
122 protected void updateNode() {
123 node.setEnabled(isPaintingEnabled());
126 boolean isPaintingEnabled() {
127 boolean enabled = isMapEnabled();
128 Boolean globalDisable = getHint(Hints.KEY_DISABLE_PAINTING);
129 return enabled && !Boolean.TRUE.equals(globalDisable);
132 public boolean isMapEnabled() {
133 Boolean enabled = getHint(KEY_MAP_ENABLED);
134 return !Boolean.FALSE.equals(enabled);
137 public void setEnabled(boolean enabled) {
138 setHint(KEY_MAP_ENABLED, enabled);