1 19 20 package org.netbeans.modules.websvc.registry.ui; 21 22 import javax.swing.DefaultCellEditor ; 23 import javax.swing.table.TableCellEditor ; 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 ; 35 36 import java.util.ArrayList ; 37 import java.util.Iterator ; 38 import java.util.HashMap ; 39 import java.util.Calendar ; 40 import java.util.Date ; 41 import java.text.DateFormat ; 42 import java.text.ParseException ; 43 import java.net.URI ; 44 import java.net.URISyntaxException ; 45 46 import java.math.BigDecimal ; 47 import java.math.BigInteger ; 48 import java.awt.Component ; 49 50 import javax.swing.JTextField ; 51 import javax.swing.JComboBox ; 52 import java.awt.event.ActionEvent ; 53 54 58 public class TypeCellEditor extends DefaultCellEditor implements TableCellEditor { 59 Component lastComponent; 60 JavaType type; 61 62 63 public TypeCellEditor() { 64 super(new JTextField ()); 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 79 public Object getCellEditorValue() { 80 if(null == type) { 81 return ((JTextField )lastComponent).getText(); 82 } else { 83 if(lastComponent instanceof JTextField ) { 84 String valueString = ((JTextField )lastComponent).getText(); 85 Object value = createValue(valueString); 86 return value; 87 } else if(lastComponent instanceof JComboBox ) { 88 return ((JComboBox )lastComponent).getSelectedItem(); 89 } else return null; 90 } 91 92 } 93 94 95 96 public java.awt.Component getTableCellEditorComponent(javax.swing.JTable table, Object value, boolean isSelected, int row, int column) { 97 102 103 106 107 NodeRowModel rowModel = ((OutlineModel)table.getModel()).getRowNodeModel(); 108 DefaultMutableTreeNode node = (DefaultMutableTreeNode )rowModel.getNodeForRow(row); 109 112 if(null == node.getUserObject()) { 113 JTextField txtField = new JTextField (); 114 txtField.setText((String )value); 115 lastComponent = (Component )txtField; 116 117 } else { 118 TypeNodeData data = (TypeNodeData)node.getUserObject(); 119 type = data.getParameterType(); 120 121 if(type instanceof JavaSimpleType) { 122 125 if(type.getRealName().equalsIgnoreCase("boolean") || 126 type.getRealName().equalsIgnoreCase("java.lang.Boolean")) { 127 JComboBox combo = new JComboBox (); 128 lastComponent = (Component )combo; 129 combo.addItem(new Boolean (true)); 130 combo.addItem(new Boolean (false)); 131 132 135 136 Object parameterValue = data.getParameterValue(); 137 138 combo.setSelectedItem(parameterValue); 139 combo.addActionListener(new java.awt.event.ActionListener () { 140 public void actionPerformed(java.awt.event.ActionEvent evt) { 141 comboActionPerformed(evt); 142 } 143 }); 144 145 } else { 146 147 JTextField txtField = new JTextField (); 148 151 txtField.setText(value != null ? value.toString() : ""); lastComponent = (Component )txtField; 153 } 154 155 } else if(type instanceof JavaEnumerationType) { 156 JComboBox combo = new JComboBox (); 157 lastComponent = (Component )combo; 158 JavaEnumerationType enumType = (JavaEnumerationType)type; 159 Iterator iterator = enumType.getEntries(); 160 while(iterator.hasNext()) { 161 JavaEnumerationEntry entry = (JavaEnumerationEntry)iterator.next(); 162 combo.addItem(entry.getLiteralValue()); 163 } 164 165 168 169 Object parameterValue = data.getParameterValue(); 170 171 combo.setSelectedItem(parameterValue); 172 173 } 174 175 176 } 177 178 return lastComponent; 179 } 180 181 private void comboActionPerformed(ActionEvent evt) { 182 JComboBox combo = (JComboBox )evt.getSource(); 183 this.fireEditingStopped(); 184 185 } 186 187 private Object createValue(String inValue) { 188 Object returnValue = null; 189 String currentType = type.getRealName(); 190 191 if(currentType.equalsIgnoreCase("int") || 192 currentType.equalsIgnoreCase("java.lang.Integer")) { 193 try { 194 returnValue = new Integer (inValue); 195 } catch(NumberFormatException nfe) { 196 returnValue = new Integer (0); 197 } 198 } else if(currentType.equalsIgnoreCase("byte") || 199 currentType.equalsIgnoreCase("java.lang.Byte")) { 200 try { 201 returnValue = new Byte (inValue); 202 } catch(NumberFormatException nfe) { 203 returnValue = new Byte (" "); 204 } 205 } else if(currentType.equalsIgnoreCase("boolean") || 206 currentType.equalsIgnoreCase("java.lang.Boolean")) { 207 try { 208 returnValue = new Boolean (inValue); 209 } catch(NumberFormatException nfe) { 210 returnValue = new Boolean (false); 211 } 212 } else if(currentType.equalsIgnoreCase("float") || 213 currentType.equalsIgnoreCase("java.lang.Float")) { 214 try { 215 returnValue = new Float (inValue); 216 } catch(NumberFormatException nfe) { 217 returnValue = new Float (0); 218 } 219 } else if(currentType.equalsIgnoreCase("double") || 220 currentType.equalsIgnoreCase("java.lang.Double")) { 221 try { 222 returnValue = new Double (inValue); 223 } catch(NumberFormatException nfe) { 224 returnValue = new Double (0); 225 } 226 } else if(currentType.equalsIgnoreCase("long") || 227 currentType.equalsIgnoreCase("java.lang.Long")) { 228 try { 229 returnValue = new Long (inValue); 230 } catch(NumberFormatException nfe) { 231 returnValue = new Long (0); 232 } 233 } else if(currentType.equalsIgnoreCase("short") || 234 currentType.equalsIgnoreCase("java.lang.Short")) { 235 try { 236 returnValue = new Short (inValue); 237 } catch(NumberFormatException nfe) { 238 returnValue = new Short (" "); 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 (inValue); 245 } catch(NumberFormatException nfe) { 246 returnValue = new BigDecimal (0); 247 } 248 } else if(currentType.equalsIgnoreCase("java.math.BigInteger")) { 249 try { 250 returnValue = new BigInteger (inValue); 251 } catch(NumberFormatException nfe) { 252 returnValue = new BigInteger ("0"); 253 } 254 } else if(currentType.equalsIgnoreCase("java.net.URI")) { 255 try { 256 returnValue = new URI (inValue); 257 } catch(URISyntaxException uri) { 258 try { 259 returnValue = new URI ("http://java.sun.com"); 260 } catch(URISyntaxException uri2) {} 261 } 262 } else if(currentType.equalsIgnoreCase("java.util.Calendar")) { 263 try { 264 Date date = DateFormat.getDateTimeInstance().parse(inValue); 265 returnValue = Calendar.getInstance(); 266 ((Calendar )returnValue).setTime(date); 267 } catch (java.text.ParseException 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 pe) { 274 returnValue = new Date (); 275 } 276 } 277 278 return returnValue; 279 280 } 281 282 } 283 | Popular Tags |