View Javadoc

1   /*
2    * $Id: SchemaURIsPanel.java,v 1.18 2008/01/14 21:23:24 edankert Exp $
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.parser;
23  
24  import java.awt.BorderLayout;
25  import java.awt.Font;
26  import java.awt.event.ItemEvent;
27  import java.awt.event.ItemListener;
28  import java.net.URI;
29  import java.util.ArrayList;
30  import java.util.List;
31  
32  import javax.swing.Box;
33  import javax.swing.Icon;
34  import javax.swing.ImageIcon;
35  import javax.swing.JComboBox;
36  import javax.swing.JFileChooser;
37  import javax.swing.JLabel;
38  import javax.swing.JPanel;
39  import javax.swing.border.EmptyBorder;
40  import javax.xml.XMLConstants;
41  
42  import org.bounce.DefaultFileFilter;
43  import org.bounce.image.ImageLoader;
44  import org.xmlhammer.gui.history.HistoryUtilities;
45  import org.xmlhammer.gui.overview.OverviewNode;
46  import org.xmlhammer.gui.overview.OverviewTreeModel;
47  import org.xmlhammer.gui.util.URIsPanel;
48  import org.xmlhammer.gui.util.UndoableComboBoxItemListener;
49  import org.xmlhammer.model.project.Document;
50  
51  /***
52   * Input Panel.
53   * 
54   * Allows to select either one URI, multiple URIs or 
55   * a range of files.
56   * 
57   * @version $Revision: 1.18 $, $Date: 2008/01/14 21:23:24 $
58   * @author Edwin Dankert <edankert@gmail.com>
59   */
60  
61  public class SchemaURIsPanel extends JPanel implements ItemListener, OverviewNode {
62  	private static final long serialVersionUID = 3689910673756729653L;
63  	private static final String NO_LANGUAGE = "<No Language Specified>";
64  
65      private static final ImageIcon COMBO_ICON = ImageLoader.get().getImage( "/org/xmlhammer/gui/icons/elcl16/view_menu.gif");
66      
67  	private JFileChooser chooser 	= null;
68  	private ParserPage parent 		= null;
69  	private JComboBox schemaLanguageField	= null;
70  	private JLabel schemaLabel		= null;
71  	private URIsPanel urisPanel		= null;
72      private DefaultFileFilter schemaFilter = null;
73      private DefaultFileFilter relaxNGFilter = null;
74      private boolean removing = false;
75      private boolean updatingLanguages = false;
76  
77  	/***
78  	 * Constructs a new Input Panel.
79  	 */
80  	public SchemaURIsPanel( ParserPage parent) {
81  		super( new BorderLayout());
82  		
83  		this.parent = parent;
84  		
85  		setBorder( new EmptyBorder( 15, 0, 0, 0));
86  
87  		schemaLanguageField = new JComboBox();
88  		schemaLanguageField.addItem( NO_LANGUAGE);
89  		schemaLanguageField.addItem( XMLConstants.W3C_XML_SCHEMA_NS_URI);
90  		schemaLanguageField.addItem( XMLConstants.RELAXNG_NS_URI);
91  		schemaLanguageField.setEditable( false);
92  		schemaLanguageField.setFont( schemaLanguageField.getFont().deriveFont( Font.PLAIN));
93  		schemaLanguageField.setToolTipText( "Specify a Schema language.");
94  		schemaLanguageField.addItemListener( this);
95          schemaLanguageField.addItemListener(new UndoableComboBoxItemListener(parent, schemaLanguageField));
96  
97          if (parent != null) {
98              parent.getProjectView().getFieldManager().addField( schemaLanguageField);
99          }
100 
101 		schemaLabel = new JLabel( "Language:");
102 		
103 		chooser = new JFileChooser();
104         chooser.setAcceptAllFileFilterUsed( true);
105         chooser.addChoosableFileFilter( getSchemaFileFilter());
106         chooser.addChoosableFileFilter( getRelaxNGFileFilter());
107 		
108         urisPanel = new URIsPanel(parent, this, HistoryUtilities.getInstance().getSchemasModel(), chooser, schemaLabel.getPreferredSize().width);
109 
110 		JPanel schemaPanel = new JPanel( new BorderLayout( 10, 0));
111 		schemaPanel.add( schemaLabel, BorderLayout.WEST);
112 		schemaPanel.add( schemaLanguageField, BorderLayout.CENTER);
113 		schemaPanel.add( Box.createHorizontalStrut( urisPanel.getButtonWidth() + 10), BorderLayout.EAST);
114 
115 		JPanel mainPanel = new JPanel( new BorderLayout( 0, 5));
116 		mainPanel.add( schemaPanel, BorderLayout.NORTH);
117 		mainPanel.add( urisPanel, BorderLayout.CENTER);
118 		
119 		add( mainPanel, BorderLayout.NORTH);
120         
121         urisPanel.setEnabled(false);
122 	}
123     
124     public int getLabelWidth() {
125         return schemaLabel.getPreferredSize().width;
126     }
127 	
128     public void setSchemaLanguage(String schemaLanguage) {
129         if (schemaLanguage == null || schemaLanguage.trim().length() == 0) {
130             schemaLanguageField.setSelectedIndex(0);
131         } else {
132             boolean itemFound = false;
133     
134             for (int i = 0; i < schemaLanguageField.getItemCount(); i++) {
135                 Object item = schemaLanguageField.getItemAt(i);
136                 
137                 if (item.equals(schemaLanguage)) {
138                     itemFound = true;
139                 }
140             }
141             
142             if (!itemFound) {
143                 schemaLanguageField.addItem(schemaLanguage);
144             }
145 
146             schemaLanguageField.setSelectedItem(schemaLanguage);
147         }
148    }
149 
150     public void setSchemaLanguages(List<String> languages) {
151         Object item = schemaLanguageField.getSelectedItem();
152         
153         if (parent != null) {
154             updatingLanguages = true;
155             parent.getProjectView().getFieldManager().setIgnoreChanges(true);
156             parent.getProjectView().getUndoManager().startCompound();
157         }
158 
159         try {
160             if (schemaLanguageField.getItemCount() > 0) {
161                 schemaLanguageField.removeAllItems();
162             }
163             
164             schemaLanguageField.addItem(NO_LANGUAGE);
165 
166             for (String language : languages) {
167                 schemaLanguageField.addItem(language);
168             }
169             
170             setSchemaLanguage(item.toString());
171         } finally {
172             if (parent != null) {
173                 updatingLanguages = false;
174                 parent.getProjectView().getFieldManager().setIgnoreChanges(false);
175                 parent.getProjectView().getUndoManager().discardCompound();
176             }
177         }
178    }
179 
180     public String getSchemaLanguage() {
181         String language = (String)schemaLanguageField.getSelectedItem();
182 
183         if ( NO_LANGUAGE.equals( language)) {
184             return null;
185         }
186         
187         return language;
188     }
189     
190     public void removeAllURIs() {
191         try {
192             removing = true;
193             urisPanel.removeAllURIs();
194         } finally {
195             removing = false;
196         }
197     }
198 
199     public void setURIs( URI base, List uris) {
200         urisPanel.reset();
201         urisPanel.setURIs(base, uris);
202 	}
203     
204     public List<Document> getURIs(URI base) { 
205         return urisPanel.getURIs(base);
206     }
207 
208     public String getNodeName() {
209         String name = (String)schemaLanguageField.getSelectedItem();
210 
211         if ( NO_LANGUAGE.equals( name)) {
212             return name;
213         }
214         return name + " (schemaLanguage)";
215     }
216 
217     public ArrayList<OverviewNode> getChildNodes() {
218     	if (NO_LANGUAGE.equals(getNodeName()) && !removing) {
219     		return new ArrayList<OverviewNode>();
220     	}
221 
222         return urisPanel.getNodes();
223     }
224 
225     public String getError() {
226         return null;
227     }
228 
229     public OverviewNode getParentNode() {
230         return parent;
231     }
232 
233     public Icon getNodeIcon() {
234     	return COMBO_ICON;
235     }
236     
237     public void dispose() {
238         urisPanel.dispose();
239     }
240 
241     private DefaultFileFilter getSchemaFileFilter() {
242         if ( schemaFilter == null) {
243             schemaFilter = new DefaultFileFilter( "xsd,xs", "XML Schema Document");
244         }
245         
246         return schemaFilter;
247     }
248     
249     private DefaultFileFilter getRelaxNGFileFilter() {
250         if ( relaxNGFilter == null) {
251             relaxNGFilter = new DefaultFileFilter( "rng,rnc", "RelaxNG Document");
252         }
253         
254         return relaxNGFilter;
255     }
256 
257 	/* (non-Javadoc)
258 	 * @see javax.swing.event.UndoableEditListener#undoableEditHappened(javax.swing.event.UndoableEditEvent)
259 	 */
260     public void itemStateChanged(ItemEvent event) {
261         if (!updatingLanguages) {
262             if (event.getStateChange() == ItemEvent.DESELECTED) {
263                 if (parent != null) {
264                     parent.getProjectView().getUndoManager().startCompound();
265                 }
266             } else if (event.getStateChange() == ItemEvent.SELECTED) {
267                 if (schemaLanguageField.getSelectedItem() != null) {
268                     String value = schemaLanguageField.getSelectedItem().toString();
269     
270                     if (value.equals(NO_LANGUAGE)) {
271                         removeAllURIs();
272                         urisPanel.setEnabled( false);
273             		} else {
274             		    if (value.equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
275                             chooser.setFileFilter(getSchemaFileFilter());
276                         } else if ( value.equals( XMLConstants.RELAXNG_NS_URI)) {
277                             chooser.setFileFilter(getRelaxNGFileFilter());
278                         } else {
279                             chooser.setFileFilter(null);
280                         }
281         
282                         urisPanel.setEnabled( true);
283             		}
284             
285                 	OverviewNode node = parent;
286                 	
287                 	while ( node != null && !(node.getParentNode() instanceof OverviewTreeModel)) {
288                 		node = node.getParentNode();
289                 	}
290                 	
291                 	if ( node != null) {
292                 		((OverviewTreeModel)node.getParentNode()).structureChanged( this);
293                 	}
294             
295                     if (parent != null) {
296                         parent.getProjectView().getOverviewPanel().expandNode( this);
297                         parent.getProjectView().getUndoManager().endCompound();
298                     }
299     
300                     revalidate();
301             		repaint();
302                 } else if (parent != null) {
303                     parent.getProjectView().getUndoManager().endCompound();
304                 }
305             }
306         }
307 	}
308 }