1 19 20 package org.netbeans.modules.java.ui.nodes.editors; 21 22 import java.beans.*; 23 import java.util.*; 24 25 import javax.swing.*; 26 import javax.jmi.reflect.JmiException; 27 28 import org.openide.*; 29 import org.openide.util.NbBundle; 30 import org.openide.util.Utilities; 31 import org.openide.explorer.propertysheet.ExPropertyEditor; 32 import org.openide.explorer.propertysheet.PropertyEnv; 33 import org.openide.explorer.propertysheet.editors.EnhancedCustomPropertyEditor; 34 import org.netbeans.jmi.javamodel.Parameter; 35 import org.netbeans.jmi.javamodel.JavaModelPackage; 36 import org.netbeans.jmi.javamodel.MultipartId; 37 import org.netbeans.modules.java.ui.nodes.elements.ElementNode; 38 import org.netbeans.modules.java.ui.nodes.elements.ElementFormat; 39 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 40 41 45 public class MethodParameterArrayEditor extends PropertyEditorSupport implements ExPropertyEditor { 46 47 48 MethodParameterArrayPanel panel; 49 50 53 boolean ignoreEditor = false; 54 55 58 boolean ignorePanel = false; 59 60 private PropertyEnv env; 61 62 private JavaModelPackage model; 63 64 private boolean isWritable; 65 66 public MethodParameterArrayEditor() { 67 this(true); 68 } 69 70 public MethodParameterArrayEditor(boolean isWritable) { 71 this.isWritable = isWritable; 72 } 73 74 80 public String getAsText() { 81 Parameter[] params = (Parameter[]) getValue(); 82 String text = ""; if (params != null) { 84 try { 85 JavaMetamodel.getDefaultRepository().beginTrans(false); 86 try { 87 text = params2String(params, ", ", false); } finally { 89 JavaMetamodel.getDefaultRepository().endTrans(); 90 } 91 } catch (JmiException ex) { 92 ErrorManager.getDefault().notify(ex); 93 } 94 } 95 return text; 96 } 97 98 public static String params2String(Parameter[] params, String delimiter, boolean listJustTypes) { 99 StringBuffer buf = new StringBuffer (30); 100 for (int i = 0; i < params.length; i++) { 101 if (i > 0) 102 buf.append(delimiter); buf.append(param2String(params[i], listJustTypes)); 104 } 105 return buf.toString(); 106 } 107 108 private static String param2String(Parameter p, boolean listJustTypes) { 109 StringBuffer sb = new StringBuffer (30); 110 if (!listJustTypes && p.isFinal()) { 111 sb.append("final"); sb.append(' '); 113 } 114 sb.append(ElementFormat.elementName(p.getType())); 115 if (p.isVarArg()) { 116 sb.append("..."); } 118 119 if (!listJustTypes) { 120 sb.append(' '); 121 sb.append(p.getName()); 122 } 123 return sb.toString(); 124 } 125 126 132 public void setAsText(String text) throws IllegalArgumentException { 133 throw new IllegalArgumentException ("Use refactoring action instead"); } 167 168 169 public void setValue(Object o) { 170 ignoreEditor = true; 171 boolean saveIgnorePanel = ignorePanel; 172 173 ignorePanel = false; 174 super.setValue(o); 175 if ((panel != null) & !saveIgnorePanel) { 176 panel.setMethodParameters((Parameter[])o); 177 } 178 ignoreEditor = false; 179 } 180 181 182 public boolean supportsCustomEditor () { 183 return true; 184 } 185 186 189 public java.awt.Component getCustomEditor () { 190 if (panel == null) { 191 panel = new MethodParameterArrayPanel(model, isWritable); 192 panel.setMnemonics(env); 193 panel.setMethodParameters((Parameter[]) getValue()); 194 195 panel.addPropertyChangeListener(new PropertyChangeListener() { 196 public void propertyChange(PropertyChangeEvent evt) { 197 if (!ignoreEditor && MethodParameterArrayPanel.PROP_METHOD_PARAMETERS.equals(evt.getPropertyName())) { 198 ignorePanel = true; 199 setValue(evt.getNewValue()); 200 ignorePanel = false; 201 } 202 } 203 }); 204 205 } 206 return panel; 207 } 208 209 213 public void attachEnv(PropertyEnv env) { 214 this.env = env; 215 model = ElementNode.getModel(env.getFeatureDescriptor()); 216 } 217 218 221 static class MethodParameterArrayPanel extends ObjectArrayPanel2 222 implements EnhancedCustomPropertyEditor { 223 224 public static final String PROP_METHOD_PARAMETERS = "methodParameters"; 226 227 Parameter[] prevValue; 228 229 final JavaModelPackage javaModel; 230 231 232 public MethodParameterArrayPanel(JavaModelPackage javaModel, boolean isWritable) { 233 super(isWritable); 234 this.javaModel = javaModel; 235 prevValue = new Parameter[0]; 236 237 this.getListComponent().setCellRenderer(new DefaultListCellRenderer() { 238 public java.awt.Component getListCellRendererComponent(JList list, 239 Object value, int index, boolean isSelected, boolean cellHasFocus) { 240 java.awt.Component comp = super.getListCellRendererComponent(list, 241 value, index, isSelected, cellHasFocus); 242 if (comp == this) { 243 try { 244 JavaMetamodel.getDefaultRepository().beginTrans(false); 245 try { 246 setText(param2String((Parameter) value, false)); 247 } finally { 248 JavaMetamodel.getDefaultRepository().endTrans(); 249 } 250 } catch (JmiException ex) { 251 ErrorManager.getDefault().notify(ex); 252 } 253 } 254 return comp; 255 } 256 }); 257 } 258 259 261 public Parameter[] getMethodParameters() { 262 Parameter[] ret = new Parameter[model.size()]; 263 model.copyInto(ret); 264 return ret; 265 } 266 267 269 public void setMethodParameters(Parameter[] data) { 270 model = new DefaultListModel(); 271 if (data != null) { 272 for (int i = 0; i < data.length; i++) 273 model.addElement(data[i]); 274 } 275 this.getListComponent().setModel(model); 276 modelChanged(); 277 } 278 279 280 protected void modelChanged() { 281 super.modelChanged(); 282 Parameter[] newValue = getMethodParameters(); 283 firePropertyChange(PROP_METHOD_PARAMETERS, prevValue, newValue); 284 prevValue = newValue; 285 } 286 287 291 protected Object insertNewValue() { 292 return openInputDialog(null); 293 } 294 295 300 protected Object editValue(Object oldValue) { 301 return openInputDialog((Parameter) oldValue); 302 } 303 304 308 protected Parameter openInputDialog(Parameter origValue) { 309 MethodParameterPanel panel = new MethodParameterPanel(); 310 311 NotifyDescriptor desriptor = new NotifyDescriptor( 312 panel, 313 getString("LAB_EnterParameter"), NotifyDescriptor.OK_CANCEL_OPTION, 315 NotifyDescriptor.PLAIN_MESSAGE, null, null); 316 317 if (origValue != null) { 318 String origTypeName = origValue.getType().getName(); 319 if (origValue.isVarArg()) { 320 origTypeName += "..."; } 322 panel.nameTextField.setText(origValue.getName()); 323 panel.typeCombo.setSelectedItem(origTypeName); 324 panel.finalCheckBox.setSelected(origValue.isFinal()); 325 } 326 327 for (;;) { 328 Object ret = DialogDisplayer.getDefault().notify(desriptor); 329 if (ret == NotifyDescriptor.OK_OPTION) { 330 String errMsg = null; 331 String name = panel.nameTextField.getText().trim(); 332 String typeName = panel.typeCombo.getSelectedItem().toString().trim(); 333 if (!Utilities.isJavaIdentifier(name)) 334 errMsg = "MSG_NotValidID"; else if (typeName.length() == 0) 336 errMsg = "MSG_NotValidType"; else { 338 try { 339 JavaMetamodel.getDefaultRepository().beginTrans(false); 340 try { 341 boolean isFinal = panel.finalCheckBox.isSelected(); 342 Enumeration methodEnum = model.elements(); 343 344 while(methodEnum.hasMoreElements()) { 345 Parameter oldPar = (Parameter)methodEnum.nextElement(); 346 347 if (origValue!=null && oldPar.equals(origValue)) 348 continue; 349 if (name.equals(oldPar.getName())) { 350 errMsg = "MSG_DuplicateName"; break; 352 } 353 } 354 if (errMsg == null) { 355 boolean isVarArg = typeName.endsWith("..."); typeName = isVarArg? typeName.substring(0, typeName.length() - 3): typeName; 357 358 MultipartId typeID = javaModel.getMultipartId().createMultipartId(typeName, null, null); 362 Parameter p = javaModel.getParameter().createParameter(); 363 p.setName(name); 364 p.setFinal(isFinal); 365 p.setTypeName(typeID); 367 p.setVarArg(isVarArg); 368 return p; 369 } 370 } finally { 371 JavaMetamodel.getDefaultRepository().endTrans(); 372 } 373 } catch (JmiException ex) { 374 ErrorManager.getDefault().notify(ex); 375 return null; 376 } 377 } 378 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(getString(errMsg))); 379 } else { 380 return null; 381 } 382 } 383 } 384 385 public Object getPropertyValue() throws IllegalStateException { 386 return getMethodParameters(); 387 } 388 389 } 390 391 private static String getString(String key) { 392 return NbBundle.getMessage(MethodParameterArrayEditor.class, key); 393 } 394 395 } 396 | Popular Tags |