Merge "Bug 2347: DOMConcurrentDataCommitCoordinator uses wrong phase name"
[controller.git] / third-party / net.sf.jung2 / src / main / java / edu / uci / ics / jung / algorithms / layout / util / VisRunner.java
1 /*
2  * Copyright (c) 2005, the JUNG Project and the Regents of the University of
3  * California All rights reserved.
4  *
5  * This software is open-source under the BSD license; see either "license.txt"
6  * or http://jung.sourceforge.net/license.txt for a description.
7  *
8  * 
9  */
10 package edu.uci.ics.jung.algorithms.layout.util;
11
12 import edu.uci.ics.jung.algorithms.util.IterativeContext;
13
14 /**
15  * 
16  * Implementation of a relaxer thread for layouts.
17  * Extracted from the {@code VisualizationModel} in previous
18  * versions of JUNG.
19  * 
20  * @author Tom Nelson - tomnelson@dev.java.net
21  *
22  */
23 public class VisRunner implements Relaxer, Runnable {
24         
25         protected boolean running;
26         protected IterativeContext process;
27         protected boolean stop;
28         protected boolean manualSuspend;
29         protected Thread thread;
30         
31         /**
32          * how long the relaxer thread pauses between iteration loops.
33          */
34         protected long sleepTime = 100L;
35
36         
37         /**
38          * Creates an instance for the specified process.
39          */
40         public VisRunner(IterativeContext process) {
41                 this.process = process;
42         }
43
44         /**
45          * @return the relaxerThreadSleepTime
46          */
47         public long getSleepTime() {
48                 return sleepTime;
49         }
50
51         /**
52          * @param sleepTime the sleep time to set for this thread
53          */
54         public void setSleepTime(long sleepTime) {
55                 this.sleepTime = sleepTime;
56         }
57         
58         public void prerelax() {
59                 manualSuspend = true;
60                 long timeNow = System.currentTimeMillis();
61                 while (System.currentTimeMillis() - timeNow < 500 && !process.done()) {
62                         process.step();
63                 }
64                 manualSuspend = false;
65         }
66
67         public void pause() {
68                 manualSuspend = true;
69         }
70
71         public void relax() {
72                 // in case its running
73                 stop();
74                 stop = false;
75                 thread = new Thread(this);
76                 thread.setPriority(Thread.MIN_PRIORITY);
77                 thread.start();
78         }
79         
80         /**
81          * Used for synchronization.
82          */
83         public Object pauseObject = new String("PAUSE OBJECT");
84
85         public void resume() {
86                 manualSuspend = false;
87                 if(running == false) {
88                         prerelax();
89                         relax();
90                 } else {
91                         synchronized(pauseObject) {
92                                 pauseObject.notifyAll();
93                         }
94                 }
95         }
96
97         public synchronized void stop() {
98                 if(thread != null) {
99                         manualSuspend = false;
100                         stop = true;
101                         // interrupt the relaxer, in case it is paused or sleeping
102                         // this should ensure that visRunnerIsRunning gets set to false
103                         try { thread.interrupt(); }
104                         catch(Exception ex) {
105                                 // the applet security manager may have prevented this.
106                                 // just sleep for a second to let the thread stop on its own
107                                 try { Thread.sleep(1000); }
108                                 catch(InterruptedException ie) {} // ignore
109                         }
110                         synchronized (pauseObject) {
111                                 pauseObject.notifyAll();
112                         }
113                 }
114         }
115
116         public void run() {
117             running = true;
118             try {
119                 while (!process.done() && !stop) {
120                     synchronized (pauseObject) {
121                         while (manualSuspend && !stop) {
122                             try {
123                                 pauseObject.wait();
124                             } catch (InterruptedException e) {
125                                 // ignore
126                             }
127                         }
128                     }
129                     process.step();
130                     
131                     if (stop)
132                         return;
133                     
134                     try {
135                         Thread.sleep(sleepTime);
136                     } catch (InterruptedException ie) {
137                         // ignore
138                     }
139                 }
140
141             } finally {
142                 running = false;
143             }
144         }
145 }