KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > designer > OddjobDesigner


1 package org.oddjob.designer;
2
3 import java.awt.Component JavaDoc;
4 import java.awt.event.ActionEvent JavaDoc;
5 import java.awt.event.WindowAdapter JavaDoc;
6 import java.awt.event.WindowEvent JavaDoc;
7 import java.io.File JavaDoc;
8 import java.io.FileNotFoundException JavaDoc;
9 import java.io.FileOutputStream JavaDoc;
10 import java.io.IOException JavaDoc;
11 import java.io.OutputStream JavaDoc;
12
13 import javax.swing.AbstractAction JavaDoc;
14 import javax.swing.Action JavaDoc;
15 import javax.swing.JFileChooser JavaDoc;
16 import javax.swing.JFrame JavaDoc;
17 import javax.swing.JMenu JavaDoc;
18 import javax.swing.JMenuItem JavaDoc;
19 import javax.swing.JOptionPane JavaDoc;
20 import javax.swing.JSeparator JavaDoc;
21 import javax.swing.SwingUtilities JavaDoc;
22 import javax.swing.UIManager JavaDoc;
23 import javax.swing.WindowConstants JavaDoc;
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 JavaDoc;
48 import org.xml.sax.SAXParseException JavaDoc;
49
50 /**
51  * @oddjob.description Run a GUI designer for Oddjob.
52  *
53  * @author Rob Gordon
54  */

55
56 public class OddjobDesigner extends SimpleJob implements Stoppable {
57
58     /**
59      * @oddjob.property
60      * @oddjob.description The oddjob file.
61      * @oddjob.required Yes.
62      */

63     private transient volatile File JavaDoc file;
64
65     /** The default directory. used when opening files. */
66     private transient File JavaDoc dir;
67
68     private transient DesignerModel designerModel;
69
70     /** The frame */
71     private transient JFrame JavaDoc frame;
72
73     /** The menu bar. */
74     private transient DesignerMenuBar menuBar;
75
76     private transient final Action JavaDoc newAction = new NewAction();
77
78     private transient final Action JavaDoc openAction = new OpenAction();
79
80     private transient final Action JavaDoc closeAction = new CloseAction();
81     
82     private transient final Action JavaDoc saveAction = new SaveAction();
83
84     private transient final Action JavaDoc saveAsAction = new SaveAsAction();
85
86     private transient final Action JavaDoc exitAction = new ExitAction();
87     
88     private transient final Action JavaDoc explorerAction = new ExplorerAction();
89
90     private transient OddjobExplorer explorer;
91     
92     /**
93      * Constructor.
94      *
95      */

96     public OddjobDesigner() {
97         closeAction.setEnabled(false);
98         saveAction.setEnabled(false);
99         saveAsAction.setEnabled(false);
100     }
101
102     /**
103      * Set the config file name.
104      *
105      * @param configFile
106      * The config file name.
107      */

108     public void setFile(File JavaDoc configFile) {
109         this.file = configFile;
110         if (file != null) {
111             this.dir = configFile.getAbsoluteFile().getParentFile();
112         }
113         title();
114     }
115
116     /**
117      * Change the title.
118      */

119     void title() {
120         if (frame != null) {
121             frame.setTitle("OddJob Designer" + (file == null
122                     ? "" : " - " + file.getName()));
123         }
124         
125     }
126     
127     /**
128      * Get the config file name.
129      *
130      * @return The config file name.
131      */

132     public File JavaDoc getFile() {
133         return this.file;
134     }
135
136     /**
137      * Set the default directory.
138      *
139      * @param dir A directory.
140      */

141     public void setDir(File JavaDoc dir) {
142         this.dir = dir;
143     }
144
145     public void setExplorer(OddjobExplorer expl) {
146         if (this.explorer != null) {
147             throw new IllegalStateException JavaDoc("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 JavaDoc file) {
160         if (frame == null) {
161             throw new IllegalStateException JavaDoc("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     /**
176      * Load Oddjob from the configuration file.
177      */

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 JavaDoc 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 JavaDoc out = null;
203         try {
204             out = new FileOutputStream JavaDoc(file);
205         } catch (FileNotFoundException JavaDoc 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 JavaDoc ex) {
215             logger().error(ex);
216         }
217         try {
218             out.close();
219         } catch (IOException JavaDoc ex) {
220             logger().error(ex);
221         }
222     }
223     
224     private boolean destroyView() {
225         if (designerModel == null) {
226             // already destroyed
227
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         // one day check if file has changed!
240
return true;
241     }
242
243     private void createView() {
244         closeAction.setEnabled(true);
245         saveAction.setEnabled(true);
246         saveAsAction.setEnabled(true);
247         
248         Component JavaDoc 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 JavaDoc fileMenu = menuBar.getFileMenu();
259         
260         fileMenu.add(new JMenuItem JavaDoc(newAction));
261         fileMenu.add(new JMenuItem JavaDoc(openAction));
262         fileMenu.add(new JMenuItem JavaDoc(closeAction));
263         fileMenu.add(new JMenuItem JavaDoc(saveAction));
264         fileMenu.add(new JMenuItem JavaDoc(saveAsAction));
265         fileMenu.add(new JSeparator JavaDoc());
266         fileMenu.add(new JMenuItem JavaDoc(explorerAction));
267         fileMenu.add(new JSeparator JavaDoc());
268         fileMenu.add(new JMenuItem JavaDoc(exitAction));
269     }
270     
271     /*
272      * (non-Javadoc)
273      *
274      * @see org.oddjob.jobs.AbstractJob#execute()
275      */

276     protected int execute() throws Exception JavaDoc {
277         UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
278         
279         frame = new JFrame JavaDoc();
280         title();
281         createMenuBar();
282         
283         frame.addWindowListener(new WindowAdapter JavaDoc() {
284
285             public void windowClosed(WindowEvent JavaDoc 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 JavaDoc e) {
311                     e.printStackTrace();
312                 }
313             }
314         }
315         frame = null;
316         return 0;
317     }
318
319     /**
320      * Stop the monitor.
321      */

322     public void onStop() {
323         SwingUtilities.invokeLater(new Runnable JavaDoc() {
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 JavaDoc {
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         /*
342          * (non-Javadoc)
343          *
344          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
345          */

346         public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc {
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         /*
365          * (non-Javadoc)
366          *
367          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
368          */

369         public void actionPerformed(ActionEvent JavaDoc e) {
370             JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
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 JavaDoc {
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         /*
397          * (non-Javadoc)
398          *
399          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
400          */

401         public void actionPerformed(ActionEvent JavaDoc e) {
402             if (!destroyView()) {
403                 return;
404             }
405             setFile(null);
406         }
407     }
408     
409     class SaveAction extends AbstractAction JavaDoc {
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         /*
417          * (non-Javadoc)
418          *
419          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
420          */

421         public void actionPerformed(ActionEvent JavaDoc e) {
422             if (file == null) {
423                 saveAsAction.actionPerformed(e);
424             } else {
425                 save();
426             }
427         }
428     }
429
430     class SaveAsAction extends AbstractAction JavaDoc {
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         /*
438          * (non-Javadoc)
439          *
440          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
441          */

442         public void actionPerformed(ActionEvent JavaDoc e) {
443             JFileChooser JavaDoc chooser = new JFileChooser JavaDoc();
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 JavaDoc {
458         ExitAction() {
459             putValue(Action.NAME, "Exit");
460             putValue(Action.MNEMONIC_KEY, Standards.EXIT_MNEMONIC_KEY);
461         }
462
463         /*
464          * (non-Javadoc)
465          *
466          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
467          */

468         public void actionPerformed(ActionEvent JavaDoc e) {
469             if (!destroyView()) {
470                 return;
471             }
472             stop();
473         }
474     }
475     
476     class ExplorerAction extends AbstractAction JavaDoc {
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         /*
484          * (non-Javadoc)
485          *
486          * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
487          */

488         public void actionPerformed(ActionEvent JavaDoc 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 JavaDoc t = new Thread JavaDoc(explorer);
496                     t.start();
497                 }
498                 else {
499                     explorer.show();
500                 }
501             }
502             catch (Exception JavaDoc 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