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.schemavalidator;
23  
24  import java.awt.BorderLayout;
25  import java.awt.Font;
26  import java.net.URISyntaxException;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import javax.swing.JComboBox;
31  import javax.swing.JLabel;
32  import javax.swing.JPanel;
33  import javax.xml.XMLConstants;
34  
35  import org.xmlhammer.gui.overview.ComboBoxNode;
36  import org.xmlhammer.gui.overview.OverviewNode;
37  import org.xmlhammer.gui.util.UndoableComboBoxItemListener;
38  import org.xmlhammer.model.project.SchemaValidator;
39  
40  /***
41   * Input Panel.
42   * 
43   * Allows to select either one URI, multiple URIs or 
44   * a range of files.
45   * 
46   * @version $Revision$, $Date$
47   * @author Edwin Dankert <edankert@gmail.com>
48   */
49  
50  public class SchemaPanel extends JPanel {
51      private static final long serialVersionUID = 5530326695690039953L;
52  
53      private JComboBox schemaLanguageField       = null;
54      private ArrayList<OverviewNode> nodes       = null;
55  
56  	/***
57  	 * Constructs a new Input Panel.
58  	 */
59  	public SchemaPanel(SchemaPage page) {
60  		super(new BorderLayout( 10, 0));
61  		
62          nodes = new ArrayList<OverviewNode>();
63          
64          schemaLanguageField = new JComboBox();
65          schemaLanguageField.addItem(XMLConstants.W3C_XML_SCHEMA_NS_URI);
66          schemaLanguageField.addItem(XMLConstants.RELAXNG_NS_URI);
67          schemaLanguageField.setEditable(false);
68          schemaLanguageField.setFont(schemaLanguageField.getFont().deriveFont( Font.PLAIN));
69          schemaLanguageField.setToolTipText("Specify a Schema language.");
70          schemaLanguageField.addItemListener(new UndoableComboBoxItemListener(page, schemaLanguageField));
71  
72          if (page != null) {
73              page.getProjectView().getFieldManager().addField(schemaLanguageField);
74          }
75  
76          add(new JLabel("Language:"), BorderLayout.WEST);
77          add(schemaLanguageField, BorderLayout.CENTER);
78  
79          nodes.add(new ComboBoxNode(page, schemaLanguageField, "schema"));
80  	}
81  	
82      public void setSchemaLanguage(String schemaLanguage) {
83          if ( schemaLanguage == null || schemaLanguage.trim().length() == 0) {
84              schemaLanguageField.setSelectedIndex(0);
85          } else {
86              schemaLanguageField.setSelectedItem(schemaLanguage);
87          }
88     }
89  
90      public void setSchemaLanguages(List<String> languages) {
91          int index = schemaLanguageField.getSelectedIndex();
92  
93          if (schemaLanguageField.getItemCount() > 0) {
94              schemaLanguageField.removeAllItems();
95          }
96          
97          for (String language : languages) {
98              schemaLanguageField.addItem(language);
99          }
100         
101         if (languages.size() > index) {
102             schemaLanguageField.setSelectedIndex(index);
103         } else {
104             schemaLanguageField.setSelectedIndex(0);
105         }
106    }
107 
108     public String getSchemaLanguage() {
109         return (String)schemaLanguageField.getSelectedItem();
110     }
111 
112     /***
113 	 * Sets the input model.
114 	 * Null resets current values.
115 	 * 
116 	 * @param parser the parser settings.
117 	 * @throws URISyntaxException
118 	 */
119 	public void setSchemaValidator(SchemaValidator validator) {
120         if (validator != null && validator.getLanguage() != null) {
121             schemaLanguageField.setSelectedItem(validator.getLanguage());
122         } else {
123             schemaLanguageField.setSelectedIndex(0);
124         }
125 	}
126     
127 	public SchemaValidator getSchemaValidator() {
128         SchemaValidator validator = new SchemaValidator();
129         
130         if (schemaLanguageField.getSelectedItem() != null) {
131             validator.setLanguage(schemaLanguageField.getSelectedItem().toString());
132         }
133 		
134 		return validator;
135 	}
136     
137     /* (non-Javadoc)
138      * @see org.xmlhammer.gui.overview.OverviewNode#getChildNodes()
139      */
140     public ArrayList<OverviewNode> getChildNodes() {
141         return nodes;
142     }
143 
144     public void dispose() {
145     }
146 }