JAL-3026 srcjar files for VARNA and log4j
[jalview.git] / srcjar / fr / orsay / lri / varna / controlers / ControleurBlinkingThread.java
1 /*
2  VARNA is a tool for the automated drawing, visualization and annotation of the secondary structure of RNA, designed as a companion software for web servers and databases.
3  Copyright (C) 2008  Kevin Darty, Alain Denise and Yann Ponty.
4  electronic mail : Yann.Ponty@lri.fr
5  paper mail : LRI, bat 490 Université Paris-Sud 91405 Orsay Cedex France
6
7  This file is part of VARNA version 3.1.
8  VARNA version 3.1 is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
9  as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10
11  VARNA version 3.1 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  See the GNU General Public License for more details.
14
15  You should have received a copy of the GNU General Public License along with VARNA version 3.1.
16  If not, see http://www.gnu.org/licenses.
17  */
18 package fr.orsay.lri.varna.controlers;
19
20 import java.awt.event.ActionEvent;
21 import java.awt.event.ActionListener;
22
23 import javax.swing.Timer;
24
25 import fr.orsay.lri.varna.VARNAPanel;
26
27 /**
28  * BH SwingJS converted to Timer mechanism for compatibility with JavaScript 
29  *
30  */
31 public class ControleurBlinkingThread extends Thread implements ActionListener {
32         public static final long DEFAULT_FREQUENCY = 50;
33         private long _period;
34         private VARNAPanel _parent;
35         private double _minVal, _maxVal, _val, _incr;
36         private boolean _increasing = true;
37         private boolean _active = false;
38
39         public ControleurBlinkingThread(VARNAPanel vp) {
40                 this(vp, DEFAULT_FREQUENCY, 0, 1.0, 0.0, 0.2);
41         }
42
43         public ControleurBlinkingThread(VARNAPanel vp, long period, double minVal,
44                         double maxVal, double val, double incr) {
45                 _parent = vp;
46                 _period = period;
47                 _minVal = minVal;
48                 _maxVal = maxVal;
49                 _incr = incr;
50         }
51
52         public void setActive(boolean b) {
53                 if (_active == b)
54                 {}
55                 else
56                 {
57                 _active = b;
58                 if (_active) {
59                         interrupt();
60                 }
61                 }
62         }
63
64         public boolean getActive() {
65                 return _active;
66         }
67         
68         
69         public double getVal() {
70                 return _val;
71         }
72
73         protected final int START = 0;
74         protected final int LOOP = 1;
75         protected final int STOP = -1;
76         
77         protected int nextMode = START;
78         private Timer timer;
79         
80         
81         public void interrupt() {
82                 super.interrupt();
83                 stopTimer();
84                 run();
85         }
86         
87         @Override
88         public void actionPerformed(ActionEvent e) {
89                 run();
90         }
91         public void run() {
92         //   same as:
93         //                      while (true) {
94         //                      try {
95         //                              if (_active) {
96         //                                      sleep(_period);
97         //                                      if (_increasing) {
98         //                                              _val = Math.min(_val + _incr, _maxVal);
99         //                                              if (_val == _maxVal) {
100         //                                                      _increasing = false;
101         //                                              }
102         //                                      } else {
103         //                                              _val = Math.max(_val - _incr, _minVal);
104         //                                              if (_val == _minVal) {
105         //                                                      _increasing = true;
106         //                                              }
107         //                                      }
108         //                                      _parent.repaint();
109         //                              } else {
110         //                                      sleep(10000);
111         //                              }
112         //                      } catch (InterruptedException e) {
113         //                      }
114         //              }
115                 long delay = 0;
116                 while (true) { 
117                         try {
118                                 switch (nextMode) {
119                                 case START:
120                                         if (_active) {
121                                                 delay = _period;
122                                                 nextMode = LOOP;
123                                         } else {
124                                                 delay = 10000;
125                                                 nextMode = START;
126                                         }
127                                         startTimer(delay);
128                                         return;
129                                 case STOP:
130                                         break;
131                                 case LOOP:
132                                         if (_increasing) {
133                                                 _val = Math.min(_val + _incr, _maxVal);
134                                                 if (_val == _maxVal) {
135                                                         _increasing = false;
136                                                 }
137                                         } else {
138                                                 _val = Math.max(_val - _incr, _minVal);
139                                                 if (_val == _minVal) {
140                                                         _increasing = true;
141                                                 }
142                                         }
143                                         _parent.repaint();
144                                         nextMode = START;
145                                         continue;
146                                 }
147                                 sleep(0);
148                         } catch (InterruptedException e) {
149                                 // ignore??
150                         }
151                         break;
152                         }
153         }
154
155         private void startTimer(long delay) {
156                 stopTimer();
157                 timer = new Timer((int) delay, this);
158                 timer.setRepeats(false);
159                 timer.start();
160         }
161
162         private void stopTimer() {
163                 if (timer != null) {
164                         timer.stop();
165                         timer = null;
166                 }
167         }
168
169 }