1 package com.opensymphony.workflow.designer; 2 3 import java.awt.*; 4 import java.awt.event.WindowAdapter ; 5 import java.awt.event.WindowEvent ; 6 import java.io.InputStream ; 7 import java.io.PrintWriter ; 8 import java.io.File ; 9 import java.util.*; 10 import java.net.URL ; 11 import java.net.MalformedURLException ; 12 import javax.swing.*; 13 14 import org.jgraph.event.GraphSelectionEvent; 15 import org.jgraph.event.GraphSelectionListener; 16 import org.w3c.dom.Document ; 17 import org.w3c.dom.Element ; 18 19 import com.opensymphony.workflow.FactoryException; 20 import com.opensymphony.workflow.InvalidWorkflowDescriptorException; 21 import com.opensymphony.workflow.config.WorkspaceManager; 22 import com.opensymphony.workflow.designer.editor.*; 23 import com.opensymphony.workflow.designer.swing.*; 24 import com.opensymphony.workflow.designer.swing.status.StatusBar; 25 import com.opensymphony.workflow.designer.dialogs.NewWorkspaceDialog; 26 import com.opensymphony.workflow.loader.PaletteDescriptor; 27 import com.opensymphony.workflow.loader.WorkflowDescriptor; 28 import com.opensymphony.workflow.loader.Workspace; 29 30 import javax.xml.parsers.DocumentBuilderFactory ; 31 import javax.xml.parsers.DocumentBuilder ; 32 import javax.xml.parsers.ParserConfigurationException ; 33 34 39 public class WorkflowDesigner extends JFrame implements GraphSelectionListener 40 { 41 public static final String WORKSPACE_SUFFIX = ".wsf"; 42 43 private Navigator navigator; 44 private WorkspaceManager manager = new WorkspaceManager(); 45 private GraphTabbedPane graphTabs = new GraphTabbedPane(); 46 private JSplitPane mainSplitPane; 47 private EmptyBorderSplitPane leftSplitPane; 48 private CardPanel detailPanel = new CardPanel(); 49 private FramePanel detailFramePanel; 50 public static WorkflowDesigner INSTANCE = null; 51 private PaletteDescriptor palette = null; 52 public StatusBar statusBar; 53 54 public WorkflowDesigner(Splash splash) 55 { 56 super(ResourceManager.getString("app.name")); 57 INSTANCE = this; 58 setJMenuBar(BarFactory.createMenubar(manager)); 59 splash.setProgress(30); 60 navigator = new Navigator(this); 61 JScrollPane sp = new JScrollPane(detailPanel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 62 detailFramePanel = new FramePanel(ResourceManager.getString("details"), false); 63 detailFramePanel.setContent(sp); 64 65 splash.setProgress(40); 66 loadPalette(); 67 splash.setProgress(50); 69 FramePanel flowsPanel = new FramePanel(ResourceManager.getString("workspace"), false); 70 flowsPanel.setContent(new JScrollPane(navigator)); 71 72 leftSplitPane = new EmptyBorderSplitPane(JSplitPane.VERTICAL_SPLIT, flowsPanel, detailFramePanel); 74 mainSplitPane = new EmptyBorderSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftSplitPane, new JScrollPane(graphTabs)); 75 graphTabs.setVisible(false); 76 mainSplitPane.setDividerLocation(Prefs.INSTANCE.getInt(Prefs.MAIN_DIVIDER_LOCATION, 150)); 77 leftSplitPane.setDividerLocation(Prefs.INSTANCE.getInt(Prefs.DETAIL_DIVIDER_LOCATION, 150)); 78 79 splash.setProgress(60); 80 String bounds = Prefs.INSTANCE.get(Prefs.DESIGNER_BOUNDS, "100, 100, 800, 600"); 82 StringTokenizer tok = new StringTokenizer(bounds, ","); 83 int x = Integer.parseInt(tok.nextToken().trim()); 84 int y = Integer.parseInt(tok.nextToken().trim()); 85 int w = Integer.parseInt(tok.nextToken().trim()); 86 int h = Integer.parseInt(tok.nextToken().trim()); 87 setLocation(x, y); 88 getContentPane().setLayout(new BorderLayout()); 89 getContentPane().add(BarFactory.createToolbar(), BorderLayout.NORTH); 90 splash.setProgress(65); 91 getContentPane().add(mainSplitPane, BorderLayout.CENTER); 92 statusBar = BarFactory.createStatusBar(); 93 getContentPane().add(statusBar, BorderLayout.SOUTH); 94 95 splash.setProgress(70); 96 mainSplitPane.setPreferredSize(new Dimension(w, h)); 97 98 addWindowListener(new WindowAdapter () 99 { 100 public void windowClosing(WindowEvent evt) 101 { 102 quit(); 103 } 104 }); 105 String lastOpened = Prefs.INSTANCE.get(Prefs.LAST_WORKSPACE, null); 106 if(lastOpened != null) 107 { 108 try 109 { 110 if(lastOpened.indexOf(":/") == -1) 111 { 112 openWorkspace(new File (lastOpened).toURL()); 113 } 114 else 115 { 116 openWorkspace(new URL (lastOpened)); 117 } 118 } 119 catch(MalformedURLException e) 120 { 121 e.printStackTrace(); 122 } 123 String workflow = Prefs.INSTANCE.get(Prefs.WORKFLOW_CURRENT, null); 124 if(workflow != null) 125 { 126 navigator.selectWorkflow(workflow); 127 } 128 } 129 } 130 131 public WorkflowGraph getCurrentGraph() 132 { 133 return graphTabs.getCurrentGraph(); 134 } 135 136 public void deleteWorkflow(String workflowName) 137 { 138 graphTabs.removeGraph(workflowName); 139 navigator.removeWorkflow(workflowName); 140 manager.getCurrentWorkspace().deleteWorkflow(workflowName); 141 } 142 143 public void closeCurrentWorkflow() 144 { 145 graphTabs.removeGraph(graphTabs.getCurrentGraph()); 146 } 147 148 public void createGraph(String workflowName) 149 { 150 Workspace currentWorkspace = manager.getCurrentWorkspace(); 151 Layout layout = currentWorkspace.getLayout(workflowName); 152 WorkflowGraphModel model = new WorkflowGraphModel(layout); 153 model.setPalette(palette); 154 boolean hasLayout = layout != null; 155 if(layout == null) layout = new Layout(); 156 WorkflowDescriptor descriptor; 157 try 158 { 159 descriptor = currentWorkspace.getWorkflow(workflowName); 160 } 161 catch(FactoryException e) 162 { 163 e.printStackTrace(); 164 return; 165 } 166 WorkflowGraph graph = new WorkflowGraph(model, descriptor, layout, !hasLayout); 167 graph.addGraphSelectionListener(this); 168 graph.setName(workflowName); 169 graphTabs.addGraph(graph); 170 graphTabs.setVisible(true); 171 } 172 173 public void quit() 174 { 175 Point location = getLocation(); 176 Prefs.INSTANCE.putInt(Prefs.MAIN_DIVIDER_LOCATION, mainSplitPane.getDividerLocation()); 177 Prefs.INSTANCE.putInt(Prefs.DETAIL_DIVIDER_LOCATION, leftSplitPane.getDividerLocation()); 178 Prefs.INSTANCE.put(Prefs.DESIGNER_BOUNDS, location.x + "," + location.y + "," + mainSplitPane.getWidth() + "," + mainSplitPane.getHeight()); 179 try 180 { 181 Prefs.INSTANCE.flush(); 182 } 183 catch(Exception e) 184 { 185 e.printStackTrace(); 186 } 187 System.exit(0); 188 } 189 190 public void renameWorkflow(String oldName, String newName) 191 { 192 graphTabs.renameGraph(oldName, newName); 193 manager.getCurrentWorkspace().renameWorkflow(oldName, newName); 194 } 195 196 public void valueChanged(GraphSelectionEvent e) 197 { 198 Object lastAdded = null; 199 for(int i = e.getCells().length-1; i >= 0; i--) 200 { 201 if(e.isAddedCell(i)) 202 { 203 lastAdded = e.getCells()[i]; 204 break; 205 } 206 } 207 if(lastAdded instanceof WorkflowCell || lastAdded instanceof WorkflowEdge) 208 { 209 showDetails(lastAdded); 210 } 211 } 212 213 public void showDetails(Object node) 214 { 215 String panelName = node.getClass().getName(); 216 DetailPanel current = (DetailPanel)detailPanel.getVisibleCard(); 217 if(current != null) current.closeView(); 218 DetailPanel panel = (DetailPanel)detailPanel.showCard(panelName); 219 if(panel == null) 220 { 221 if(node instanceof StepCell) 222 { 223 panel = new StepEditor(); 224 } 225 else if(node instanceof SplitCell) 226 { 227 panel = new SplitEditor(); 228 } 229 else if(node instanceof JoinCell) 230 { 231 panel = new JoinEditor(); 232 } 233 else if(node instanceof ResultEdge) 234 { 235 panel = new ResultEditor(); 236 } 237 else if(node instanceof InitialActionCell) 238 { 239 panel = new InitialActionEditor(); 240 } 241 if(panel != null) 242 { 243 panel.setName(panelName); 244 } 246 } 247 if(panel != null) 248 { 249 WorkflowGraph currentGraph = graphTabs.getCurrentGraph(); 250 panel.setModel(currentGraph.getWorkflowGraphModel()); 251 if(node instanceof WorkflowCell) 252 { 253 panel.setCell((WorkflowCell)node); 254 } 255 else if(node instanceof WorkflowEdge) 256 { 257 panel.setEdge((WorkflowEdge)node); 258 } 259 260 String title = panel.getTitle(); 261 detailFramePanel.setTitle(ResourceManager.getString("details") + (title != null ? (" - " + title) : "")); 262 detailPanel.showCard(panel); 263 } 264 else 265 { 266 System.out.println("WARN: no detail panel for " + node.getClass()); 267 } 268 } 269 270 public void openWorkspace(URL file) 271 { 272 if(file != null) 273 { 274 try 275 { 276 Prefs.INSTANCE.put(Prefs.LAST_WORKSPACE, file.toString()); 277 manager.loadWorkspace(file); 278 Workspace workspace = manager.getCurrentWorkspace(); 279 navigator.setWorkspace(workspace); 280 String [] workflows = workspace.getWorkflowNames(); 281 for(int i = 0; i < workflows.length; i++) 282 { 283 createGraph(workflows[i]); 284 } 285 } 286 catch(Exception t) 287 { 288 t.printStackTrace(); 289 } 290 } 291 } 292 293 public void checkWorkspaceExists() 294 { 295 if(manager.getCurrentWorkspace()==null) 296 { 297 NewWorkspaceDialog newSpace = new NewWorkspaceDialog(this, ResourceManager.getString("workspace.new"), true); 298 newSpace.pack(); 299 newSpace.getBanner().setTitle(""); 300 newSpace.getBanner().setSubtitle(ResourceManager.getString("workspace.new.long")); 301 Utils.centerComponent(this, newSpace); 302 Dimension size = newSpace.getSize(); 303 newSpace.setSize(size.width+10, size.height+15); 304 newSpace.show(); 305 } 306 } 307 308 private void save(WorkflowGraph graph) 309 { 310 Layout layout = graph.getGraphLayout(); 311 WorkflowGraphModel model = (WorkflowGraphModel)graph.getModel(); 312 layout.setAllEntries(model.getActivitiesList()); 313 String workflowName = graph.getName(); 314 manager.getCurrentWorkspace().setLayout(workflowName, layout); 315 try 316 { 317 manager.getCurrentWorkspace().saveWorkflow(workflowName, manager.getCurrentWorkspace().getWorkflow(workflowName), true); 318 } 319 catch(InvalidWorkflowDescriptorException e) 320 { 321 try 322 { 323 System.out.println("Error saving workflow: " + e); 324 PrintWriter out = new PrintWriter (System.out); 325 manager.getCurrentWorkspace().getWorkflow(workflowName).writeXML(out, 0); 326 out.flush(); 327 } 328 catch(FactoryException e1) 329 { 330 e1.printStackTrace(); 331 } 332 JOptionPane.showMessageDialog(this, ResourceManager.getString("error.save.workflow", new Object []{e.getMessage()}), 333 ResourceManager.getString("error.save.workflow.long", new Object []{workflowName}), JOptionPane.ERROR_MESSAGE); 334 } 335 catch(FactoryException e) 336 { 337 e.printStackTrace(); 338 } 339 } 340 341 public void saveOpenGraphs() 342 { 343 WorkflowGraph[] graphs = graphTabs.getGraphs(); 344 for(int i = 0; i < graphs.length; i++) 345 { 346 save(graphs[i]); 347 } 348 } 349 350 public void saveWorkspace() 351 { 352 manager.saveWorkspace(); 353 } 354 355 public Workspace newWorkspace() 356 { 357 closeWorkspace(); 358 Workspace workspace = new Workspace(); 359 manager.setCurrentWorkspace(workspace); 360 navigator.setWorkspace(workspace); 361 return workspace; 362 } 363 364 public void closeWorkspace() 365 { 366 if(!graphTabs.isVisible()) return; 368 graphTabs.removeAll(); 369 manager.setCurrentWorkspace(null); 370 navigator.setWorkspace(null); 371 Prefs.INSTANCE.remove(Prefs.LAST_WORKSPACE); 372 graphTabs.setVisible(false); 373 } 374 375 public void newWorkflowCreated(String name) 376 { 377 navigator.addWorkflow(name); 378 navigator.selectWorkflow(name); 379 } 380 381 public Navigator navigator() 382 { 383 return navigator; 384 } 385 386 public void selectWorkflow(String workflowName) 387 { 388 if(graphTabs.selectWorkflow(workflowName)) 389 { 390 Prefs.INSTANCE.put(Prefs.WORKFLOW_CURRENT, workflowName); 391 return; 392 } 393 createGraph(workflowName); 394 Prefs.INSTANCE.put(Prefs.WORKFLOW_CURRENT, workflowName); 395 graphTabs.setVisible(true); 396 } 397 398 private void loadPalette() 399 { 400 try 401 { 402 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 403 dbf.setNamespaceAware(true); 404 405 DocumentBuilder db = null; 406 407 try 408 { 409 db = dbf.newDocumentBuilder(); 410 } 411 catch(ParserConfigurationException e) 412 { 413 e.printStackTrace(); 414 System.exit(1); 415 } 416 417 InputStream is = WorkflowDesigner.class.getResourceAsStream("/META-INF/palette.xml"); 418 Document doc = db.parse(is); 419 ResourceBundle bundle = ResourceBundle.getBundle("META-INF/palette", Locale.getDefault(), getClass().getClassLoader()); 420 Element root = (Element )doc.getElementsByTagName("plugin").item(0); 421 422 palette = new PaletteDescriptor(root, new EnhancedResourceBundle(bundle)); 423 } 424 catch(Exception e) 425 { 426 e.printStackTrace(); 427 } 428 } 429 } 430 | Popular Tags |