1 package com.ca.commons.cbutil; 2 3 import javax.swing.*; 4 import java.awt.event.ActionEvent ; 5 import java.awt.event.ActionListener ; 6 7 12 13 public class CBStopMonitor extends JButton 14 { 15 Thread thingToStop = null; 16 boolean debugStopMon = false; 17 CBpbar pbar = null; 19 20 28 29 public CBStopMonitor(String text, Icon icon) 30 { 31 super(text, icon); 32 33 addActionListener(new ActionListener () 34 { 35 public void actionPerformed(ActionEvent 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 52 53 public void register(Thread stopMe, CBpbar newPbar) 54 { 55 if (debugStopMon) System.out.println("StopMon registering thread " + ((stopMe == null) ? "null" : stopMe.getName())); 56 stop(thingToStop); 58 pbar = newPbar; 59 thingToStop = stopMe; 60 setEnabled(true); 61 } 62 63 73 74 public void free(Thread 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(); } 83 setEnabled(false); 84 } 85 86 94 95 public void stop(Thread 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(); thingToStop.interrupt(); 104 try 105 { 106 (Thread.currentThread()).sleep(10); 107 } catch (InterruptedException e) 109 { 110 } 111 112 if (thingToStop.isInterrupted() == false) { 114 if (debugStopMon) System.out.println("attempting to force thread stoppage"); 115 } 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 132 public boolean isBusy() 133 { 134 return (isEnabled()); 135 } 136 137 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; } 159 return true; 160 } 161 162 } | Popular Tags |