1 package org.oddjob.designer; 2 3 import java.awt.Component ; 4 import java.awt.event.ActionEvent ; 5 import java.awt.event.WindowAdapter ; 6 import java.awt.event.WindowEvent ; 7 import java.io.File ; 8 import java.io.FileNotFoundException ; 9 import java.io.FileOutputStream ; 10 import java.io.IOException ; 11 import java.io.OutputStream ; 12 13 import javax.swing.AbstractAction ; 14 import javax.swing.Action ; 15 import javax.swing.JFileChooser ; 16 import javax.swing.JFrame ; 17 import javax.swing.JMenu ; 18 import javax.swing.JMenuItem ; 19 import javax.swing.JOptionPane ; 20 import javax.swing.JSeparator ; 21 import javax.swing.SwingUtilities ; 22 import javax.swing.UIManager ; 23 import javax.swing.WindowConstants ; 24 25 import org.oddjob.OddjobException; 26 import org.oddjob.Stoppable; 27 import org.oddjob.arooa.ArooaFactory; 28 import org.oddjob.arooa.ArooaContext; 29 import org.oddjob.arooa.handlers.MainHandler; 30 import org.oddjob.arooa.handlers.XmlHandler; 31 import org.oddjob.designer.arooa.DesignComponentHandler; 32 import org.oddjob.designer.arooa.DesignElementHandler; 33 import org.oddjob.designer.arooa.DesignParser; 34 import org.oddjob.designer.arooa.DesignStartHandler; 35 import org.oddjob.designer.arooa.DesignValueHandler; 36 import org.oddjob.designer.components.RootDC; 37 import org.oddjob.designer.factory.DesignFactory; 38 import org.oddjob.designer.model.DesignComponent; 39 import org.oddjob.designer.model.DesignerModel; 40 import org.oddjob.designer.view.DesignerPanel; 41 import org.oddjob.designer.view.DesignerMenuBar; 42 import org.oddjob.framework.SimpleJob; 43 import org.oddjob.monitor.OddjobExplorer; 44 import org.oddjob.monitor.Standards; 45 import org.oddjob.state.AbstractJobStateListener; 46 import org.oddjob.state.JobStateEvent; 47 import org.xml.sax.InputSource ; 48 import org.xml.sax.SAXParseException ; 49 50 55 56 public class OddjobDesigner extends SimpleJob implements Stoppable { 57 58 63 private transient volatile File file; 64 65 66 private transient File dir; 67 68 private transient DesignerModel designerModel; 69 70 71 private transient JFrame frame; 72 73 74 private transient DesignerMenuBar menuBar; 75 76 private transient final Action newAction = new NewAction(); 77 78 private transient final Action openAction = new OpenAction(); 79 80 private transient final Action closeAction = new CloseAction(); 81 82 private transient final Action saveAction = new SaveAction(); 83 84 private transient final Action saveAsAction = new SaveAsAction(); 85 86 private transient final Action exitAction = new ExitAction(); 87 88 private transient final Action explorerAction = new ExplorerAction(); 89 90 private transient OddjobExplorer explorer; 91 92 96 public OddjobDesigner() { 97 closeAction.setEnabled(false); 98 saveAction.setEnabled(false); 99 saveAsAction.setEnabled(false); 100 } 101 102 108 public void setFile(File configFile) { 109 this.file = configFile; 110 if (file != null) { 111 this.dir = configFile.getAbsoluteFile().getParentFile(); 112 } 113 title(); 114 } 115 116 119 void title() { 120 if (frame != null) { 121 frame.setTitle("OddJob Designer" + (file == null 122 ? "" : " - " + file.getName())); 123 } 124 125 } 126 127 132 public File getFile() { 133 return this.file; 134 } 135 136 141 public void setDir(File dir) { 142 this.dir = dir; 143 } 144 145 public void setExplorer(OddjobExplorer expl) { 146 if (this.explorer != null) { 147 throw new IllegalStateException ("Explorer already set."); 148 } 149 this.explorer = expl; 150 151 explorer.addJobStateListener(new AbstractJobStateListener() { 152 public void hasFinishedRunning(JobStateEvent event) { 153 explorer.removeJobStateListener(this); 154 explorer = null; 155 } 156 }); 157 } 158 159 public void forceLoad(File file) { 160 if (frame == null) { 161 throw new IllegalStateException ("No frame - explorer must have stopped."); 162 } 163 frame.toFront(); 164 if (!destroyView()) { 165 return; 166 } 167 setFile(file); 168 if (file == null) { 169 return; 170 } 171 load(); 172 createView(); 173 } 174 175 178 protected void load() { 179 logger().debug("Loading oddjob designer."); 180 181 ArooaFactory af = new ArooaFactory(); 182 af.setComponentFactory(DesignFactory.componentFactory()); 183 af.setComponentHandler(new DesignComponentHandler()); 184 af.setElementHandler(new DesignElementHandler()); 185 af.setPropertyHandler(new DesignValueHandler()); 186 187 DesignComponent root = new RootDC(); 188 af.setDocumentStartHandler(new DesignStartHandler(root)); 189 af.setDocumentTag("oddjob"); 190 InputSource inputSource = null; 191 if (file != null) { 192 af.build(file); 193 } else { 194 throw new OddjobException("No input specified."); 195 } 196 197 198 designerModel = new DesignerModel(root); 199 } 200 201 public void save() { 202 OutputStream out = null; 203 try { 204 out = new FileOutputStream (file); 205 } catch (FileNotFoundException ex) { 206 logger().error(ex); 207 return; 208 } 209 XmlHandler handler = new XmlHandler(out); 210 DesignParser dp = new DesignParser(new ArooaContext()); 211 try { 212 dp.parse("oddjob", designerModel.getRootComponent(), 213 new MainHandler("oddjob", handler)); 214 } catch (SAXParseException ex) { 215 logger().error(ex); 216 } 217 try { 218 out.close(); 219 } catch (IOException ex) { 220 logger().error(ex); 221 } 222 } 223 224 private boolean destroyView() { 225 if (designerModel == null) { 226 return true; 228 } 229 designerModel = null; 230 231 closeAction.setEnabled(false); 232 saveAction.setEnabled(false); 233 saveAsAction.setEnabled(false); 234 235 frame.getContentPane().removeAll(); 236 frame.getContentPane().validate(); 237 frame.getContentPane().repaint(); 238 menuBar.setDesignerModel(null); 239 return true; 241 } 242 243 private void createView() { 244 closeAction.setEnabled(true); 245 saveAction.setEnabled(true); 246 saveAsAction.setEnabled(true); 247 248 Component treeComp = new DesignerPanel(designerModel, menuBar); 249 250 frame.getContentPane().add(treeComp); 251 frame.pack(); 252 frame.validate(); 253 menuBar.setDesignerModel(designerModel); 254 } 255 256 void createMenuBar() { 257 menuBar = new DesignerMenuBar(); 258 JMenu fileMenu = menuBar.getFileMenu(); 259 260 fileMenu.add(new JMenuItem (newAction)); 261 fileMenu.add(new JMenuItem (openAction)); 262 fileMenu.add(new JMenuItem (closeAction)); 263 fileMenu.add(new JMenuItem (saveAction)); 264 fileMenu.add(new JMenuItem (saveAsAction)); 265 fileMenu.add(new JSeparator ()); 266 fileMenu.add(new JMenuItem (explorerAction)); 267 fileMenu.add(new JSeparator ()); 268 fileMenu.add(new JMenuItem (exitAction)); 269 } 270 271 276 protected int execute() throws Exception { 277 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 278 279 frame = new JFrame (); 280 title(); 281 createMenuBar(); 282 283 frame.addWindowListener(new WindowAdapter () { 284 285 public void windowClosed(WindowEvent e) { 286 stop = true; 287 synchronized (OddjobDesigner.this) { 288 OddjobDesigner.this.notifyAll(); 289 } 290 logger().debug("Monitor closed."); 291 } 292 }); 293 294 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 295 frame.setJMenuBar(menuBar); 296 if (file != null) { 297 load(); 298 createView(); 299 } 300 else { 301 frame.setSize(Looks.DESIGNER_WIDTH, Looks.DESIGNER_HEIGHT); 302 } 303 frame.setVisible(true); 304 305 306 while (!stop) { 307 synchronized (this) { 308 try { 309 wait(); 310 } catch (InterruptedException e) { 311 e.printStackTrace(); 312 } 313 } 314 } 315 frame = null; 316 return 0; 317 } 318 319 322 public void onStop() { 323 SwingUtilities.invokeLater(new Runnable () { 324 public void run() { 325 if (frame != null) { 326 frame.dispose(); 327 } else { 328 logger().debug("Designer hasn't been started."); 329 } 330 } 331 }); 332 } 333 334 class NewAction extends AbstractAction { 335 NewAction() { 336 putValue(Action.NAME, "New"); 337 putValue(Action.MNEMONIC_KEY, Standards.NEW_MNEMONIC_KEY); 338 putValue(Action.ACCELERATOR_KEY, Standards.NEW_ACCELERATOR_KEY); 339 } 340 341 346 public void actionPerformed(ActionEvent e) { 347 if (!destroyView()) { 348 return; 349 } 350 351 DesignComponent root = new RootDC(); 352 designerModel = new DesignerModel(root); 353 createView(); 354 } 355 } 356 357 class OpenAction extends AbstractAction { 358 OpenAction() { 359 putValue(Action.NAME, "Open"); 360 putValue(Action.MNEMONIC_KEY, Standards.OPEN_MNEMONIC_KEY); 361 putValue(Action.ACCELERATOR_KEY, Standards.OPEN_ACCELERATOR_KEY); 362 } 363 364 369 public void actionPerformed(ActionEvent e) { 370 JFileChooser chooser = new JFileChooser (); 371 if (dir != null) { 372 chooser.setCurrentDirectory(dir); 373 } 374 375 int option = chooser.showOpenDialog(frame); 376 if (option != JFileChooser.APPROVE_OPTION) { 377 return; 378 } 379 if (!destroyView()) { 380 return; 381 } 382 383 setFile(chooser.getSelectedFile()); 384 load(); 385 createView(); 386 } 387 } 388 389 class CloseAction extends AbstractAction { 390 CloseAction() { 391 putValue(Action.NAME, "Close"); 392 putValue(Action.MNEMONIC_KEY, Standards.CLOSE_MNEMONIC_KEY); 393 putValue(Action.ACCELERATOR_KEY, Standards.CLOSE_ACCELERATOR_KEY); 394 } 395 396 401 public void actionPerformed(ActionEvent e) { 402 if (!destroyView()) { 403 return; 404 } 405 setFile(null); 406 } 407 } 408 409 class SaveAction extends AbstractAction { 410 SaveAction() { 411 putValue(Action.NAME, "Save"); 412 putValue(Action.MNEMONIC_KEY, Standards.SAVE_MNEMONIC_KEY); 413 putValue(Action.ACCELERATOR_KEY, Standards.SAVE_ACCELERATOR_KEY); 414 } 415 416 421 public void actionPerformed(ActionEvent e) { 422 if (file == null) { 423 saveAsAction.actionPerformed(e); 424 } else { 425 save(); 426 } 427 } 428 } 429 430 class SaveAsAction extends AbstractAction { 431 SaveAsAction() { 432 putValue(Action.NAME, "Save As..."); 433 putValue(Action.MNEMONIC_KEY, Standards.SAVEAS_MNEMONIC_KEY); 434 putValue(Action.ACCELERATOR_KEY, Standards.SAVEAS_ACCELERATOR_KEY); 435 } 436 437 442 public void actionPerformed(ActionEvent e) { 443 JFileChooser chooser = new JFileChooser (); 444 if (dir != null) { 445 chooser.setCurrentDirectory(dir); 446 } 447 int option = chooser.showSaveDialog(frame); 448 if (option != JFileChooser.APPROVE_OPTION) { 449 return; 450 } 451 452 setFile(chooser.getSelectedFile()); 453 save(); 454 } 455 } 456 457 class ExitAction extends AbstractAction { 458 ExitAction() { 459 putValue(Action.NAME, "Exit"); 460 putValue(Action.MNEMONIC_KEY, Standards.EXIT_MNEMONIC_KEY); 461 } 462 463 468 public void actionPerformed(ActionEvent e) { 469 if (!destroyView()) { 470 return; 471 } 472 stop(); 473 } 474 } 475 476 class ExplorerAction extends AbstractAction { 477 ExplorerAction() { 478 putValue(Action.NAME, "Oddjob Explorer"); 479 putValue(Action.MNEMONIC_KEY, Standards.EXPLORER_MNEMONIC_KEY); 480 putValue(Action.ACCELERATOR_KEY, Standards.EXPLORER_ACCELERATOR_KEY); 481 } 482 483 488 public void actionPerformed(ActionEvent e) { 489 try { 490 if (explorer == null) { 491 OddjobExplorer expl = new OddjobExplorer(); 492 expl.setDir(dir); 493 expl.setFile(file); 494 setExplorer(expl); 495 Thread t = new Thread (explorer); 496 t.start(); 497 } 498 else { 499 explorer.show(); 500 } 501 } 502 catch (Exception ex) { 503 logger().debug("Oddjob Explorer Failed to launch.", ex); 504 JOptionPane.showMessageDialog(frame, ex, "Exception!", JOptionPane.ERROR_MESSAGE); 505 } 506 507 } 508 } 509 510 } 511 | Popular Tags |