001: package com.opensymphony.workflow.designer;
002:
003: import javax.swing.*;
004: import javax.swing.event.*;
005: import javax.swing.tree.*;
006:
007: import java.awt.Component;
008:
009: import com.opensymphony.workflow.loader.*;
010: import org.jgraph.graph.CellView;
011: import org.jgraph.graph.DefaultGraphCell;
012: import org.jgraph.graph.GraphConstants;
013:
014: public class RelationshipsNavigator extends JTree implements
015: TreeSelectionListener, TreeModelListener {
016: private WorkflowDesigner designer;
017: private WorkflowGraph currentGraph = null;
018: private DefaultGraphCell currentCell = null;
019: private Object currentObject = null;
020: private DefaultTreeCellRenderer cellRenderer = new WorkflowCellRenderer();
021:
022: public RelationshipsNavigator(WorkflowDesigner designer) {
023: super (new DefaultTreeModel(new DefaultMutableTreeNode(
024: ResourceManager.getString("relationships"))));
025: setRootVisible(false);
026: this .designer = designer;
027: addTreeSelectionListener(this );
028: setEditable(true);
029: getSelectionModel().setSelectionMode(
030: TreeSelectionModel.SINGLE_TREE_SELECTION);
031: setShowsRootHandles(true);
032: getModel().addTreeModelListener(this );
033: setCellRenderer(cellRenderer);
034: }
035:
036: private void clearRelationships(Object rootObject) {
037: DefaultTreeModel model = (DefaultTreeModel) getModel();
038: DefaultMutableTreeNode root = new DefaultMutableTreeNode(
039: rootObject);
040: setRootVisible(true);
041: model.setRoot(root);
042: model.reload(root);
043: currentObject = root.getUserObject();
044: }
045:
046: public void showRelationships(Object node, WorkflowGraph graph) {
047: if (node instanceof DefaultGraphCell) {
048: currentGraph = graph;
049: currentCell = (DefaultGraphCell) node;
050: clearRelationships(node);
051: }
052: if (node instanceof StepCell) {
053: showStepRelationships((StepCell) node);
054: }
055: // TODO: handle other cell types
056: }
057:
058: public void showStepRelationships(StepCell cell) {
059: DefaultTreeModel model = (DefaultTreeModel) getModel();
060: DefaultMutableTreeNode root = (DefaultMutableTreeNode) model
061: .getRoot();
062: StepDescriptor stepDescriptor = cell.getDescriptor();
063: Object[] cells = new Object[] { cell };
064: java.util.Set edgeSet = WorkflowGraphModel.getEdges(
065: currentGraph.getModel(), cells);
066: for (int i = 0; i < stepDescriptor.getActions().size(); i++) {
067: ActionDescriptor action = (ActionDescriptor) (stepDescriptor
068: .getActions().get(i));
069: DefaultMutableTreeNode actionNode = new DefaultMutableTreeNode(
070: action) {
071: public String toString() {
072: if (getUserObject() instanceof ActionDescriptor) {
073: return ((ActionDescriptor) getUserObject())
074: .getName();
075: } else {
076: return super .toString();
077: }
078: }
079: };
080: model
081: .insertNodeInto(actionNode, root, root
082: .getChildCount());
083: java.util.Iterator edges = edgeSet.iterator();
084: while (edges.hasNext()) {
085: ResultEdge edge = (ResultEdge) edges.next();
086: if (edge.getDescriptor().getParent() == action) {
087: model.insertNodeInto(new DefaultMutableTreeNode(
088: edge), actionNode, actionNode
089: .getChildCount());
090: }
091: }
092: }
093: if (stepDescriptor.getActions().size() > 0) {
094: setRootVisible(false);
095: }
096: model.reload(root);
097: }
098:
099: public void valueChanged(TreeSelectionEvent e) {
100: DefaultMutableTreeNode node = (DefaultMutableTreeNode) getLastSelectedPathComponent();
101: if (node == null || node.equals(getModel().getRoot()))
102: return;
103:
104: currentObject = node.getUserObject();
105: if (node.isLeaf()) {
106: if (node.getUserObject() instanceof DefaultGraphCell) {
107: designer.showDetails(node.getUserObject());
108: }
109: }
110: }
111:
112: public DefaultGraphCell getCell() {
113: return currentCell;
114: }
115:
116: public void treeNodesChanged(TreeModelEvent e) {
117: DefaultMutableTreeNode node;
118:
119: node = (DefaultMutableTreeNode) (e.getTreePath()
120: .getLastPathComponent());
121: try {
122: int index = e.getChildIndices()[0];
123: node = (DefaultMutableTreeNode) (node.getChildAt(index));
124: } catch (NullPointerException exc) {
125: }
126: if (currentObject instanceof ActionDescriptor) {
127: ((ActionDescriptor) currentObject).setName((String) node
128: .getUserObject());
129: } else if (currentObject instanceof ResultEdge) {
130: ((ResultEdge) currentObject).getDescriptor()
131: .setDisplayName((String) node.getUserObject());
132: } else if (currentObject instanceof StepCell) {
133: ((StepCell) currentObject).getDescriptor().setName(
134: (String) node.getUserObject());
135: }
136: node.setUserObject(currentObject);
137: RefreshUIForNode(node);
138: currentGraph.paintAll(currentGraph.getGraphics());
139: DefaultTreeModel model = (DefaultTreeModel) getModel();
140: model.reload(node);
141: designer.refreshUI();
142: }
143:
144: protected void RefreshUIForNode(DefaultMutableTreeNode node) {
145: Object currentObject = node.getUserObject();
146: if (currentObject != null) {
147: CellView currentView = currentGraph.getGraphLayoutCache()
148: .getMapping(currentObject, false);
149: if (currentView != null) {
150: currentView.update();
151: //currentView.refresh(false);
152: currentGraph.updateAutoSize(currentView);
153: }
154: for (int i = 0; i < node.getChildCount(); i++) {
155: RefreshUIForNode((DefaultMutableTreeNode) node
156: .getChildAt(i));
157: }
158: }
159: }
160:
161: public void treeNodesInserted(TreeModelEvent e) {
162: }
163:
164: public void treeNodesRemoved(TreeModelEvent e) {
165: }
166:
167: public void treeStructureChanged(TreeModelEvent e) {
168: }
169:
170: static class WorkflowCellRenderer extends DefaultTreeCellRenderer {
171:
172: public WorkflowCellRenderer() {
173: super ();
174: setClosedIcon(ResourceManager.getIcon("action"));
175: setOpenIcon(ResourceManager.getIcon("action"));
176: setLeafIcon(ResourceManager.getIcon("step"));
177: }
178:
179: public Component getTreeCellRendererComponent(JTree tree,
180: Object value, boolean sel, boolean expanded,
181: boolean leaf, int row, boolean hasFocus) {
182: Component result = super .getTreeCellRendererComponent(tree,
183: value, sel, expanded, leaf, row, hasFocus);
184: if (value instanceof DefaultMutableTreeNode) {
185: if (((DefaultMutableTreeNode) value).getUserObject() instanceof ResultEdge) {
186: ResultEdge edge = (ResultEdge) ((DefaultMutableTreeNode) value)
187: .getUserObject();
188: if (!sel) {
189: setForeground(GraphConstants.getForeground(edge
190: .getAttributes()));
191: }
192: if (tree.isEnabled()) {
193: if (edge.getDescriptor() instanceof ConditionalResultDescriptor) {
194: setIcon(ResourceManager
195: .getIcon("conditional.result"));
196: } else {
197: setIcon(ResourceManager
198: .getIcon("unconditional.result"));
199: }
200: }
201: }
202: }
203: return result;
204: }
205:
206: }
207:
208: }
|