KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > shark > corbaclient > workflowadmin > instantiation > ProcessInstantiationManagement


1 package org.enhydra.shark.corbaclient.workflowadmin.instantiation;
2
3 import java.awt.*;
4 import java.awt.event.*;
5 import java.util.*;
6
7 import javax.swing.*;
8 import javax.swing.border.*;
9 import javax.swing.event.*;
10 import javax.swing.table.*;
11 import javax.swing.tree.*;
12
13 import org.omg.WfBase.*;
14 import org.omg.WorkflowModel.*;
15 import org.omg.CORBA.ORB JavaDoc;
16 import org.enhydra.shark.corbaclient.*;
17 import org.enhydra.shark.corbaclient.workflowadmin.*;
18 import org.enhydra.shark.corbaclient.workflowadmin.instantiation.actions.*;
19
20 import org.enhydra.jawe.JaWE;
21 import org.enhydra.jawe.xml.elements.Package;
22
23 /**
24  * Implements the user interface and program logic to instantiate
25  * the process from it's definition.
26  *
27  * @author Sasa Bojanic
28  * @version 1.0
29  */

30 public class ProcessInstantiationManagement extends ActionPanel {
31
32    private WfProcessMgr currentMgr=null;
33
34    private ProcessInstantiatorInterface processInstantiator;
35    private SharkAdmin workflowAdmin;
36    private ProcessViewer processViewer;
37
38    private static Dimension treeDimension=new Dimension(300,500);
39
40    private DefaultTreeModel treeModel;
41    private PIMTreeSelectionListener pimSL;
42    private JTree allItems;
43
44    private DescriptionTableModel processDescription;
45
46
47    public ProcessInstantiationManagement (SharkAdmin wa) {
48       super();
49       this.workflowAdmin=wa;
50       this.processViewer=new ProcessViewer(wa.getFrame());
51       super.init();
52
53       addFocusListener(new FocusListener () {
54                public void focusGained (FocusEvent fe) {
55                   pimSL.valueChanged(null);
56                }
57                public void focusLost (FocusEvent fe) {
58                   //
59
}
60             });
61
62    }
63
64    public void clear () {
65       processDescription.setData(null);
66       processViewer.displayProcess(null,null,null);
67       DefaultMutableTreeNode root=(DefaultMutableTreeNode)treeModel.getRoot();
68       treeModel.setRoot(
69          new DefaultMutableTreeNode(
70                           ResourceManager.getLanguageDependentString("OpenedPackagesKey")));
71    }
72
73    protected void createActions () {
74       defaultActions=new Action[] {
75          new InstantiateProcess(this),
76             new ViewProcess(this),
77             new ProcessDescription(this),
78             new WorkflowObjectProperties(this),
79             new EnableDefinition(this),
80             new DisableDefinition(this),
81             new ReevaluateAssignments(this)
82       };
83    }
84
85    /**
86     * Create the center component of this panel. This creates a scroll-
87     * pane for the current graph variable and stores the scrollpane
88     * in the scrollPane variable.
89     */

90    protected Component createCenterComponent() {
91       Component treePanel=createTreePanel();
92       Component tablePanel=createTablePanel();
93       JSplitPane ttPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
94                                        treePanel,tablePanel);
95       ttPane.setBorder(BorderFactory.createEtchedBorder());
96       return ttPane;
97    }
98
99    public SharkAdmin getWorkflowAdmin () {
100       return workflowAdmin;
101    }
102
103    public ProcessInstantiatorInterface getProcessInstantiator () {
104        if (processInstantiator == null)
105        if (SharkClient.POA)
106          processInstantiator = new ProcessInstantiatorPOA();
107        else
108          processInstantiator = new ProcessInstantiatorCORBA();
109        return processInstantiator;
110    }
111
112    public TreeSelectionListener getTreeSelectionListener () {
113       return pimSL;
114    }
115
116    public ProcessViewer getProcessViewer () {
117       return processViewer;
118    }
119
120    public WfProcessMgr getSelectedProcessMgr () {
121       return currentMgr;
122    }
123
124    public Object JavaDoc getSelectedUserObject () {
125       DefaultMutableTreeNode dmtn=(DefaultMutableTreeNode)allItems.getLastSelectedPathComponent();
126       if (dmtn!=null) {
127          return dmtn.getUserObject();
128       } else {
129          return null;
130       }
131    }
132
133    public synchronized void refresh (boolean mandatoryRefreshing) {
134       if (!(mandatoryRefreshing || isShowing())) return;
135       EngineTreeModel etm=workflowAdmin.getEngineTreeModel();
136       Collection pkgs=JaWE.getInstance().getXMLInterface().getAllPackageIds();
137       Collection myPkgs=getMyPackageIds();
138       Collection rar=new HashSet(myPkgs);
139       rar.removeAll(pkgs);
140       Collection aar=new HashSet(pkgs);
141       aar.removeAll(myPkgs);
142
143       DefaultMutableTreeNode root=(DefaultMutableTreeNode)treeModel.getRoot();
144       // remove old packages
145
Iterator pkgIds=rar.iterator();
146       while (pkgIds.hasNext()) {
147          DefaultMutableTreeNode toRemove=getModelNode((String JavaDoc)pkgIds.next());
148          treeModel.removeNodeFromParent(toRemove);
149       }
150
151       // add new packages
152
pkgIds=aar.iterator();
153       while (pkgIds.hasNext()) {
154          DefaultMutableTreeNode newPackage=etm.
155             getCopyOfModelNodeForPackage((String JavaDoc)pkgIds.next());
156          if (newPackage!=null) {
157             int hmc=root.getChildCount();
158             treeModel.insertNodeInto(newPackage,root,hmc);
159          }
160       }
161    }
162
163    private Collection getMyPackageIds () {
164       ArrayList allMyPkgs=new ArrayList();
165       DefaultMutableTreeNode root=(DefaultMutableTreeNode)treeModel.getRoot();
166       Enumeration pkgs=root.children();
167       while (pkgs.hasMoreElements()) {
168          DefaultMutableTreeNode dmtn=(DefaultMutableTreeNode)pkgs.nextElement();
169          Object JavaDoc userObj=dmtn.getUserObject();
170          if (userObj instanceof Package JavaDoc) {
171             allMyPkgs.add(((Package JavaDoc)userObj).get("Id").toString());
172          }
173       }
174       return allMyPkgs;
175    }
176
177    private DefaultMutableTreeNode getModelNode (String JavaDoc pkgId) {
178       DefaultMutableTreeNode root=(DefaultMutableTreeNode)treeModel.getRoot();
179       Enumeration pkgs=root.children();
180       while (pkgs.hasMoreElements()) {
181          DefaultMutableTreeNode dmtn=(DefaultMutableTreeNode)pkgs.nextElement();
182          Object JavaDoc userObj=dmtn.getUserObject();
183          if ((userObj instanceof Package JavaDoc) && ((Package JavaDoc)userObj).get("Id").toString().equals(pkgId)) {
184             return dmtn;
185          }
186       }
187       return null;
188    }
189
190    private Component createTreePanel() {
191       this.treeModel=new DefaultTreeModel(new DefaultMutableTreeNode(
192                                              ResourceManager.getLanguageDependentString("OpenedPackagesKey")));
193
194
195       JPanel tp=new JPanel();
196       int emptyBorderHSize=10;
197       int emptyBorderVSize=10;
198       Border emptyb=BorderFactory.createEmptyBorder(emptyBorderVSize,emptyBorderHSize,
199                                                     emptyBorderVSize,emptyBorderHSize);
200       Border inb=BorderFactory.createMatteBorder(1,1,1,1,Color.darkGray);
201       inb=BorderFactory.createTitledBorder(inb,
202                                            ResourceManager.getLanguageDependentString("EngineContextKey"));
203
204       tp.setBorder(BorderFactory.createCompoundBorder(emptyb,inb));
205       tp.setLayout(new BoxLayout(tp,BoxLayout.Y_AXIS));
206
207       // creating a tree which do not allow cell editing
208
allItems=new JTree(treeModel) {
209          public boolean isCellEditable(int row, int col) {
210             return false;
211          }
212       };
213
214       // adding mouse listener that willl perform action of arising popup menu
215
// with properties action, when right button is clicked
216
allItems.addMouseListener(new MouseAdapter() {
217                public void mousePressed(MouseEvent e) {
218                   if (e.getButton()==MouseEvent.BUTTON3) {
219                      DefaultMutableTreeNode node = (DefaultMutableTreeNode)
220                         allItems.getLastSelectedPathComponent();
221                      if ((node == null) || (node.getUserObject() instanceof String JavaDoc)) return;
222
223                      JPopupMenu jpm = new JPopupMenu();
224                      // adding Properties action
225
jpm.add(BarFactory.createMenuItem(
226                                 Utils.getUnqualifiedClassName(WorkflowObjectProperties.class),
227                                 commands));
228                      jpm.show(allItems,e.getX(),e.getY());
229                   }
230                }
231             });
232
233       // setting some tree properties
234
ToolTipManager.sharedInstance().registerComponent(allItems);
235       allItems.getSelectionModel().setSelectionMode
236          (TreeSelectionModel.SINGLE_TREE_SELECTION);
237       //allItems.setRootVisible(false);
238
allItems.setCellRenderer(new EngineTreeRenderer());
239
240       // creates panel
241
JScrollPane allItemsPane=new JScrollPane();
242       allItemsPane.setViewportView(allItems);
243       allItemsPane.setPreferredSize(treeDimension);
244
245       tp.add(allItemsPane);
246       //tp.add(Box.createVerticalGlue());
247

248       pimSL=new PIMTreeSelectionListener();
249       allItems.addTreeSelectionListener(pimSL);
250
251       return tp;
252    }
253
254    private Component createTablePanel() {
255       // setting table
256
JPanel tp=new JPanel();
257       int emptyBorderHSize=10;
258       int emptyBorderVSize=10;
259       Border emptyb=BorderFactory.createEmptyBorder(emptyBorderVSize,emptyBorderHSize,
260                                                     emptyBorderVSize,emptyBorderHSize);
261       Border inb=BorderFactory.createMatteBorder(1,1,1,1,Color.darkGray);
262       inb=BorderFactory.createTitledBorder(inb,
263                                            ResourceManager.getLanguageDependentString("ProcessPropertiesKey"));
264
265       tp.setBorder(BorderFactory.createCompoundBorder(emptyb,inb));
266       tp.setLayout(new BoxLayout(tp,BoxLayout.Y_AXIS));
267       JScrollPane tablePane=new JScrollPane();
268       processDescription=new DescriptionTableModel();
269       JTable t=new JTable(processDescription);
270       // setting some table properties
271
t.setColumnSelectionAllowed(false);
272       t.setRowSelectionAllowed(true);
273       t.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
274       t.getTableHeader().setReorderingAllowed(false);
275       t.setPreferredScrollableViewportSize(new Dimension(500,300));
276       tablePane.setViewportView(t);
277       tp.add(tablePane);
278       return tp;
279    }
280
281    class PIMTreeSelectionListener implements TreeSelectionListener {
282       public void valueChanged(TreeSelectionEvent e) {
283          DefaultMutableTreeNode node = (DefaultMutableTreeNode)
284             allItems.getLastSelectedPathComponent();
285
286          if (node == null) return;
287
288          if (node.getUserObject() instanceof WfProcessMgr) {
289             try {
290                currentMgr=(WfProcessMgr)node.getUserObject();
291                DefaultMutableTreeNode pkgN=(DefaultMutableTreeNode)node.getParent();
292                processDescription.setData(currentMgr);
293                processViewer.displayProcess((Package JavaDoc)pkgN.getUserObject(),
294                                                (WfProcessMgr)node.getUserObject(),
295                                             null);
296             } catch (Exception JavaDoc ex) {
297                processDescription.setData(null);
298                processViewer.displayProcess(null,null,null);
299             }
300          } else {
301             currentMgr=null;
302             processDescription.setData(null);
303             processViewer.displayProcess(null,null,null);
304          }
305       }
306    }
307
308    class DescriptionTableModel extends DefaultTableModel {
309       DescriptionTableModel () {
310          super(new String JavaDoc[] {
311                   ResourceManager.getLanguageDependentString("PropertyKey"),
312                      ResourceManager.getLanguageDependentString("ValueKey")
313                },5);
314          setValueAt(ResourceManager.getLanguageDependentString("NameKey"),0,0);
315          setValueAt(ResourceManager.getLanguageDependentString("CategoryKey"),1,0);
316          setValueAt(ResourceManager.getLanguageDependentString("VersionKey"),2,0);
317          setValueAt(ResourceManager.getLanguageDependentString("ActiveProcessesKey"),3,0);
318          setValueAt(ResourceManager.getLanguageDependentString("StateKey"),4,0);
319       }
320
321       public boolean isCellEditable(int row, int col) {
322          return false;
323       }
324
325       public void setData (WfProcessMgr mgr) {
326          try {
327             setValueAt(mgr.name(),0,1);
328             setValueAt(mgr.category(),1,1);
329             setValueAt(mgr.version(),2,1);
330
331             WfProcessIterator pi=SharkClient.getCommonExpressionBuilder().getOpenProcessesForManager(mgr.name());
332             WfProcess[] pis=pi.get_next_n_sequence(0);
333             setValueAt(new Integer JavaDoc(pis.length),3,1);
334             if (mgr.process_mgr_state().equals(process_mgr_stateType.enabled)) {
335                setValueAt(ResourceManager.getLanguageDependentString("EnabledKey"),4,1);
336             } else {
337                setValueAt(ResourceManager.getLanguageDependentString("DisabledKey"),4,1);
338             }
339          } catch (Exception JavaDoc ex) {
340             setValueAt("",0,1);
341             setValueAt("",1,1);
342             setValueAt("",2,1);
343             setValueAt("",3,1);
344             setValueAt("",4,1);
345          }
346       }
347    }
348
349 }
350
Popular Tags