1 package org.enhydra.shark.swingclient.worklist; 2 3 4 import java.awt.*; 5 import java.awt.event.*; 6 import java.util.*; 7 8 import javax.swing.*; 9 import javax.swing.table.*; 10 11 12 import org.enhydra.shark.api.client.timebase.*; 13 14 15 import org.enhydra.shark.api.client.wfmodel.*; 16 import org.enhydra.shark.swingclient.*; 17 import org.enhydra.shark.swingclient.worklist.actions.*; 18 19 27 public class Worklist extends ActionPanel { 28 29 30 private Map workitems=new HashMap(); 31 32 private Map procVariables=new HashMap(); 33 34 35 private JTable worklistTable; 36 40 private WorklistTableModel worklistTableModel; 41 42 48 SharkClient workflowClient; 49 50 54 WfResource myResource; 55 56 59 public Worklist (SharkClient wc,boolean disableButtons) { 60 super(); 61 this.workflowClient=wc; 62 super.init(); 63 64 setButtonPanelEnabled(!disableButtons); 65 } 66 67 protected void createActions () { 68 defaultActions=new Action[] { 69 new CompleteWorkitem(this), 70 new UpdateActivityVariables(this), 71 new WorkitemDescription(this) 72 }; 73 } 74 78 protected Component createCenterComponent () { 79 JScrollPane tablePane=new JScrollPane(); 80 worklistTableModel=new WorklistTableModel(); 81 worklistTable=new JTable(worklistTableModel); 82 worklistTable.setColumnSelectionAllowed(false); 84 worklistTable.setRowSelectionAllowed(true); 85 worklistTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 86 worklistTable.getTableHeader().setReorderingAllowed(false); 87 worklistTable.setPreferredScrollableViewportSize(new Dimension(500,300)); 88 TableColumn column=worklistTable.getColumnModel().getColumn(0); 90 column.setMinWidth(0); 91 column.setMaxWidth(0); 92 column.setPreferredWidth(0); 93 column.setResizable(false); 94 95 worklistTable.addMouseListener(new MouseAdapter() { 97 public void mouseClicked (MouseEvent me) { 98 101 if (me.getClickCount()>1) { 102 getAction(Utils.getUnqualifiedClassName(CompleteWorkitem.class)). 103 actionPerformed(null); 104 } 105 } 106 }); 107 108 worklistTable.getInputMap(JComponent.WHEN_FOCUSED). 110 put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER,0,false),"edit"); 111 worklistTable.getActionMap().put("edit",new AbstractAction() { 112 public void actionPerformed(ActionEvent e) { 113 getAction(Utils.getUnqualifiedClassName(CompleteWorkitem.class)). 114 actionPerformed(null); 115 } 116 }); 117 118 tablePane.setViewportView(worklistTable); 119 return tablePane; 120 } 121 122 127 public void setResource (WfResource newResource) { 128 try { 129 if (!newResource.resource_key().equals(myResource.resource_key())) { 130 clear(); 131 } 132 } catch (Exception ex) {} 133 myResource=newResource; 134 refresh(); 135 } 136 137 142 public void setButtonPanelEnabled (boolean enabled) { 143 for (int i=0; i<buttonPanel.getComponentCount()-1; i++) { 145 buttonPanel.getComponent(i).setEnabled(enabled); 146 } 147 } 148 149 152 public WfResource getResource () { 153 return myResource; 154 } 155 156 161 public WfAssignment getSelectedAssignment () { 162 int selRow=worklistTable.getSelectedRow(); 163 if (selRow>=0) { 164 String id=(String )worklistTableModel.getValueAt(selRow,0); 165 return (WfAssignment)workitems.get(id); 166 } 167 return null; 168 } 169 170 175 public boolean isWorkitemContextUpdated (String workitemKey) { 176 return procVariables.containsKey(workitemKey); 177 } 178 179 182 public Map getWorkitemContext (String workitemKey) { 183 return (Map)procVariables.get(workitemKey); 184 } 185 186 189 public void putWorkitemContext (String workitemKey,Map context) { 190 procVariables.put(workitemKey,context); 191 } 192 193 197 public void clear () { 198 myResource=null; 199 workitems.clear(); 200 procVariables.clear(); 201 worklistTableModel.clearTable(); 202 } 203 204 208 public void releaseAllAssignments () { 209 Iterator it=workitems.values().iterator(); 210 while (it.hasNext()) { 211 WfAssignment ass=(WfAssignment)it.next(); 212 try { 213 ass.set_accepted_status(false); 214 } catch (Exception ex) {} 215 } 216 workitems.clear(); 217 } 218 219 227 public synchronized void refresh () { 228 long t1, t2, t3, t4, t5, t6; 229 t1=System.currentTimeMillis(); 230 WfAssignment[] ass=null; 232 try { 233 ass=myResource.get_sequence_work_item(0); 234 } catch (Exception ex) { 235 return; 236 } 237 t2=System.currentTimeMillis(); 238 ArrayList toAdd=new ArrayList(); 241 ArrayList toRemove=new ArrayList(); 242 ArrayList toRetain=new ArrayList(); 243 createWorkitemLists(ass,toAdd,toRemove,toRetain); 244 t3=System.currentTimeMillis(); 245 247 Iterator it=toRemove.iterator(); 249 while (it.hasNext()) { 250 try { 252 worklistTableModel.removeRow(it.next()); 254 } catch (Exception ex){ 255 ex.printStackTrace(); 256 } 257 } 258 t4=System.currentTimeMillis(); 259 261 it=toAdd.iterator(); 263 while (it.hasNext()) { 264 WfActivity wa=(WfActivity)it.next(); 265 try { 266 if (wa.state().startsWith("open")) { 267 Vector vRow=new Vector(); 268 vRow.add(wa.key()); 269 if (wa.state().equals("open.running")) { 270 vRow.add(new Boolean (true)); 271 } else { 272 vRow.add(new Boolean (false)); 273 } 274 WfProcess proc=wa.container(); 275 String name=proc.name(); 276 String pDefName=SharkClient.getAdminMiscUtilities().getProcessMgrProcDefName(proc.manager().name()); 277 if (pDefName.equals("") || pDefName.equals(name)) { 278 String procId=proc.key(); 279 int ind_=procId.indexOf("_"); 280 if (ind_>0) { 281 name=name+"-"+procId.substring(0,ind_); 282 } 283 } 284 vRow.add(name); 285 vRow.add(wa.name()); 286 vRow.add(new Integer (wa.priority())); 287 String date=""; 288 String duration=""; 289 if (wa.state().startsWith("open.running")) { 290 try { 291 UtcT lastStateT=wa.last_state_time(); 292 date=WorkflowUtilities.getDateFromUTC(lastStateT); 293 duration=WorkflowUtilities.getDuration(lastStateT); 294 } catch (Exception ex) { 295 } 296 } 297 vRow.add(date); 298 vRow.add(duration); 299 worklistTableModel.addRow(vRow); 300 } 301 } catch (Exception ex){ 302 ex.printStackTrace(); 303 } 304 } 305 t5=System.currentTimeMillis(); 306 308 it=toRetain.iterator(); 311 while (it.hasNext()) { 312 WfActivity wa=(WfActivity)it.next(); 313 try { 314 String id=wa.key(); 315 boolean accepted=((WfAssignment)workitems.get(id)).get_accepted_status(); 316 String date=""; 317 String duration=""; 318 if (accepted) { 319 try { 320 UtcT lastStateT=wa.last_state_time(); 321 date=WorkflowUtilities.getDateFromUTC(lastStateT); 322 duration=WorkflowUtilities.getDuration(lastStateT); 323 } catch (Exception ex) { 324 } 325 } 326 worklistTableModel.updateWorkitemProperties(wa.key(),accepted, 327 date,duration); 328 } catch (Exception ex){ 329 ex.printStackTrace(); 330 } 331 } 332 t6=System.currentTimeMillis(); 333 335 } 336 337 341 private void createWorkitemLists (WfAssignment[] ass, 342 ArrayList toAdd,ArrayList toRemove,ArrayList toRetain) { 343 344 Map newWorkitems=new HashMap(); 346 if (ass!=null) { 347 for (int i=0; i<ass.length; i++) { 348 WfActivity wa; 349 try { 350 wa=ass[i].activity(); 351 newWorkitems.put(wa.key(),ass[i]); 352 } catch (Exception ex) {} 353 } 354 } 355 356 ArrayList toAddIds=new ArrayList(newWorkitems.keySet()); 358 toAddIds.removeAll(workitems.keySet()); 359 ArrayList toRemoveIds=new ArrayList(workitems.keySet()); 360 toRemoveIds.removeAll(newWorkitems.keySet()); 361 ArrayList toRetainIds=new ArrayList(workitems.keySet()); 362 toRetainIds.retainAll(newWorkitems.keySet()); 363 368 Iterator addIt=toAddIds.iterator(); 370 while (addIt.hasNext()) { 371 try { 372 toAdd.add(((WfAssignment)newWorkitems.get(addIt.next())).activity()); 373 } catch (Exception ex) {} 374 } 375 Iterator removeIt=toRemoveIds.iterator(); 377 while (removeIt.hasNext()) { 378 try { 379 Object nextId=removeIt.next(); 380 toRemove.add(nextId); 382 procVariables.remove(nextId); 383 } catch (Exception ex) {} 384 } 385 Iterator retIt=toRetainIds.iterator(); 387 while (retIt.hasNext()) { 388 try { 389 toRetain.add(((WfAssignment)newWorkitems.get(retIt.next())).activity()); 390 } catch (Exception ex) {} 391 } 392 393 workitems.clear(); 395 workitems=newWorkitems; 396 } 397 398 401 class WorklistTableModel extends DefaultTableModel { 402 406 WorklistTableModel () { 407 super(new String [] { 408 ResourceManager.getLanguageDependentString("IdKey"), 409 ResourceManager.getLanguageDependentString("AcceptedKey"), 410 ResourceManager.getLanguageDependentString("ProcessNameKey"), 412 ResourceManager.getLanguageDependentString("WorkitemKey"), 413 ResourceManager.getLanguageDependentString("PriorityKey"), 414 ResourceManager.getLanguageDependentString("StartedKey"), 415 ResourceManager.getLanguageDependentString("DurationKey") 416 },0); 417 } 418 419 422 public void removeRow (Object workitemKey) { 423 int rowCnt=getRowCount(); 424 for (int row=0; row<rowCnt;row++) { 425 if (getValueAt(row,0).equals(workitemKey)) { 426 removeRow(row); 427 break; 428 } 429 } 430 } 431 432 435 public void clearTable () { 436 int rowCnt=getRowCount(); 437 for (int row=rowCnt-1; row>=0; row--) { 439 removeRow(row); 440 } 441 } 442 443 452 public void updateWorkitemProperties (String workitemKey,boolean accept, 453 String newDate,String newDuration) { 454 int rowCnt=getRowCount(); 455 for (int row=0; row<rowCnt;row++) { 456 if (getValueAt(row,0).equals(workitemKey)) { 457 super.setValueAt(new Boolean (accept),row,1); 458 super.setValueAt(newDate,row,5); 459 super.setValueAt(newDuration,row,6); 460 break; 461 } 462 } 463 } 464 465 471 public Class getColumnClass(int c) { 472 return getValueAt(0, c).getClass(); 473 } 474 475 478 public boolean isCellEditable(int row, int col) { 479 if (col==1) { 480 return true; 481 } else { 482 return false; 483 } 484 } 485 486 490 public void setValueAt(Object value, int row, int col) { 491 super.setValueAt(value,row,col); 492 if (col==1) { 493 boolean accepted=((Boolean )value).booleanValue(); 494 try { 495 WfAssignment ass=(WfAssignment)workitems.get(getValueAt(row,0)); 496 ass.set_accepted_status(accepted); 497 if (accepted) { 498 UtcT lastStateT=ass.activity().last_state_time(); 499 String newDate=WorkflowUtilities.getDateFromUTC(lastStateT); 500 String newDuration=WorkflowUtilities.getDuration(lastStateT); 501 super.setValueAt(newDate,row,5); 502 super.setValueAt(newDuration,row,6); 503 } else { 504 super.setValueAt("",row,5); 505 super.setValueAt("",row,6); 506 } 507 } catch (CannotAcceptSuspended cas) { 508 JOptionPane.showMessageDialog(workflowClient.getFrame(), 509 ResourceManager.getLanguageDependentString( 510 "WarningCannotAcceptSuspendedWorkitem"), 511 workflowClient.getAppTitle(),JOptionPane.WARNING_MESSAGE); 512 refresh(); 513 } catch (Exception ex) { 514 JOptionPane.showMessageDialog(workflowClient.getFrame(), 515 ResourceManager.getLanguageDependentString( 516 "WarningTheWorkitemIsPerformedByAnotherUser"), 517 workflowClient.getAppTitle(),JOptionPane.WARNING_MESSAGE); 518 refresh(); 519 } 520 } 521 } 522 523 } 524 525 } 526 | Popular Tags |