KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tools > generator > SAXGeneratorMethodPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.tools.generator;
20
21 import java.awt.*;
22 import java.util.*;
23 import java.beans.*;
24
25 import javax.swing.*;
26 import javax.swing.table.*;
27
28 import org.openide.util.*;
29 import java.net.URL JavaDoc;
30 import java.net.MalformedURLException JavaDoc;
31
32
33 /**
34  * Customizes element => (method, method type) bindings in visual way.
35  *
36  * @author Petr Kuzel
37  * @version
38  */

39 public final class SAXGeneratorMethodPanel extends SAXGeneratorAbstractPanel {
40
41     /** Serial Version UID */
42     private static final long serialVersionUID =-4925652670676144240L;
43
44
45     private TableModel tableModel;
46     private MethodsTable table;
47
48     // constants
49

50     static final int ELEMENT_COLUMN = 0;
51     static final int TYPE_COLUMN = 1;
52     static final int METHOD_COLUMN = 2;
53     static final int COLUMNS = 3;
54     
55     private final String JavaDoc[] COLUMN_NAMES = new String JavaDoc[] {
56         Util.THIS.getString ("SAXGeneratorMethodPanel.table.column1"),
57         Util.THIS.getString ("SAXGeneratorMethodPanel.table.column2"),
58         Util.THIS.getString ("SAXGeneratorMethodPanel.table.column3"),
59     };
60
61     
62
63     private final ValidatingTextField.Validator METHOD_VALIDATOR = new ValidatingTextField.Validator() {
64         public boolean isValid(String JavaDoc text) {
65             boolean ret = Utilities.isJavaIdentifier("_" + text); // NOI18N
66
setValid(ret);
67             return ret;
68         }
69         
70         public String JavaDoc getReason() {
71             return Util.THIS.getString("MSG_method_err_1");
72         }
73     };
74     
75     /** Creates new form SAXGeneratorMethodPanel */
76     public SAXGeneratorMethodPanel() {
77 // try {
78
// this.putClientProperty("WizardPanel_helpURL", new URL("nbresloc:/org/netbeans/modules/xml/tools/generator/SAXGeneratorMethodPanel.html")); //NOI18N
79
// } catch (MalformedURLException ex) {
80
// }
81
}
82
83     /** This method is called from within the constructor to
84      * initialize the form.
85      * WARNING: Do NOT modify this code. The content of this method is
86      * always regenerated by the FormEditor.
87      */

88     private void initComponents() {//GEN-BEGIN:initComponents
89
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
90
91         descTextArea = new javax.swing.JTextArea JavaDoc();
92         tableScrollPane = new javax.swing.JScrollPane JavaDoc();
93
94         setLayout(new java.awt.GridBagLayout JavaDoc());
95
96         setPreferredSize(new java.awt.Dimension JavaDoc(480, 350));
97         setName(Util.THIS.getString ("SAXGeneratorMethodPanel.Form.name"));
98         descTextArea.setWrapStyleWord(true);
99         descTextArea.setLineWrap(true);
100         descTextArea.setEditable(false);
101         descTextArea.setForeground(new java.awt.Color JavaDoc(102, 102, 153));
102         descTextArea.setFont(javax.swing.UIManager.getFont ("Label.font"));
103         descTextArea.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/xml/tools/generator/Bundle").getString("DESC_saxw_methods"));
104         descTextArea.setDisabledTextColor(javax.swing.UIManager.getColor ("Label.foreground"));
105         descTextArea.setEnabled(false);
106         descTextArea.setOpaque(false);
107         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
108         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
109         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
110         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
111         gridBagConstraints.weightx = 1.0;
112         add(descTextArea, gridBagConstraints);
113
114         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
115         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
116         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
117         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
118         gridBagConstraints.weightx = 1.0;
119         gridBagConstraints.weighty = 1.0;
120         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
121         add(tableScrollPane, gridBagConstraints);
122
123     }//GEN-END:initComponents
124

125     private void initModels() {
126         tableModel = new MethodsTableModel();
127     }
128
129     protected void initView() {
130         initModels();
131         initComponents ();
132
133         table = new MethodsTable();
134         table.setModel(tableModel);
135         tableScrollPane.setViewportView(table); //install it
136

137         initAccessibility();
138     }
139     
140     protected void updateView() {
141         checkNames();
142     }
143     
144     protected void updateModel() {
145         //tablemodel writes directly to model, no need for update
146
}
147     
148     private void checkNames() {
149     }
150     
151     // ~~~~~~~~~~~~~~~~~ Table models ~~~~~~~~~~~~~~`
152

153     /**
154      * The table using dynamic cell editors for TYPE_COLUMN
155      */

156     private class MethodsTable extends JTable {
157         
158         /** Serial Version UID */
159         private static final long serialVersionUID =-8352980237774025436L;
160         
161         public MethodsTable() {
162             getTableHeader().setReorderingAllowed(false);
163             setRowHeight(Util.getTextCellHeight(this));
164         }
165         
166         /**
167          * We need a cell editor that is initialized again and again to
168          * that it contains fresh values.
169          */

170         public TableCellEditor getCellEditor(int row, int column) {
171             if (column == TYPE_COLUMN) {
172                 ElementBindings.Entry entry = model.getElementBindings().getEntry(row);
173                 final String JavaDoc element = entry.getElement();
174                 final JComboBox editor =
175                     new JComboBox(entry.displayTypesFor(model.getElementDeclarations().getEntry(element)));
176                 
177                 return new DefaultCellEditor(editor);
178             } else if (column == METHOD_COLUMN) {
179                 ValidatingTextField input = new ValidatingTextField();
180                 input.setValidator(METHOD_VALIDATOR);
181                 return new DefaultCellEditor(input);
182             } else {
183                 return super.getCellEditor(row, column);
184             }
185         }
186                 
187     }
188
189     /**
190      * TableModel that directly access <tt>model</tt> field
191      */

192     private class MethodsTableModel extends AbstractTableModel {
193
194         /** Serial Version UID */
195         private static final long serialVersionUID =7287934953974099492L;
196         
197         public String JavaDoc getColumnName(int col) {
198             return COLUMN_NAMES[col];
199         }
200
201         public int getRowCount() {
202             if (model == null) return 0;
203             return model.getElementBindings().size();
204         }
205
206         public int getColumnCount() {
207             return COLUMNS;
208         }
209
210         /**
211          * Return String (ELEMENT) or String (TYPE) or String (METHOD).
212          */

213         public Object JavaDoc getValueAt(int row, int column) {
214             ElementBindings.Entry entry = model.getElementBindings().getEntry(row);
215             switch (column) {
216                 case ELEMENT_COLUMN:
217                     return entry.getElement();
218                 case TYPE_COLUMN:
219                     return entry.displayTypeFor(entry.getType());
220                 case METHOD_COLUMN:
221                     return entry.getMethod();
222                 default:
223                     return null;
224             }
225         }
226
227         public void setValueAt(Object JavaDoc value, int row, int col) {
228             
229             ElementBindings.Entry entry = model.getElementBindings().getEntry(row);
230             switch (col) {
231                 case TYPE_COLUMN:
232                     entry.setType(entry.typeFor((String JavaDoc) value));
233                     return;
234                 case METHOD_COLUMN:
235                     // the "_" emulates actual prefix added by generator
236
if (Utilities.isJavaIdentifier("_" + (String JavaDoc) value) == false) { // NOI18N
237
setValid(false);
238                         return;
239                     } else {
240                         checkNames();
241                     }
242                     entry.setMethod((String JavaDoc) value); //!!! check for duplicities
243
return;
244             }
245         }
246
247         public boolean isCellEditable(int row, int col) {
248             return col != ELEMENT_COLUMN;
249         }
250     }
251
252     // ~~~~~~~~~~~~~~~~~~~ Conversion routines ~~~~~~~~~~~~~~`
253

254     
255
256
257         
258     // Variables declaration - do not modify//GEN-BEGIN:variables
259
private javax.swing.JTextArea JavaDoc descTextArea;
260     private javax.swing.JScrollPane JavaDoc tableScrollPane;
261     // End of variables declaration//GEN-END:variables
262

263     /** Initialize accesibility
264      */

265     public void initAccessibility(){
266
267         this.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_SAXGeneratorMethodPanel"));
268         table.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_table"));
269         table.getAccessibleContext().setAccessibleName(Util.THIS.getString("ACSN_table"));
270     }
271 }
272
Popular Tags