KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tax > beans > editor > TreeAttlistDeclAttributeListCustomizer


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.tax.beans.editor;
20
21 import java.awt.event.KeyAdapter JavaDoc;
22 import java.awt.event.KeyEvent JavaDoc;
23 import javax.swing.JPanel JavaDoc;
24 import javax.swing.JTable JavaDoc;
25 import javax.swing.ListSelectionModel JavaDoc;
26 import javax.swing.table.TableColumn JavaDoc;
27 import javax.swing.table.AbstractTableModel JavaDoc;
28 import javax.swing.JTextField JavaDoc;
29 import javax.swing.DefaultCellEditor JavaDoc;
30 import java.beans.Customizer JavaDoc;
31 import java.beans.PropertyChangeEvent JavaDoc;
32 import java.beans.PropertyChangeListener JavaDoc;
33
34 import org.netbeans.tax.TreeNamedObjectMap;
35 import org.netbeans.tax.TreeException;
36 import org.netbeans.tax.TreeAttlistDeclAttributeDef;
37
38 import org.netbeans.modules.xml.tax.util.TAXUtil;
39
40 /**
41  *
42  * @author Petr Kuzel
43  * @author Vladimir Zboril
44  * @version 1.0
45  */

46 public class TreeAttlistDeclAttributeListCustomizer extends JPanel JavaDoc implements Customizer JavaDoc, PropertyChangeListener JavaDoc {
47
48     /** Serial Version UID */
49     private static final long serialVersionUID =3524220594991141235L;
50
51     /** */
52     private static final String JavaDoc headerTitles[] = {
53     Util.THIS.getString ("TEXT_attlistdecl_attributelist_header_Name"),
54     Util.THIS.getString ("TEXT_attlistdecl_attributelist_header_Type"),
55     Util.THIS.getString ("TEXT_attlistdecl_attributelist_header_Enumerated"),
56     Util.THIS.getString ("TEXT_attlistdecl_attributelist_header_Default"),
57     Util.THIS.getString ("TEXT_attlistdecl_attributelist_header_Value")
58     };
59     
60     private static final int COL_NAME = 0;
61     private static final int COL_TYPE = 1;
62     private static final int COL_ENUM_TYPE = 2;
63     private static final int COL_DEFAULT_TYPE = 3;
64     private static final int COL_DEFAULT_VALUE = 4;
65
66     private static final int COL_COUNT = 5;
67
68
69     //
70
// init
71
//
72

73     public TreeAttlistDeclAttributeListCustomizer() {
74         initComponents ();
75         attrTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
76         
77         // Cells should become editable on single mouse click
78
final JTextField JavaDoc editorComponent = new JTextField JavaDoc();
79         editorComponent.getCaret().setVisible(true);
80         final DefaultCellEditor JavaDoc singleClickEditor = new DefaultCellEditor JavaDoc(editorComponent);
81         singleClickEditor.setClickCountToStart(1);
82         attrTable.setDefaultEditor(String JavaDoc.class, singleClickEditor);
83         
84         
85         initAccessibility();
86     }
87
88     
89     //
90
// itself
91
//
92

93     /** This method is called from within the constructor to
94      * initialize the form.
95      * WARNING: Do NOT modify this code. The content of this method is
96      * always regenerated by the FormEditor.
97      */

98     private void initComponents() {//GEN-BEGIN:initComponents
99
java.awt.GridBagConstraints JavaDoc gridBagConstraints;
100
101         tableScrollPane = new javax.swing.JScrollPane JavaDoc();
102         attrTable = new javax.swing.JTable JavaDoc();
103
104         setLayout(new java.awt.GridBagLayout JavaDoc());
105
106         attrTable.setPreferredScrollableViewportSize(new java.awt.Dimension JavaDoc(250, 150));
107         tableScrollPane.setViewportView(attrTable);
108
109         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
110         gridBagConstraints.gridx = 0;
111         gridBagConstraints.gridy = 0;
112         gridBagConstraints.gridwidth = java.awt.GridBagConstraints.REMAINDER;
113         gridBagConstraints.gridheight = java.awt.GridBagConstraints.REMAINDER;
114         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
115         gridBagConstraints.weightx = 1.0;
116         gridBagConstraints.weighty = 1.0;
117         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 12, 0, 11);
118         add(tableScrollPane, gridBagConstraints);
119
120     }//GEN-END:initComponents
121

122
123     // Variables declaration - do not modify//GEN-BEGIN:variables
124
private javax.swing.JTable JavaDoc attrTable;
125     private javax.swing.JScrollPane JavaDoc tableScrollPane;
126     // End of variables declaration//GEN-END:variables
127

128
129     private TreeNamedObjectMap peer;
130     private AbstractTableModel JavaDoc tableModel;
131
132
133     /**
134      */

135     public void setObject (Object JavaDoc obj) {
136         peer = (TreeNamedObjectMap) obj;
137         tableModel = new AttlistTableModel(/*peer*/);
138         attrTable.setModel(tableModel);
139         // attrTable.addKeyListener(new RowKeyListener(attrTable)); // DTD is read only now --> it will be ready in future
140

141         /** First table column is row selector. */
142         TableColumn JavaDoc column = null;
143         for (int i = 0; i < COL_COUNT; i++) {
144             column = attrTable.getColumnModel().getColumn (i);
145         column.setPreferredWidth (50);
146         }
147
148         updateView (peer);
149         peer.addPropertyChangeListener (org.openide.util.WeakListeners.propertyChange (this, peer));
150     }
151
152
153     /** Udate atate accordingly*/
154     public void propertyChange(final PropertyChangeEvent JavaDoc e) {
155         if (e.getSource() == null)
156         return;
157         if (e.getSource() != peer)
158         return;
159
160         updateView ((TreeNamedObjectMap) e.getSource());
161     }
162
163     /** Update visualization accordingly. */
164     private void updateView (TreeNamedObjectMap model) {
165         tableModel.fireTableDataChanged();
166     }
167
168
169     //
170
// class RowKeyListener
171
//
172

173     /** deletes whole row by pressing DELETE on row column. */
174     private class RowKeyListener extends KeyAdapter JavaDoc {
175
176     /** */
177         private JTable JavaDoc table;
178
179     
180     //
181
// init
182
//
183

184         public RowKeyListener (JTable JavaDoc table) {
185             this.table = table;
186         }
187
188     
189     //
190
// itself
191
//
192

193     /**
194      */

195         public void keyReleased (KeyEvent JavaDoc e) {
196             //if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug("Event: " + e); // NOI18N
197
if (e.getKeyCode() == KeyEvent.VK_DELETE) {
198                 peer.remove (peer.get (table.getSelectedRow()));
199                 tableModel.fireTableDataChanged();
200             }
201         }
202     }
203
204
205     //
206
// class AttlistTableModel
207
//
208

209     /**
210      *
211      */

212     private class AttlistTableModel extends AbstractTableModel JavaDoc {
213
214         /** Serial Version UID */
215         private static final long serialVersionUID =2920999049977694084L;
216         
217     //
218
// init
219
//
220

221         /** Create a data node for a given data object.
222          * The provided children object will be used to hold all child nodes.
223          * @param obj object to work with
224          * @param ch children container for the node
225          */

226         public AttlistTableModel () {
227             super();
228         }
229
230         /** Returns the number of rows in the model */
231         public int getRowCount () {
232             return peer.size();
233         }
234
235         /** Returns the number of columns in the model */
236         public int getColumnCount () {
237             return COL_COUNT;
238         }
239
240         /** Returns the class for a model. */
241         public Class JavaDoc getColumnClass (int index) {
242             return String JavaDoc.class;
243         }
244
245     /**
246      */

247         public Object JavaDoc getValueAt (int row, int column) {
248             TreeAttlistDeclAttributeDef attr = (TreeAttlistDeclAttributeDef) peer.get(row);
249             switch (column) {
250             case COL_NAME:
251                 return attr.getName();
252             case COL_TYPE:
253                 return attr.getTypeName();
254             case COL_ENUM_TYPE:
255                 return ( (attr.getEnumeratedTypeString() == null) ? "" : attr.getEnumeratedTypeString() ); // NOI18N
256
case COL_DEFAULT_TYPE:
257                 return attr.getDefaultTypeName();
258             case COL_DEFAULT_VALUE:
259                 String JavaDoc defVal = attr.getDefaultValue();
260                 if ( defVal == null ) {
261                     defVal = ""; // NOI18N
262
}
263                 return defVal;
264             default:
265                 return null;
266             }
267         }
268
269     /**
270      */

271         public void setValueAt (Object JavaDoc val, int row, int column) {
272             TreeAttlistDeclAttributeDef attr = (TreeAttlistDeclAttributeDef) peer.get (row);
273             try {
274                 if (column == COL_NAME) {
275                     attr.setName ((String JavaDoc) val);
276                 }
277             } catch (TreeException exc) {
278                 TAXUtil.notifyTreeException (exc);
279             }
280         }
281
282     /**
283      */

284         public String JavaDoc getColumnName (int column) {
285             return column < getColumnCount() ? headerTitles[column] : "" ; // NOI18N
286
}
287
288         /** Returns true for all cells which are editable. For a
289          * a new cell is editable only name field.
290          */

291         public boolean isCellEditable (int rowIndex, int columnIndex) {
292             return false;
293         }
294
295     } // end: class AttlistTableModel
296

297     
298    /** Initialize accesibility
299      */

300     public void initAccessibility(){
301         
302        this.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_TreeAttlistDeclAttributeListCustomizer"));
303        attrTable.getAccessibleContext().setAccessibleDescription(Util.THIS.getString("ACSD_attrTable1"));
304        attrTable.getAccessibleContext().setAccessibleName(Util.THIS.getString("ACSN_attrTable1"));
305     }
306
307 }
308
Popular Tags