001: /*
002: * The contents of this file are subject to the
003: * Mozilla Public License Version 1.1 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
006: *
007: * Software distributed under the License is distributed on an "AS IS"
008: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
009: * See the License for the specific language governing rights and
010: * limitations under the License.
011: *
012: * The Initial Developer of the Original Code is Simulacra Media Ltd.
013: * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
014: *
015: * All Rights Reserved.
016: *
017: * Contributor(s):
018: */
019: package org.openharmonise.him.editors.pagegui;
020:
021: import java.awt.Component;
022: import java.awt.Container;
023: import java.awt.Dialog;
024: import java.awt.Dimension;
025: import java.awt.Frame;
026: import java.awt.GraphicsConfiguration;
027: import java.awt.HeadlessException;
028: import java.awt.LayoutManager;
029: import java.awt.event.ActionEvent;
030: import java.awt.event.ActionListener;
031: import java.io.StringReader;
032:
033: import javax.swing.BorderFactory;
034: import javax.swing.JButton;
035: import javax.swing.JDialog;
036: import javax.swing.JLabel;
037: import javax.swing.JSpinner;
038: import javax.swing.SpinnerModel;
039: import javax.swing.SpinnerNumberModel;
040: import javax.xml.parsers.DocumentBuilderFactory;
041:
042: import org.openharmonise.commons.xml.*;
043: import org.openharmonise.him.*;
044: import org.openharmonise.him.harmonise.*;
045: import org.openharmonise.him.swing.resourcetree.*;
046: import org.openharmonise.vfs.*;
047: import org.openharmonise.vfs.context.*;
048: import org.openharmonise.vfs.gui.*;
049: import org.openharmonise.vfs.servers.ServerList;
050: import org.w3c.dom.Document;
051: import org.w3c.dom.Element;
052: import org.w3c.dom.Node;
053: import org.w3c.dom.NodeList;
054: import org.w3c.dom.Text;
055:
056: /**
057: * Dialog for editing page definitions.
058: *
059: * @author Matthew Large
060: * @version $Revision: 1.1 $
061: *
062: */
063: public class PageDefinitionDialog extends JDialog implements
064: LayoutManager, ActionListener, ContextListener {
065:
066: /**
067: * OK button.
068: */
069: private JButton m_okButton = null;
070:
071: /**
072: * Cancel button.
073: */
074: private JButton m_cancelButton = null;
075:
076: /**
077: * Add resource button.
078: */
079: private JButton m_addButton = null;
080:
081: /**
082: * Remove resource button.
083: */
084: private JButton m_removeButton = null;
085:
086: /**
087: * Resource tree.
088: */
089: private ResourceTree m_tree = null;
090:
091: /**
092: * XML resource field.
093: */
094: private ResourceDisplayPanel m_xmlResourceDisplay = null;
095:
096: /**
097: * XSL resource field.
098: */
099: private ResourceDisplayPanel m_xslResourceDisplay = null;
100:
101: /**
102: * Timeout field.
103: */
104: private JSpinner m_spinner = null;
105:
106: /**
107: * Label for dialog.
108: */
109: private JLabel m_dialogLabel = null;
110:
111: /**
112: * Label for XML field.
113: */
114: private JLabel m_xmlLabel = null;
115:
116: /**
117: * Label for XSL field.
118: */
119: private JLabel m_xslLabel = null;
120:
121: /**
122: * Label for timeout field.
123: */
124: private JLabel m_timeoutLabel = null;
125:
126: /**
127: * true if cancel was pressed.
128: */
129: private boolean m_bCancelled = false;
130:
131: /**
132: * Constructs a new page definition editor dialog.
133: *
134: * @param arg0 Owning frame
135: * @param arg1 Title
136: * @throws java.awt.HeadlessException
137: */
138: public PageDefinitionDialog(Frame arg0, String arg1)
139: throws HeadlessException {
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(
150: ContextType.CONTEXT_APP_FOCUS, this );
151: this .getContentPane().setLayout(this );
152:
153: this .m_okButton = new JButton("OK");
154: this .m_okButton.setActionCommand("OK");
155: this .m_okButton.addActionListener(this );
156: this .getContentPane().add(m_okButton);
157:
158: this .m_cancelButton = new JButton("Cancel");
159: this .m_cancelButton.setActionCommand("CANCEL");
160: this .m_cancelButton.addActionListener(this );
161: this .getContentPane().add(m_cancelButton);
162:
163: this .m_addButton = new JButton();
164: this .m_addButton.setActionCommand("ADD");
165: this .m_addButton.setIcon(IconManager.getInstance().getIcon(
166: "16-command-right.png"));
167: this .m_addButton.addActionListener(this );
168: this .getContentPane().add(this .m_addButton);
169:
170: this .m_removeButton = new JButton();
171: this .m_removeButton.setActionCommand("REMOVE");
172: this .m_removeButton.setIcon(IconManager.getInstance().getIcon(
173: "16-command-left.png"));
174: this .m_removeButton.addActionListener(this );
175: this .getContentPane().add(this .m_removeButton);
176:
177: this .m_tree = new ResourceTree();
178: this .m_tree.setBorder(BorderFactory.createEtchedBorder());
179: this .m_tree.setShowLeafNodes(true);
180: this .m_tree.setShowApprovedOnly(true);
181: AbstractVirtualFileSystem vfs = ServerList.getInstance()
182: .getHarmoniseServer().getVFS();
183: this .m_tree.addCollection(vfs.getVirtualFile(
184: HarmonisePaths.PATH_PAGE_TEMPLATES).getResource());
185: this .m_tree.addCollection(vfs.getVirtualFile(
186: HarmonisePaths.PATH_XSLT).getResource());
187: this .getContentPane().add(this .m_tree);
188:
189: this .m_xmlResourceDisplay = new ResourceDisplayPanel();
190: this .getContentPane().add(this .m_xmlResourceDisplay);
191:
192: this .m_xslResourceDisplay = new ResourceDisplayPanel();
193: this .getContentPane().add(this .m_xslResourceDisplay);
194:
195: SpinnerModel model = new SpinnerNumberModel(-1, -1, 100000, 1);
196: this .m_spinner = new JSpinner(model);
197: this .m_spinner.setEditor(new JSpinner.NumberEditor(
198: this .m_spinner));
199: this .getContentPane().add(this .m_spinner);
200:
201: this .m_dialogLabel = new JLabel(
202: "Select the Page Template, XSLT and a timeout value for this Page Definition.");
203: this .getContentPane().add(this .m_dialogLabel);
204:
205: this .m_xmlLabel = new JLabel("Page Template");
206: this .getContentPane().add(this .m_xmlLabel);
207:
208: this .m_xslLabel = new JLabel("XSLT");
209: this .getContentPane().add(this .m_xslLabel);
210:
211: this .m_timeoutLabel = new JLabel("Timeout (seconds)");
212: this .getContentPane().add(this .m_timeoutLabel);
213:
214: this .setSize(450, 300);
215:
216: int x = this .getGraphicsConfiguration().getBounds().width / 2
217: - this .getSize().width / 2;
218: int y = this .getGraphicsConfiguration().getBounds().height / 2
219: - this .getSize().height / 2;
220:
221: this .setLocation(x, y);
222:
223: }
224:
225: /* (non-Javadoc)
226: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
227: */
228: public void actionPerformed(ActionEvent ae) {
229: if (ae.getActionCommand().equals("OK")) {
230: this .hide();
231: } else if (ae.getActionCommand().equals("CANCEL")) {
232: this .m_bCancelled = true;
233: this .hide();
234: } else if (ae.getActionCommand().equals("ADD")) {
235: VirtualFile vfFile = this .m_tree.getSelectedResource();
236: if (vfFile.getFullPath().startsWith(
237: HarmonisePaths.PATH_PAGE_TEMPLATES)
238: && !vfFile.isDirectory()) {
239: this .m_xmlResourceDisplay.setVirtualFile(vfFile);
240: } else if (vfFile.getFullPath().startsWith(
241: HarmonisePaths.PATH_XSLT)
242: && !vfFile.isDirectory()) {
243: this .m_xslResourceDisplay.setVirtualFile(vfFile);
244: }
245: } else if (ae.getActionCommand().equals("REMOVE")) {
246: if (this .m_xmlResourceDisplay.isSelected()) {
247: this .m_xmlResourceDisplay.setVirtualFile(null);
248: }
249: if (this .m_xslResourceDisplay.isSelected()) {
250: this .m_xslResourceDisplay.setVirtualFile(null);
251: }
252: }
253: }
254:
255: /* (non-Javadoc)
256: * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
257: */
258: public void layoutContainer(Container arg0) {
259: this .m_dialogLabel.setSize(420, 20);
260: this .m_dialogLabel.setLocation(10, 10);
261:
262: this .m_tree.setSize(200, 230);
263: this .m_tree.setLocation(10, 30);
264:
265: this .m_addButton.setSize(20, 20);
266: this .m_addButton.setLocation(220, 90);
267:
268: this .m_removeButton.setSize(20, 20);
269: this .m_removeButton.setLocation(220, 130);
270:
271: this .m_okButton.setSize(70, 20);
272: this .m_okButton.setLocation(270, 240);
273:
274: this .m_cancelButton.setSize(70, 20);
275: this .m_cancelButton.setLocation(350, 240);
276:
277: this .m_xmlLabel.setSize(100, 20);
278: this .m_xmlLabel.setLocation(270, 50);
279:
280: this .m_xmlResourceDisplay.setSize(150, 20);
281: this .m_xmlResourceDisplay.setLocation(270, 70);
282:
283: this .m_xslLabel.setSize(100, 20);
284: this .m_xslLabel.setLocation(270, 130);
285:
286: this .m_xslResourceDisplay.setSize(150, 20);
287: this .m_xslResourceDisplay.setLocation(270, 150);
288:
289: this .m_timeoutLabel.setSize(100, 20);
290: this .m_timeoutLabel.setLocation(270, 180);
291:
292: this .m_spinner.setSize(80, 20);
293: this .m_spinner.setLocation(270, 200);
294: }
295:
296: /* (non-Javadoc)
297: * @see com.simulacramedia.contentmanager.context.ContextListener#contextMessage(com.simulacramedia.contentmanager.context.ContextEvent)
298: */
299: public void contextMessage(ContextEvent ce) {
300: if (ce.CONTEXT_TYPE == ContextType.CONTEXT_APP_FOCUS) {
301: this .toFront();
302: }
303: }
304:
305: /**
306: * Sets the data for dialog to edit.
307: *
308: * @param data Data
309: */
310: public void setData(byte[] data) {
311: AbstractVirtualFileSystem vfs = ServerList.getInstance()
312: .getHarmoniseServer().getVFS();
313:
314: try {
315: Document xml = DocumentBuilderFactory
316: .newInstance()
317: .newDocumentBuilder()
318: .parse(
319: new org.xml.sax.InputSource(
320: new StringReader(new String(data))));
321:
322: Element rootEl = xml.getDocumentElement();
323:
324: int nTimeout = -1;
325: if (rootEl.hasAttribute("timeout")) {
326: nTimeout = Integer.parseInt(rootEl
327: .getAttribute("timeout"));
328: this .m_spinner.getModel().setValue(
329: new Integer(nTimeout));
330: }
331:
332: NodeList nl = rootEl.getChildNodes();
333: for (int i = 0; i < nl.getLength(); i++) {
334: Node node = nl.item(i);
335: if (node.getNodeType() == Node.ELEMENT_NODE) {
336: Element element = (Element) node;
337: if (element.getTagName().equals("XML")) {
338: NodeList nl2 = element.getChildNodes();
339: for (int j = 0; j < nl2.getLength(); j++) {
340: Node child = nl2.item(j);
341: if (child.getNodeType() == Node.ELEMENT_NODE
342: && ((Element) child).getTagName()
343: .equals("href")
344: && child.getChildNodes()
345: .getLength() == 1
346: && child.getFirstChild()
347: .getNodeType() == Node.TEXT_NODE) {
348: Text txt = (Text) child.getFirstChild();
349: VirtualFile vfFile = vfs
350: .getVirtualFile(
351: txt.getNodeValue())
352: .getResource();
353: this .m_xmlResourceDisplay
354: .setVirtualFile(vfFile);
355: }
356: }
357: } else if (element.getTagName().equals("XSL")) {
358: NodeList nl2 = element.getChildNodes();
359: for (int j = 0; j < nl2.getLength(); j++) {
360: Node child = nl2.item(j);
361: if (child.getNodeType() == Node.ELEMENT_NODE
362: && ((Element) child).getTagName()
363: .equals("href")
364: && child.getChildNodes()
365: .getLength() == 1
366: && child.getFirstChild()
367: .getNodeType() == Node.TEXT_NODE) {
368: Text txt = (Text) child.getFirstChild();
369: VirtualFile vfFile = vfs
370: .getVirtualFile(
371: txt.getNodeValue())
372: .getResource();
373: this .m_xslResourceDisplay
374: .setVirtualFile(vfFile);
375: }
376: }
377: }
378: }
379: }
380:
381: } catch (Exception e) {
382: e.printStackTrace(System.out);
383: }
384: }
385:
386: /**
387: * Returns the edited data.
388: *
389: * @return Data or null if editing was cancelled
390: */
391: public byte[] getData() {
392: byte[] data = null;
393:
394: if (!this .m_bCancelled) {
395: try {
396: Document xml = DocumentBuilderFactory.newInstance()
397: .newDocumentBuilder().newDocument();
398:
399: Element rootEl = xml.createElement("PageDef");
400: xml.appendChild(rootEl);
401:
402: rootEl.setAttribute("timeout",
403: ((Integer) this .m_spinner.getValue())
404: .toString());
405:
406: Element elXML = xml.createElement("XML");
407: rootEl.appendChild(elXML);
408: Element elHREF = xml.createElement("href");
409: elXML.appendChild(elHREF);
410: VirtualFile vfFile = this .m_xmlResourceDisplay
411: .getVirtualFile();
412: if (vfFile != null) {
413: Text txt = xml.createTextNode(vfFile.getFullPath());
414: elHREF.appendChild(txt);
415: }
416:
417: Element elXSL = xml.createElement("XSL");
418: rootEl.appendChild(elXSL);
419: elHREF = xml.createElement("href");
420: elXSL.appendChild(elHREF);
421: vfFile = this .m_xslResourceDisplay.getVirtualFile();
422: if (vfFile != null) {
423: Text txt = xml.createTextNode(vfFile.getFullPath());
424: elHREF.appendChild(txt);
425: }
426:
427: XMLPrettyPrint printer = new XMLPrettyPrint();
428: printer.setNamespaceAware(false);
429:
430: String sXML = printer.printNode(xml
431: .getDocumentElement());
432: data = sXML.getBytes();
433:
434: } catch (Exception e) {
435: e.printStackTrace(System.out);
436: }
437: }
438:
439: return data;
440: }
441:
442: /**
443: * @throws java.awt.HeadlessException
444: */
445: private PageDefinitionDialog() throws HeadlessException {
446: super ();
447: }
448:
449: /**
450: * @param arg0
451: * @throws java.awt.HeadlessException
452: */
453: private PageDefinitionDialog(Dialog arg0) throws HeadlessException {
454: super (arg0);
455: }
456:
457: /**
458: * @param arg0
459: * @param arg1
460: * @throws java.awt.HeadlessException
461: */
462: private PageDefinitionDialog(Dialog arg0, boolean arg1)
463: throws HeadlessException {
464: super (arg0, arg1);
465: }
466:
467: /**
468: * @param arg0
469: * @throws java.awt.HeadlessException
470: */
471: private PageDefinitionDialog(Frame arg0) throws HeadlessException {
472: super (arg0);
473: }
474:
475: /**
476: * @param arg0
477: * @param arg1
478: * @throws java.awt.HeadlessException
479: */
480: private PageDefinitionDialog(Frame arg0, boolean arg1)
481: throws HeadlessException {
482: super (arg0, arg1);
483: }
484:
485: /**
486: * @param arg0
487: * @param arg1
488: * @throws java.awt.HeadlessException
489: */
490: private PageDefinitionDialog(Dialog arg0, String arg1)
491: throws HeadlessException {
492: super (arg0, arg1);
493: }
494:
495: /**
496: * @param arg0
497: * @param arg1
498: * @param arg2
499: * @throws java.awt.HeadlessException
500: */
501: private PageDefinitionDialog(Dialog arg0, String arg1, boolean arg2)
502: throws HeadlessException {
503: super (arg0, arg1, arg2);
504: }
505:
506: /**
507: * @param arg0
508: * @param arg1
509: * @param arg2
510: * @throws java.awt.HeadlessException
511: */
512: private PageDefinitionDialog(Frame arg0, String arg1, boolean arg2)
513: throws HeadlessException {
514: super (arg0, arg1, arg2);
515: }
516:
517: /**
518: * @param arg0
519: * @param arg1
520: * @param arg2
521: * @param arg3
522: * @throws java.awt.HeadlessException
523: */
524: private PageDefinitionDialog(Dialog arg0, String arg1,
525: boolean arg2, GraphicsConfiguration arg3)
526: throws HeadlessException {
527: super (arg0, arg1, arg2, arg3);
528: }
529:
530: /**
531: * @param arg0
532: * @param arg1
533: * @param arg2
534: * @param arg3
535: */
536: private PageDefinitionDialog(Frame arg0, String arg1, boolean arg2,
537: GraphicsConfiguration arg3) {
538: super (arg0, arg1, arg2, arg3);
539: }
540:
541: /* (non-Javadoc)
542: * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
543: */
544: public void removeLayoutComponent(Component arg0) {
545: }
546:
547: /* (non-Javadoc)
548: * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, java.awt.Component)
549: */
550: public void addLayoutComponent(String arg0, Component arg1) {
551: }
552:
553: /* (non-Javadoc)
554: * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
555: */
556: public Dimension minimumLayoutSize(Container arg0) {
557: return this .getSize();
558: }
559:
560: /* (non-Javadoc)
561: * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
562: */
563: public Dimension preferredLayoutSize(Container arg0) {
564: return this.getSize();
565: }
566:
567: }
|