View Javadoc

1   /*
2    * $Id$
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  
22  package org.xmlhammer.gui.xslt;
23  
24  import java.awt.BorderLayout;
25  import java.awt.event.ItemEvent;
26  import java.awt.event.ItemListener;
27  import java.net.URI;
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  import javax.swing.Box;
32  import javax.swing.JCheckBox;
33  import javax.swing.JComboBox;
34  import javax.swing.JFileChooser;
35  import javax.swing.JLabel;
36  import javax.swing.JPanel;
37  import javax.swing.border.EmptyBorder;
38  
39  import org.bounce.FormConstraints;
40  import org.bounce.FormLayout;
41  import org.bounce.util.URIUtils;
42  import org.xmlhammer.gui.history.HistoryComboBox;
43  import org.xmlhammer.gui.history.HistoryComboBoxModel;
44  import org.xmlhammer.gui.history.HistoryUtilities;
45  import org.xmlhammer.gui.overview.CheckBoxNode;
46  import org.xmlhammer.gui.overview.ComboBoxNode;
47  import org.xmlhammer.gui.overview.OverviewNode;
48  import org.xmlhammer.gui.util.URISelectionNode;
49  import org.xmlhammer.gui.util.URISelectionPane;
50  import org.xmlhammer.gui.util.UndoableCheckBoxItemListener;
51  import org.xmlhammer.gui.util.UndoableComboBoxItemListener;
52  import org.xmlhammer.model.tools.xslt.OutputProperty;
53  
54  /***
55   * Input Panel.
56   * 
57   * Allows to select either one URI, multiple URIs or 
58   * a range of files.
59   * 
60   * @version $Revision$, $Date$
61   * @author Edwin Dankert <edankert@gmail.com>
62   */
63  
64  public class XMLOutputPanel extends JPanel {
65      private static final long serialVersionUID = -277458243364064175L;
66      public static final String OUTPUT_METHOD_DEFAULT = "default";
67      public static final String OUTPUT_METHOD_XML = "xml";
68      public static final String OUTPUT_METHOD_HTML = "html";
69      public static final String OUTPUT_METHOD_TEXT = "text";
70      public static final String OUTPUT_METHOD_OTHER = "other";
71  
72      private ArrayList<OverviewNode> nodes      = null;
73      private OutputPage parent = null;
74      private static String VERSION_10 = "1.0";
75      private static String VERSION_11 = "1.1";
76      private static String DEFAULT_XML_MEDIA_TYPE = "text/xml";
77      private static String DEFAULT_HTML_MEDIA_TYPE = "text/html";
78      private static String DEFAULT_TEXT_MEDIA_TYPE = "text/plain";
79  
80      private JFileChooser chooser    = null;
81      private JComboBox versionField             = null;
82  	private JComboBox encodingField            = null;
83  	private JCheckBox omitXMLDeclarationCheck  = null;
84  	private JComboBox standaloneCombo		   = null;
85  	private HistoryComboBox doctypePublicField = null;
86      private HistoryComboBox methodField = null;
87      private URISelectionPane doctypeSystemField = null;
88  
89      private HistoryComboBox cdataSectionElements  = null;
90      private JCheckBox indentCheck                   = null;
91      private JComboBox mediaTypeField                = null;
92      private String method                           = null;
93      private OverviewNode versionNode                = null;
94      private OverviewNode encodingNode                = null;
95      private OverviewNode standaloneNode                = null;
96  
97      /***
98  	 * Constructs a new Input Panel.
99  	 */
100 	public XMLOutputPanel(OutputPage page, String method) {
101 		super( new FormLayout( 11, 5));
102 		
103 		setBorder( new EmptyBorder( 10, 10, 10, 10));
104         
105         this.method = method;
106         this.parent = page;
107         chooser = new JFileChooser();
108         
109         JLabel longestLabel = null;
110         
111         if (method == OUTPUT_METHOD_XML || method == OUTPUT_METHOD_DEFAULT) {
112             longestLabel = new JLabel( "cdata-section-elements:");
113         } else if (method == OUTPUT_METHOD_HTML) {
114             longestLabel = new JLabel( "doctype-system:");
115         }
116 		
117         if (method == OUTPUT_METHOD_OTHER) {
118             methodField = new HistoryComboBox(parent, new HistoryComboBoxModel(HistoryUtilities.getInstance().getDoctypePublics()));
119             
120             if (parent != null) {
121                 parent.getProjectView().getFieldManager().addField(doctypePublicField);
122             }
123 
124             add(new JLabel("method:"), new FormConstraints(FormConstraints.LEFT, FormConstraints.RIGHT));
125             add(methodField, FormLayout.RIGHT_FILL);
126         }
127 
128         if (method != OUTPUT_METHOD_HTML && method != OUTPUT_METHOD_TEXT && method != OUTPUT_METHOD_OTHER) {
129             add( new JLabel(), FormLayout.LEFT);
130             omitXMLDeclarationCheck = new JCheckBox( "omit-xml-declaration");
131             omitXMLDeclarationCheck.setEnabled(method != OUTPUT_METHOD_DEFAULT);
132             
133             if (parent != null) {
134                 parent.getProjectView().getFieldManager().addField(omitXMLDeclarationCheck);
135             }
136             
137             add(omitXMLDeclarationCheck, FormLayout.RIGHT);
138             omitXMLDeclarationCheck.addItemListener(new UndoableCheckBoxItemListener(parent, omitXMLDeclarationCheck));
139             omitXMLDeclarationCheck.addItemListener(new ItemListener() {
140                 public void itemStateChanged(ItemEvent arg0) {
141                     versionField.setEnabled(!omitXMLDeclarationCheck.isSelected());
142                     encodingField.setEnabled(!omitXMLDeclarationCheck.isSelected());
143                     standaloneCombo.setEnabled(!omitXMLDeclarationCheck.isSelected());
144                     
145                     if (omitXMLDeclarationCheck.isSelected()) {
146                         if (parent != null) {
147                             parent.getProjectView().getOverviewPanel().selectNode(parent);
148                             parent.getProjectView().getOverviewPanel().getModel().nodeRemoved(versionNode);
149                         }
150                         
151                         nodes.remove(versionNode);
152 
153                         if (parent != null) {
154                             parent.getProjectView().getOverviewPanel().getModel().nodeRemoved(encodingNode);
155                         }
156                         
157                         nodes.remove(encodingNode);
158 
159                         if (parent != null) {
160                             parent.getProjectView().getOverviewPanel().getModel().nodeRemoved(standaloneNode);
161                         }
162                         
163                         nodes.remove(standaloneNode);
164                     } else {
165                         nodes.add(1, versionNode);
166                         
167                         if (parent != null) {
168                             parent.getProjectView().getOverviewPanel().getModel().nodeInserted(versionNode);
169                         }
170                         
171                         nodes.add(2, encodingNode);
172 
173                         if (parent != null) {
174                             parent.getProjectView().getOverviewPanel().getModel().nodeInserted(encodingNode);
175                         }
176                         
177                         nodes.add(3, standaloneNode);
178 
179                         if (parent != null) {
180                             parent.getProjectView().getOverviewPanel().getModel().nodeInserted(standaloneNode);
181                         }
182                     }
183                 }
184             });
185         }
186 
187         if (method != OUTPUT_METHOD_TEXT && method != OUTPUT_METHOD_OTHER) {
188             versionField = new JComboBox();
189             versionField.setEditable(true);
190             versionField.setEnabled(method != OUTPUT_METHOD_DEFAULT);
191             
192             if (parent != null) {
193                 parent.getProjectView().getFieldManager().addField(versionField);
194             }
195             
196             versionField.addItemListener(new UndoableComboBoxItemListener(parent, versionField));
197 
198             JLabel label = new JLabel("version:");
199             label.setEnabled(method != OUTPUT_METHOD_DEFAULT);
200             label.setPreferredSize(longestLabel.getPreferredSize());
201             label.setHorizontalAlignment(JLabel.RIGHT);
202             add(label, new FormConstraints(FormConstraints.LEFT, FormConstraints.RIGHT));
203             add(versionField, FormLayout.RIGHT);
204 
205             if (method == OUTPUT_METHOD_HTML) {
206                 versionField.addItem("4.0");
207                 versionField.setSelectedIndex(0);
208             } else if (method == OUTPUT_METHOD_XML) {
209                 versionField.addItem(VERSION_10);
210                 versionField.addItem(VERSION_11);
211                 versionField.setSelectedIndex(0);
212             }
213         }
214 
215         if (method != OUTPUT_METHOD_OTHER) {
216             encodingField = new JComboBox();
217             encodingField.setEditable(true);
218             encodingField.setEnabled(method != OUTPUT_METHOD_DEFAULT);
219             
220             if (parent != null) {
221                 parent.getProjectView().getFieldManager().addField(encodingField);
222             }
223             
224             encodingField.addItemListener(new UndoableComboBoxItemListener(parent, encodingField));
225 
226             JLabel label = new JLabel("encoding:");
227             label.setEnabled(method != OUTPUT_METHOD_DEFAULT);
228             add(label, new FormConstraints( FormConstraints.LEFT, FormConstraints.RIGHT));
229             add(encodingField, FormLayout.RIGHT);
230             encodingField.addItem("UTF-8");
231             encodingField.addItem("UTF-16");
232             encodingField.addItem("US-ASCII");
233             // TODO add more encodings
234             encodingField.setSelectedItem(null);
235         }
236 
237         if (method != OUTPUT_METHOD_TEXT && method != OUTPUT_METHOD_OTHER && method != OUTPUT_METHOD_HTML) {
238             JLabel label = new JLabel("standalone:");
239             label.setEnabled(method != OUTPUT_METHOD_DEFAULT);
240             add(label, new FormConstraints(FormConstraints.LEFT, FormConstraints.RIGHT));
241             standaloneCombo = new JComboBox();
242             standaloneCombo.setEnabled(method != OUTPUT_METHOD_DEFAULT);
243             
244             if (parent != null) {
245                 parent.getProjectView().getFieldManager().addField(standaloneCombo);
246             }
247             
248             standaloneCombo.addItemListener(new UndoableComboBoxItemListener(parent, standaloneCombo));
249             add(standaloneCombo, FormLayout.RIGHT);
250             standaloneCombo.addItem("<none>");
251             standaloneCombo.addItem("no");
252             standaloneCombo.addItem("yes");
253             standaloneCombo.setSelectedIndex(0);
254         }
255 
256         if (method != OUTPUT_METHOD_TEXT && method != OUTPUT_METHOD_OTHER) {
257             add(Box.createVerticalStrut(10), FormLayout.FULL);
258         }
259 
260         if (method != OUTPUT_METHOD_TEXT && method != OUTPUT_METHOD_OTHER) {
261             if (parent != null) {
262                 doctypeSystemField = new URISelectionPane(parent, parent.getProjectView().getUndoManager(), "doctype-system:", chooser, longestLabel.getPreferredSize().width, new HistoryComboBoxModel(HistoryUtilities.getInstance().getDoctypeSystems()));
263             } else {
264                 doctypeSystemField = new URISelectionPane(null, null, "doctype-system:", chooser, longestLabel.getPreferredSize().width, new HistoryComboBoxModel(HistoryUtilities.getInstance().getDoctypeSystems()));
265             }
266 
267             doctypeSystemField.setEnabled(method != OUTPUT_METHOD_DEFAULT);
268     		add(doctypeSystemField, FormLayout.FULL_FILL);
269         }
270 		        
271         if (method != OUTPUT_METHOD_TEXT && method != OUTPUT_METHOD_OTHER) {
272             doctypePublicField = new HistoryComboBox(parent, new HistoryComboBoxModel(HistoryUtilities.getInstance().getDoctypePublics()));
273             doctypePublicField.setEnabled(method != OUTPUT_METHOD_DEFAULT);
274             JPanel panel = new JPanel(new BorderLayout(10, 0));
275             panel.add(doctypePublicField, BorderLayout.CENTER);
276             panel.add(Box.createHorizontalStrut(doctypeSystemField.getButtonWidth()), BorderLayout.EAST);
277             
278             if (parent != null) {
279                 parent.getProjectView().getFieldManager().addField(doctypePublicField);
280             }
281 
282             JLabel label = new JLabel("doctype-public:");
283             label.setEnabled(method != OUTPUT_METHOD_DEFAULT);
284             add(label, new FormConstraints(FormConstraints.LEFT, FormConstraints.RIGHT));
285             add(panel, FormLayout.RIGHT_FILL);
286         }
287 
288         add(Box.createVerticalStrut(10), FormLayout.FULL);
289 
290         if (method != OUTPUT_METHOD_TEXT && method != OUTPUT_METHOD_OTHER) {
291             add(new JLabel(), FormLayout.LEFT);
292             indentCheck = new JCheckBox("indent");
293             indentCheck.setEnabled(method != OUTPUT_METHOD_DEFAULT);
294             
295             if (parent != null) {
296                 parent.getProjectView().getFieldManager().addField(indentCheck);
297             }
298             
299             add(indentCheck, FormLayout.RIGHT);
300             indentCheck.addItemListener(new UndoableCheckBoxItemListener(parent, indentCheck));
301         }
302 
303         if (method != OUTPUT_METHOD_TEXT && method != OUTPUT_METHOD_HTML && method != OUTPUT_METHOD_OTHER) {
304             cdataSectionElements = new HistoryComboBox(parent, new HistoryComboBoxModel(HistoryUtilities.getInstance().getCDataSectionElements()));
305             cdataSectionElements.setEnabled(method != OUTPUT_METHOD_DEFAULT);
306             JPanel panel = new JPanel(new BorderLayout(10, 0));
307             panel.add(cdataSectionElements, BorderLayout.CENTER);
308             panel.add(Box.createHorizontalStrut(doctypeSystemField.getButtonWidth()), BorderLayout.EAST);
309 
310             if (parent != null) {
311                 parent.getProjectView().getFieldManager().addField(cdataSectionElements);
312             }
313             
314             JLabel label = new JLabel("cdata-section-elements:");
315             label.setEnabled(method != OUTPUT_METHOD_DEFAULT);
316 
317             add(label, new FormConstraints( FormConstraints.LEFT, FormConstraints.RIGHT));
318             add(panel, FormLayout.RIGHT_FILL);
319         }
320 
321 
322         if (method != OUTPUT_METHOD_OTHER) {
323             mediaTypeField = new JComboBox();
324             mediaTypeField.setEnabled(method != OUTPUT_METHOD_DEFAULT);
325             mediaTypeField.setEditable(true);
326             
327             if (parent != null) {
328                 parent.getProjectView().getFieldManager().addField(mediaTypeField);
329             }
330 
331             JLabel label = new JLabel("media-type:");
332             label.setEnabled(method != OUTPUT_METHOD_DEFAULT);
333             add(label, new FormConstraints( FormConstraints.LEFT, FormConstraints.RIGHT));
334             add(mediaTypeField, FormLayout.RIGHT);
335 
336             if (method == OUTPUT_METHOD_TEXT) {
337                 mediaTypeField.addItem(DEFAULT_TEXT_MEDIA_TYPE);
338                 mediaTypeField.setSelectedIndex(0);
339             } else if (method == OUTPUT_METHOD_HTML) {
340                 mediaTypeField.addItem(DEFAULT_HTML_MEDIA_TYPE);
341                 mediaTypeField.setSelectedIndex(0);
342             } else if (method == OUTPUT_METHOD_XML) {
343                 mediaTypeField.addItem(DEFAULT_XML_MEDIA_TYPE);
344                 mediaTypeField.setSelectedIndex(0);
345             }
346             
347             mediaTypeField.addItemListener(new UndoableComboBoxItemListener(parent, mediaTypeField));
348         }
349 
350         nodes = new ArrayList<OverviewNode>();
351         if (method != OUTPUT_METHOD_DEFAULT && method != OUTPUT_METHOD_OTHER) {
352             if (method == OUTPUT_METHOD_XML) {
353                 nodes.add(new CheckBoxNode(parent, omitXMLDeclarationCheck));
354             }
355 
356             if (method != OUTPUT_METHOD_TEXT) {
357                 versionNode = new ComboBoxNode(parent, versionField, "version");
358                 nodes.add(versionNode);
359             }
360             
361             encodingNode = new ComboBoxNode(parent, encodingField, "encoding");
362             nodes.add(encodingNode);
363             
364             if (method == OUTPUT_METHOD_XML) {
365                 standaloneNode = new ComboBoxNode(parent, standaloneCombo, "standalone");
366                 nodes.add(standaloneNode);
367             } 
368             
369             if (method != OUTPUT_METHOD_TEXT) {
370                 nodes.add(new URISelectionNode(parent, doctypeSystemField, "doctype-system"));
371                 nodes.add(new ComboBoxNode(parent, doctypePublicField, "doctype-public"));
372                 nodes.add(new CheckBoxNode(parent, indentCheck));
373             }
374             
375             if (method == OUTPUT_METHOD_XML) {
376                 nodes.add(new ComboBoxNode(parent, cdataSectionElements, "cdata-section-elements"));
377             }
378             
379             nodes.add(new ComboBoxNode(parent, mediaTypeField, "media-type"));
380         } else if (method == OUTPUT_METHOD_OTHER) {
381             nodes.add(new ComboBoxNode(parent, methodField, "method"));
382         }
383    	}
384     
385     public String getMethod() {
386         return method;
387     }
388 
389 	public void setProperties(List<OutputProperty> properties) {
390         if (methodField != null) {
391             methodField.setSelectedItem(null);
392         }
393 
394         if (omitXMLDeclarationCheck != null) {
395             omitXMLDeclarationCheck.setSelected(false);
396         }
397         
398         if (versionField != null) {
399             versionField.setSelectedItem(VERSION_10);
400         }
401         
402         if (encodingField != null) {
403             encodingField.setSelectedItem(null);
404         }
405 
406         if (standaloneCombo != null) {
407             standaloneCombo.setSelectedIndex(0);
408         }
409         
410         if (doctypeSystemField != null) {
411             doctypeSystemField.setURI(null);
412         }
413         
414         if (doctypePublicField != null) {
415             doctypePublicField.setSelectedItem(null);
416         }
417         
418         if (indentCheck != null) {
419             indentCheck.setSelected(false);
420         }
421         
422         if (cdataSectionElements != null) {
423             cdataSectionElements.setSelectedItem(null);
424         }
425         
426         if (mediaTypeField != null && mediaTypeField.getItemCount() > 0) {
427             mediaTypeField.setSelectedIndex(0);
428         }
429 
430         if (properties != null) {
431             for (OutputProperty property : properties) {
432                 if (omitXMLDeclarationCheck != null && property.getName().equals("omit-xml-declaration")) {
433                     omitXMLDeclarationCheck.setSelected(property.getValue().equals("yes"));
434                 } else if (methodField != null && property.getName().equals("method")) {
435                     methodField.setSelectedItem(property.getValue());
436                 } else if (versionField != null && property.getName().equals("version")) {
437                     versionField.setSelectedItem(property.getValue());
438                 } else if (encodingField != null && property.getName().equals("encoding")) {
439                     encodingField.setSelectedItem(property.getValue());
440                 } else if (standaloneCombo != null && property.getName().equals("standalone")) {
441                     standaloneCombo.setSelectedItem(property.getValue());
442                 } else if (doctypeSystemField != null && property.getName().equals("doctype-system")) {
443                     doctypeSystemField.setURI(URIUtils.createURI(property.getValue()));
444                 } else if (doctypePublicField != null && property.getName().equals("doctype-public")) {
445                     doctypePublicField.setSelectedItem(property.getValue());
446                 } else if (indentCheck != null && property.getName().equals("indent")) {
447                     indentCheck.setSelected(property.getValue().equals("yes"));
448                 } else if (cdataSectionElements != null && property.getName().equals("cdata-section-elements")) {
449                     cdataSectionElements.setSelectedItem(property.getValue());
450                 } else if (mediaTypeField != null && property.getName().equals("media-type")) {
451                     mediaTypeField.setSelectedItem(property.getValue());
452                 }
453             }
454         }
455 	}
456 	
457 	public List<OutputProperty> getProperties(URI base) {
458         List<OutputProperty> properties = new ArrayList<OutputProperty>();
459         
460         if (omitXMLDeclarationCheck != null && omitXMLDeclarationCheck.isSelected()) {
461             OutputProperty property = new OutputProperty();
462             property.setName("omit-xml-declaration");
463             property.setValue("yes");
464             properties.add(property);
465         } else {
466             if (versionField != null && versionField.getSelectedItem() != null && versionField.isEnabled()) { 
467                 OutputProperty property = new OutputProperty();
468                 property.setName("version");
469                 property.setValue(versionField.getSelectedItem().toString());
470                 properties.add(property);
471             } 
472             if (encodingField != null && encodingField.getSelectedItem() != null) { 
473                 OutputProperty property = new OutputProperty();
474                 property.setName("encoding");
475                 property.setValue(encodingField.getSelectedItem().toString());
476                 properties.add(property);
477             } 
478             if (standaloneCombo != null && standaloneCombo.getSelectedIndex() != 0) { 
479                 OutputProperty property = new OutputProperty();
480                 property.setName("standalone");
481                 property.setValue(standaloneCombo.getSelectedItem().toString());
482                 properties.add(property);
483             } 
484         } 
485         
486         if (doctypeSystemField != null && doctypeSystemField.getURI() != null) { 
487             OutputProperty property = new OutputProperty();
488             property.setName("doctype-system");
489             property.setValue(URIUtils.toString(doctypeSystemField.getURI()));
490             properties.add(property);
491         } 
492         
493         if (methodField != null && methodField.getSelectedItem() != null) { 
494             OutputProperty property = new OutputProperty();
495             property.setName("method");
496             property.setValue(methodField.getSelectedItem().toString());
497             properties.add(property);
498         } 
499 
500         if (doctypePublicField != null && doctypePublicField.getSelectedItem() != null) { 
501             OutputProperty property = new OutputProperty();
502             property.setName("doctype-public");
503             property.setValue(doctypePublicField.getSelectedItem().toString());
504             properties.add(property);
505         } 
506 
507         if (indentCheck != null && indentCheck.isSelected()) {
508             OutputProperty property = new OutputProperty();
509             property.setName("indent");
510             property.setValue("yes");
511             properties.add(property);
512         }
513 
514         if (cdataSectionElements != null && cdataSectionElements.getSelectedItem() != null) { 
515             OutputProperty property = new OutputProperty();
516             property.setName("cdata-section-elements");
517             property.setValue(cdataSectionElements.getSelectedItem().toString());
518             properties.add(property);
519         } 
520 
521         if (mediaTypeField != null && mediaTypeField.getSelectedItem() != null) { 
522             OutputProperty property = new OutputProperty();
523             property.setName("media-type");
524             property.setValue(mediaTypeField.getSelectedItem().toString());
525             properties.add(property);
526         } 
527 
528 		return properties;
529 	}
530 	
531     public ArrayList<OverviewNode> getNodes() {
532         return nodes;
533     }
534 }