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 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;
31 * MapPainter is an ICanvasContext participant that uses the scene graph
32 * {@link MapNode} to draw tiled maps in the background of the canvas.
38 public class MapPainter extends AbstractCanvasParticipant {
41 * Grid enabled status. Default value is True
43 public static final Key KEY_MAP_ENABLED = new KeyOf(Boolean.class);
45 public static final double ZOOM_IN_LIMIT = 10000000.0;
47 public static final double ZOOM_OUT_LIMIT = 10.0;
49 IHintListener mapListener = new HintListenerAdapter() {
50 public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
51 ICanvasContext cc = getContext();
54 cc.getContentContext().setDirty();
59 protected MapNode node = null;
63 public MapPainter(int scale) {
68 public void addedToContext(ICanvasContext ctx) {
69 super.addedToContext(ctx);
70 getHintStack().addKeyHintListener(getThread(), KEY_MAP_ENABLED, mapListener);
74 public void removedFromContext(ICanvasContext ctx) {
75 getHintStack().removeKeyHintListener(getThread(), KEY_MAP_ENABLED, mapListener);
76 super.removedFromContext(ctx);
79 @EventHandler(priority = 0)
80 public boolean handleKeyEvent(CommandEvent e) {
81 if (e.command.equals( Commands.MAP_ENABLE )) {
86 } else if (e.command.equals( Commands.MAP_DISABLE )) {
91 } else if (e.command.equals( Commands.MAP_TOGGLE )) {
92 setEnabled(!isMapEnabled());
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
109 public void cleanupSG() {
113 protected void updateNode() {
114 node.setEnabled(isPaintingEnabled());
117 boolean isPaintingEnabled() {
118 boolean enabled = isMapEnabled();
119 Boolean globalDisable = getHint(Hints.KEY_DISABLE_PAINTING);
120 return enabled && !Boolean.TRUE.equals(globalDisable);
123 public boolean isMapEnabled() {
124 Boolean enabled = getHint(KEY_MAP_ENABLED);
125 return !Boolean.FALSE.equals(enabled);
128 public void setEnabled(boolean enabled) {
129 setHint(KEY_MAP_ENABLED, enabled);