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 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;
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;
31 * Restores orientation of the canvas back to normal (north is up)
34 * @author Toni Kalajainen
36 public class OrientationRestorer extends AbstractCanvasParticipant {
43 CanvasBoundsParticipant bounds;
46 Point2D centerPoint = new Point2D.Double();
48 transient ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor();
49 transient boolean checkRotatePending = false;
51 Runnable task = new Runnable() {
54 ICanvasContext ctx = getContext();
55 if (ctx.getDisposeState() != DisposeState.Alive)
57 IThreadWorkQueue thread = ctx.getThreadAccess();
60 thread.asyncExec(new Runnable() {
63 if (checkRotatePending)
65 checkRotatePending = true;
73 public void removedFromContext(ICanvasContext ctx) {
76 if (!timer.awaitTermination(1, TimeUnit.SECONDS)) {
79 } catch (InterruptedException e) {
82 super.removedFromContext(ctx);
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.scheduleWithFixedDelay(task, delay, delay, TimeUnit.MILLISECONDS);
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());
101 ICanvasContext ctx = getContext();
104 if (ctx.getDisposeState() != DisposeState.Alive)
106 long cur = System.currentTimeMillis();
107 long delta = cur - lastTrigger;
109 checkRotatePending = false;
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;
122 // turn the canvas a bit
123 double theta = util.getRotate();
126 // if (Math.abs(theta) < 0.0001) {
130 if (Math.abs(theta) < 0.001) {
131 util.restoreOrientation(centerPoint);
136 double momentum = theta * 100;
137 double dt = ((double) delta) / 1000;
140 angVel += momentum * dt;
141 util.rotate(centerPoint, -angVel * dt);