1 package com.ca.directory.jxplorer.broker; 2 3 import java.util.Vector ; 4 import java.util.logging.Logger ; 5 import java.awt.*; 6 import java.awt.event.*; 7 import javax.swing.*; 8 9 import com.ca.directory.jxplorer.*; 10 import com.ca.commons.cbutil.*; 11 12 20 21 public class StopMonitor 22 { 23 27 28 Vector watchList; Frame parent; 31 Vector watchingComponents; 33 StopMonitorGUI monitor; 34 35 static int threadID = 0; 37 private static Logger log = Logger.getLogger(StopMonitor.class.getName()); 38 39 42 protected class QueryBroker 43 { 44 public Broker broker; 45 public DataQuery query; 46 public int id; 47 48 QueryBroker(Broker b, DataQuery q) 49 { 50 broker = b; 51 query = q; 52 id = q.id; 53 } 54 } 55 56 60 61 public StopMonitor(Broker[] brokerList, Frame parent) 62 { 63 this.parent = parent; 64 65 watchList = new Vector (brokerList.length); 66 67 watchingComponents = new Vector (4); 68 69 for (int i=0; i<brokerList.length; i++) 70 add(brokerList[i]); 71 } 72 73 76 77 public void add(Broker b) 78 { 79 watchList.add(b); 80 b.registerStopMonitor(this); 81 } 82 83 88 89 protected Vector getQueryBrokers() 90 { 91 int i, noBrokers = watchList.size(); 92 93 Vector doublets = new Vector (noBrokers*2); 95 for (i=0; i<noBrokers; i++) 97 { 98 Broker broker = (Broker)watchList.get(i); 99 DataQuery query = broker.getCurrent(); 100 if (query != null && !query.isCancelled()) 101 doublets.add(new QueryBroker(broker, query)); 102 } 103 104 for (i=0; i<noBrokers; i++) 106 { 107 Broker broker = (Broker)watchList.get(i); 108 Vector queries = broker.getRequestQueue(); 109 for (int j=0; j<queries.size(); j++) 110 doublets.add(new QueryBroker(broker, (DataQuery)queries.get(j))); 111 } 112 113 return doublets; 114 } 115 116 119 public boolean queriesPending() 120 { 121 for (int i=0; i<watchList.size(); i++) 123 { 124 if ( ((Broker)watchList.get(i)).hasRequests() ) 125 return true; 126 } 127 128 return false; 129 } 130 131 136 public void addWatcher(Component c) 137 { 138 watchingComponents.add(c); 139 } 140 141 146 147 public void updateWatchers() 148 { 149 boolean enableStatus = queriesPending(); 150 151 for (int i=0; i<watchingComponents.size(); i++) 152 { 153 Component c = (Component)watchingComponents.get(i); 154 if (c.isEnabled() != enableStatus) 155 { 156 c.setEnabled(enableStatus); 157 c.repaint(); 158 } 159 } 160 161 } 162 163 164 167 173 174 protected void cancelQuery(QueryBroker pair) 175 { 176 if (pair == null) return; 177 178 DataQuery query = pair.query; 179 Broker broker = pair.broker; 180 181 184 synchronized(this) 185 { 186 if (query.isRunning()) { query.cancel(); 189 190 new Thread (broker, "restarted thread for: " + (threadID++) + " " + broker.getClass()).start(); 191 } 192 else { 194 broker.removeQuery(query); 195 } 196 } 197 } 198 199 203 204 public void show() 205 { 206 Vector QBs = getQueryBrokers(); 207 208 if (QBs.size() == 0) 209 { 210 JOptionPane.showMessageDialog(parent, 211 CBIntText.get("There are no outstanding directory queries to cancel."), 212 CBIntText.get("Nothing to do."), JOptionPane.INFORMATION_MESSAGE); 213 return; 214 } 215 216 if (monitor!=null) 217 { 218 monitor.update(); 219 if (monitor.singleDelete() == false) 220 monitor.setVisible(true); 221 } 222 else 223 { 224 monitor = new StopMonitorGUI(this, QBs); 225 monitor.setSize(300, 400); 226 CBUtility.center(monitor, parent); 227 228 Thread buttonMonitor = new Thread (monitor, "Stop Monitor Thread"); 231 buttonMonitor.start(); 232 233 if (monitor.singleDelete() == false) 234 monitor.setVisible(true); 235 } 236 } 237 238 239 240 244 251 252 class StopMonitorGUI extends JDialog implements Runnable 253 { 254 JList list; 255 DefaultListModel model; 256 StopMonitor myMonitor; 257 boolean finished = false; boolean selected = false; QueryBroker selectedQuery = null; 261 StopMonitorGUI(StopMonitor myMonitor, Vector queryBrokers) 262 { 263 super(myMonitor.parent); 264 this.myMonitor = myMonitor; 265 266 model = new DefaultListModel(); 267 list = new JList(model); 268 for (int i=0; i<queryBrokers.size(); i++) 269 model.addElement(queryBrokers.get(i)); 270 271 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 272 list.setSelectionModel(new CBSingleSelectionModel(list)); list.setCellRenderer(new StopMonitorListRenderer()); 274 list.setVisibleRowCount(6); 275 list.setSelectedIndex(0); 276 277 JScrollPane scrollPane = new JScrollPane(); 278 scrollPane.getViewport().setView(list); 279 getContentPane().add("Center", scrollPane); 280 281 JPanel buttonPanel = new JPanel(); 282 buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER)); 283 284 CBButton ok, cancel, help; 285 buttonPanel.add(ok = new CBButton(CBIntText.get("Delete Query"), CBIntText.get("Delete the current query."))); 286 buttonPanel.add(cancel = new CBButton(CBIntText.get("Exit"), CBIntText.get("Exit this window."))); 287 buttonPanel.add(help = new CBButton(CBIntText.get("Help"), CBIntText.get("Open the help."))); 288 CBHelpSystem.useDefaultHelp(help, HelpIDs.ABORT); getContentPane().add("South", buttonPanel); 290 291 ok.addActionListener(new ActionListener() 292 { 293 public void actionPerformed(ActionEvent e) 294 { 295 cancelSelectedQuery(); } 297 }); 298 299 cancel.addActionListener(new ActionListener() 300 { 301 public void actionPerformed(ActionEvent e) 302 { 303 setVisible(false); 304 } 305 }); 306 307 list.addMouseListener(new MouseAdapter() 308 { 309 public void mouseClicked(MouseEvent e) 310 { 311 if (e.getClickCount() == 2) cancelSelectedQuery(); } 313 }); 314 } 315 316 317 318 321 protected void update() 322 { 323 int selectedID = -1; 324 325 Vector queryBrokers = myMonitor.getQueryBrokers(); 326 327 QueryBroker selectedQuery = (QueryBroker)list.getSelectedValue(); if (selectedQuery != null) 329 selectedID = selectedQuery.id; 330 331 model.removeAllElements(); 332 333 for (int i=0; i<queryBrokers.size(); i++) 334 { 335 QueryBroker newQuery = (QueryBroker)queryBrokers.get(i); 336 model.addElement(newQuery); 337 if (selectedID == newQuery.id) 338 list.setSelectedValue(newQuery, true); } 340 341 } 342 343 344 345 349 350 protected void cancelSelectedQuery() 351 { 352 synchronized(this) 353 { 354 if (list.getSelectedIndex() == -1) 355 { 356 JOptionPane.showMessageDialog(this.getContentPane(), 357 CBIntText.get("You must select a query to stop from the list") + "\n" + 358 CBIntText.get("otherwise you can just use the 'cancel' button."), 359 CBIntText.get("Stop Monitor Help"), JOptionPane.INFORMATION_MESSAGE); 360 } 361 else 362 { 363 selectedQuery = (QueryBroker)list.getSelectedValue(); 364 selected = true; 365 model.removeElement(selectedQuery); 366 notify(); 367 } 368 } 369 } 370 371 public void setVisible(boolean status) 372 { 373 super.setVisible(status); 374 synchronized(this) 375 { 376 if (status == true) notify(); } 378 } 379 380 386 public boolean singleDelete() 387 { 388 if (model.size() == 0) return false; list.setSelectedIndex(0); 390 if (model.size() == 1) 391 { 392 cancelSelectedQuery(); 393 return true; 394 } 395 return false; 396 } 397 398 405 406 public void run() 407 { 408 while (!finished) 409 { 410 try 411 { 412 synchronized(this) 413 { 414 if (selectedQuery != null) 415 { 416 myMonitor.cancelQuery(selectedQuery); selectedQuery = null; } 419 420 if (isVisible()) wait(250); 422 else 423 wait(); 424 425 update(); 426 } 427 } 428 catch (InterruptedException e) 429 { 430 CBUtility.error("Exception in stop monitor: ", e); 431 } 432 } 433 } 434 } 435 436 437 438 442 443 447 448 453 454 class StopMonitorListRenderer extends JLabel implements ListCellRenderer 455 { 456 Color highlight = new Color(0,0,128); 457 Color active = new Color(128,0,0); 458 Color pending = new Color(0,128,0); 459 Color cancelled = new Color(64,64,64); 460 461 StopMonitorListRenderer() 462 { 463 setOpaque(true); 464 } 465 466 public Component getListCellRendererComponent(JList list, Object value, int index, 467 boolean isSelected, boolean cellHasFocus) 468 { 469 if (value instanceof QueryBroker == false) { 471 log.warning("Rendering error in StopMonitor"); setText("error"); return this; 472 } 473 474 if (index == -1) 475 { 476 int selected = list.getSelectedIndex(); 477 if (selected == -1) 478 return this; 479 else 480 index = selected; 481 } 482 483 DataQuery item = ((QueryBroker)value).query; 484 485 if (item == null) { 487 setBackground(Color.white); 488 setForeground(cancelled); 489 setText("<deleted>"); 490 return this; 491 } 492 493 setText(item.toString()); 494 Color back = Color.white; 495 Color fore = Color.black; 496 497 if (item.isCancelled()) 498 fore = cancelled; 499 else if (item.isRunning()) 500 fore = active; 501 else 502 fore = pending; 503 504 if (isSelected) 505 { 506 setBackground(highlight); 507 setForeground(Color.white); 508 } 509 else 510 { 511 setBackground(back); 512 setForeground(fore); 513 } 514 return this; 515 } 516 } 517 518 } | Popular Tags |