1 package com.opensymphony.workflow.designer; 2 3 import java.awt.*; 4 import java.awt.event.MouseAdapter ; 5 import java.awt.event.MouseEvent ; 6 import java.io.File ; 7 import java.net.MalformedURLException ; 8 import javax.swing.*; 9 import javax.swing.event.*; 10 import javax.swing.tree.*; 11 12 import com.opensymphony.workflow.designer.actions.AssignPalette; 13 import com.opensymphony.workflow.designer.actions.DeleteWorkflow; 14 import com.opensymphony.workflow.designer.actions.ImportWorkflow; 15 import com.opensymphony.workflow.designer.swing.FileDropHandler; 16 import com.opensymphony.workflow.loader.Workspace; 17 18 23 public class Navigator extends JTree implements TreeSelectionListener, TreeModelListener 24 { 25 private WorkflowDesigner designer; 26 private DefaultMutableTreeNode rootNode; 27 private String currentWorkflow; 28 private JPopupMenu popup; 29 private DeleteWorkflow deleteWorkflow; 30 private AssignPalette assignPalette; 31 private ImportWorkflow importWorkflow; 32 33 public Navigator(WorkflowDesigner designer) 34 { 35 super(new DefaultTreeModel(new DefaultMutableTreeNode(ResourceManager.getString("workspace.none")))); 36 rootNode = (DefaultMutableTreeNode)getModel().getRoot(); 37 this.designer = designer; 38 addTreeSelectionListener(this); 39 setEditable(true); 40 getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION); 41 setShowsRootHandles(true); 42 getModel().addTreeModelListener(this); 43 popup = new JPopupMenu(); 44 popup.setInvoker(this); 45 importWorkflow = (ImportWorkflow)ActionManager.get("importflow"); 48 deleteWorkflow = new DeleteWorkflow(designer); 49 assignPalette = new AssignPalette(designer); 50 popup.add(new JMenuItem(ActionManager.register("deleteflow", deleteWorkflow))); 51 ActionManager.register("assign.palette", assignPalette); 52 assignPalette.setEnabled(false); 53 popup.add(new JMenuItem(assignPalette)); 54 55 addMouseListener(new MouseAdapter () 56 { 57 public void mousePressed(MouseEvent e) 58 { 59 handlePopup(e); 60 } 61 62 public void mouseReleased(MouseEvent e) 63 { 64 handlePopup(e); 65 } 66 67 private void handlePopup(MouseEvent e) 68 { 69 if(e.isPopupTrigger()) 70 { 71 int row = getRowForLocation(e.getX(), e.getY()); 72 if(row != -1) 73 { 74 TreePath path = getPathForRow(row); 75 if(path.getPathCount() != 2) return; 76 String workflowName = path.getLastPathComponent().toString(); 77 Point p = new Point(e.getX(), e.getY()); 78 SwingUtilities.convertPointToScreen(p, Navigator.this); 79 deleteWorkflow.setWorkflow(workflowName); 80 assignPalette.setWorkflow(workflowName); 81 popup.setLocation(p.x, p.y); 82 popup.setVisible(true); 83 } 84 } 85 } 86 }); 87 88 new FileDropHandler(this, true) 89 { 90 public void filesDropped(File [] files) 91 { 92 for(int i = 0; i < files.length; i++) 93 { 94 File file = files[i]; 95 if(file.getName().endsWith(".xml") && !file.isDirectory()) 96 { 97 try 98 { 99 importWorkflow.importURL(file.toURL()); 100 } 101 catch(MalformedURLException e) 102 { 103 e.printStackTrace(); 104 } 105 } 106 } 107 } 108 }; 109 } 110 111 public void selectWorkflow(String name) 112 { 113 Object root = getModel().getRoot(); 114 int count = getModel().getChildCount(root); 115 for(int i = 0; i < count; i++) 116 { 117 DefaultMutableTreeNode node = (DefaultMutableTreeNode)getModel().getChild(root, i); 118 if(node.getUserObject().equals(name)) 119 { 120 TreePath path = new TreePath(new Object []{root, node}); 121 getSelectionModel().setSelectionPath(path); 122 expandPath(path); 123 designer.selectWorkflow(name); 124 return; 125 } 126 } 127 } 128 129 public void removeWorkflow(String name) 130 { 131 Object root = getModel().getRoot(); 132 DefaultTreeModel model = (DefaultTreeModel)getModel(); 133 int count = model.getChildCount(root); 134 for(int i = 0; i < count; i++) 135 { 136 DefaultMutableTreeNode node = (DefaultMutableTreeNode)model.getChild(root, i); 137 if(node.getUserObject().equals(name)) 138 { 139 model.removeNodeFromParent(node); 140 return; 141 } 142 } 143 } 144 145 public void valueChanged(TreeSelectionEvent e) 146 { 147 DefaultMutableTreeNode node = (DefaultMutableTreeNode)getLastSelectedPathComponent(); 148 if(node == null || node.equals(getModel().getRoot())) 149 return; 150 151 if(node.isLeaf()) 152 { 153 currentWorkflow = node.getUserObject().toString(); 154 designer.selectWorkflow(currentWorkflow); 155 } 156 } 157 158 public void treeNodesChanged(TreeModelEvent e) 159 { 160 DefaultMutableTreeNode node; 161 node = (DefaultMutableTreeNode)(e.getTreePath().getLastPathComponent()); 162 168 try 169 { 170 int index = e.getChildIndices()[0]; 171 node = (DefaultMutableTreeNode)(node.getChildAt(index)); 172 } 173 catch(NullPointerException exc) 174 {} 175 String newValue = (String )node.getUserObject(); 176 designer.renameWorkflow(currentWorkflow, newValue); 177 currentWorkflow = newValue; 178 } 179 180 public void setWorkspace(Workspace workspace) 181 { 182 rootNode.removeAllChildren(); 183 ((DefaultTreeModel)getModel()).reload(); 184 DefaultMutableTreeNode root = (DefaultMutableTreeNode)getModel().getRoot(); 185 if(workspace != null) 186 { 187 String [] workflows = workspace.getWorkflowNames(); 188 for(int i = 0; i < workflows.length; i++) 189 { 190 addWorkflow(workflows[i]); 191 } 192 root.setUserObject(workspace.getName()); 193 expandRow(0); 194 } 195 else 196 { 197 root.setUserObject(ResourceManager.getString("workspace.none")); 198 } 199 } 200 201 public void addWorkflow(String name) 202 { 203 DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(name); 204 DefaultTreeModel model = (DefaultTreeModel)getModel(); 205 DefaultMutableTreeNode root = (DefaultMutableTreeNode)model.getRoot(); 206 model.insertNodeInto(childNode, root, root.getChildCount()); 207 } 208 209 public void treeNodesInserted(TreeModelEvent e) 210 { 211 } 212 213 public void treeNodesRemoved(TreeModelEvent e) 214 { 215 } 216 217 public void treeStructureChanged(TreeModelEvent e) 218 { 219 } 220 } 221 | Popular Tags |