KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > websvc > registry > ui > TypeCellEditor


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
20 package org.netbeans.modules.websvc.registry.ui;
21
22 import javax.swing.DefaultCellEditor JavaDoc;
23 import javax.swing.table.TableCellEditor JavaDoc;
24 import com.sun.xml.rpc.processor.model.java.JavaEnumerationType;
25 import com.sun.xml.rpc.processor.model.java.JavaEnumerationEntry;
26 import com.sun.xml.rpc.processor.model.java.JavaSimpleType;
27 import com.sun.xml.rpc.processor.model.java.JavaType;
28
29 import org.openide.util.NbBundle;
30
31 import org.netbeans.swing.outline.OutlineModel;
32 import org.netbeans.swing.outline.NodeRowModel;
33
34 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
35
36 import java.util.ArrayList JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Calendar JavaDoc;
40 import java.util.Date JavaDoc;
41 import java.text.DateFormat JavaDoc;
42 import java.text.ParseException JavaDoc;
43 import java.net.URI JavaDoc;
44 import java.net.URISyntaxException JavaDoc;
45
46 import java.math.BigDecimal JavaDoc;
47 import java.math.BigInteger JavaDoc;
48 import java.awt.Component JavaDoc;
49
50 import javax.swing.JTextField JavaDoc;
51 import javax.swing.JComboBox JavaDoc;
52 import java.awt.event.ActionEvent JavaDoc;
53
54 /**
55  *
56  * @author david
57  */

58 public class TypeCellEditor extends DefaultCellEditor JavaDoc implements TableCellEditor JavaDoc {
59     Component JavaDoc lastComponent;
60     JavaType type;
61     
62     /** Creates a new instance of TypeCellRenderer */
63     public TypeCellEditor() {
64         super(new JTextField JavaDoc());
65         this.setClickCountToStart(1);
66     }
67     
68     public void cancelCellEditing() {
69         return;
70     }
71     
72     public boolean stopCellEditing() {
73         return super.stopCellEditing();
74     }
75     
76     /**
77      * return the value of the last component.
78      */

79     public Object JavaDoc getCellEditorValue() {
80         if(null == type) {
81             return ((JTextField JavaDoc)lastComponent).getText();
82         } else {
83             if(lastComponent instanceof JTextField JavaDoc) {
84                 String JavaDoc valueString = ((JTextField JavaDoc)lastComponent).getText();
85                 Object JavaDoc value = createValue(valueString);
86                 return value;
87             } else if(lastComponent instanceof JComboBox JavaDoc) {
88                 return ((JComboBox JavaDoc)lastComponent).getSelectedItem();
89             } else return null;
90         }
91         
92     }
93     
94     
95     
96     public java.awt.Component JavaDoc getTableCellEditorComponent(javax.swing.JTable JavaDoc table, Object JavaDoc value, boolean isSelected, int row, int column) {
97         /**
98          * We need to create the correct editing component for the type of field we have.
99          * JavaSimpleTypes all except Date and Calendar - JTextField()
100          * JavaEnumerationType - JComboBox
101          */

102         
103         /**
104          * First, we need to get the JavaType for the node of the object to be edited.
105          */

106         
107         NodeRowModel rowModel = ((OutlineModel)table.getModel()).getRowNodeModel();
108         DefaultMutableTreeNode JavaDoc node = (DefaultMutableTreeNode JavaDoc)rowModel.getNodeForRow(row);
109         /**
110          * Now depending on the type, create a component to edit/display the type.
111          */

112         if(null == node.getUserObject()) {
113             JTextField JavaDoc txtField = new JTextField JavaDoc();
114             txtField.setText((String JavaDoc)value);
115             lastComponent = (Component JavaDoc)txtField;
116             
117         } else {
118             TypeNodeData data = (TypeNodeData)node.getUserObject();
119             type = data.getParameterType();
120             
121             if(type instanceof JavaSimpleType) {
122                 /**
123                  * If the type is boolean or Boolean, create a JComboBox with true,false
124                  */

125                 if(type.getRealName().equalsIgnoreCase("boolean") ||
126                 type.getRealName().equalsIgnoreCase("java.lang.Boolean")) {
127                     JComboBox JavaDoc combo = new JComboBox JavaDoc();
128                     lastComponent = (Component JavaDoc)combo;
129                     combo.addItem(new Boolean JavaDoc(true));
130                     combo.addItem(new Boolean JavaDoc(false));
131                     
132                     /**
133                      * Set the value as the current Enumeration value.
134                      */

135                     
136                     Object JavaDoc parameterValue = data.getParameterValue();
137                     
138                     combo.setSelectedItem(parameterValue);
139                     combo.addActionListener(new java.awt.event.ActionListener JavaDoc() {
140                         public void actionPerformed(java.awt.event.ActionEvent JavaDoc evt) {
141                            comboActionPerformed(evt);
142                         }
143                     });
144                     
145                 } else {
146                     
147                     JTextField JavaDoc txtField = new JTextField JavaDoc();
148                     /**
149                      * figure out what kind of simple field this is to set the value.
150                      */

151                     txtField.setText(value != null ? value.toString() : ""); // NOI18N
152
lastComponent = (Component JavaDoc)txtField;
153                 }
154                 
155             } else if(type instanceof JavaEnumerationType) {
156                 JComboBox JavaDoc combo = new JComboBox JavaDoc();
157                 lastComponent = (Component JavaDoc)combo;
158                 JavaEnumerationType enumType = (JavaEnumerationType)type;
159                 Iterator JavaDoc iterator = enumType.getEntries();
160                 while(iterator.hasNext()) {
161                     JavaEnumerationEntry entry = (JavaEnumerationEntry)iterator.next();
162                     combo.addItem(entry.getLiteralValue());
163                 }
164                 
165                 /**
166                  * Set the value as the current Enumeration value.
167                  */

168                 
169                 Object JavaDoc parameterValue = data.getParameterValue();
170                 
171                 combo.setSelectedItem(parameterValue);
172                 
173             }
174             
175             
176         }
177         
178         return lastComponent;
179     }
180     
181     private void comboActionPerformed(ActionEvent JavaDoc evt) {
182         JComboBox JavaDoc combo = (JComboBox JavaDoc)evt.getSource();
183         this.fireEditingStopped();
184                 
185     }
186     
187     private Object JavaDoc createValue(String JavaDoc inValue) {
188         Object JavaDoc returnValue = null;
189         String JavaDoc currentType = type.getRealName();
190         
191         if(currentType.equalsIgnoreCase("int") ||
192         currentType.equalsIgnoreCase("java.lang.Integer")) {
193             try {
194                 returnValue = new Integer JavaDoc(inValue);
195             } catch(NumberFormatException JavaDoc nfe) {
196                 returnValue = new Integer JavaDoc(0);
197             }
198         } else if(currentType.equalsIgnoreCase("byte") ||
199         currentType.equalsIgnoreCase("java.lang.Byte")) {
200             try {
201                 returnValue = new Byte JavaDoc(inValue);
202             } catch(NumberFormatException JavaDoc nfe) {
203                 returnValue = new Byte JavaDoc(" ");
204             }
205         } else if(currentType.equalsIgnoreCase("boolean") ||
206         currentType.equalsIgnoreCase("java.lang.Boolean")) {
207             try {
208                 returnValue = new Boolean JavaDoc(inValue);
209             } catch(NumberFormatException JavaDoc nfe) {
210                 returnValue = new Boolean JavaDoc(false);
211             }
212         } else if(currentType.equalsIgnoreCase("float") ||
213         currentType.equalsIgnoreCase("java.lang.Float")) {
214             try {
215                 returnValue = new Float JavaDoc(inValue);
216             } catch(NumberFormatException JavaDoc nfe) {
217                 returnValue = new Float JavaDoc(0);
218             }
219         } else if(currentType.equalsIgnoreCase("double") ||
220         currentType.equalsIgnoreCase("java.lang.Double")) {
221             try {
222                 returnValue = new Double JavaDoc(inValue);
223             } catch(NumberFormatException JavaDoc nfe) {
224                 returnValue = new Double JavaDoc(0);
225             }
226         } else if(currentType.equalsIgnoreCase("long") ||
227         currentType.equalsIgnoreCase("java.lang.Long")) {
228             try {
229                 returnValue = new Long JavaDoc(inValue);
230             } catch(NumberFormatException JavaDoc nfe) {
231                 returnValue = new Long JavaDoc(0);
232             }
233         } else if(currentType.equalsIgnoreCase("short") ||
234         currentType.equalsIgnoreCase("java.lang.Short")) {
235             try {
236                 returnValue = new Short JavaDoc(inValue);
237             } catch(NumberFormatException JavaDoc nfe) {
238                 returnValue = new Short JavaDoc(" ");
239             }
240         } else if(currentType.equalsIgnoreCase("java.lang.String")) {
241             returnValue = inValue;
242         } else if(currentType.equalsIgnoreCase("java.math.BigDecimal")) {
243             try {
244                 returnValue = new BigDecimal JavaDoc(inValue);
245             } catch(NumberFormatException JavaDoc nfe) {
246                 returnValue = new BigDecimal JavaDoc(0);
247             }
248         } else if(currentType.equalsIgnoreCase("java.math.BigInteger")) {
249             try {
250                 returnValue = new BigInteger JavaDoc(inValue);
251             } catch(NumberFormatException JavaDoc nfe) {
252                 returnValue = new BigInteger JavaDoc("0");
253             }
254         } else if(currentType.equalsIgnoreCase("java.net.URI")) {
255             try {
256                 returnValue = new URI JavaDoc(inValue);
257             } catch(URISyntaxException JavaDoc uri) {
258                 try {
259                     returnValue = new URI JavaDoc("http://java.sun.com");
260                 } catch(URISyntaxException JavaDoc uri2) {}
261             }
262         } else if(currentType.equalsIgnoreCase("java.util.Calendar")) {
263             try {
264                 Date JavaDoc date = DateFormat.getDateTimeInstance().parse(inValue);
265                 returnValue = Calendar.getInstance();
266                 ((Calendar JavaDoc)returnValue).setTime(date);
267             } catch (java.text.ParseException JavaDoc ex) {
268                 returnValue = Calendar.getInstance();
269             }
270         } else if(currentType.equalsIgnoreCase("java.util.Date")) {
271             try {
272                 returnValue = DateFormat.getInstance().parse(inValue);
273             } catch(ParseException JavaDoc pe) {
274                 returnValue = new Date JavaDoc();
275             }
276         }
277         
278         return returnValue;
279         
280     }
281     
282 }
283
Popular Tags