1 19 20 package org.openide.explorer.propertysheet.editors; 21 22 import java.beans.*; 23 import java.util.*; 24 25 import javax.swing.*; 26 27 import org.openide.*; 28 import org.openide.src.MethodParameter; 29 import org.openide.src.Type; 30 import org.openide.util.NbBundle; 31 import org.openide.util.Utilities; 32 import org.openide.explorer.propertysheet.ExPropertyEditor; 33 import org.openide.explorer.propertysheet.PropertyEnv; 34 35 39 public class MethodParameterArrayEditor extends PropertyEditorSupport implements ExPropertyEditor { 40 41 42 MethodParameterArrayPanel panel; 43 44 47 boolean ignoreEditor = false; 48 49 52 boolean ignorePanel = false; 53 54 private PropertyEnv env; 55 56 62 public String getAsText() { 63 MethodParameter[] params = (MethodParameter[]) getValue(); 64 StringBuffer buf = new StringBuffer (); 65 if (params != null) { 66 for (int i = 0; i < params.length; i++) { 67 if (i > 0) 68 buf.append(", "); buf.append(params[i].getSourceString()); 70 } 71 } 72 return buf.toString(); 73 } 74 75 81 public void setAsText(String text) throws IllegalArgumentException { 82 StringTokenizer tok = new StringTokenizer(text, ",", false); ArrayList list = new ArrayList(); 84 while (tok.hasMoreTokens()) { 85 String token = tok.nextToken(); 86 MethodParameter par; 87 88 try { 89 par = MethodParameter.parse(token); 90 } catch (IllegalArgumentException ex) { 91 String msg = java.text.MessageFormat.format( 92 getString("MSG_IllegalMethodParameter"), new Object [] { token }); 94 ErrorManager.getDefault().annotate(ex, 95 ErrorManager.USER, null, msg, null, null); 96 throw ex; 97 } 98 for (Iterator it = list.iterator(); it.hasNext(); ) { 99 if (par.getName().equals(((MethodParameter)it.next()).getName())) { 100 String msg = java.text.MessageFormat.format( 101 getString("MSG_DuplicateName2"), new Object [] { par.getName() }); 103 IllegalArgumentException ex= new IllegalArgumentException ("Ambiguous name"); ErrorManager.getDefault().annotate(ex, 105 ErrorManager.USER, null, msg, null, null); 106 throw ex; 107 } 108 } 109 list.add(par); 110 } 111 MethodParameter[] params = new MethodParameter[list.size()]; 112 list.toArray(params); 113 setValue(params); 114 } 115 116 117 public void setValue(Object o) { 118 ignoreEditor = true; 119 boolean saveIgnorePanel = ignorePanel; 120 121 ignorePanel = false; 122 super.setValue(o); 123 if ((panel != null) & !saveIgnorePanel) { 124 panel.setMethodParameters((MethodParameter[])o); 125 } 126 ignoreEditor = false; 127 } 128 129 130 public boolean supportsCustomEditor () { 131 return true; 132 } 133 134 137 public java.awt.Component getCustomEditor () { 138 if (panel == null) { 139 panel = new MethodParameterArrayPanel(); 140 panel.setMnemonics(env); 141 panel.setMethodParameters((MethodParameter[]) getValue()); 142 143 panel.addPropertyChangeListener(new PropertyChangeListener() { 144 public void propertyChange(PropertyChangeEvent evt) { 145 if (!ignoreEditor && MethodParameterArrayPanel.PROP_METHOD_PARAMETERS.equals(evt.getPropertyName())) { 146 ignorePanel = true; 147 setValue(evt.getNewValue()); 148 ignorePanel = false; 149 } 150 } 151 }); 152 153 } 154 return panel; 155 } 156 157 161 public void attachEnv(PropertyEnv env) { 162 this.env = env; 163 } 164 165 168 static class MethodParameterArrayPanel extends ObjectArrayPanel2 169 implements EnhancedCustomPropertyEditor { 170 171 public static final String PROP_METHOD_PARAMETERS = "methodParameters"; 173 174 MethodParameter[] prevValue; 175 176 177 public MethodParameterArrayPanel() { 178 prevValue = new MethodParameter[0]; 179 180 this.getListComponent().setCellRenderer(new DefaultListCellRenderer() { 181 public java.awt.Component getListCellRendererComponent(JList list, 182 Object value, int index, boolean isSelected, boolean cellHasFocus) { 183 java.awt.Component comp = super.getListCellRendererComponent(list, 184 value, index, isSelected, cellHasFocus); 185 if (comp == this) { 186 setText(((MethodParameter)value).toString()); 187 } 188 return comp; 189 } 190 }); 191 } 192 193 195 public MethodParameter[] getMethodParameters() { 196 MethodParameter[] ret = new MethodParameter[model.size()]; 197 model.copyInto(ret); 198 return ret; 199 } 200 201 203 public void setMethodParameters(MethodParameter[] data) { 204 model = new DefaultListModel(); 205 if (data != null) { 206 for (int i = 0; i < data.length; i++) 207 model.addElement(data[i]); 208 } 209 this.getListComponent().setModel(model); 210 modelChanged(); 211 } 212 213 214 protected void modelChanged() { 215 super.modelChanged(); 216 MethodParameter[] newValue = getMethodParameters(); 217 firePropertyChange(PROP_METHOD_PARAMETERS, prevValue, newValue); 218 prevValue = newValue; 219 } 220 221 225 protected Object insertNewValue() { 226 return openInputDialog(null); 227 } 228 229 234 protected Object editValue(Object oldValue) { 235 return openInputDialog((MethodParameter) oldValue); 236 } 237 238 243 protected MethodParameter openInputDialog(MethodParameter origValue) { 244 MethodParameterPanel panel = new MethodParameterPanel(); 245 246 NotifyDescriptor desriptor = new NotifyDescriptor( 247 panel, 248 getString("LAB_EnterParameter"), 249 NotifyDescriptor.OK_CANCEL_OPTION, 250 NotifyDescriptor.PLAIN_MESSAGE, null, null); 251 252 if (origValue != null) { 253 panel.nameTextField.setText(origValue.getName().toString()); 254 panel.typeCombo.setSelectedItem(origValue.getType().toString()); 255 panel.finalCheckBox.setSelected(origValue.isFinal()); 256 } 257 258 for (;;) { 259 Object ret = DialogDisplayer.getDefault().notify(desriptor); 260 if (ret == NotifyDescriptor.OK_OPTION) { 261 String errMsg = null; 262 String name = panel.nameTextField.getText(); 263 if (!Utilities.isJavaIdentifier(name)) 264 errMsg = "MSG_NotValidID"; else { 266 try { 267 Type type = Type.parse(panel.typeCombo.getSelectedItem().toString()); 268 boolean isFinal = panel.finalCheckBox.isSelected(); 269 Enumeration methodEnum = model.elements(); 270 271 while(methodEnum.hasMoreElements()) 272 { MethodParameter oldPar = (MethodParameter)methodEnum.nextElement(); 273 274 if (origValue!=null && oldPar.equals(origValue)) 275 continue; 276 if (name.equals(oldPar.getName())) 277 { errMsg = "MSG_DuplicateName"; break; 279 } 280 } 281 if (errMsg == null) 282 return new MethodParameter(name, type, isFinal); 283 } 284 catch (IllegalArgumentException e) { 285 errMsg = "MSG_NotValidType"; } 287 } 288 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(getString(errMsg))); 289 } 290 else { 291 return null; 292 } 293 } 294 } 295 296 301 public Object getPropertyValue() throws IllegalStateException { 302 return getMethodParameters(); 303 } 304 305 } 306 307 private static String getString(String key) { 308 return NbBundle.getBundle("org.openide.explorer.propertysheet.editors.Bundle2", Locale.getDefault(), MethodParameterArrayEditor.class.getClassLoader()).getString(key); 309 } 310 311 } 312 | Popular Tags |