View Javadoc

1   /*
2    * Copyright (c) 2003, Andreas Pataki 
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    *
9    * Redistributions of source code must retain the above copyright notice,
10   * this list of conditions and the following disclaimer.
11   * 
12   * Redistributions in binary form must reproduce the above copyright notice,
13   * this list of conditions and the following disclaimer in the documentation
14   * and/or other materials provided with the distribution.
15   * 
16   * Neither the name of the author nor the names of its contributors
17   * may be used to endorse or promote products derived from this software
18   * without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS ``AS
21   * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22   * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   */
32  
33  package net.sf.tm.plugin;
34  
35  import org.eclipse.jface.preference.IPreferenceStore;
36  import org.eclipse.jface.util.IPropertyChangeListener;
37  import org.eclipse.jface.util.PropertyChangeEvent;
38  import org.eclipse.swt.widgets.Display;
39  
40  /***
41   * Class provides background update of a {@link IUpdatable} object. The
42   * update thread is running every seconds and checks if a new update must be
43   * fired. The thread is running as a UI Thread to enable display updates.
44   * 
45   * <p>The updater is listening to changes of the following preference values:
46   * 
47   * <ul>
48   *      <li>autopdate</li>
49   *      <li>update_interval</li>
50   * </ul>
51   * 
52   * <p>If a change occurs the timer is reseted.
53   * 
54   * @author Andreas Pataki
55   * @version $Id: AutoUpdate.java,v 1.2 2003/09/12 08:34:31 apataki Exp $
56   */
57  public class AutoUpdate {
58      
59      /***
60       * The time the thread is waiting between excecutions.
61       */
62      private static final int WAIT_TIME = 1000;
63  
64      /***
65       * The plugins preference store
66       */
67      private IPreferenceStore store;
68  
69      /***
70       * The current runner object. 
71       */
72      private Runner currentRunner = null;
73  
74      /***
75       * Private class containing the UI thread.
76       */
77      private class Runner implements Runnable {
78          
79          /***
80           * If true the running thread will stop 
81           */
82          private boolean forceStop;
83          /***
84           * The counter variable
85           */
86          private int seconds;
87          /***
88           * The display object which creates the timer  
89           */
90          private Display display;
91          /***
92           * Actual object which performs the update
93           */
94          private IUpdatable updatable;
95  
96          /***
97           * Constructor 
98           * 
99           * @param newUpdatable The update object
100          * @param newDisplay The display object
101          */
102         public Runner(IUpdatable newUpdatable, Display newDisplay) {
103             updatable = newUpdatable;
104             display = newDisplay;
105             forceStop = false;
106             seconds = 1;
107         }
108 
109         /***
110          * Main loop of the thread. The loop is ended when the <code>forceStop
111          * </code variable is set to true.
112          * 
113          * @see java.lang.Runnable#run()
114          */
115         public void run() {
116             if (!forceStop) {
117                 if (isAutoUpdateEnabled()) {
118                     if (seconds >= getUpdateInterval()) {
119 
120                         if (Logger.isDebugEnabled()) {
121                             Logger.debug("performing auto update");
122                         }
123 
124                         seconds = 1;
125                         updatable.update();
126 
127                     } else {
128                         seconds++;
129                     }
130                 }
131                 display.timerExec(WAIT_TIME, this);
132             }
133         }
134 
135         /***
136          * Ends the current running thread.
137          */
138         public void stop() {
139             forceStop = true;
140         }
141 
142         /***
143          * Resets the internal timer variable.
144          */
145         public void reset() {
146             seconds = 0;
147             forceStop = false;
148         }
149 
150     }
151 
152     /***
153      * Constructor
154      * 
155      * @param newStore The plugins preference store
156      */
157     public AutoUpdate(IPreferenceStore newStore) {
158         store = newStore;
159 
160         store.addPropertyChangeListener(new IPropertyChangeListener() {
161 
162             public void propertyChange(PropertyChangeEvent event) {
163                 if (PreferenceConstants
164                     .PREF_AUTOUPDATE
165                     .equals(event.getProperty())
166                     || PreferenceConstants.PREF_UPDATE_INTERVAL.equals(
167                         event.getProperty())) {
168 
169                     if (currentRunner != null) {
170                         currentRunner.reset();
171                     }
172                 }
173 
174             }
175 
176         });
177     }
178 
179     /***
180      * Starts a new update timer. If the timer is currently running it will 
181      * be stopped. 
182      * 
183      * @param updatable The object which must be updated
184      * @param display The display object for thread creation
185      */
186     public synchronized void start(IUpdatable updatable, Display display) {
187         if (currentRunner != null) {
188             currentRunner.stop();
189         }
190         currentRunner = new Runner(updatable, display);
191         display.timerExec(WAIT_TIME, currentRunner);
192     }
193 
194     /***
195      * Stops the current timer by calling the stop method of the runner. 
196      */
197     public synchronized void stop() {
198         if (currentRunner != null) {
199             currentRunner.stop();
200             currentRunner = null;
201         }
202     }
203 
204     /***
205      * @return Current autoupdate preference setting
206      */
207     private boolean isAutoUpdateEnabled() {
208         return store.getBoolean(PreferenceConstants.PREF_AUTOUPDATE);
209     }
210 
211     /***
212      * @return Current update interval
213      */
214     private int getUpdateInterval() {
215         return store.getInt(PreferenceConstants.PREF_UPDATE_INTERVAL);
216     }
217 
218 }