1 /**
2 * This Java Source Code and its associated Class files are
3 * <P>
4 * (c) 1998 Battelle Pacific Northwest National Laboratories
5 * <P>
6 * For further information, contact :
7 * <P>
8 * Chris Parkinson
9 * Environmental Molecular Sciences Laboratory
10 * Battelle Pacific Northwest National Laboratories
11 * Richland
12 * WA 99352
13 * USA
14 */
15
16
17 import java.awt.*;
18 import java.awt.event.*;
19 import java.util.*;
20
21
22
23 /**
24 *
25 * CommunicateGUI provides the underlying GUI framework for both
26 * sending emails, and retreiving them.
27 * <P>
28 * Note that the Send or Receiving class does not need to call 'displayWindow'
29 * if it doesn't want a GUI. In this case, all messages will be written to
30 * stdout
31 *
32 *
33 * @version 1.0 August 1998
34 * @author Chris Parkinson
35 */
36
37
38 public class CommunicateGUI implements Runnable, ActionListener
39 {
40 private ActionListener actionListener; //Who is listening
41 private boolean isGUI;
42
43 private String errorMessage; //Error message we received
44 private boolean isCancelled; //Should we cancel operations
45 private boolean isClosed; //Have we closed the window yet
46
47 private Thread motor;
48
49 ////////////////////////////////////////////////////////
50 //
51 // Constructor
52 //
53 ////////////////////////////////////////////////////////
54 /**
55 * Constructor will initialize a new CommunciateGUI Window
56 * <p>
57 * @param parentFrame the parent frame that owns this object
58 */
CommunicateGUI(Frame parentFrame)59 public CommunicateGUI(Frame parentFrame) {
60
61 setCancelled(false);
62 setError(null);
63 isClosed = false;
64 }
65
66 ////////////////////////////////////////////////////////
67 //
68 // Are we running as a GUI
69 //
70 ////////////////////////////////////////////////////////
71 /**
72 * Are we running a GUI
73 */
isGUI()74 public boolean isGUI() {
75 return false;
76 }
77
78
79 ////////////////////////////////////////////////////////
80 //
81 // Get the ProgressBar object
82 //
83 ////////////////////////////////////////////////////////
84 /**
85 * Get the progress bar object
86 * <P>
87 * @return a JProgressBar object
88 */
getProgressBar()89 public Component getProgressBar() {
90 return null;
91 }
92
93
94 ////////////////////////////////////////////////////////
95 //
96 // Get the Text Message object
97 //
98 ////////////////////////////////////////////////////////
99 /**
100 * Get the textArea for text messages
101 * <P>
102 * @return a GUILabel object
103 */
getTextArea()104 public Component getTextArea() {
105 return null;
106 }
107
108
109
110 ////////////////////////////////////////////////////////
111 //
112 // Get and Set the 'Cancel' status
113 //
114 ////////////////////////////////////////////////////////
115 /**
116 * Get the cancel status. If this returns true, then the email
117 * process has been cancelled (either by user or by error)
118 * and the current process should stop and exit.
119 * <P>
120 * @return a boolean, true if process has been cancelled or should stop
121 */
isCancelled()122 public boolean isCancelled() {
123 return isCancelled;
124 }
125
126 /**
127 * Set the cancel status. If this is set to be true then the email
128 * processes should stop at their next convenience and exit.
129 * <P>
130 * @param isCancelled a boolean, true if process should be stopped
131 */
setCancelled(boolean isCancelled)132 public void setCancelled(boolean isCancelled) {
133 this.isCancelled = isCancelled;
134 if (isCancelled==true) {
135 motor = null;
136 closeWindow();
137 }
138 }
139
140
141
142 ////////////////////////////////////////////////////////
143 //
144 // Set and Get an error message to display on exit
145 //
146 ////////////////////////////////////////////////////////
147 /**
148 * Set the error message to display on exit and also set our cancelled status
149 * <P>
150 * @param error the message to display on exit
151 * @returns a boolean result which is always false
152 */
setError(String error)153 public boolean setError(String error) {
154 if (error!=null && errorMessage!=null)return false; //Once an error is set, keep it
155 errorMessage = error;
156 setCancelled(errorMessage!=null);
157 return false;
158 }
159
160 /**
161 * Get the error message to display on exit. This will return 'null'
162 * if no error message is present
163 * <P>
164 * @return the message to display on exit
165 */
getError()166 public String getError() {
167 return errorMessage;
168 }
169
170
171 ////////////////////////////////////////////////////////
172 //
173 // Increment the Progress Bar
174 //
175 ////////////////////////////////////////////////////////
176 /**
177 * Increment the ProgressBar by one unit and display the given message
178 * If the GUI is not active, the message will be send to stdout
179 * <P>
180 * @param message the text message to display in the progress bar
181 */
setProgress(String message)182 public void setProgress(String message) {
183 //java.lang.System.out.println(message);
184 }
185
186
187
188
189 ////////////////////////////////////////////////////////
190 //
191 // Thread runner
192 //
193 ////////////////////////////////////////////////////////
start()194 public void start() {
195 run();
196 }
197
198
run()199 public void run() {
200 //System.out.println("Starting Run...");
201 mainMethod();
202 closeWindow();
203 fireActionEvent("Email Communication Successful");
204 // System.out.println("*** Thread finished");
205 }
206
207
mainMethod()208 public void mainMethod() {
209 }
210
211
212
213
214
215 ////////////////////////////////////////////////////////
216 //
217 // Listen to exit buttons being pressed
218 //
219 ////////////////////////////////////////////////////////
220 /**
221 * ActionPerformed
222 */
actionPerformed(ActionEvent event)223 public void actionPerformed(ActionEvent event) {
224 String command = event.getActionCommand();
225 if (command.equals("Cancel"))setError("Operation Cancelled by User");
226 if (command.equals("Close"))setError("Operation Cancelled by User");
227 }
228 ////////////////////////////////////////////////////////
229 //
230 // Display Window
231 //
232 ////////////////////////////////////////////////////////
233 /**
234 * Display the Window if we have one
235 */
displayWindow(Component owner)236 public void displayWindow(Component owner) {
237 }
238
239
240 ////////////////////////////////////////////////////////
241 //
242 // Close the Window and Exit
243 //
244 ////////////////////////////////////////////////////////
245 /**
246 * Close the Window and Exit. If an error occurred up to this point
247 * we will display an error message
248 */
closeWindow()249 public boolean closeWindow() {
250 if (isClosed==false) {
251 isClosed=true;
252 //Do we have an error to display
253 if (getError()!=null)displayErrorMessage(getError());
254 }
255 return true;
256 }
257
258
259 ////////////////////////////////////////////////////////
260 //
261 // Display a message
262 //
263 ////////////////////////////////////////////////////////
264 /**
265 * Display an error message. If the GUI is not active, this will
266 * write the message to stdout
267 * <P>
268 * @param errorMessage the error Message to display
269 */
displayErrorMessage(String errorMessage)270 public void displayErrorMessage(String errorMessage) {
271 System.err.println("\nError...");
272 System.err.println(" An error occurred while talking to the Mail Server :");
273 System.err.println(" " + errorMessage);
274 }
275
276
277
278
279 /////////////////////////////////////////////////////////////////
280 //
281 // Event Processing
282 //
283 /////////////////////////////////////////////////////////////////
284 /**
285 * Add Action Listener
286 */
addActionListener(ActionListener l)287 public void addActionListener(ActionListener l) {
288 actionListener = AWTEventMulticaster.add(actionListener, l);
289 }
290
291 /**
292 * Fire an action event
293 */
fireActionEvent(String message)294 public void fireActionEvent(String message) {
295 if (actionListener==null)return;
296 ActionEvent event = new ActionEvent(this, 0, message);
297 actionListener.actionPerformed(event);
298 }
299
300
301 }
302
303
304
305
306
307
308
309
310
311
312
313
314