KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ca > commons > cbutil > CBStopMonitor


1 package com.ca.commons.cbutil;
2
3 import javax.swing.*;
4 import java.awt.event.ActionEvent JavaDoc;
5 import java.awt.event.ActionListener JavaDoc;
6
7 /**
8  * A smart button that, when pressed, will interrupt a
9  * (single) registered thread... (could conceivably be
10  * expanded in future to handle lists of stoppable threads)
11  */

12
13 public class CBStopMonitor extends JButton
14 {
15     Thread JavaDoc thingToStop = null;
16     boolean debugStopMon = false;
17     CBpbar pbar = null; // the progress bar of the stopped process (if any)
18

19
20     /**
21      * Initialise the stop monitor with the given text and icon (for display
22      * in the button) and creates an action monitor to (on a button press)
23      * stop any registered thread.
24      *
25      * @param text the button text
26      * @param icon the (optional) display icon (may be null)
27      */

28
29     public CBStopMonitor(String JavaDoc text, Icon icon)
30     {
31         super(text, icon);
32
33         addActionListener(new ActionListener JavaDoc()
34         {
35             public void actionPerformed(ActionEvent JavaDoc e)
36             {
37                 if (debugStopMon) System.out.println("StopMon button pressed; about to stop " + ((thingToStop == null) ? "null" : thingToStop.getName()));
38                 stop(thingToStop);
39             }
40         });
41     }
42
43     /**
44      * Registers a thread to monitor and possibly stop. An (optional)
45      * progress bar can also be passed in, to be also closed if
46      * the thread is stopped by the user. Use of this register ftn.
47      * <i>automatically stops any pre-existing thread</i>.
48      *
49      * @param stopMe the thread to monitor, and possibly stop
50      * @param newPbar a progress bar to close if the thread is stopped (may be null)
51      */

52
53     public void register(Thread JavaDoc stopMe, CBpbar newPbar)
54     {
55         if (debugStopMon) System.out.println("StopMon registering thread " + ((stopMe == null) ? "null" : stopMe.getName()));
56         stop(thingToStop); // stop any existing monitored thread...
57

58         pbar = newPbar;
59         thingToStop = stopMe;
60         setEnabled(true);
61     }
62
63     /**
64      * Stop monitoring a particular thread. Since there can only be one
65      * thread, the paramater thread must equal the montored thread, otherwise
66      * it is ignored (thus the ftn. is safe to call, even if a thread has
67      * already been freed and replaced with another). Also closes the progress
68      * bar (if any, and only if the parameter thread is indeed the same as the
69      * monitored thread).
70      *
71      * @param ignoreMe the thread to de-register.
72      */

73
74     public void free(Thread JavaDoc ignoreMe)
75     {
76         if (debugStopMon) System.out.println("StopMon de-registering thread " + ((ignoreMe == null) ? "null" : ignoreMe.getName()));
77
78         if (thingToStop == ignoreMe)
79         {
80             thingToStop = null;
81             if (pbar != null) pbar.close(); // if we have a progress bar, close it.
82
}
83         setEnabled(false);
84     }
85
86     /**
87      * Actually stop a particular thread (not just stop monitoring, actually
88      * stop the thread). Called automatically by the button action listener,
89      * can also be called externally. Also clears any associated progress bar.
90      *
91      * @param stopMe the thread to stop (must be the monitored thread for anything
92      * to happen...)
93      */

94
95     public void stop(Thread JavaDoc stopMe)
96     {
97         if (debugStopMon) System.out.println("StopMon asked to stop thread " + ((stopMe == null) ? "null" : stopMe.getName()));
98
99         if (thingToStop == stopMe)
100         {
101             if (stopMe == null) return;
102             if (pbar != null) pbar.close(); // if we have a progress bar, close it.
103
thingToStop.interrupt();
104             try
105             {
106                 (Thread.currentThread()).sleep(10);
107             } // pause 10 ms
108
catch (InterruptedException JavaDoc e)
109             {
110             }
111
112             if (thingToStop.isInterrupted() == false) // we tried to be nice but...
113
{
114                 if (debugStopMon) System.out.println("attempting to force thread stoppage");
115 // thingToStop.stop();
116
}
117             else
118             {
119                 if (debugStopMon) System.out.println("StopMon interrupting thread " + ((thingToStop == null) ? "null" : thingToStop.getName() + " :" + thingToStop.isInterrupted()));
120             }
121             thingToStop = null;
122             setEnabled(false);
123         }
124         else if (debugStopMon) System.out.println("unable to stop: " + stopMe.getName() + " - not registered");
125     }
126
127     /**
128      * Returns whether the stop monitor is currently monitoring a thread.
129      *
130      * @return the status of the stop monitor
131      */

132     public boolean isBusy()
133     {
134         return (isEnabled());
135     }
136
137     /**
138      * Ask user if they wish to abandon the current operation, if such exists.
139      * Otherwise return true.
140      *
141      * @return whether to continue with threading operation
142      */

143     public boolean abandonAnyExistingOperation()
144     {
145         if (thingToStop != null)
146         {
147             int option = JOptionPane.showConfirmDialog(this,
148                     "You are already doing an operation. Cancel the old one and start a new operation?",
149                     "Cancel Old Operation", JOptionPane.YES_NO_OPTION);
150
151             if (option == JOptionPane.YES_OPTION)
152             {
153                 stop(thingToStop);
154                 return true;
155             }
156             else
157                 return false; // keep going with current operation!
158
}
159         return true;
160     }
161
162 }
Popular Tags