]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/participant/OrientationRestorer.java
Replace OrientationRestorer TimerTask usage with ScheduledExecutor
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / participant / OrientationRestorer.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 java.awt.geom.Point2D;
15 import java.awt.geom.Rectangle2D;
16 import java.util.concurrent.Executors;
17 import java.util.concurrent.ScheduledExecutorService;
18 import java.util.concurrent.TimeUnit;
19
20 import org.simantics.g2d.canvas.Hints;
21 import org.simantics.g2d.canvas.ICanvasContext;
22 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
23 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
24 import org.simantics.g2d.canvas.impl.HintReflection.HintListener;
25 import org.simantics.utils.datastructures.disposable.DisposeState;
26 import org.simantics.utils.datastructures.hints.IHintContext.Key;
27 import org.simantics.utils.datastructures.hints.IHintObservable;
28 import org.simantics.utils.threads.IThreadWorkQueue;
29
30 /**
31  * Restores orientation of the canvas back to normal (north is up)
32  * 
33  * 
34  * @author Toni Kalajainen
35  */
36 public class OrientationRestorer extends AbstractCanvasParticipant {
37
38     @Dependency
39     TransformUtil           util;
40     @Dependency
41     CanvasGrab              grab;
42     @Dependency
43     CanvasBoundsParticipant bounds;
44
45     long                    lastTrigger;
46     Point2D                 centerPoint        = new Point2D.Double();
47
48     transient ScheduledExecutorService         timer              = Executors.newSingleThreadScheduledExecutor();
49     transient boolean       checkRotatePending = false;
50
51     Runnable               task               = new Runnable() {
52         @Override
53         public void run() {
54             ICanvasContext ctx = getContext();
55             if (ctx.getDisposeState() != DisposeState.Alive)
56                 return;
57             IThreadWorkQueue thread = ctx.getThreadAccess();
58             if (thread == null)
59                 return;
60             thread.asyncExec(new Runnable() {
61                 @Override
62                 public void run() {
63                     if (checkRotatePending)
64                         return;
65                     checkRotatePending = true;
66                     timerEvent();
67                 }
68             });
69         }
70     };
71
72     @Override
73     public void removedFromContext(ICanvasContext ctx) {
74         timer.shutdown();
75         try {
76             if (!timer.awaitTermination(1, TimeUnit.SECONDS)) {
77                 timer.shutdownNow();
78             }
79         } catch (InterruptedException e) {
80             // Ignore
81         }
82         super.removedFromContext(ctx);
83     }
84
85     @Override
86     public void addedToContext(ICanvasContext ctx) {
87         super.addedToContext(ctx);
88 //        long delay = 1000 / 25; this sounds quite frequent
89         long delay = 1000 / 10;
90         lastTrigger = System.currentTimeMillis();
91         timer.scheduleAtFixedRate(task, delay, delay, TimeUnit.MILLISECONDS);
92     }
93
94     @HintListener(Class = Hints.class, Field = "KEY_CANVAS_BOUNDS")
95     public void selectionChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) {
96         Rectangle2D cb = (Rectangle2D) newValue;
97         this.centerPoint.setLocation(cb.getCenterX(), cb.getCenterY());
98     }
99
100     void timerEvent() {
101         ICanvasContext ctx = getContext();
102         if (ctx == null)
103             return;
104         if (ctx.getDisposeState() != DisposeState.Alive)
105             return;
106         long cur = System.currentTimeMillis();
107         long delta = cur - lastTrigger;
108         lastTrigger = cur;
109         checkRotatePending = false;
110
111         Point2D centerPoint = new Point2D.Double();
112         int grabCount = grab.grabInfo.size();
113         if (grabCount == 0) {
114             centerPoint = this.centerPoint;
115         } else if (grabCount == 1) {
116             centerPoint = grab.grabInfo.values().iterator().next().anchorPos;
117         } else {
118             angVel = 0.0;
119             return;
120         }
121
122         // turn the canvas a bit
123         double theta = util.getRotate();
124         if (theta == 0.0)
125             return;
126 //        if (Math.abs(theta) < 0.0001) {
127 //            angVel = 0.0;
128 //            return;
129 //        }
130         if (Math.abs(theta) < 0.001) {
131             util.restoreOrientation(centerPoint);
132             angVel = 0.0;
133             return;
134         }
135
136         double momentum = theta * 100;
137         double dt = ((double) delta) / 1000;
138         if (dt > 0.150)
139             dt = 0.150;
140         angVel += momentum * dt;
141         util.rotate(centerPoint, -angVel * dt);
142         angVel *= 0.5;
143     }
144
145     double angVel = 0.0;
146
147 }