KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.openide.src.Type;
26
//import org.openide.src.Identifier;
27
import org.openide.util.*;
28
29 import javax.swing.*;
30 import javax.swing.table.*;
31 import java.net.URL JavaDoc;
32 import java.net.MalformedURLException JavaDoc;
33
34 /**
35  * Allows to define mapping element name => (method identifier, return type)
36  * in visual way. Class holding methods performing convertions is called parslet (set
37  * of mini-parsers).
38  *
39  * @author Petr Kuzel
40  * @version
41  */

42 public final class SAXGeneratorParsletPanel extends SAXGeneratorAbstractPanel {
43
44     /** Serial Version UID */
45     private static final long serialVersionUID =6038099487791703339L;
46
47
48     private TableModel tableModel; //it is based on model
49

50     private static final int ELEMENT_COLUMN = 0;
51     private static final int METHOD_COLUMN = 1; // represents parslet
52
private static final int TYPE_COLUMN = 2;
53     private static final int COLUMNS = 3;
54     
55     private static final String JavaDoc NO_METHOD = "[none]"; // NOI18N
56

57     private final String JavaDoc[] COLUMN_NAMES = new String JavaDoc[] {
58         Util.THIS.getString ("SAXGeneratorParsletPanel.table.column1"),
59         Util.THIS.getString ("SAXGeneratorParsletPanel.table.column2"),
60         Util.THIS.getString ("SAXGeneratorParsletPanel.table.column3"),
61     };
62
63     private final ValidatingTextField.Validator TYPE_VALIDATOR = new ValidatingTextField.Validator() {
64         public boolean isValid(String JavaDoc value) {
65             boolean ret = GenerateSupportUtils.isValidReturnType(value);
66             setValid(ret);
67             return ret;
68         }
69         
70         public String JavaDoc getReason() {
71             return Util.THIS.getString("MSG_parslet_err_1");
72         }
73     };
74     
75     private final ValidatingTextField.Validator METHOD_VALIDATOR = new ValidatingTextField.Validator() {
76         public boolean isValid(String JavaDoc value) {
77             boolean ret = NO_METHOD.equals(value) || Utilities.isJavaIdentifier(value);
78             setValid(ret);
79             return ret;
80         }
81         
82         public String JavaDoc getReason() {
83             return Util.THIS.getString("MSG_parslet_err_2");
84         }
85         
86     };
87     
88     /** Creates new form SAXGeneratorParsletPanel */
89     public SAXGeneratorParsletPanel() {
90 // try {
91
// this.putClientProperty("WizardPanel_helpURL", new URL("nbresloc:/org/netbeans/modules/xml/tools/generator/SAXGeneratorParsletPanel.html")); //NOI18N
92
// } catch (MalformedURLException ex) {
93
// }
94
}
95
96     /** This method is called from within the constructor to
97      * initialize the form.
98      * WARNING: Do NOT modify this code. The content of this method is
99      * always regenerated by the FormEditor.
100      */

101     private void initComponents() {//GEN-BEGIN:initComponents
102
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
103
104         descTextArea = new javax.swing.JTextArea JavaDoc();
105         tableScrollPane = new javax.swing.JScrollPane JavaDoc();
106         table = new javax.swing.JTable JavaDoc();
107
108         setLayout(new java.awt.GridBagLayout JavaDoc());
109
110         setPreferredSize(new java.awt.Dimension JavaDoc(480, 350));
111         setName(Util.THIS.getString ("SAXGeneratorParsletPanel.Form.name"));
112         descTextArea.setWrapStyleWord(true);
113         descTextArea.setLineWrap(true);
114         descTextArea.setEditable(false);
115         descTextArea.setForeground(new java.awt.Color JavaDoc(102, 102, 153));
116         descTextArea.setFont(javax.swing.UIManager.getFont ("Label.font"));
117         descTextArea.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/xml/tools/generator/Bundle").getString("DESC_saxw_convertors"));
118         descTextArea.setDisabledTextColor(javax.swing.UIManager.getColor ("Label.foreground"));
119         descTextArea.setEnabled(false);
120         descTextArea.setOpaque(false);
121         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
122         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
123         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
124         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;
125         gridBagConstraints.weightx = 1.0;
126         add(descTextArea, gridBagConstraints);
127
128         table.setModel(tableModel);
129         tableScrollPane.setViewportView(table);
130
131         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
132         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
133         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
134         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
135         gridBagConstraints.weightx = 1.0;
136         gridBagConstraints.weighty = 1.0;
137         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
138         add(tableScrollPane, gridBagConstraints);
139
140     }//GEN-END:initComponents
141

142
143     /**
144      * Prepare all models used by this form
145      */

146     private void initModels() {
147         
148         // there is one huge table allowing main customization
149

150         tableModel = new ParsletsTableModel();
151     }
152     
153     
154     /**
155      * Upadate view according to model.
156      */

157     protected void initView() {
158         initModels();
159         initComponents ();
160     
161         table = new ParsletsTable();
162         table.setModel(tableModel);
163         tableScrollPane.setViewportView(table); //install it
164

165         initAccessibility();
166     }
167
168     protected void updateView() {
169     }
170     
171     /**
172      * Our table model directly updates model data.
173      * We need just reset parslets if not used.
174      */

175     protected void updateModel() {
176 // if (parsletsCheckBox.isSelected()==false) {
177
// model.getParsletBindings().clear();
178
// }
179
}
180
181     
182     // ~~~~~~~~~~~~~~ Table and its model ~~~~~~~~~~~~~~~~~~~~
183

184     /**
185      * The table using dynamic cell editors for TYPE_COLUMN and METHOD_COLUMN
186      */

187     private class ParsletsTable extends JTable {
188
189         /** Serial Version UID */
190         private static final long serialVersionUID =1076091649418718319L;
191         
192         public ParsletsTable() {
193             getTableHeader().setReorderingAllowed(false);
194             setRowHeight(Util.getTextCellHeight(this));
195         }
196         
197         public TableCellEditor getCellEditor(int row, int column) {
198             
199             switch (column) {
200                 
201                 case METHOD_COLUMN:
202                     Vector all = new Vector(model.getParsletBindings().keySet());
203                     JComboBox editor = new JComboBox(all);
204                     editor.addItem(NO_METHOD);
205                     ValidatingTextField methodInput = new ValidatingTextField();
206                     methodInput.setValidator(METHOD_VALIDATOR);
207                     editor.setEditor(methodInput);
208                     editor.setEditable(true);
209                     return new DefaultCellEditor(editor);
210                 
211                 case TYPE_COLUMN:
212                     String JavaDoc[] vals = {
213                         "int", "boolean","long", "java.util.Date", "java.net.URL", // NOI18N
214
"java.lang.String", "java.lang.String[]" // NOI18N
215
};
216                     JComboBox type_editor = new JComboBox(vals);
217                     ValidatingTextField typeInput = new ValidatingTextField();
218                     typeInput.setValidator(TYPE_VALIDATOR);
219                     type_editor.setEditor(typeInput);
220                     type_editor.setEditable(true);
221                     return new DefaultCellEditor(type_editor);
222
223                     
224                 default:
225                     return super.getCellEditor(row, column);
226             }
227         }
228         
229     }
230     
231     /**
232      * Model contrainig some parslets issues
233      */

234     private class ParsletsTableModel extends AbstractTableModel {
235             
236         /** Serial Version UID */
237         private static final long serialVersionUID =3516749023312029787L;
238         
239         public String JavaDoc getColumnName(int col) {
240             return COLUMN_NAMES[col];
241         }
242
243         public int getRowCount() {
244             if (model == null) return 0;
245             return model.getElementBindings().size();
246         }
247
248         public int getColumnCount() {
249             return COLUMNS;
250         }
251
252
253         /**
254           * @return Type or String depending on column
255           */

256         public Object JavaDoc getValueAt(int row, int column) {
257             ElementBindings.Entry entry = model.getElementBindings().getEntry(row);
258             ParsletBindings parslets = model.getParsletBindings();
259
260             // set to parslet or null if none assigned
261
String JavaDoc parslet = entry.getParslet();
262
263             switch (column) {
264                 
265                 case ELEMENT_COLUMN:
266                     return entry.getElement();
267                     
268                 case TYPE_COLUMN:
269                     if (parslet == null) {
270                         return String JavaDoc.class.getName();
271                     } else {
272                         //TODO: Retouche
273
// return parslets.getReturnType(parslet).toString();
274
}
275                     
276                 case METHOD_COLUMN:
277                     if (parslet == null) {
278                         return NO_METHOD;
279                     } else {
280                         return parslet;
281                     }
282             }
283             return null;
284         }
285
286         /**
287           * @param value must be a String regardless column
288           */

289         public void setValueAt(Object JavaDoc value, int row, int col) {
290
291 // new RuntimeException("Setting Value to " + value).printStackTrace();
292

293             //TODO: Retouche
294
// if (col == TYPE_COLUMN) {
295
// if (TYPE_VALIDATOR.isValid((String) value) == false) return;
296
// } else if (col == METHOD_COLUMN) {
297
// if (METHOD_VALIDATOR.isValid((String)value) == false) return;
298
// }
299
//
300
// ElementBindings binds = model.getElementBindings();
301
// ElementBindings.Entry entry = binds.getEntry(row);
302
//
303
// // set to parslet or null if none assigned
304
// String currentParslet = entry.getParslet();
305
//
306
// ParsletBindings parslets = model.getParsletBindings();
307
// ParsletBindings.Entry parsletEntry = parslets.getEntry(currentParslet);
308
//
309
// switch (col) {
310
// case TYPE_COLUMN:
311
// // all parslets of the same name must return same type
312
// if (currentParslet == null) {
313
// break; // can not set a value
314
// } else {
315
// if (binds.getParsletUsageCount(currentParslet)>1) {
316
// if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("Cannot change"); // NOI18N
317
// } else {
318
// Type returnType =
319
// Type.createClass(Identifier.create((String)value));
320
// parsletEntry.setReturnType(returnType);
321
// }
322
// }
323
// break;
324
//
325
// // setting parslet determines return value
326
// case METHOD_COLUMN:
327
//
328
// String identifier = (String)value;
329
// String oldReturnType = parslets.getReturnType(currentParslet).toString();
330
//
331
// if (binds.getParsletUsageCount(currentParslet) == 1) {
332
// parslets.remove(currentParslet);
333
// }
334
// if (NO_METHOD.equals(identifier) == false) {
335
// entry.setParslet(identifier);
336
//
337
// if (parslets.getEntry(identifier) == null) {
338
// //parslets.put(identifier, (String) getValueAt(row, TYPE_COLUMN));
339
// parslets.put(identifier, oldReturnType);
340
// } else {
341
// fireTableRowsUpdated(row,row);
342
// }
343
// } else {
344
// entry.setParslet(null);
345
// fireTableRowsUpdated(row,row);
346
// }
347
// break;
348
//
349
// }
350
// if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug(model.toString());
351

352         }
353
354         public boolean isCellEditable(int row, int col) {
355             return col != ELEMENT_COLUMN;
356         }
357     }; //ParsletsTableModel
358

359     
360     // Variables declaration - do not modify//GEN-BEGIN:variables
361
private javax.swing.JTable JavaDoc table;
362     private javax.swing.JTextArea JavaDoc descTextArea;
363     private javax.swing.JScrollPane JavaDoc tableScrollPane;
364     // End of variables declaration//GEN-END:variables
365

366     /** Initialize accesibility
367      */

368     public void initAccessibility(){
369
370         this.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_SAXGeneratorParsletPanel"));
371         table.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_ParsletsTable"));
372         table.getAccessibleContext().setAccessibleName(Util.THIS.getString("ACSN_ParsletsTable"));
373     }
374 }
375
Popular Tags