View Javadoc

1   /*
2    * $Id: SchemaLanguagesPanel.java,v 1.4 2006/09/06 17:48:19 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) 2002 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  
22  package org.xmlhammer.gui.preferences;
23  
24  import java.awt.BorderLayout;
25  import java.awt.Color;
26  import java.awt.Dimension;
27  import java.awt.FlowLayout;
28  import java.awt.event.ActionEvent;
29  import java.awt.event.ActionListener;
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  import javax.swing.DefaultCellEditor;
34  import javax.swing.JButton;
35  import javax.swing.JFrame;
36  import javax.swing.JOptionPane;
37  import javax.swing.JPanel;
38  import javax.swing.JScrollPane;
39  import javax.swing.JTable;
40  import javax.swing.JTextField;
41  import javax.swing.ListSelectionModel;
42  import javax.swing.border.EmptyBorder;
43  import javax.swing.table.DefaultTableModel;
44  import javax.xml.XMLConstants;
45  
46  import org.xmlhammer.model.jaxp.SchemaFactoryProperty;
47  
48  /***
49   * This PrefixNamespaceMappingPanel is used to ...
50   *
51   * @version $Revision: 1.4 $, $Date: 2006/09/06 17:48:19 $
52   * @author Edwin Dankert <edankert@gmail.com>
53   */
54  public class SchemaLanguagesPanel extends JPanel {
55  	private static final long serialVersionUID = 3403969820780811484L;
56  
57      private JFrame parent = null;
58  	
59  	private JTable schemaLanguagesTable = null;
60  	private DefaultTableModel schemaLanguagesModel = null;
61  	private JButton deleteButton = null;
62  	private JButton addButton = null;
63  	
64  	public SchemaLanguagesPanel( JFrame parent, String name) {
65  		super( new BorderLayout());
66  		
67  		this.parent = parent;
68  	
69  		init();
70  	}
71  	
72  	private void init() {
73  		schemaLanguagesModel = new PropertyTableModel();
74  		schemaLanguagesModel.setColumnCount( 2);
75  		schemaLanguagesModel.setColumnIdentifiers( new String[] { "Language", "Factory"});
76  
77  		schemaLanguagesTable = new JTable( schemaLanguagesModel);
78  		schemaLanguagesTable.setSelectionMode( ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
79  		schemaLanguagesTable.setFont( schemaLanguagesTable.getFont().deriveFont((float)11));
80  		
81          JTextField field = new JTextField();
82          field.setBorder( new EmptyBorder(0, 1, 0, 1));
83          field.setFont(field.getFont().deriveFont((float)11));
84          schemaLanguagesTable.setDefaultEditor(String.class, new DefaultCellEditor(field));
85  
86          JPanel buttonPanel = new JPanel( new FlowLayout( FlowLayout.RIGHT));
87  		buttonPanel.setBorder( new EmptyBorder( 0, 0, 0, 0));
88  	
89  		addButton = new JButton( "Add");
90  		addButton.addActionListener( new ActionListener() {
91  			public void actionPerformed( ActionEvent e) {
92  				addButtonPressed();
93  			}
94  		});
95          addButton.setMnemonic('A');
96  		buttonPanel.add( addButton);
97  	
98  		deleteButton = new JButton( "Delete");
99  		deleteButton.addActionListener( new ActionListener() {
100 			public void actionPerformed( ActionEvent e) {
101 				deleteButtonPressed();
102 			}
103 		});
104         deleteButton.setMnemonic('t');
105 		buttonPanel.add( deleteButton);
106 	
107 		JScrollPane scroller = new JScrollPane( schemaLanguagesTable);
108 		scroller.getViewport().setBackground( Color.white);
109 		scroller.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
110 		
111 		scroller.setPreferredSize( new Dimension( 100, 100));
112 		add( scroller, BorderLayout.CENTER);
113 		add( buttonPanel, BorderLayout.SOUTH);
114 	}
115 	
116 	private void deleteButtonPressed() {
117 	    int[] indices = schemaLanguagesTable.getSelectedRows();
118 	    
119         for( int i = 0; i < indices.length; i++) {
120             for ( int index : indices) {
121                 if ( index == 0) {
122                     JOptionPane.showMessageDialog( parent, "Cannot delete the XML Schema language.", "Error", JOptionPane.ERROR_MESSAGE);
123                     return;
124                 } else if ( index == 1) {
125                     JOptionPane.showMessageDialog( parent, "Cannot delete the Relax NG language.", "Error", JOptionPane.ERROR_MESSAGE);
126                     return;
127                 }
128             }
129 
130             int result = -1;
131             
132 	        if( indices.length - i > 1) {
133 	        	result = JOptionPane.showOptionDialog( parent, "Are you sure you want to delete:\n"+schemaLanguagesModel.getValueAt( indices[i], 0).toString(), "Please Confirm", JOptionPane.YES_NO_CANCEL_OPTION,
134 					JOptionPane.QUESTION_MESSAGE, null, new String[] { "Yes", "No", "All"}, "Yes");
135 	        } else {
136 	        	result = JOptionPane.showConfirmDialog( parent, "Are you sure you want to delete:\n"+schemaLanguagesModel.getValueAt( indices[i], 0).toString(), "Please Confirm", JOptionPane.YES_NO_CANCEL_OPTION);
137             }
138             		            
139             if( result == 2) { // ALL
140             	for ( int j = i; j < indices.length; j++) { 
141                     schemaLanguagesModel.removeRow( indices[i]);
142             	}
143             	
144             	return;
145             } else if( result == JOptionPane.YES_OPTION) {
146                 schemaLanguagesModel.removeRow( indices[i]);
147 			}
148 	            
149         } //end for loop
150 	}
151 	
152 	public void setEnabled( boolean enabled) {
153 		schemaLanguagesTable.setEnabled( enabled);
154 		addButton.setEnabled( enabled);
155 		deleteButton.setEnabled( enabled);
156 	}
157 
158 	public void setProperties( List<SchemaFactoryProperty> properties) {
159         properties = new ArrayList<SchemaFactoryProperty>(properties);
160 		
161         while ( schemaLanguagesModel.getRowCount() > 0) {
162 			schemaLanguagesModel.removeRow( 0);
163 		}
164 		
165         boolean xmlSchema = false;
166 
167         for ( SchemaFactoryProperty property : properties) {
168             if ( property.getLanguage().equals(XMLConstants.W3C_XML_SCHEMA_NS_URI)) {
169                 schemaLanguagesModel.addRow( new Object[] { XMLConstants.W3C_XML_SCHEMA_NS_URI, property.getFactory()});
170                 properties.remove(property);
171                 xmlSchema = true;
172                 break;
173             }
174         }
175         
176         if ( !xmlSchema) {
177             schemaLanguagesModel.addRow( new Object[] { XMLConstants.W3C_XML_SCHEMA_NS_URI, ""});
178         }
179 
180         boolean relaxNG = false;
181 
182         for ( SchemaFactoryProperty property : properties) {
183             if ( property.getLanguage().equals(XMLConstants.RELAXNG_NS_URI)) {
184                 schemaLanguagesModel.addRow( new Object[] { XMLConstants.RELAXNG_NS_URI, property.getFactory()});
185                 properties.remove(property);
186                 relaxNG = true;
187                 break;
188             }
189         }
190         
191         if ( !relaxNG) {
192             schemaLanguagesModel.addRow( new Object[] { XMLConstants.RELAXNG_NS_URI, ""});
193         }
194 
195         for ( SchemaFactoryProperty property : properties) {
196 			schemaLanguagesModel.addRow( new Object[] { property.getLanguage(), property.getFactory()});
197 		}
198 	}
199 	
200 	public List<SchemaFactoryProperty> getProperties() {
201 		List<SchemaFactoryProperty> properties = new ArrayList<SchemaFactoryProperty>();
202 
203 		for ( int i = 0; i < schemaLanguagesModel.getRowCount(); i++) {
204             SchemaFactoryProperty property = new SchemaFactoryProperty();
205 			property.setLanguage( (String)schemaLanguagesModel.getValueAt( i, 0));
206 			property.setFactory( (String)schemaLanguagesModel.getValueAt( i, 1));
207 			
208 			properties.add( property);
209 		}
210 		
211 		return properties;
212 	}
213 	
214 	private void addButtonPressed() {
215 		schemaLanguagesModel.addRow( new Object[] { "New Schema Language", "New Schema Factory"});
216 		schemaLanguagesTable.scrollRectToVisible( schemaLanguagesTable.getCellRect( schemaLanguagesModel.getRowCount()-1, 1, false));
217 	}
218 	
219 	public class PropertyTableModel extends DefaultTableModel {
220 		private static final long serialVersionUID = -7751440247019185844L;
221 
222         @SuppressWarnings("unchecked")
223         public Class getColumnClass( int col) {
224 			if ( col == 0) {
225 				return String.class;
226 			}
227 			
228             return String.class;
229 		}
230         
231         public boolean isCellEditable(int row, int col) {
232             if (col == 0 && (row == 0 || row == 1)) {
233                 return false;
234             }
235             
236             return true;
237         }
238         
239 	}
240 }