KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.enhydra.shark.swingclient.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
14 import org.enhydra.shark.api.client.wfbase.*;
15
16 import org.enhydra.shark.api.client.wfmodel.*;
17 import org.enhydra.shark.swingclient.*;
18 import org.enhydra.shark.swingclient.workflowadmin.*;
19 import org.enhydra.shark.swingclient.workflowadmin.instantiation.actions.*;
20
21 import org.enhydra.jawe.JaWE;
22 import org.enhydra.jawe.xml.elements.Package;
23
24 /**
25  * Implements the user interface and program logic to instantiate
26  * the process from it's definition.
27  *
28  * @author Sasa Bojanic
29  * @version 1.0
30  */

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

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

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