1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.beans.*; 23 import java.text.MessageFormat ; 24 import java.util.*; 25 import org.netbeans.core.UIExceptions; 26 import org.openide.explorer.propertysheet.ExPropertyEditor; 27 import org.openide.explorer.propertysheet.PropertyEnv; 28 29 import org.openide.loaders.ExtensionList; 30 import org.openide.util.NbBundle; 31 import org.openide.nodes.Node; 32 33 40 public class ExtensionListEditor extends Object implements PropertyEditor, 41 ExPropertyEditor { 42 43 private ExtensionList value; 44 private PropertyChangeSupport support = new PropertyChangeSupport (this); 45 46 private boolean editable=true; 47 48 58 public void setValue(Object value) { 59 if ( value != null ) 60 this.value = (ExtensionList)((ExtensionList)value).clone (); 61 else 62 this.value = null; 63 support.firePropertyChange (null, null, null); 64 } 65 66 70 public Object getValue() { 71 return value; 72 } 73 74 77 public boolean isPaintable() { 78 return false; 79 } 80 81 98 public void paintValue (java.awt.Graphics gfx, java.awt.Rectangle box) { 99 } 100 101 113 public String getJavaInitializationString() { 114 return null; 115 } 116 117 123 public String getAsText () { 124 StringBuffer sb = new StringBuffer (); 125 String [] exts = getStringArray (); 126 for (int i = 0; i < exts.length; i++) { 127 if (i > 0) sb.append (", "); sb.append (exts[i]); 129 } 130 return sb.toString (); 131 } 132 133 140 public void setAsText (String text) throws java.lang.IllegalArgumentException { 141 try { 142 if ( NbBundle.getMessage ( org.openide.explorer.propertysheet.PropertyPanel.class, "CTL_Different_Values").equals( text ) ) { 143 setValue( new String [] { text } ); 145 } 146 else { 147 StringTokenizer st = new StringTokenizer (text, ",. \n\t"); setAs (st); 149 } 150 } catch (Exception e) { 151 IllegalArgumentException iae = new IllegalArgumentException (e.getMessage()); 152 String msg = e.getLocalizedMessage(); 153 if (msg == null) { 154 msg = MessageFormat.format( 155 NbBundle.getMessage( 156 ExtensionListEditor.class, "FMT_EXC_GENERIC_BAD_VALUE"), new Object [] {text}); 158 } 159 UIExceptions.annotateUser(iae, iae.getMessage(), msg, e, new Date()); 160 throw iae; 161 } 162 } 163 164 176 public String [] getTags() { 177 return null; 178 } 179 180 195 196 public java.awt.Component getCustomEditor() { 197 return new ExtensionListCustomEditor(this); 198 } 199 200 203 public boolean supportsCustomEditor() { 204 return true; 205 } 206 207 216 public void addPropertyChangeListener(PropertyChangeListener listener) { 217 support.addPropertyChangeListener (listener); 218 } 219 220 225 public void removePropertyChangeListener(PropertyChangeListener listener) { 226 support.removePropertyChangeListener (listener); 227 } 228 229 232 @SuppressWarnings ("unchecked") public String [] getStringArray() { 233 if (value == null) { 234 return new String [0]; 235 } 236 List<String > l = new ArrayList<String > (); 237 Enumeration<String > e = (Enumeration<String >)value.extensions (); 238 while (e.hasMoreElements ()) 239 l.add (e.nextElement ()); 240 241 242 e = (Enumeration<String >)value.mimeTypes (); 243 while (e.hasMoreElements ()) 244 l.add (e.nextElement ()); 245 246 Collections.sort (l); 247 return l.toArray (new String [l.size ()]); 248 } 249 250 253 public void setStringArray(String [] nue) { 254 setAs (org.openide.util.Enumerations.array (nue)); 255 } 256 257 258 261 private void setAs (Enumeration en) { 262 ExtensionList list = new ExtensionList (); 263 264 while (en.hasMoreElements()) { 265 String tok = (String )en.nextElement (); 266 if (tok.indexOf('/') >= 0) { 267 list.addMimeType(tok); 269 } else { 270 list.addExtension (tok); 271 } 272 } 273 value = list; 274 275 support.firePropertyChange (null, null, null); 276 } 277 278 279 public boolean isEditable(){ 280 return (editable); 281 } 282 283 public void attachEnv(PropertyEnv env) { 284 FeatureDescriptor desc = env.getFeatureDescriptor(); 285 if (desc instanceof Node.Property){ 286 Node.Property prop = (Node.Property)desc; 287 editable = prop.canWrite(); 288 } 289 } 290 } 291 | Popular Tags |