1 19 20 21 package org.netbeans.modules.group; 22 23 import java.awt.datatransfer.Transferable ; 24 import java.beans.PropertyChangeEvent ; 25 import java.beans.PropertyChangeListener ; 26 import java.io.IOException ; 27 import java.text.MessageFormat ; 28 import java.util.List ; 29 import org.openide.filesystems.FileObject; 30 import org.openide.filesystems.FileStateInvalidException; 31 import org.openide.loaders.DataNode; 32 import org.openide.loaders.DataObject; 33 import org.openide.nodes.Children; 34 import org.openide.nodes.Node; 35 import org.openide.nodes.NodeTransfer; 36 import org.openide.nodes.PropertySupport; 37 import org.openide.nodes.Sheet; 38 import org.openide.util.NbBundle; 39 import org.openide.util.datatransfer.ExTransferable; 40 import org.openide.util.datatransfer.PasteType; 41 42 43 public class GroupNode extends DataNode implements PropertyChangeListener { 44 45 46 static final String GS_ICON_BASE 47 = "org/netbeans/modules/group/resources/groupShadow"; 49 50 private static MessageFormat groupFormat; 51 52 53 59 public GroupNode(final GroupShadow group, Children children) { 60 super(group, children); 61 setIconBase(GS_ICON_BASE); 62 } 63 64 66 public String getDisplayName() { 67 if (groupFormat == null) { 68 String fmtString = NbBundle.getMessage( 69 GroupNode.class, 70 "FMT_groupShadowName"); groupFormat = new MessageFormat (fmtString); 72 } 73 74 String name = getName(); 75 DataObject dataObject = getDataObject(); 76 FileObject fileObject = dataObject.getPrimaryFile(); 77 String defaultDisplayName = super.getDisplayName(); 78 String displayName = groupFormat.format( 79 new Object [] {name != null ? name : "", "", fileObject.toString(), 82 "", defaultDisplayName != null 84 ? defaultDisplayName 85 : ""}); try { 87 displayName = fileObject.getFileSystem().getStatus().annotateName( 88 displayName, 89 dataObject.files()); 90 91 } catch (FileStateInvalidException e) { 92 93 } 94 return displayName; 95 } 97 98 103 private GroupShadow getGroup() { 104 return (GroupShadow) getCookie(DataObject.class); 105 } 106 107 112 protected Sheet createSheet() { 113 Sheet s = super.createSheet(); 114 updateSheet(s); 115 return s; 116 } 117 118 120 public void propertyChange(PropertyChangeEvent evt) { 121 String propertyName = evt.getPropertyName(); 122 if (propertyName == null) { 123 return; 124 } 125 if (propertyName.equals(DataObject.PROP_TEMPLATE)) { 126 Object oldValue = evt.getOldValue(); 127 Object newValue = evt.getNewValue(); 128 if (newValue != null && !newValue.equals(oldValue)) { 129 updateSheet(getSheet()); 130 } 131 } else if (propertyName.equals(GroupShadow.PROP_USE_PATTERN)) { 132 updateSheet(getSheet()); 133 } 134 } 135 136 137 private void fillExpertSet(Sheet.Set set){ 138 final DataObject obj = getDataObject(); 139 Node.Property p; 140 141 try { 143 150 if (obj.isTemplate()){ 151 152 String dispName; 153 String descr; 154 final boolean isReadOnly = obj.getPrimaryFile().isReadOnly(); 155 156 157 p = new PropertySupport.Reflection( 158 obj, 159 Boolean.TYPE, 160 "getTemplateAll", isReadOnly ? null : "setTemplateAll"); p.setName(GroupShadow.PROP_TEMPLATE_ALL); 163 p.setDisplayName(NbBundle.getMessage( 164 GroupNode.class, 165 "PROP_templateall")); p.setShortDescription(NbBundle.getMessage( 167 GroupNode.class, 168 "HINT_templateall")); set.put(p); 170 171 172 class TemplatePatternProperty 173 extends PropertySupport.Reflection { 174 public TemplatePatternProperty(DataObject obj) 175 throws NoSuchMethodException { 176 super(obj, String .class, 177 "getTemplatePattern", obj.getPrimaryFile().isReadOnly() 179 ? null 180 : "setTemplatePattern"); } 182 public boolean canWrite() { 183 return GroupNode.this.getGroup().isUsePattern(); 184 } 185 } 186 p = new TemplatePatternProperty(obj); 187 p.setName(GroupShadow.PROP_TEMPLATE_PATTERN); 188 p.setDisplayName(NbBundle.getMessage( 189 GroupNode.class, 190 "PROP_templatePattern")); p.setShortDescription(NbBundle.getMessage( 192 GroupNode.class, 193 "HINT_templatePattern")); set.put(p); 195 196 197 p = new PropertySupport.Reflection( 198 obj, 199 Boolean.TYPE, 200 "isUsePattern", "setUsePattern"); p.setName(GroupShadow.PROP_USE_PATTERN); 203 p.setDisplayName(NbBundle.getMessage( 204 GroupNode.class, 205 "PROP_UsePattern")); p.setShortDescription(NbBundle.getMessage( 207 GroupNode.class, 208 "HINT_UsePattern")); set.put(p); 210 } 211 } catch (Exception ex) { 212 throw new InternalError (); 213 } 214 } 215 216 226 private void updateSheet(Sheet sheet){ 227 if (getDataObject().isTemplate()) { 228 Sheet.Set set = sheet.get(Sheet.EXPERT); 229 if (set == null) { 230 set = Sheet.createExpertSet(); 231 fillExpertSet(set); 232 sheet.put(set); 233 } else { 234 fillExpertSet(set); 235 } 236 } else { 237 sheet.remove(Sheet.EXPERT); 238 } 239 } 240 241 250 protected void createPasteTypes(Transferable t, List s) { 251 super.createPasteTypes(t, s); 252 if (getDataObject().getPrimaryFile().isReadOnly()) { 253 return; 254 } 255 256 DataObject objToPaste = null; 257 258 259 objToPaste = (DataObject) NodeTransfer.cookie( 260 t, 261 NodeTransfer.CLIPBOARD_COPY | NodeTransfer.CLIPBOARD_CUT, 262 DataObject.class); 263 264 if (objToPaste != null && objToPaste.isCopyAllowed()) { 265 s.add(new Paste("PT_copy", objToPaste, false)); } 268 } 269 270 271 272 private class Paste extends PasteType { 273 274 275 private String nameBundleKey; 276 277 private DataObject obj; 278 282 private boolean clearClipboard; 283 284 285 294 public Paste(String nameBundleKey, DataObject obj, boolean clear) { 295 this.nameBundleKey = nameBundleKey; 296 this.obj = obj; 297 this.clearClipboard = clear; 298 } 299 300 301 302 public String getName() { 303 return NbBundle.getMessage(GroupNode.class, nameBundleKey); 304 } 305 306 307 public final Transferable paste() throws IOException { 308 handle(obj); 309 return clearClipboard ? ExTransferable.EMPTY : null; 310 } 311 312 321 public void handle(DataObject objToPaste) throws IOException { 322 List list = getGroup().readLinks(); 323 String name = GroupShadow.getLinkName(objToPaste.getPrimaryFile()); 324 if (!list.contains(name)) { 325 list.add(name); 326 } 327 getGroup().writeLinks(list); 328 } 329 } 330 331 } 332 | Popular Tags |