]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/participant/SubCanvas.java
Fixed invalid comparisons which were identified by Eclipse IDE 2018-09
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / participant / SubCanvas.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 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.g2d.participant;
13
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;
22
23 /**
24  * Subcanvas encapsulates a canvas (another) in one participant.
25  * 
26  * 
27  * @author Toni Kalajainen
28  */
29 public class SubCanvas extends AbstractCanvasParticipant {
30
31         @Dependency TransformUtil util;
32
33         private boolean enabled = true;
34         private ICanvasContext superCtx = null;
35         
36         /** Sub-Canvas */
37         private ICanvasContext sub;
38         int eventPriority, paintPriority, hintPriority;
39         //IHintContext subHintStack;
40         IMouseCursorContext origSubCursorCtx;
41         IMouseCaptureContext origSubCaptureCtx;
42         
43         // Subcanvas has become dirty, make super canvas also dirty
44         IContentListener paintableCtxListener = new IContentListener() {
45
46                 @Override
47                 public void onDirty(IContentContext sender) {
48                         if (superCtx==null) return;
49                         superCtx.getContentContext().setDirty();
50                 }
51         };
52         
53         public SubCanvas(ICanvasContext subCanvas, int eventPriority, int paintPriority, int hintPriority) {
54                 assert(subCanvas!=null);
55                 this.sub = subCanvas;
56                 this.eventPriority = eventPriority;
57                 this.paintPriority = paintPriority;
58                 this.hintPriority = hintPriority;
59                 //subHintStack = sub.getHintStack().createStackRead(sub.getDefaultHintContext());
60                 
61 //              subCanvas.getMouseCaptureContext().addMouseCaptureListener(new IMouseCaptureListener() {
62 //                      @Override
63 //                      public void onMouseCaptured(IMouseCaptureContext sender, int mouseId) {
64 //                              if (superCtx==null) return;
65 //                              superCtx.getMouseCaptureContext().
66 //                      }
67 //                      @Override
68 //                      public void onMouseReleased(IMouseCaptureContext sender, int mouseId) {
69 //                      }});
70         }
71         
72         @Override
73         public void addedToContext(ICanvasContext ctx) {
74                 super.addedToContext(ctx);
75                 superCtx = ctx;
76                 if (enabled) enable();
77         }
78         
79         @Override
80         public void removedFromContext(ICanvasContext ctx) {
81                 assert(superCtx==ctx);
82                 if (enabled) disable();
83                 superCtx = null;
84                 super.removedFromContext(ctx);
85         }
86         
87         public void setEnabled(boolean enabled)
88         {
89                 if (this.enabled == enabled) return;
90                 if (enabled) enable(); else disable();
91         }
92         
93         void enable() {
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);
104                 this.enabled = true;
105         }
106         
107         void disable() {
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;
116         }
117
118 }