1 19 20 21 package org.netbeans.modules.group; 22 23 import java.beans.PropertyChangeEvent ; 24 import java.beans.PropertyChangeListener ; 25 import java.io.IOException ; 26 import java.text.MessageFormat ; 27 import java.util.ArrayList ; 28 import java.util.List ; 29 import java.util.ListIterator ; 30 import org.openide.ErrorManager; 31 import org.openide.filesystems.FileObject; 32 import org.openide.loaders.DataObject; 33 import org.openide.nodes.Children; 34 import org.openide.nodes.FilterNode; 35 import org.openide.nodes.Node; 36 import org.openide.util.NbBundle; 37 38 39 class LinkNode extends FilterNode implements PropertyChangeListener { 40 41 46 private static MessageFormat displayFormatWLink; 47 52 private static MessageFormat displayFormatWOLink; 53 54 private final GroupShadow group; 55 56 private String linkedFileName = null; 57 58 private DataObject linkedDataObj = null; 59 60 61 66 public LinkNode(GroupShadow group, Node original) { 67 super(original); 68 69 this.group = group; 70 71 linkedDataObj = (DataObject) original.getCookie(DataObject.class); 72 if (linkedDataObj == null) { 73 return; } 75 FileObject originalFO = linkedDataObj.getPrimaryFile(); 76 if (originalFO == null) { 77 return; } 79 linkedFileName = GroupShadow.getLinkName(originalFO); 80 81 linkedDataObj.addPropertyChangeListener(this); 82 } 83 84 85 90 public boolean canDestroy () { 91 return group.isDeleteAllowed(); 92 } 93 94 95 public void destroy() throws IOException { 96 linkedDataObj.removePropertyChangeListener(this); 97 98 List list = group.readLinks(); 99 for (int i = 0; i < list.size(); i++) { 100 String fileName = (String ) list.get(i); 101 if (fileName.equals(linkedFileName)) { 102 list.remove(i); 103 i--; 104 } 105 } 106 group.writeLinks(list); 107 } 108 109 112 public boolean canRename () { 113 return super.canRename() && group.isRenameAllowed(); 114 } 115 116 119 public boolean canCut () { 120 return super.canCut() && group.isMoveAllowed(); 121 } 122 123 124 126 public String getDisplayName() { 127 String foldername; 128 FileObject primary = linkedDataObj.getPrimaryFile(); 129 String fullname = primary.getPath(); 130 int index = fullname.lastIndexOf('/'); 131 if (index != -1) { 132 foldername = fullname.substring(0, index + 1); 133 } else { 134 foldername = ""; } 136 Object [] objs = new Object [] {linkedDataObj.getName(), 137 fullname, 138 foldername, 139 linkedDataObj.getNodeDelegate() 140 .getDisplayName()}; 141 142 if (group.showLinks) { 143 if (displayFormatWLink == null) { 144 String pattern = NbBundle.getMessage( 145 LinkNode.class, 146 "FMT_linkWithTarget"); displayFormatWLink = new MessageFormat (pattern); 148 } 149 return displayFormatWLink.format(objs); 150 } else { 151 if (displayFormatWOLink == null) { 152 String pattern = NbBundle.getMessage( 153 LinkNode.class, 154 "FMT_linkWithoutTarget"); displayFormatWOLink = new MessageFormat (pattern); 156 } 157 return displayFormatWOLink.format(objs); 158 } 159 } 160 161 162 public void propertyChange(PropertyChangeEvent evt) { 163 String propertyName = evt.getPropertyName(); 164 if (propertyName == null) { 165 return; 166 } 167 if (propertyName.equals(DataObject.PROP_PRIMARY_FILE)) { 168 169 170 try { 171 List fileNames = new ArrayList (group.readLinks()); 172 String newFileName = GroupShadow.getLinkName( 173 linkedDataObj.getPrimaryFile()); 174 175 for (ListIterator i = fileNames.listIterator(); i.hasNext(); ) { 176 String fileName = (String ) i.next(); 177 if (fileName.equals(linkedFileName)) { 178 i.set(newFileName); 179 } 180 } 181 linkedFileName = newFileName; 182 group.writeLinks(fileNames); 183 fireDisplayNameChange(null, null); 184 } catch (IOException ex) { 185 ErrorManager errMgr = ErrorManager.getDefault(); 186 String msg = NbBundle.getMessage( 187 LinkNode.class, 188 "MSG_Cannot_rename_link", group.getPrimaryFile().getPath()); 190 errMgr.notify(ErrorManager.INFORMATIONAL, 191 errMgr.annotate(ex, msg)); 192 } 193 } else if (propertyName.equals(DataObject.PROP_VALID)) { 194 195 196 if (!linkedDataObj.isValid()) { 197 linkedDataObj.removePropertyChangeListener(this); 198 ((GroupNodeChildren) group.getNodeDelegate().getChildren()) 199 .update(); 200 } 201 } 202 } 203 } 204 | Popular Tags |