1 19 20 package org.netbeans.modules.xml.xam.ui.customizer; 21 22 import java.io.IOException ; 23 import org.netbeans.modules.xml.xam.Model; 24 import org.openide.loaders.DataObject; 25 import org.openide.nodes.FilterNode; 26 import org.openide.nodes.Node; 27 import org.netbeans.modules.xml.xam.ui.ModelCookie; 28 import org.openide.ErrorManager; 29 import org.openide.filesystems.FileObject; 30 import org.openide.nodes.Node.Property; 31 import org.openide.nodes.PropertySupport.Reflection; 32 import org.openide.nodes.Sheet; 33 import org.openide.nodes.Sheet.Set; 34 import org.openide.util.NbBundle; 35 36 42 public class ExternalReferenceDataNode extends FilterNode 43 implements ExternalReferenceNode { 44 45 public static final String PROP_SELECTED = "selected"; 46 47 public static final String PROP_PREFIX = "prefix"; 48 49 private ExternalReferenceDecorator decorator; 50 51 private Sheet sheet; 52 53 private boolean selected; 54 55 private String prefix; 56 57 63 public ExternalReferenceDataNode(Node original, 64 ExternalReferenceDecorator decorator) { 65 super(original, new Children(original, decorator)); 66 this.decorator = decorator; 67 } 68 69 public boolean canRename() { 70 return false; 73 } 74 75 80 public boolean canSelect() { 81 DataObject dobj = (DataObject) getLookup().lookup(DataObject.class); 82 return dobj != null && !dobj.getPrimaryFile().isFolder() && 83 decorator.validate(this) == null; 84 } 85 86 97 private Node.Property createProperty(String key, Class type, Object inst, 98 String getter, String setter) { 99 Property prop = null; 100 try { 101 prop = new Reflection(inst, type, getter, setter); 102 prop.setName(key); 103 prop.setDisplayName(NbBundle.getMessage( 104 ExternalReferenceDataNode.class, 105 "CTL_ExternalReferenceCreator_Column_Name_" + key)); 106 prop.setShortDescription(NbBundle.getMessage( 107 ExternalReferenceDataNode.class, 108 "CTL_ExternalReferenceCreator_Column_Desc_" + key)); 109 } catch (NoSuchMethodException nsme) { 110 ErrorManager.getDefault().notify(nsme); 111 } 112 return prop; 113 } 114 115 protected Sheet createSheet() { 116 Sheet sheet = Sheet.createDefault(); 117 Set set = sheet.get(Sheet.PROPERTIES); 118 set.put(createProperty(PROP_NAME, String .class, this, 119 "getHtmlDisplayName", null)); 120 if (canSelect()) { 121 set.put(createProperty(PROP_SELECTED, Boolean.TYPE, this, 122 "isSelected", "setSelected")); 123 Node.Property prop = createProperty(PROP_PREFIX, String .class, 124 this, "getPrefix", "setPrefix"); 125 prop.setValue("suppressCustomEditor", Boolean.TRUE); 127 set.put(prop); 128 } else { 129 Node.Property prop = createProperty(PROP_PREFIX, String .class, 133 this, "getPrefix", null); 134 prop.setValue("suppressCustomEditor", Boolean.TRUE); 136 set.put(prop); 137 } 138 return sheet; 139 } 140 141 protected final synchronized Sheet getSheet() { 142 if (sheet != null) { 143 return sheet; 144 } 145 sheet = createSheet(); 146 firePropertySetsChange(null, null); 147 return sheet; 148 } 149 150 public PropertySet[] getPropertySets() { 151 Sheet s = getSheet(); 152 return s.toArray(); 153 } 154 155 public String getHtmlDisplayName() { 156 String name = getOriginal().getHtmlDisplayName(); 157 if (decorator != null) { 158 if (name == null) { 159 name = getDisplayName(); 160 } 161 name = decorator.getHtmlDisplayName(name, this); 162 } 163 return name; 164 } 165 166 public String getNamespace() { 167 DataObject dobj = (DataObject) getLookup().lookup(DataObject.class); 168 if (dobj != null) { 169 ModelCookie cookie = (ModelCookie) dobj.getCookie(ModelCookie.class); 170 if (cookie != null) { 171 try { 172 Model model = cookie.getModel(); 173 return decorator.getNamespace(model); 174 } catch (IOException ioe) { 175 return null; 176 } 177 } 178 } 179 return null; 180 } 181 182 public Model getModel() { 183 DataObject dobj = (DataObject) getLookup().lookup(DataObject.class); 184 if (dobj != null) { 185 ModelCookie cookie = (ModelCookie) dobj.getCookie(ModelCookie.class); 186 if (cookie != null) { 187 try { 188 return cookie.getModel(); 189 } catch (IOException ioe) { 190 return null; 191 } 192 } 193 } 194 return null; 195 } 196 197 public String getPrefix() { 198 if (prefix == null) { 199 prefix = decorator.generatePrefix(this); 200 } 201 return prefix; 202 } 203 204 public boolean isSelected() { 205 return selected; 206 } 207 208 public boolean hasModel() { 209 DataObject dobj = (DataObject) getLookup().lookup(DataObject.class); 210 if (dobj != null) { 211 ModelCookie cookie = (ModelCookie) dobj.getCookie(ModelCookie.class); 212 return cookie != null; 216 } 217 return false; 218 } 219 220 public void setDisplayName(String s) { 221 super.disableDelegation(DELEGATE_GET_DISPLAY_NAME|DELEGATE_SET_DISPLAY_NAME); 222 super.setDisplayName(s); 223 } 224 225 230 public void setPrefix(String prefix) { 231 String old = this.prefix; 232 this.prefix = prefix; 233 firePropertyChange(PROP_PREFIX, old, prefix); 234 } 235 236 241 public void setSelected(boolean selected) { 242 if (!canSelect()) { 243 throw new IllegalStateException ("node cannot be selected"); 244 } 245 boolean old = this.selected; 246 this.selected = selected; 247 firePropertyChange(PROP_SELECTED, old, selected); 248 } 249 250 private static class Children extends FilterNode.Children { 251 252 private ExternalReferenceDecorator decorator; 253 254 public Children(Node original, ExternalReferenceDecorator decorator) { 255 super(original); 256 this.decorator = decorator; 257 } 258 259 protected Node[] createNodes(Node n) { 260 DataObject dobj = (DataObject) n.getLookup().lookup(DataObject.class); 261 if (dobj != null) { 262 FileObject fobj = dobj.getPrimaryFile(); 263 if (fobj.isFolder() && fobj.getNameExt().equals("nbproject") && 264 fobj.getFileObject("project.xml") != null) { 265 return new Node[0]; 267 } 268 ModelCookie cookie = (ModelCookie) dobj.getCookie(ModelCookie.class); 269 String fname = fobj.getNameExt(); 270 String ext = decorator.getDocumentType().toString(); 271 if (fobj.isFolder() || cookie != null && fname.endsWith(ext)) { 272 return super.createNodes(n); 273 } 274 } 275 return new Node[0]; 276 } 277 278 protected Node copyNode(Node node) { 279 return decorator.createExternalReferenceNode(node); 280 } 281 } 282 } 283 | Popular Tags |