1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.beans.*; 23 import java.text.MessageFormat ; 24 import org.netbeans.core.UIExceptions; 25 26 import org.openide.loaders.DataObject; 27 import org.openide.nodes.Node; 28 import org.openide.nodes.NodeAcceptor; 29 import org.openide.loaders.DataFolder; 30 import org.openide.loaders.DataFilter; 31 import org.openide.explorer.propertysheet.*; 32 import org.openide.util.NbBundle; 33 34 39 public class DataObjectArrayEditor extends PropertyEditorSupport implements ExPropertyEditor { 40 41 42 private static final String PROPERTY_CURRENT_FOLDER = "currentFolder"; 44 private static final String PROPERTY_ROOT_FOLDER = "rootFolder"; 46 private static final String PROPERTY_ROOT_NODE = "rootNode"; 48 private static final String PROPERTY_COOKIES = "cookies"; 50 private static final String PROPERTY_DATA_FILTER = "dataFilter"; 52 private static final String PROPERTY_FOLDER_FILTER = "folderFilter"; 54 private static final String PROPERTY_NODE_ACCEPTOR = "nodeAcceptor"; 56 private static final String PROPERTY_LABEL = "label"; 58 private static final String PROPERTY_TITLE = "title"; 60 private static final String PROPERTY_INSET = "inset"; 62 private static final String PROPERTY_DESCRIPTION = "description"; 64 private static final String PROPERTY_GUI_TYPE = "guitype"; 66 private static final String PROPERTY_SELECTION_MODE = "selectionMode"; 68 69 private DataObjectPanel customEditor; 70 71 72 private DataFolder rootFolder; 73 74 private Node rootNode; 75 76 private DataObject currentFolder; 77 78 private Class [] cookies; 79 80 private DataFilter dataFilter; 81 82 private DataFilter folderFilter; 83 84 private NodeAcceptor nodeAcceptor; 85 86 private String label; 87 88 private String title; 89 90 private Integer insets; 91 92 private String description; 93 96 private String guiType; 97 106 private Integer selectionMode; 107 108 private PropertyChangeSupport supp = new PropertyChangeSupport(this); 109 110 private PropertyEnv env; 111 112 116 public void attachEnv(PropertyEnv env) { 117 Object newObj = env.getFeatureDescriptor().getValue(PROPERTY_CURRENT_FOLDER); 118 if (newObj instanceof DataObject) { 119 currentFolder = (DataObject)newObj; 120 } 121 newObj = env.getFeatureDescriptor().getValue(PROPERTY_ROOT_FOLDER); 122 if (newObj instanceof DataFolder) { 123 rootFolder = (DataFolder)newObj; 124 } 125 newObj = env.getFeatureDescriptor().getValue(PROPERTY_ROOT_NODE); 126 if (newObj instanceof Node) { 127 rootNode = (Node)newObj; 128 } 129 newObj = env.getFeatureDescriptor().getValue(PROPERTY_COOKIES); 130 if (newObj instanceof Class []) { 131 cookies = (Class [])newObj; 132 } 133 newObj = env.getFeatureDescriptor().getValue(PROPERTY_DATA_FILTER); 134 if (newObj instanceof DataFilter) { 135 dataFilter = (DataFilter)newObj; 136 } 137 newObj = env.getFeatureDescriptor().getValue(PROPERTY_FOLDER_FILTER); 138 if (newObj instanceof DataFilter) { 139 folderFilter = (DataFilter)newObj; 140 } 141 newObj = env.getFeatureDescriptor().getValue(PROPERTY_NODE_ACCEPTOR); 142 if (newObj instanceof NodeAcceptor) { 143 nodeAcceptor = (NodeAcceptor)newObj; 144 } 145 newObj = env.getFeatureDescriptor().getValue(PROPERTY_LABEL); 146 if (newObj instanceof String ) { 147 label = (String )newObj; 148 } 149 newObj = env.getFeatureDescriptor().getValue(PROPERTY_TITLE); 150 if (newObj instanceof String ) { 151 title = (String )newObj; 152 } 153 newObj = env.getFeatureDescriptor().getValue(PROPERTY_INSET); 154 if (newObj instanceof Integer ) { 155 insets = (Integer )newObj; 156 } 157 newObj = env.getFeatureDescriptor().getValue(PROPERTY_DESCRIPTION); 158 if (newObj instanceof String ) { 159 description = (String )newObj; 160 } 161 newObj = env.getFeatureDescriptor().getValue(PROPERTY_GUI_TYPE); 162 if (newObj instanceof String ) { 163 guiType = (String )newObj; 164 } 165 newObj = env.getFeatureDescriptor().getValue(PROPERTY_SELECTION_MODE); 166 if (newObj instanceof Integer ) { 167 selectionMode = (Integer )newObj; 168 } 169 env.getFeatureDescriptor().setValue( "canEditAsText", Boolean.FALSE ); 171 172 if (env.getClass().equals(PropertyEnv.class)) { 173 if ((customEditor != null && customEditor.isVisible()) || 177 customEditor==null) { 178 this.env = env; 179 } 180 } 181 } 182 183 187 public java.awt.Component getCustomEditor() { 188 return getDataObjectPanel(); 189 } 190 191 void setOkButtonEnabled(boolean state) { 192 if (env != null) { 193 env.setState(state ? PropertyEnv.STATE_VALID:PropertyEnv.STATE_INVALID); 194 } 195 } 196 197 201 private DataObjectPanel getDataObjectPanel() { 202 if (customEditor == null) { 208 if (guiType != null) { 209 if ("TreeView".equals(guiType)) { 210 customEditor = new DataObjectTreeView(this,env); 211 } else if ("ListView".equals(guiType)) { 212 customEditor = new DataObjectListView(this,env); 213 } else { 214 customEditor = new DataObjectListView(this,env); 215 } 216 } else { 217 customEditor = new DataObjectListView(this,env); 218 } 219 } else { 220 if (env.getClass().equals(PropertyEnv.class)) { 222 customEditor.setEnv(env); 223 } 224 } 225 if (cookies != null) { 226 customEditor.setDataFilter(new CookieFilter(cookies, dataFilter)); 227 } else { 228 customEditor.setDataFilter(dataFilter); 229 } 230 Object value = getValue(); 231 if ( value != null && value instanceof DataObject) { 232 customEditor.setDataObject( (DataObject)value ); 233 } 234 else if (currentFolder != null) { 235 customEditor.setDataObject(currentFolder); 236 } 237 if (label != null) { 238 customEditor.setText(label); 239 } 240 if (title != null) { 241 customEditor.putClientProperty("title", title); } 243 if (nodeAcceptor != null) { 244 customEditor.setNodeFilter(nodeAcceptor); 245 } 246 if (folderFilter != null) { 247 customEditor.setFolderFilter(folderFilter); 248 } 249 if (rootFolder != null) { 250 customEditor.setRootObject(rootFolder); 251 } 252 if (rootNode != null) { 253 customEditor.setRootNode(rootNode); 254 } 255 if (insets != null) { 256 customEditor.setInsetValue(insets.intValue()); 257 } 258 if (description != null) { 259 customEditor.setDescription(description); 260 } 261 if (selectionMode != null) { 262 customEditor.setSelectionMode(selectionMode.intValue()); 263 } 264 customEditor.setMultiSelection(true); 265 return customEditor; 266 } 267 268 272 public boolean supportsCustomEditor() { 273 return true; 274 } 275 276 277 public void addPropertyChangeListener(PropertyChangeListener l) { 278 super.addPropertyChangeListener(l); 279 supp.addPropertyChangeListener(l); 280 } 281 282 283 public void removePropertyChangeListener(PropertyChangeListener l) { 284 super.removePropertyChangeListener(l); 285 supp.removePropertyChangeListener(l); 286 } 287 288 public String getAsText() { 289 Object value = getValue(); 290 if (value instanceof DataObject) { 291 return ((DataObject)value).getNodeDelegate().getDisplayName(); 292 } 293 return ""; 294 } 295 296 public void setAsText(String text) throws java.lang.IllegalArgumentException { 297 try { 298 if ((text==null)||("".equals(text))) setValue(null); 299 } catch (Exception e) { 300 IllegalArgumentException iae = new IllegalArgumentException (e.getMessage()); 301 String msg = e.getLocalizedMessage(); 302 if (msg == null) { 303 msg = MessageFormat.format( 304 NbBundle.getMessage( 305 DataObjectArrayEditor.class, 306 "FMT_EXC_GENERIC_BAD_VALUE"), new Object [] {text}); } 308 UIExceptions.annotateUser(iae, iae.getMessage(), msg, e, 309 new java.util.Date ()); 310 throw iae; 311 } 312 } 313 314 317 private static class CookieFilter implements DataFilter { 318 private Class [] cookieArray; 319 private DataFilter originalFilter; 320 321 322 public CookieFilter(Class [] cookieArray, DataFilter originalFilter) { 323 this.cookieArray = cookieArray; 324 this.originalFilter = originalFilter; 325 } 326 333 public boolean acceptDataObject (DataObject obj) { 334 if (cookieArray == null) { 335 if (originalFilter != null) { 336 return originalFilter.acceptDataObject(obj); 337 } else { 338 return true; 339 } 340 } 341 for (int i = 0; i < cookieArray.length; i++) { 342 if (obj.getCookie(cookieArray[i]) == null) { 343 return false; 344 } 345 } 346 if (originalFilter != null) { 347 return originalFilter.acceptDataObject(obj); 348 } else { 349 return true; 350 } 351 } 352 } 353 } 354 | Popular Tags |