1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 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.g2d.participant;
14 import org.simantics.g2d.canvas.Hints;
15 import org.simantics.g2d.canvas.ICanvasContext;
16 import org.simantics.g2d.canvas.IMouseCaptureContext;
17 import org.simantics.g2d.canvas.IMouseCursorContext;
18 import org.simantics.g2d.canvas.IContentContext;
19 import org.simantics.g2d.canvas.IContentContext.IContentListener;
20 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
21 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
24 * Subcanvas encapsulates a canvas (another) in one participant.
27 * @author Toni Kalajainen
29 public class SubCanvas extends AbstractCanvasParticipant {
31 @Dependency TransformUtil util;
33 private boolean enabled = true;
34 private ICanvasContext superCtx = null;
37 private ICanvasContext sub;
38 int eventPriority, paintPriority, hintPriority;
39 //IHintContext subHintStack;
40 IMouseCursorContext origSubCursorCtx;
41 IMouseCaptureContext origSubCaptureCtx;
43 // Subcanvas has become dirty, make super canvas also dirty
44 IContentListener paintableCtxListener = new IContentListener() {
47 public void onDirty(IContentContext sender) {
48 if (superCtx==null) return;
49 superCtx.getContentContext().setDirty();
53 public SubCanvas(ICanvasContext subCanvas, int eventPriority, int paintPriority, int hintPriority) {
54 assert(subCanvas!=null);
56 this.eventPriority = eventPriority;
57 this.paintPriority = paintPriority;
58 this.hintPriority = hintPriority;
59 //subHintStack = sub.getHintStack().createStackRead(sub.getDefaultHintContext());
61 // subCanvas.getMouseCaptureContext().addMouseCaptureListener(new IMouseCaptureListener() {
63 // public void onMouseCaptured(IMouseCaptureContext sender, int mouseId) {
64 // if (superCtx==null) return;
65 // superCtx.getMouseCaptureContext().
68 // public void onMouseReleased(IMouseCaptureContext sender, int mouseId) {
73 public void addedToContext(ICanvasContext ctx) {
74 super.addedToContext(ctx);
76 if (enabled) enable();
80 public void removedFromContext(ICanvasContext ctx) {
81 assert(superCtx==ctx);
82 if (enabled) disable();
84 super.removedFromContext(ctx);
87 public void setEnabled(boolean enabled)
89 if (this.enabled == enabled) return;
90 if (enabled) enable(); else disable();
94 superCtx.getEventHandlerStack().add(sub.getEventHandlerStack(), eventPriority);
95 // superCtx.getPainterStack().add(sub.getPainterStack(), paintPriority);
96 //superCtx.getHintStack().addHintContext(subHintStack, hintPriority);
97 sub.getContentContext().addPaintableContextListener(paintableCtxListener);
98 origSubCursorCtx = sub.getMouseCursorContext();
99 sub.setMouseCursorContext(getContext().getMouseCursorContext());
100 origSubCaptureCtx = sub.getMouseCaptureContext();
101 sub.setMouseCaptureContext(getContext().getMouseCaptureContext());
102 assert(sub.getDefaultHintContext().getHint(Hints.KEY_SUPER_CANVAS)==null);
103 //sub.getDefaultHintContext().setHint(Hints.KEY_SUPER_CANVAS, ctx);
108 sub.getDefaultHintContext().removeHint(Hints.KEY_SUPER_CANVAS);
109 sub.setMouseCaptureContext(origSubCaptureCtx);
110 sub.setMouseCursorContext(origSubCursorCtx);
111 sub.getContentContext().removePaintableContextListener(paintableCtxListener);
112 //ctx.getHintStack().removeHintContext(subHintStack);
113 // superCtx.getPainterStack().remove(sub.getPainterStack());
114 superCtx.getEventHandlerStack().remove(sub.getEventHandlerStack());
115 this.enabled = false;