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.Utilities; 30 import org.openide.util.NbBundle; 31 import org.openide.explorer.propertysheet.ExPropertyEditor; 32 import org.openide.explorer.propertysheet.PropertyEnv; 33 import org.netbeans.jmi.javamodel.MultipartId; 34 import org.netbeans.jmi.javamodel.JavaModelPackage; 35 import org.netbeans.jmi.javamodel.NamedElement; 36 import org.netbeans.jmi.javamodel.Type; 37 import org.netbeans.jmi.javamodel.PrimitiveType; 38 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 39 import org.netbeans.modules.java.ui.nodes.elements.ElementNode; 40 41 42 46 public class IdentifierArrayEditor extends PropertyEditorSupport implements ExPropertyEditor { 47 48 public static final String JAVA_LANG_OBJECT = "java.lang.Object"; 50 51 IdentifierArrayPanel panel; 52 53 56 boolean ignoreEditor = false; 57 58 61 boolean ignorePanel = false; 62 63 PropertyEnv env; 64 65 private JavaModelPackage model; 66 67 68 public String getAsText() { 69 MultipartId[] id = (MultipartId[]) getValue(); 70 if ( id == null ) 71 return ""; 73 StringBuffer buf = new StringBuffer (); 74 75 for (int i = 0; i < id.length; i++) { 76 if (i > 0) 77 buf.append(", "); buf.append(multipartIdToName(id[i])); 79 } 80 81 return buf.toString(); 82 } 83 84 public static String multipartIdToName(MultipartId id) { 85 LinkedList list = new LinkedList(); 86 while (id != null) { 87 String stringRep; 88 if (id.getTypeArguments().size() > 0) { 89 JavaModelPackage pkg = (JavaModelPackage) id.refImmediatePackage(); 90 NamedElement e = id.getElement(); 91 stringRep = e.getName(); 92 stringRep += '<'; 93 for (Iterator tIt = id.getTypeArguments().iterator(); ;) { 94 stringRep += ((NamedElement) tIt.next()).getName(); 95 if (tIt.hasNext()) { 96 stringRep += ", "; } else { 98 break; 99 } 100 } 101 stringRep += '>'; 102 } else { 103 stringRep = id.getName(); 104 } 105 list.addFirst(stringRep); 106 id = id.getParent(); 107 } 108 StringBuffer buf = new StringBuffer (); 109 for (Iterator iter = list.iterator(); iter.hasNext();) { 110 buf.append((String )iter.next()); 111 if (iter.hasNext()) 112 buf.append('.'); 113 } 114 return buf.toString(); 115 } 116 117 118 public void setAsText(String text) throws IllegalArgumentException { 119 List list = Collections.EMPTY_LIST; 120 121 try { 122 JavaMetamodel.getDefaultRepository().beginTrans(false); 123 try { 124 list = resolveIdentifiers(text); 125 } finally { 126 JavaMetamodel.getDefaultRepository().endTrans(); 127 } 128 } catch (JmiException ex) { 129 ErrorManager.getDefault().notify(ex); 130 } 131 132 MultipartId[] ret = new MultipartId[list.size()]; 133 list.toArray(ret); 134 setValue(ret); 135 } 136 137 140 private List resolveIdentifiers(String txt) { 141 StringTokenizer tukac = new StringTokenizer(txt, ", ", false); List list = new ArrayList(); 143 while (tukac.hasMoreTokens()) { 144 String id = tukac.nextToken(); 145 JavaModelPackage jmp = model; 146 Type t = jmp.getType().resolve(id); 147 148 if (t instanceof PrimitiveType) { 150 IllegalArgumentException ex = new IllegalArgumentException (); 151 String msg = java.text.MessageFormat.format( 152 getString("MSG_InvalidIdentifier"), new Object [] { id }); 154 ErrorManager.getDefault().annotate(ex, ErrorManager.USER, null, msg, null, null); 155 throw ex; 156 } 157 158 MultipartId mid = jmp.getMultipartId().createMultipartId(id, null, null); 159 list.add(mid); 160 } 161 return list; 162 } 163 164 165 public void setValue(Object o) { 166 ignoreEditor = true; 167 boolean saveIgnorePanel = ignorePanel; 168 169 ignorePanel = false; 170 super.setValue(o); 171 if ((panel != null) & !saveIgnorePanel) { 172 panel.setIdentifiers((MultipartId[]) o); 173 } 174 ignoreEditor = false; 175 } 176 177 178 public boolean supportsCustomEditor () { 179 return true; 180 } 181 182 185 public java.awt.Component getCustomEditor () { 186 if (panel == null) { 187 panel = new IdentifierArrayPanel(model); 188 panel.setIdentifiers((MultipartId[]) getValue()); 189 panel.setMnemonics(env); 190 panel.addPropertyChangeListener(new PropertyChangeListener() { 191 public void propertyChange(PropertyChangeEvent evt) { 192 if (!ignoreEditor && IdentifierArrayPanel.PROP_IDENTIFIERS.equals(evt.getPropertyName())) { 193 ignorePanel = true; 194 setValue(evt.getNewValue()); 195 ignorePanel = false; 196 } 197 } 198 }); 199 } 200 return panel; 201 } 202 203 207 public void attachEnv(PropertyEnv env) { 208 this.env = env; 209 model = ElementNode.getModel(env.getFeatureDescriptor()); 210 } 211 212 215 static class IdentifierArrayPanel extends ObjectArrayPanel2 { 216 217 218 public static final String PROP_IDENTIFIERS = "identifiers"; 220 221 MultipartId[] prevValue; 222 223 private final JavaModelPackage javaModel; 224 225 226 public IdentifierArrayPanel(JavaModelPackage javaModel) { 227 this.javaModel = javaModel; 228 prevValue = new MultipartId[0]; 229 230 this.getListComponent().setCellRenderer(new DefaultListCellRenderer() { 231 public java.awt.Component getListCellRendererComponent(JList list, 232 Object value, int index, boolean isSelected, boolean cellHasFocus) { 233 java.awt.Component comp = super.getListCellRendererComponent(list, 234 value, index, isSelected, cellHasFocus); 235 if (comp == this) { 236 setText(multipartIdToName((MultipartId) value)); 237 } 238 return comp; 239 } 240 }); 241 } 242 243 244 public MultipartId[] getIdentifiers() { 245 MultipartId[] ret = new MultipartId[model.size()]; 246 model.copyInto(ret); 247 return ret; 248 } 249 250 252 public void setIdentifiers(MultipartId[] data) { 253 model = new DefaultListModel(); 254 if (data != null) { 255 for (int i = 0; i < data.length; i++) 256 model.addElement(data[i]); 257 } 258 this.getListComponent().setModel(model); 259 modelChanged(); 260 } 261 262 263 protected void modelChanged() { 264 MultipartId[] newValue = getIdentifiers(); 265 firePropertyChange(PROP_IDENTIFIERS, prevValue, newValue); 266 prevValue = newValue; 267 } 268 269 273 protected Object insertNewValue() { 274 return openInputDialog(null); 275 } 276 277 282 protected Object editValue(Object oldValue) { 283 return openInputDialog((MultipartId) oldValue); 284 } 285 286 288 protected MultipartId openInputDialog(MultipartId origValue) { 289 NotifyDescriptor.InputLine input = new NotifyDescriptor.InputLine( 290 getString("LAB_NewName"), getString("LAB_NewIdentifier") ); 293 if (origValue != null) 294 input.setInputText(multipartIdToName(origValue)); 295 296 for (;;) { 297 Object ret = DialogDisplayer.getDefault().notify(input); 298 if (ret == NotifyDescriptor.OK_OPTION) { 299 String retValue = input.getInputText(); 300 if (retValue != null && !"".equals(retValue)) { if (!retValue.startsWith(".") && !retValue.endsWith(".") && (retValue.indexOf("..") == -1)) { boolean ok = true; 304 StringTokenizer tokenizer = new StringTokenizer(retValue, ".", false); while (tokenizer.hasMoreTokens()) { 306 String token = tokenizer.nextToken(); 307 if (!Utilities.isJavaIdentifier(token)) { 308 ok = false; 309 break; 310 } 311 } 312 if (ok) 313 return javaModel.getMultipartId().createMultipartId(retValue, null, null); 314 } 315 } 316 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(getString("MSG_NotValidID"))); } else { 318 return null; 319 } 320 } 321 } 322 } 323 324 private static String getString(String key) { 325 return NbBundle.getMessage(IdentifierArrayEditor.class, key); 326 } 327 328 } 329 | Popular Tags |