1 19 20 package org.netbeans.beaninfo.editors; 21 22 import java.beans.*; 23 24 import org.openide.loaders.DataObject; 25 import org.openide.nodes.Node; 26 import org.openide.nodes.NodeAcceptor; 27 import org.openide.loaders.DataFolder; 28 import org.openide.loaders.DataFilter; 29 import org.openide.explorer.propertysheet.*; 30 31 36 public class DataObjectEditor extends PropertyEditorSupport implements ExPropertyEditor { 37 38 39 private static final String PROPERTY_CURRENT_FOLDER = "currentFolder"; 41 private static final String PROPERTY_ROOT_FOLDER = "rootFolder"; 43 private static final String PROPERTY_ROOT_NODE = "rootNode"; 45 private static final String PROPERTY_COOKIES = "cookies"; 47 private static final String PROPERTY_DATA_FILTER = "dataFilter"; 49 private static final String PROPERTY_FOLDER_FILTER = "folderFilter"; 51 private static final String PROPERTY_NODE_ACCEPTOR = "nodeAcceptor"; 53 private static final String PROPERTY_LABEL = "label"; 55 private static final String PROPERTY_TITLE = "title"; 57 private static final String PROPERTY_INSET = "inset"; 59 private static final String PROPERTY_DESCRIPTION = "description"; 61 private static final String PROPERTY_GUI_TYPE = "guitype"; 63 private static final String PROPERTY_SELECTION_MODE = "selectionMode"; 65 66 private DataObjectPanel customEditor; 67 68 69 private DataFolder rootFolder; 70 71 private Node rootNode; 72 73 private DataObject currentFolder; 74 75 private Class [] cookies; 76 77 private DataFilter dataFilter; 78 79 private DataFilter folderFilter; 80 81 private NodeAcceptor nodeAcceptor; 82 83 private String label; 84 85 private String title; 86 87 private Integer insets; 88 89 private String description; 90 93 private String guiType; 94 103 private Integer selectionMode; 104 105 private PropertyChangeSupport supp = new PropertyChangeSupport(this); 106 107 private PropertyEnv env; 108 109 113 public void attachEnv(PropertyEnv env) { 114 Object newObj = env.getFeatureDescriptor().getValue(PROPERTY_CURRENT_FOLDER); 115 if (newObj instanceof DataObject) { 116 currentFolder = (DataObject)newObj; 117 } 118 newObj = env.getFeatureDescriptor().getValue(PROPERTY_ROOT_FOLDER); 119 if (newObj instanceof DataFolder) { 120 rootFolder = (DataFolder)newObj; 121 } 122 newObj = env.getFeatureDescriptor().getValue(PROPERTY_ROOT_NODE); 123 if (newObj instanceof Node) { 124 rootNode = (Node)newObj; 125 } 126 newObj = env.getFeatureDescriptor().getValue(PROPERTY_COOKIES); 127 if (newObj instanceof Class []) { 128 cookies = (Class [])newObj; 129 } 130 newObj = env.getFeatureDescriptor().getValue(PROPERTY_DATA_FILTER); 131 if (newObj instanceof DataFilter) { 132 dataFilter = (DataFilter)newObj; 133 } 134 newObj = env.getFeatureDescriptor().getValue(PROPERTY_FOLDER_FILTER); 135 if (newObj instanceof DataFilter) { 136 folderFilter = (DataFilter)newObj; 137 } 138 newObj = env.getFeatureDescriptor().getValue(PROPERTY_NODE_ACCEPTOR); 139 if (newObj instanceof NodeAcceptor) { 140 nodeAcceptor = (NodeAcceptor)newObj; 141 } 142 newObj = env.getFeatureDescriptor().getValue(PROPERTY_LABEL); 143 if (newObj instanceof String ) { 144 label = (String )newObj; 145 } 146 newObj = env.getFeatureDescriptor().getValue(PROPERTY_TITLE); 147 if (newObj instanceof String ) { 148 title = (String )newObj; 149 } 150 newObj = env.getFeatureDescriptor().getValue(PROPERTY_INSET); 151 if (newObj instanceof Integer ) { 152 insets = (Integer )newObj; 153 } 154 newObj = env.getFeatureDescriptor().getValue(PROPERTY_DESCRIPTION); 155 if (newObj instanceof String ) { 156 description = (String )newObj; 157 } 158 newObj = env.getFeatureDescriptor().getValue(PROPERTY_GUI_TYPE); 159 if (newObj instanceof String ) { 160 guiType = (String )newObj; 161 } 162 newObj = env.getFeatureDescriptor().getValue(PROPERTY_SELECTION_MODE); 163 if (newObj instanceof Integer ) { 164 selectionMode = (Integer )newObj; 165 } 166 if ( env.getFeatureDescriptor().getValue( "canEditAsText" ) == null ) { env.getFeatureDescriptor().setValue( "canEditAsText", Boolean.FALSE ); } 170 if (env.getClass().equals(PropertyEnv.class)) { 171 if ((customEditor != null && customEditor.isVisible()) || 172 customEditor==null) { 173 this.env = env; 177 } 178 } 179 } 180 181 185 public java.awt.Component getCustomEditor() { 186 return getDataObjectPanel(); 187 } 188 189 193 private DataObjectPanel getDataObjectPanel() { 194 if (customEditor == null) { 200 if (guiType != null) { 201 if ("TreeView".equals(guiType)) { 202 customEditor = new DataObjectTreeView(this,env); 203 } else if ("ListView".equals(guiType)) { 204 customEditor = new DataObjectListView(this,env); 205 } else { 206 customEditor = new DataObjectListView(this,env); 207 } 208 } else { 209 customEditor = new DataObjectListView(this,env); 210 } 211 } else { 212 customEditor.setEnv(env); 215 } 216 if (cookies != null) { 217 customEditor.setDataFilter(new CookieFilter(cookies, dataFilter)); 218 } else { 219 customEditor.setDataFilter(dataFilter); 220 } 221 Object value = getValue(); 222 if ( value != null && value instanceof DataObject) { 223 customEditor.setDataObject( (DataObject)value ); 224 } 225 else if (currentFolder != null) { 226 customEditor.setDataObject(currentFolder); 227 } 228 if (label != null) { 229 customEditor.setText(label); 230 } 231 if (title != null) { 232 customEditor.putClientProperty("title", title); } 234 if (nodeAcceptor != null) { 235 customEditor.setNodeFilter(nodeAcceptor); 236 } 237 if (folderFilter != null) { 238 customEditor.setFolderFilter(folderFilter); 239 } 240 if (rootFolder != null) { 241 customEditor.setRootObject(rootFolder); 242 } 243 if (rootNode != null) { 244 customEditor.setRootNode(rootNode); 245 } 246 if (insets != null) { 247 customEditor.setInsetValue(insets.intValue()); 248 } 249 if (description != null) { 250 customEditor.setDescription(description); 251 } 252 if (selectionMode != null) { 253 customEditor.setSelectionMode(selectionMode.intValue()); 254 } 255 customEditor.setMultiSelection(false); 256 return customEditor; 257 } 258 259 263 public boolean supportsCustomEditor() { 264 return true; 265 } 266 267 268 public void addPropertyChangeListener(PropertyChangeListener l) { 269 super.addPropertyChangeListener(l); 270 supp.addPropertyChangeListener(l); 271 } 272 273 274 public void removePropertyChangeListener(PropertyChangeListener l) { 275 super.removePropertyChangeListener(l); 276 supp.removePropertyChangeListener(l); 277 } 278 279 public String getAsText() { 280 Object value = getValue(); 281 if (value instanceof DataObject) { 282 return ((DataObject)value).getNodeDelegate().getDisplayName(); 283 } 284 return ""; 285 } 286 287 public void setAsText(String text) throws java.lang.IllegalArgumentException { 288 if ((text==null)||("".equals(text))) setValue(null); 289 } 290 291 294 private static class CookieFilter implements DataFilter { 295 private Class [] cookieArray; 296 private DataFilter originalFilter; 297 298 299 public CookieFilter(Class [] cookieArray, DataFilter originalFilter) { 300 this.cookieArray = cookieArray; 301 this.originalFilter = originalFilter; 302 } 303 310 public boolean acceptDataObject (DataObject obj) { 311 if (cookieArray == null) { 312 if (originalFilter != null) { 313 return originalFilter.acceptDataObject(obj); 314 } else { 315 return true; 316 } 317 } 318 for (int i = 0; i < cookieArray.length; i++) { 319 if (obj.getCookie(cookieArray[i]) == null) { 320 return false; 321 } 322 } 323 if (originalFilter != null) { 324 return originalFilter.acceptDataObject(obj); 325 } else { 326 return true; 327 } 328 } 329 } 330 } 331 | Popular Tags |