1 19 20 package org.netbeans.modules.apisupport.project.ui; 21 22 import java.awt.Image ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.Set ; 26 import org.openide.ErrorManager; 27 import org.openide.filesystems.FileObject; 28 import org.openide.filesystems.FileStateInvalidException; 29 import org.openide.filesystems.FileStatusEvent; 30 import org.openide.filesystems.FileStatusListener; 31 import org.openide.filesystems.FileSystem; 32 import org.openide.filesystems.FileUtil; 33 import org.openide.nodes.AbstractNode; 34 import org.openide.nodes.Children; 35 import org.openide.util.Lookup; 36 import org.openide.util.RequestProcessor; 37 38 class AnnotatedNode extends AbstractNode implements Runnable , FileStatusListener { 39 40 private Set files; 41 private Set fileSystemListeners; 42 private RequestProcessor.Task task; 43 private volatile boolean iconChange; 44 private volatile boolean nameChange; 45 private boolean forceAnnotation; 46 47 protected AnnotatedNode(Children children) { 48 super(children, null); 49 } 50 51 protected AnnotatedNode(Children children, Lookup lookup) { 52 super(children, lookup); 53 } 54 55 protected final void setFiles(final Set files) { 56 fileSystemListeners = new HashSet (); 57 this.files = files; 58 if (files == null) { 59 return; 60 } 61 Iterator it = files.iterator(); 62 Set hookedFileSystems = new HashSet (); 63 while (it.hasNext()) { 64 FileObject fo = (FileObject) it.next(); 65 try { 66 FileSystem fs = fo.getFileSystem(); 67 if (hookedFileSystems.contains(fs)) { 68 continue; 69 } 70 hookedFileSystems.add(fs); 71 FileStatusListener fsl = FileUtil.weakFileStatusListener(this, fs); 72 fs.addFileStatusListener(fsl); 73 fileSystemListeners.add(fsl); 74 } catch (FileStateInvalidException e) { 75 ErrorManager err = ErrorManager.getDefault(); 76 err.annotate(e, "Cannot get " + fo + " filesystem, ignoring..."); err.notify(ErrorManager.INFORMATIONAL, e); 78 } 79 } 80 } 81 82 protected final Set <FileObject> getFiles() { 83 return files; 84 } 85 86 protected void setForceAnnotation(boolean forceAnnotation) { 87 this.forceAnnotation = forceAnnotation; 88 } 89 90 protected final Image annotateIcon(final Image img, final int type) { 91 Image annotatedImg = img; 92 if (files != null && files.iterator().hasNext()) { 93 try { 94 FileObject fo = (FileObject) files.iterator().next(); 95 annotatedImg = fo.getFileSystem().getStatus().annotateIcon(img, type, files); 96 } catch (FileStateInvalidException e) { 97 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 98 } 99 } 100 return annotatedImg; 101 } 102 103 protected final String annotateName(final String name) { 104 String annotatedName = name; 105 if (files != null && files.iterator().hasNext()) { 106 try { 107 FileObject fo = (FileObject) files.iterator().next(); 108 annotatedName = fo.getFileSystem().getStatus().annotateName(name, files); 109 } catch (FileStateInvalidException e) { 110 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e); 111 } 112 } 113 return annotatedName; 114 } 115 116 public final void annotationChanged(FileStatusEvent event) { 117 if (task == null) { 118 task = RequestProcessor.getDefault().create(this); 119 } 120 121 boolean changed = false; 122 if (forceAnnotation || ((iconChange == false && event.isIconChange()) || (nameChange == false && event.isNameChange()))) { 123 Iterator it = files.iterator(); 124 while (it.hasNext()) { 125 FileObject fo = (FileObject) it.next(); 126 if (event.hasChanged(fo)) { 127 iconChange |= event.isIconChange(); 128 nameChange |= event.isNameChange(); 129 changed = true; 130 } 131 } 132 } 133 134 if (changed) { 135 task.schedule(50); } 137 } 138 139 public final void run() { 140 if (forceAnnotation || iconChange) { 141 fireIconChange(); 142 fireOpenedIconChange(); 143 iconChange = false; 144 } 145 if (forceAnnotation || nameChange) { 146 fireDisplayNameChange(null, null); 147 nameChange = false; 148 } 149 } 150 151 } 152 | Popular Tags |