View Javadoc

1   /*
2    *  
3    *  Fosstrak LLRP Commander (www.fosstrak.org)
4    * 
5    *  Copyright (C) 2008 ETH Zurich
6    *
7    *  This program is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  This program is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with this program.  If not, see <http://www.gnu.org/licenses/> 
19   *
20   */
21  
22  package org.fosstrak.llrp.commander.util;
23  
24  import org.apache.log4j.Logger;
25  import org.fosstrak.llrp.commander.views.MessageboxView;
26  
27  /**
28   * helper thread that periodically refreshes the 
29   * message box view if a new message has arrived. 
30   * (refreshing whenever a message arrives kills 
31   * eclipse ui.).
32   * @author sawielan
33   *
34   */
35  public class MessageBoxRefresh implements Runnable {
36  
37  	/** the default interval to refresh the messagebox view. */
38  	public static final long DEFAULT_REFRESH_INTERVAL_MS = 1500;
39  	
40  	/** the interval to refresh the messagebox view. */ 
41  	public long refreshTime = DEFAULT_REFRESH_INTERVAL_MS;
42  	
43  	/** the log4j logger. */
44  	private static Logger log = Logger.getLogger(MessageBoxRefresh.class);
45  	
46  	/** flag whether we need a refresh in the message box. */
47  	private boolean dirty = false;
48  	
49  	/** flags whether to execute the refresher or not... */
50  	private boolean doRun = true;
51  	
52  	/** flag that is true when the refresher has stopped. */
53  	private boolean stopped = false;
54  	
55  	/** the message box to refresh. */
56  	private MessageboxView messageboxView = null;
57  	
58  	/** whether to refresh or not by default. */
59  	public static final boolean DEFAULT_REFRESH_BEHAVIOR = true;
60  	
61  	/** whether to refresh or not. */
62  	private boolean doRefresh = DEFAULT_REFRESH_BEHAVIOR;
63  	
64  	/**
65  	 * constructor for the message box refresher thread. 
66  	 * @param messageboxView the message box to be refreshed.
67  	 */
68  	public MessageBoxRefresh(MessageboxView messageboxView) {
69  		this.messageboxView = messageboxView;
70  	}
71  	
72  	/** 
73  	 * sets the messagebox to dirty, meaning a new message arrived 
74  	 * and we need to refresh the message box.
75  	 */
76  	public void setDirty() {
77  		dirty = true;
78  	}
79  	
80  	/**
81  	 * turn on/off the refresh behavior.
82  	 * @param refresh if set to true refresh the messagebox, otherwise not.
83  	 */
84  	public void setRefresh(boolean refresh) {
85  		doRefresh = refresh;
86  	}
87  	
88  	/**
89  	 * sets the messagebox to refresh.
90  	 * @param messageboxView the message box to be refreshed.
91  	 */
92  	public void setMessageBox(MessageboxView messageboxView) {
93  		this.messageboxView = messageboxView;
94  	}
95  	
96  	/**
97  	 * stops the refresher from executing. 
98  	 */
99  	public void stop() {
100 		this.doRun = false;
101 	}
102 	
103 	/**
104 	 * if the refresher has stopped true is returned.
105 	 * @return true if stopped, false otherwise.
106 	 */
107 	public boolean hasStopped() {
108 		return stopped;
109 	}
110 	
111 	/**
112 	 * set the refresh time to use. if never set, the default refresh
113 	 * time is used.
114 	 * @param refreshTime the new refresh time.
115 	 */
116 	public void setRefreshTime(long refreshTime) {
117 		if (refreshTime > 0) {
118 			this.refreshTime = refreshTime;
119 		}
120 	}
121 	
122 	/**
123 	 * @return the refresh time.
124 	 */
125 	public long getRefreshTime() {
126 		return refreshTime;
127 	}
128 	
129 	/**
130 	 * execute the refresher thread.
131 	 */
132 	public void run() {
133 		try {
134 			while (doRun) {
135 				if ((true == dirty) && (true == doRefresh)) {
136 					dirty = false;
137 					
138 					// SWT threads do not allow other threads to access 
139 					// the SWT widgets. to circumvent this issue one has 
140 					// to run the call through a asyncExec/syncExec API 
141 					// on the corresponding display
142 					if (messageboxView != null) {
143 						// execute synchronous
144 						messageboxView.getDisplay().syncExec(
145 							new Runnable() {
146 								public void run() {
147 									messageboxView.updateViewer(false);
148 									log.debug("update message box.");
149 								}
150 							}
151 						);
152 
153 					} else {
154 						log.debug("no message box set...");
155 					}
156 				}
157 
158 				// wait for the next refresh...
159 				Thread.sleep(refreshTime);
160 			}
161 		} catch (InterruptedException e) {
162 			log.info("received interrupt, stop refreshing messagebox.");
163 		} catch (Exception e) {
164 			log.error("some unknown error occured:\n" + e.getMessage());
165 			e.printStackTrace();
166 			System.exit(-1);
167 		}
168 		
169 		stopped = true;
170 	}
171 }