KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > him > editors > pagegui > PageDefinitionDialog


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.him.editors.pagegui;
20
21 import java.awt.Component JavaDoc;
22 import java.awt.Container JavaDoc;
23 import java.awt.Dialog JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.Frame JavaDoc;
26 import java.awt.GraphicsConfiguration JavaDoc;
27 import java.awt.HeadlessException JavaDoc;
28 import java.awt.LayoutManager JavaDoc;
29 import java.awt.event.ActionEvent JavaDoc;
30 import java.awt.event.ActionListener JavaDoc;
31 import java.io.StringReader JavaDoc;
32
33 import javax.swing.BorderFactory JavaDoc;
34 import javax.swing.JButton JavaDoc;
35 import javax.swing.JDialog JavaDoc;
36 import javax.swing.JLabel JavaDoc;
37 import javax.swing.JSpinner JavaDoc;
38 import javax.swing.SpinnerModel JavaDoc;
39 import javax.swing.SpinnerNumberModel JavaDoc;
40 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
41
42 import org.openharmonise.commons.xml.*;
43 import org.openharmonise.him.*;
44 import org.openharmonise.him.harmonise.*;
45 import org.openharmonise.him.swing.resourcetree.*;
46 import org.openharmonise.vfs.*;
47 import org.openharmonise.vfs.context.*;
48 import org.openharmonise.vfs.gui.*;
49 import org.openharmonise.vfs.servers.ServerList;
50 import org.w3c.dom.Document JavaDoc;
51 import org.w3c.dom.Element JavaDoc;
52 import org.w3c.dom.Node JavaDoc;
53 import org.w3c.dom.NodeList JavaDoc;
54 import org.w3c.dom.Text JavaDoc;
55
56
57 /**
58  * Dialog for editing page definitions.
59  *
60  * @author Matthew Large
61  * @version $Revision: 1.1 $
62  *
63  */

64 public class PageDefinitionDialog extends JDialog JavaDoc implements LayoutManager JavaDoc, ActionListener JavaDoc, ContextListener {
65     
66     /**
67      * OK button.
68      */

69     private JButton JavaDoc m_okButton = null;
70     
71     /**
72      * Cancel button.
73      */

74     private JButton JavaDoc m_cancelButton = null;
75
76     /**
77      * Add resource button.
78      */

79     private JButton JavaDoc m_addButton = null;
80     
81     /**
82      * Remove resource button.
83      */

84     private JButton JavaDoc m_removeButton = null;
85
86     /**
87      * Resource tree.
88      */

89     private ResourceTree m_tree = null;
90     
91     /**
92      * XML resource field.
93      */

94     private ResourceDisplayPanel m_xmlResourceDisplay = null;
95     
96     /**
97      * XSL resource field.
98      */

99     private ResourceDisplayPanel m_xslResourceDisplay = null;
100     
101     /**
102      * Timeout field.
103      */

104     private JSpinner JavaDoc m_spinner = null;
105     
106     /**
107      * Label for dialog.
108      */

109     private JLabel JavaDoc m_dialogLabel = null;
110     
111     /**
112      * Label for XML field.
113      */

114     private JLabel JavaDoc m_xmlLabel = null;
115     
116     /**
117      * Label for XSL field.
118      */

119     private JLabel JavaDoc m_xslLabel = null;
120     
121     /**
122      * Label for timeout field.
123      */

124     private JLabel JavaDoc m_timeoutLabel = null;
125     
126     /**
127      * true if cancel was pressed.
128      */

129     private boolean m_bCancelled = false;
130     
131
132     /**
133      * Constructs a new page definition editor dialog.
134      *
135      * @param arg0 Owning frame
136      * @param arg1 Title
137      * @throws java.awt.HeadlessException
138      */

139     public PageDefinitionDialog(Frame JavaDoc arg0, String JavaDoc arg1) throws HeadlessException JavaDoc {
140         super(arg0, arg1, true);
141         this.setup();
142     }
143     
144     /**
145      * Configures this dialog.
146      *
147      */

148     private void setup() {
149         ContextHandler.getInstance().addListener(ContextType.CONTEXT_APP_FOCUS, this);
150         this.getContentPane().setLayout(this);
151         
152         this.m_okButton = new JButton JavaDoc("OK");
153         this.m_okButton.setActionCommand("OK");
154         this.m_okButton.addActionListener(this);
155         this.getContentPane().add(m_okButton);
156         
157         this.m_cancelButton = new JButton JavaDoc("Cancel");
158         this.m_cancelButton.setActionCommand("CANCEL");
159         this.m_cancelButton.addActionListener(this);
160         this.getContentPane().add(m_cancelButton);
161         
162         this.m_addButton = new JButton JavaDoc();
163         this.m_addButton.setActionCommand("ADD");
164         this.m_addButton.setIcon( IconManager.getInstance().getIcon("16-command-right.png") );
165         this.m_addButton.addActionListener(this);
166         this.getContentPane().add(this.m_addButton);
167         
168         this.m_removeButton = new JButton JavaDoc();
169         this.m_removeButton.setActionCommand("REMOVE");
170         this.m_removeButton.setIcon( IconManager.getInstance().getIcon("16-command-left.png") );
171         this.m_removeButton.addActionListener(this);
172         this.getContentPane().add(this.m_removeButton);
173         
174         this.m_tree = new ResourceTree();
175         this.m_tree.setBorder(BorderFactory.createEtchedBorder());
176         this.m_tree.setShowLeafNodes(true);
177         this.m_tree.setShowApprovedOnly(true);
178         AbstractVirtualFileSystem vfs = ServerList.getInstance().getHarmoniseServer().getVFS();
179         this.m_tree.addCollection(vfs.getVirtualFile( HarmonisePaths.PATH_PAGE_TEMPLATES ).getResource());
180         this.m_tree.addCollection(vfs.getVirtualFile( HarmonisePaths.PATH_XSLT).getResource());
181         this.getContentPane().add(this.m_tree);
182         
183         this.m_xmlResourceDisplay = new ResourceDisplayPanel();
184         this.getContentPane().add(this.m_xmlResourceDisplay);
185         
186         this.m_xslResourceDisplay = new ResourceDisplayPanel();
187         this.getContentPane().add(this.m_xslResourceDisplay);
188         
189         SpinnerModel JavaDoc model = new SpinnerNumberModel JavaDoc(-1, -1, 100000, 1);
190         this.m_spinner = new JSpinner JavaDoc(model);
191         this.m_spinner.setEditor(new JSpinner.NumberEditor JavaDoc(this.m_spinner));
192         this.getContentPane().add(this.m_spinner);
193
194         this.m_dialogLabel = new JLabel JavaDoc("Select the Page Template, XSLT and a timeout value for this Page Definition.");
195         this.getContentPane().add(this.m_dialogLabel);
196
197         this.m_xmlLabel = new JLabel JavaDoc("Page Template");
198         this.getContentPane().add(this.m_xmlLabel);
199
200         this.m_xslLabel = new JLabel JavaDoc("XSLT");
201         this.getContentPane().add(this.m_xslLabel);
202
203         this.m_timeoutLabel = new JLabel JavaDoc("Timeout (seconds)");
204         this.getContentPane().add(this.m_timeoutLabel);
205
206         this.setSize(450,300);
207         
208         int x = this.getGraphicsConfiguration().getBounds().width/2-this.getSize().width/2;
209         int y = this.getGraphicsConfiguration().getBounds().height/2-this.getSize().height/2;
210         
211         this.setLocation(x, y);
212         
213         
214     }
215
216     /* (non-Javadoc)
217      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
218      */

219     public void actionPerformed(ActionEvent JavaDoc ae) {
220         if(ae.getActionCommand().equals("OK")) {
221             this.hide();
222         } else if(ae.getActionCommand().equals("CANCEL")) {
223             this.m_bCancelled = true;
224             this.hide();
225         } else if(ae.getActionCommand().equals("ADD")) {
226             VirtualFile vfFile = this.m_tree.getSelectedResource();
227             if(vfFile.getFullPath().startsWith(HarmonisePaths.PATH_PAGE_TEMPLATES) && !vfFile.isDirectory()) {
228                 this.m_xmlResourceDisplay.setVirtualFile(vfFile);
229             } else if(vfFile.getFullPath().startsWith(HarmonisePaths.PATH_XSLT) && !vfFile.isDirectory()) {
230                 this.m_xslResourceDisplay.setVirtualFile(vfFile);
231             }
232         } else if(ae.getActionCommand().equals("REMOVE")) {
233             if(this.m_xmlResourceDisplay.isSelected()) {
234                 this.m_xmlResourceDisplay.setVirtualFile(null);
235             }
236             if(this.m_xslResourceDisplay.isSelected()) {
237                 this.m_xslResourceDisplay.setVirtualFile(null);
238             }
239         }
240     }
241
242     /* (non-Javadoc)
243      * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
244      */

245     public void layoutContainer(Container JavaDoc arg0) {
246         this.m_dialogLabel.setSize(420,20);
247         this.m_dialogLabel.setLocation(10, 10);
248         
249         this.m_tree.setSize(200,230);
250         this.m_tree.setLocation(10, 30);
251         
252         this.m_addButton.setSize(20, 20);
253         this.m_addButton.setLocation(220, 90);
254         
255         this.m_removeButton.setSize(20, 20);
256         this.m_removeButton.setLocation(220, 130);
257         
258         this.m_okButton.setSize(70, 20);
259         this.m_okButton.setLocation(270, 240);
260         
261         this.m_cancelButton.setSize(70, 20);
262         this.m_cancelButton.setLocation(350, 240);
263
264         this.m_xmlLabel.setSize(100,20);
265         this.m_xmlLabel.setLocation(270, 50);
266         
267         this.m_xmlResourceDisplay.setSize(150, 20);
268         this.m_xmlResourceDisplay.setLocation(270, 70);
269
270         this.m_xslLabel.setSize(100,20);
271         this.m_xslLabel.setLocation(270, 130);
272         
273         this.m_xslResourceDisplay.setSize(150, 20);
274         this.m_xslResourceDisplay.setLocation(270, 150);
275
276         this.m_timeoutLabel.setSize(100,20);
277         this.m_timeoutLabel.setLocation(270, 180);
278         
279         this.m_spinner.setSize(80, 20);
280         this.m_spinner.setLocation(270, 200);
281     }
282
283     /* (non-Javadoc)
284      * @see com.simulacramedia.contentmanager.context.ContextListener#contextMessage(com.simulacramedia.contentmanager.context.ContextEvent)
285      */

286     public void contextMessage(ContextEvent ce) {
287         if(ce.CONTEXT_TYPE==ContextType.CONTEXT_APP_FOCUS) {
288             this.toFront();
289         }
290     }
291     
292     /**
293      * Sets the data for dialog to edit.
294      *
295      * @param data Data
296      */

297     public void setData(byte[] data) {
298         AbstractVirtualFileSystem vfs = ServerList.getInstance().getHarmoniseServer().getVFS();
299         
300         try {
301             Document JavaDoc xml =
302                     DocumentBuilderFactory
303                         .newInstance()
304                         .newDocumentBuilder()
305                         .parse(
306                         new org.xml.sax.InputSource JavaDoc(new StringReader JavaDoc( new String JavaDoc(data) )));
307         
308             Element JavaDoc rootEl = xml.getDocumentElement();
309         
310             int nTimeout = -1;
311             if(rootEl.hasAttribute("timeout")) {
312                 nTimeout = Integer.parseInt( rootEl.getAttribute("timeout") );
313                 this.m_spinner.getModel().setValue(new Integer JavaDoc(nTimeout));
314             }
315             
316             NodeList JavaDoc nl = rootEl.getChildNodes();
317             for(int i=0; i<nl.getLength(); i++) {
318                 Node JavaDoc node = nl.item(i);
319                 if(node.getNodeType()==Node.ELEMENT_NODE) {
320                     Element JavaDoc element = (Element JavaDoc)node;
321                     if(element.getTagName().equals("XML")) {
322                         NodeList JavaDoc nl2 = element.getChildNodes();
323                         for(int j=0; j<nl2.getLength(); j++) {
324                             Node JavaDoc child = nl2.item(j);
325                             if(child.getNodeType()==Node.ELEMENT_NODE && ((Element JavaDoc)child).getTagName().equals("href") && child.getChildNodes().getLength()==1 && child.getFirstChild().getNodeType()==Node.TEXT_NODE) {
326                                 Text JavaDoc txt = (Text JavaDoc)child.getFirstChild();
327                                 VirtualFile vfFile = vfs.getVirtualFile(txt.getNodeValue()).getResource();
328                                 this.m_xmlResourceDisplay.setVirtualFile(vfFile);
329                             }
330                         }
331                     } else if(element.getTagName().equals("XSL")) {
332                         NodeList JavaDoc nl2 = element.getChildNodes();
333                         for(int j=0; j<nl2.getLength(); j++) {
334                             Node JavaDoc child = nl2.item(j);
335                             if(child.getNodeType()==Node.ELEMENT_NODE && ((Element JavaDoc)child).getTagName().equals("href") && child.getChildNodes().getLength()==1 && child.getFirstChild().getNodeType()==Node.TEXT_NODE) {
336                                 Text JavaDoc txt = (Text JavaDoc)child.getFirstChild();
337                                 VirtualFile vfFile = vfs.getVirtualFile(txt.getNodeValue()).getResource();
338                                 this.m_xslResourceDisplay.setVirtualFile(vfFile);
339                             }
340                         }
341                     }
342                 }
343             }
344             
345             
346         } catch (Exception JavaDoc e) {
347             e.printStackTrace(System.out);
348         }
349     }
350     
351     /**
352      * Returns the edited data.
353      *
354      * @return Data or null if editing was cancelled
355      */

356     public byte[] getData() {
357         byte[] data = null;
358         
359         if(!this.m_bCancelled) {
360             try {
361                 Document JavaDoc xml =
362                     DocumentBuilderFactory
363                         .newInstance()
364                         .newDocumentBuilder()
365                         .newDocument();
366
367                 Element JavaDoc rootEl = xml.createElement("PageDef");
368                 xml.appendChild(rootEl);
369             
370                 rootEl.setAttribute("timeout", ((Integer JavaDoc)this.m_spinner.getValue()).toString());
371             
372                 Element JavaDoc elXML = xml.createElement("XML");
373                 rootEl.appendChild(elXML);
374                 Element JavaDoc elHREF = xml.createElement("href");
375                 elXML.appendChild(elHREF);
376                 VirtualFile vfFile = this.m_xmlResourceDisplay.getVirtualFile();
377                 if(vfFile!=null) {
378                     Text JavaDoc txt = xml.createTextNode( vfFile.getFullPath() );
379                     elHREF.appendChild(txt);
380                 }
381             
382                 Element JavaDoc elXSL = xml.createElement("XSL");
383                 rootEl.appendChild(elXSL);
384                 elHREF = xml.createElement("href");
385                 elXSL.appendChild(elHREF);
386                 vfFile = this.m_xslResourceDisplay.getVirtualFile();
387                 if(vfFile!=null) {
388                     Text JavaDoc txt = xml.createTextNode( vfFile.getFullPath() );
389                     elHREF.appendChild(txt);
390                 }
391             
392                 XMLPrettyPrint printer = new XMLPrettyPrint();
393                 printer.setNamespaceAware(false);
394
395                 String JavaDoc sXML = printer.printNode(xml.getDocumentElement());
396                 data = sXML.getBytes();
397         
398             } catch (Exception JavaDoc e) {
399                 e.printStackTrace(System.out);
400             }
401         }
402         
403         return data;
404     }
405     
406     /**
407      * @throws java.awt.HeadlessException
408      */

409     private PageDefinitionDialog() throws HeadlessException JavaDoc {
410         super();
411     }
412
413     /**
414      * @param arg0
415      * @throws java.awt.HeadlessException
416      */

417     private PageDefinitionDialog(Dialog JavaDoc arg0) throws HeadlessException JavaDoc {
418         super(arg0);
419     }
420
421     /**
422      * @param arg0
423      * @param arg1
424      * @throws java.awt.HeadlessException
425      */

426     private PageDefinitionDialog(Dialog JavaDoc arg0, boolean arg1)
427         throws HeadlessException JavaDoc {
428         super(arg0, arg1);
429     }
430
431     /**
432      * @param arg0
433      * @throws java.awt.HeadlessException
434      */

435     private PageDefinitionDialog(Frame JavaDoc arg0) throws HeadlessException JavaDoc {
436         super(arg0);
437     }
438
439     /**
440      * @param arg0
441      * @param arg1
442      * @throws java.awt.HeadlessException
443      */

444     private PageDefinitionDialog(Frame JavaDoc arg0, boolean arg1)
445         throws HeadlessException JavaDoc {
446         super(arg0, arg1);
447     }
448
449     /**
450      * @param arg0
451      * @param arg1
452      * @throws java.awt.HeadlessException
453      */

454     private PageDefinitionDialog(Dialog JavaDoc arg0, String JavaDoc arg1)
455         throws HeadlessException JavaDoc {
456         super(arg0, arg1);
457     }
458
459     /**
460      * @param arg0
461      * @param arg1
462      * @param arg2
463      * @throws java.awt.HeadlessException
464      */

465     private PageDefinitionDialog(Dialog JavaDoc arg0, String JavaDoc arg1, boolean arg2)
466         throws HeadlessException JavaDoc {
467         super(arg0, arg1, arg2);
468     }
469
470     /**
471      * @param arg0
472      * @param arg1
473      * @param arg2
474      * @throws java.awt.HeadlessException
475      */

476     private PageDefinitionDialog(Frame JavaDoc arg0, String JavaDoc arg1, boolean arg2)
477         throws HeadlessException JavaDoc {
478         super(arg0, arg1, arg2);
479     }
480
481     /**
482      * @param arg0
483      * @param arg1
484      * @param arg2
485      * @param arg3
486      * @throws java.awt.HeadlessException
487      */

488     private PageDefinitionDialog(
489         Dialog JavaDoc arg0,
490         String JavaDoc arg1,
491         boolean arg2,
492         GraphicsConfiguration JavaDoc arg3)
493         throws HeadlessException JavaDoc {
494         super(arg0, arg1, arg2, arg3);
495     }
496
497     /**
498      * @param arg0
499      * @param arg1
500      * @param arg2
501      * @param arg3
502      */

503     private PageDefinitionDialog(
504         Frame JavaDoc arg0,
505         String JavaDoc arg1,
506         boolean arg2,
507         GraphicsConfiguration JavaDoc arg3) {
508         super(arg0, arg1, arg2, arg3);
509     }
510
511     /* (non-Javadoc)
512      * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
513      */

514     public void removeLayoutComponent(Component JavaDoc arg0) {
515     }
516
517     /* (non-Javadoc)
518      * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
519      */

520     public void addLayoutComponent(String JavaDoc arg0, Component JavaDoc arg1) {
521     }
522
523     /* (non-Javadoc)
524      * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
525      */

526     public Dimension JavaDoc minimumLayoutSize(Container JavaDoc arg0) {
527         return this.getSize();
528     }
529
530     /* (non-Javadoc)
531      * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
532      */

533     public Dimension JavaDoc preferredLayoutSize(Container JavaDoc arg0) {
534         return this.getSize();
535     }
536
537 }
Popular Tags