1 19 20 21 package org.netbeans.modules.group; 22 23 import java.io.IOException ; 24 import java.text.MessageFormat ; 25 import java.util.ArrayList ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import org.openide.ErrorManager; 29 import org.openide.actions.DeleteAction; 30 import org.openide.actions.PropertiesAction; 31 import org.openide.actions.ToolsAction; 32 import org.openide.filesystems.FileAttributeEvent; 33 import org.openide.filesystems.FileChangeAdapter; 34 import org.openide.filesystems.FileChangeListener; 35 import org.openide.filesystems.FileEvent; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileSystem; 38 import org.openide.filesystems.Repository; 39 import org.openide.loaders.DataObject; 40 import org.openide.nodes.AbstractNode; 41 import org.openide.nodes.Children; 42 import org.openide.util.NbBundle; 43 import org.openide.util.actions.SystemAction; 44 45 46 class ErrorNode extends AbstractNode { 47 48 54 private static MessageFormat nodeNameTemplate; 55 60 private static String nodeNameSimple; 61 62 63 private final GroupShadow group; 64 65 private final String linkName; 66 71 private FileObject[] parentFolders; 72 78 private FileChangeListener fileCreationListener; 79 80 81 82 public ErrorNode(GroupShadow group) { 83 this(group, null); 84 } 85 86 91 public ErrorNode(GroupShadow group, String linkName) { 92 super(Children.LEAF); 93 94 this.group = group; 95 this.linkName = linkName; 96 97 systemActions = new SystemAction[] { 98 SystemAction.get(DeleteAction.class), 99 null, 100 SystemAction.get(ToolsAction.class), 101 SystemAction.get(PropertiesAction.class) 102 }; 103 104 if (linkName != null) { 105 if (nodeNameTemplate == null) { 106 nodeNameTemplate = new MessageFormat (NbBundle.getMessage( 107 ErrorNode.class, 108 "FMT_brokenLinkWithTarget")); } 110 setDisplayName(nodeNameTemplate.format(new Object [] {linkName})); 111 try { 112 startFolderListening(); 113 } catch (Exception ex) { 114 ErrorManager.getDefault().notify(ErrorManager.ERROR, ex); 115 } 116 } else { 117 if (nodeNameSimple == null) { 118 nodeNameSimple = NbBundle.getMessage( 119 ErrorNode.class, 120 "FMT_brokenLinkWithoutTarget"); } 122 setDisplayName(nodeNameSimple); 123 } 124 } 125 126 127 public void destroy() throws IOException { 128 boolean modified = false; 129 List list = new ArrayList (group.readLinks()); 130 for (Iterator i = list.iterator(); i.hasNext(); ) { 131 String name = (String ) i.next(); 132 if (name.equals(this.linkName)) { 133 i.remove(); 134 modified = true; 135 } 136 } 137 if (modified) { 138 group.writeLinks(list); 139 } 140 141 cancelFolderListening(); 142 } 143 144 147 public boolean canDestroy() { 148 return true; 149 } 150 151 158 private void startFolderListening() { 159 int index = linkName.lastIndexOf('/'); 162 163 if (index > 0) { 164 String folder = linkName.substring(0, index); 165 DataObject dobj; 166 try { 167 dobj = GroupShadow.getDataObjectByName(folder, group.getPrimaryFile()); 168 } catch (IOException ex) { 169 return; 170 } 171 if (dobj == null) { 172 return; 173 } 174 parentFolders = new FileObject[] {dobj.getPrimaryFile()}; 175 } else { FileSystem[] fs = Repository.getDefault().toArray(); 177 parentFolders = new FileObject[fs.length]; 178 for (int i = 0; i < fs.length; i++) { 179 parentFolders[i] = fs[i].getRoot(); 180 } 181 } 182 183 fileCreationListener = new FileChangeAdapter() { 184 public void fileDataCreated(FileEvent fe) { 185 fileCreated(GroupShadow.getLinkName(fe.getFile())); 186 } 187 }; 188 for (int i = 0; i < parentFolders.length; i++) { 189 parentFolders[i].addFileChangeListener(fileCreationListener); 190 } 191 } 192 193 199 private void cancelFolderListening() { 200 if (parentFolders != null) { 201 for (int i = 0; i < parentFolders.length; i++) { 202 parentFolders[i].removeFileChangeListener(fileCreationListener); 203 } 204 parentFolders = null; 205 fileCreationListener = null; 206 } 207 } 208 209 220 private void fileCreated(String newFileName) { 221 if (newFileName.equals(linkName)) { 222 cancelFolderListening(); 223 GroupNodeChildren children 224 = (GroupNodeChildren) group.getNodeDelegate().getChildren(); 225 children.update(); 226 } 227 } 228 229 } 230 | Popular Tags |