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.Identifier; 29 import org.openide.src.Type; 30 import org.openide.util.Utilities; 31 import org.openide.util.NbBundle; 32 import org.openide.explorer.propertysheet.ExPropertyEditor; 33 import org.openide.explorer.propertysheet.PropertyEnv; 34 35 39 public class IdentifierArrayEditor extends PropertyEditorSupport implements ExPropertyEditor { 40 41 42 IdentifierArrayPanel panel; 43 44 47 boolean ignoreEditor = false; 48 49 52 boolean ignorePanel = false; 53 54 PropertyEnv env; 55 56 57 public String getAsText() { 58 Identifier[] id = (Identifier []) getValue(); 59 if ( id == null ) 60 return ""; 62 StringBuffer buf = new StringBuffer (); 63 64 for (int i = 0; i < id.length; i++) { 65 if (i > 0) 66 buf.append(", "); buf.append(id[i].getSourceName()); 68 } 69 70 return buf.toString(); 71 } 72 73 74 public void setAsText(String text) throws IllegalArgumentException { 75 StringTokenizer tukac = new StringTokenizer(text, ", ", false); ArrayList list = new ArrayList(); 77 78 while (tukac.hasMoreTokens()) { 79 String id = tukac.nextToken(); 80 try { 81 Type t = Type.parse(id); 83 if (!t.isClass()) 84 throw new IllegalArgumentException (); 85 } catch (IllegalArgumentException ex) { 86 String msg = java.text.MessageFormat.format( 87 getString("MSG_InvalidIdentifier"), 88 new Object [] { id }); 89 ErrorManager.getDefault().annotate(ex, 90 ErrorManager.USER, null, msg, null, null); 91 throw ex; 92 } 93 list.add(Identifier.create(id)); 94 } 95 96 Identifier[] ret = new Identifier[list.size()]; 97 list.toArray(ret); 98 setValue(ret); 99 } 100 101 102 public void setValue(Object o) { 103 ignoreEditor = true; 104 boolean saveIgnorePanel = ignorePanel; 105 106 ignorePanel = false; 107 super.setValue(o); 108 if ((panel != null) & !saveIgnorePanel) { 109 panel.setIdentifiers((Identifier[])o); 110 } 111 ignoreEditor = false; 112 } 113 114 115 public boolean supportsCustomEditor () { 116 return true; 117 } 118 119 122 public java.awt.Component getCustomEditor () { 123 if (panel == null) { 124 panel = new IdentifierArrayPanel(); 125 panel.setIdentifiers((Identifier[])getValue()); 126 panel.setMnemonics(env); 127 panel.addPropertyChangeListener(new PropertyChangeListener() { 128 public void propertyChange(PropertyChangeEvent evt) { 129 if (!ignoreEditor && IdentifierArrayPanel.PROP_IDENTIFIERS.equals(evt.getPropertyName())) { 130 ignorePanel = true; 131 setValue(evt.getNewValue()); 132 ignorePanel = false; 133 } 134 } 135 }); 136 } 137 return panel; 138 } 139 140 144 public void attachEnv(PropertyEnv env) { 145 this.env = env; 146 } 147 148 151 static class IdentifierArrayPanel extends ObjectArrayPanel2 { 152 153 154 public static final String PROP_IDENTIFIERS = "identifiers"; 156 157 Identifier[] prevValue; 158 159 static final long serialVersionUID =-8655189809250688928L; 160 161 public IdentifierArrayPanel() { 162 prevValue = new Identifier[0]; 163 164 this.getListComponent().setCellRenderer(new DefaultListCellRenderer() { 165 public java.awt.Component getListCellRendererComponent(JList list, 166 Object value, int index, boolean isSelected, boolean cellHasFocus) { 167 java.awt.Component comp = super.getListCellRendererComponent(list, 168 value, index, isSelected, cellHasFocus); 169 if (comp == this) { 170 setText(((Identifier)value).getFullName()); 171 } 172 return comp; 173 } 174 }); 175 } 176 177 178 public Identifier[] getIdentifiers() { 179 Identifier[] ret = new Identifier[model.size()]; 180 model.copyInto(ret); 181 return ret; 182 } 183 184 186 public void setIdentifiers(Identifier[] data) { 187 model = new DefaultListModel(); 188 if (data != null) { 189 for (int i = 0; i < data.length; i++) 190 model.addElement(data[i]); 191 } 192 this.getListComponent().setModel(model); 193 modelChanged(); 194 } 195 196 197 protected void modelChanged() { 198 Identifier[] newValue = getIdentifiers(); 199 firePropertyChange(PROP_IDENTIFIERS, prevValue, newValue); 200 prevValue = newValue; 201 } 202 203 207 protected Object insertNewValue() { 208 return openInputDialog(null); 209 } 210 211 216 protected Object editValue(Object oldValue) { 217 return openInputDialog((Identifier) oldValue); 218 } 219 220 225 protected Identifier openInputDialog(Identifier origValue) { 226 NotifyDescriptor.InputLine input = new NotifyDescriptor.InputLine( 227 getString("LAB_NewName"), 228 getString("LAB_NewIdentifier") 229 ); 230 if (origValue != null) 231 input.setInputText(origValue.getSourceName()); 232 233 for (;;) { 234 Object ret = DialogDisplayer.getDefault().notify(input); 235 if (ret == NotifyDescriptor.OK_OPTION) { 236 String retValue = input.getInputText(); 237 if (retValue != null && !"".equals(retValue)) { if (!retValue.startsWith(".") && !retValue.endsWith(".") && (retValue.indexOf("..") == -1)) { boolean ok = true; 241 StringTokenizer tokenizer = new StringTokenizer(retValue, ".", false); while (tokenizer.hasMoreTokens()) { 243 String token = tokenizer.nextToken(); 244 if (!Utilities.isJavaIdentifier(token)) { 245 ok = false; 246 break; 247 } 248 } 249 if (ok) 250 return Identifier.create(retValue); 251 } 252 } 253 DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(getString("MSG_NotValidID"))); 254 } 255 else { 256 return null; 257 } 258 } 259 } 260 } 261 262 private static String getString(String key) { 263 return NbBundle.getBundle("org.openide.explorer.propertysheet.editors.Bundle2", Locale.getDefault(), IdentifierArrayEditor.class.getClassLoader()).getString(key); 264 } 265 266 } 267 | Popular Tags |