1 19 20 package org.netbeans.modules.apisupport.project.layers; 21 22 import java.awt.Image ; 23 import java.awt.Toolkit ; 24 import java.beans.BeanInfo ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.net.URL ; 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Properties ; 32 import java.util.Set ; 33 import javax.swing.Action ; 34 import javax.swing.JSeparator ; 35 import org.netbeans.api.java.classpath.ClassPath; 36 import org.netbeans.modules.apisupport.project.Util; 37 import org.openide.ErrorManager; 38 import org.openide.awt.Actions; 39 import org.openide.cookies.InstanceCookie; 40 import org.openide.filesystems.FileAttributeEvent; 41 import org.openide.filesystems.FileChangeListener; 42 import org.openide.filesystems.FileEvent; 43 import org.openide.filesystems.FileObject; 44 import org.openide.filesystems.FileRenameEvent; 45 import org.openide.filesystems.FileStateInvalidException; 46 import org.openide.filesystems.FileStatusEvent; 47 import org.openide.filesystems.FileStatusListener; 48 import org.openide.filesystems.FileSystem; 49 import org.openide.filesystems.FileUtil; 50 import org.openide.filesystems.URLMapper; 51 import org.openide.loaders.DataObject; 52 import org.openide.util.NbBundle; 53 import org.openide.util.RequestProcessor; 54 import org.openide.util.actions.Presenter; 55 56 64 final class BadgingSupport implements FileSystem.Status, FileChangeListener { 65 66 67 private String suffix = ""; 68 69 private ClassPath classpath; 70 private final FileSystem fs; 71 private final FileChangeListener fileChangeListener; 72 private final List <FileStatusListener> listeners = new ArrayList (); 73 74 public BadgingSupport(FileSystem fs) { 75 this.fs = fs; 76 fileChangeListener = FileUtil.weakFileChangeListener(this, null); 77 fs.addFileChangeListener(fileChangeListener); 78 } 79 80 public void setClasspath(ClassPath classpath) { 81 this.classpath = classpath; 82 } 83 84 public void setSuffix(String suffix) { 85 this.suffix = suffix; 86 } 87 88 public void addFileStatusListener(FileStatusListener l) { 89 listeners.add(l); 90 } 91 92 public void removeFileStatusListener(FileStatusListener l) { 93 listeners.remove(l); 94 } 95 96 private void fireFileStatusChanged(FileStatusEvent e) { 97 Iterator it = listeners.iterator(); 98 while (it.hasNext()) { 99 ((FileStatusListener) it.next()).annotationChanged(e); 100 } 101 } 102 103 public String annotateName(String name, Set files) { 104 return annotateNameGeneral(name, files, suffix, fileChangeListener, classpath); 105 } 106 107 private static String annotateNameGeneral(String name, Set files, String suffix, FileChangeListener fileChangeListener, ClassPath cp) { 108 Iterator it = files.iterator(); 109 while (it.hasNext()) { 110 FileObject fo = (FileObject) it.next(); 111 String bundleName = (String ) fo.getAttribute("SystemFileSystem.localizingBundle"); if (bundleName != null) { 113 try { 114 URL [] u = LayerUtils.currentify(new URL ("nbresloc:/" + bundleName.replace('.', '/') + 116 ".properties"), suffix, cp); 118 for (int i = 0; i < u.length; i++) { 119 InputStream is = u[i].openStream(); 120 try { 121 Properties p = new Properties (); 122 p.load(is); 123 String key = fo.getPath(); 124 String val = p.getProperty(key); 125 FileObject ufo = URLMapper.findFileObject(u[i]); 127 if (ufo != null) { 128 ufo.removeFileChangeListener(fileChangeListener); 129 ufo.addFileChangeListener(fileChangeListener); 130 ufo.getParent().removeFileChangeListener(fileChangeListener); 132 ufo.getParent().addFileChangeListener(fileChangeListener); 133 } 134 if (val != null) { 135 if (fo.getPath().startsWith("Menu/")) { return Actions.cutAmpersand(val); 138 } else { 139 return val; 140 } 141 } 142 } finally { 144 is.close(); 145 } 146 } 147 } catch (IOException ioe) { 148 Util.err.notify(ErrorManager.INFORMATIONAL, ioe); 150 return NbBundle.getMessage(BadgingSupport.class, "LBL_no_such_bundle", name, bundleName); 151 } 152 } 153 if (fo.hasExt("instance")) { return getInstanceLabel(fo); 155 } 156 if (fo.hasExt("shadow")) { Object originalFile = fo.getAttribute("originalFile"); if (originalFile != null && originalFile instanceof String ) { 159 FileObject orig; 160 try { 161 orig = fo.getFileSystem().findResource((String ) originalFile); 162 } catch (FileStateInvalidException e) { 163 orig = null; 164 } 165 if (orig != null && orig.hasExt("instance")) { return getInstanceLabel(orig); 167 } 168 } 169 } 170 } 171 return name; 172 } 173 174 private static String getInstanceLabel(FileObject fo) { 175 try { 176 InstanceCookie ic = (InstanceCookie) DataObject.find(fo).getCookie(InstanceCookie.class); 178 if (ic != null) { 179 Object o = ic.instanceCreate(); 180 if (o instanceof Action ) { 181 String name = (String ) ((Action ) o).getValue(Action.NAME); 182 if (name != null) { 183 return Actions.cutAmpersand(name); 184 } else { 185 return toStringOf(o); 186 } 187 } else if (o instanceof Presenter.Menu) { 188 return ((Presenter.Menu) o).getMenuPresenter().getText(); 189 } else if (o instanceof JSeparator ) { 190 return NbBundle.getMessage(BadgingSupport.class, "LBL_separator"); 191 } else { 192 return toStringOf(o); 193 } 194 } 195 } catch (IOException e) { 196 } catch (ClassNotFoundException e) { 198 } 200 String clazz = (String ) fo.getAttribute("instanceClass"); if (clazz == null) { 203 clazz = fo.getName().replace('-', '.'); 204 } 205 String instanceCreate = (String ) fo.getAttribute("literal:instanceCreate"); if (instanceCreate != null && instanceCreate.startsWith("new:")) { clazz = instanceCreate.substring("new:".length()); } else if (instanceCreate != null && instanceCreate.startsWith("method:")) { String factoryDisplayLabel = instanceCreate.substring(instanceCreate.lastIndexOf('.', instanceCreate.lastIndexOf('.') - 1) + 1); 210 return NbBundle.getMessage(BadgingSupport.class, "LBL_instance_from", factoryDisplayLabel); 211 } 212 String clazzDisplayLabel = clazz.substring(clazz.lastIndexOf('.') + 1); 213 return NbBundle.getMessage(BadgingSupport.class, "LBL_instance_of", clazzDisplayLabel); 214 } 215 private static String toStringOf(Object o) { 216 String s = o.toString(); 217 if ((o.getClass().getName() + "@" + Integer.toHexString(o.hashCode())).equals(s)) { 218 String clazz = o.getClass().getName(); 220 String clazzDisplayLabel = clazz.substring(clazz.lastIndexOf('.') + 1); 221 return NbBundle.getMessage(BadgingSupport.class, "LBL_instance_of", clazzDisplayLabel); 222 } else { 223 return s; 224 } 225 } 226 227 public Image annotateIcon(Image icon, int type, Set files) { 228 return annotateIconGeneral(icon, type, files, suffix, fileChangeListener, classpath); 229 } 230 231 private static Image annotateIconGeneral(Image icon, int type, Set files, String suffix, 232 FileChangeListener fileChangeListener, ClassPath cp) { 233 String attr; 234 if (type == BeanInfo.ICON_COLOR_16x16) { 235 attr = "SystemFileSystem.icon"; } else if (type == BeanInfo.ICON_COLOR_32x32) { 237 attr = "SystemFileSystem.icon32"; } else { 239 return icon; 240 } 241 Iterator it = files.iterator(); 242 while (it.hasNext()) { 243 FileObject fo = (FileObject) it.next(); 244 Object value = fo.getAttribute(attr); 245 if (value instanceof Image ) { 246 return (Image )value; 248 } 249 if (value != null) { 250 try { 251 URL [] u = LayerUtils.currentify((URL ) value, suffix, cp); 252 FileObject ufo = URLMapper.findFileObject(u[0]); 253 if (ufo != null) { 254 ufo.removeFileChangeListener(fileChangeListener); 255 ufo.addFileChangeListener(fileChangeListener); 256 } 257 return Toolkit.getDefaultToolkit().getImage(u[0]); 258 } catch (Exception e) { 259 Util.err.notify(ErrorManager.INFORMATIONAL, e); 261 } 262 } 263 } 264 return icon; 265 } 266 267 public void fileDeleted(FileEvent fe) { 274 } 276 public void fileFolderCreated(FileEvent fe) { 277 } 279 public void fileDataCreated(FileEvent fe) { 280 someFileChange(); 284 } 285 public void fileAttributeChanged(FileAttributeEvent fe) { 286 someFileChange(); 287 } 288 public void fileRenamed(FileRenameEvent fe) { 289 someFileChange(); 290 } 291 public void fileChanged(FileEvent fe) { 292 someFileChange(); 293 } 294 private void someFileChange() { 295 RequestProcessor.getDefault().post(new Runnable () { 296 public void run() { 297 fireFileStatusChanged(new FileStatusEvent(fs, true, true)); 299 } 300 }); 301 } 302 303 } 304 | Popular Tags |