1 19 20 package org.netbeans.modules.editor.mimelookup.impl; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.io.IOException ; 25 import java.lang.ref.WeakReference ; 26 import java.util.ArrayList ; 27 import java.util.Collections ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.logging.Level ; 32 import java.util.logging.Logger ; 33 import org.netbeans.spi.editor.mimelookup.InstanceProvider; 34 import org.openide.cookies.InstanceCookie; 35 import org.openide.filesystems.FileObject; 36 import org.openide.filesystems.Repository; 37 import org.openide.loaders.DataObject; 38 import org.openide.util.lookup.AbstractLookup; 39 import org.openide.util.lookup.InstanceContent; 40 41 45 public final class FolderPathLookup extends AbstractLookup { 46 47 private static final Logger LOG = Logger.getLogger(FolderPathLookup.class.getName()); 48 49 private InstanceContent content; 50 51 private CompoundFolderChildren children; 52 private PCL listener = new PCL(); 53 54 private final String LOCK = new String ("InstanceProviderLookup.LOCK"); 56 private final InstanceConvertor CONVERTOR = new InstanceConvertor(); 57 58 59 public FolderPathLookup(String [] paths) { 60 this(paths, new InstanceContent()); 61 } 62 63 private FolderPathLookup(String [] paths, InstanceContent content) { 64 super(content); 65 66 this.content = content; 67 68 this.children = new CompoundFolderChildren(paths, false); 69 this.children.addPropertyChangeListener(listener); 70 71 rebuild(); 72 } 73 74 private void rebuild() { 75 List files = children.getChildren(); 76 ArrayList instanceFiles = new ArrayList (); 77 78 for (Iterator i = files.iterator(); i.hasNext(); ) { 79 FileObject file = (FileObject) i.next(); 80 81 try { 82 DataObject d = DataObject.find(file); 83 InstanceCookie instanceCookie = (InstanceCookie) d.getCookie(InstanceCookie.class); 84 if (instanceCookie != null) { 85 instanceFiles.add(file.getPath()); 86 } 87 } catch (Exception e) { 88 LOG.log(Level.WARNING, "Can't create DataObject", e); } 90 } 91 92 99 content.set(instanceFiles, CONVERTOR); 100 } 101 102 private class PCL implements PropertyChangeListener { 103 public void propertyChange(PropertyChangeEvent evt) { 104 rebuild(); 105 } 106 } 108 private static final class InstanceConvertor implements InstanceContent.Convertor { 109 private HashMap types = new HashMap (); 110 111 public Class type(Object filePath) { 112 synchronized (types) { 113 WeakReference ref = (WeakReference ) types.get(filePath); 114 Class type = ref == null ? null : (Class ) ref.get(); 115 if (type == null) { 116 try { 117 type = getInstanceCookie(filePath).instanceClass(); 118 types.put(filePath, new WeakReference (type)); 119 } catch (Exception e) { 120 LOG.log(Level.WARNING, "Can't determine instance class from '" + filePath + "'", e); return DeadMarker.class; } 123 } 124 125 return type; 126 } 127 } 128 129 public String id(Object filePath) { 130 return (String ) filePath; 131 } 132 133 public String displayName(Object filePath) { 134 try { 135 return getInstanceCookie(filePath).instanceName(); 136 } catch (Exception e) { 137 LOG.log(Level.WARNING, "Can't determine instance name from '" + filePath + "'", e); return DeadMarker.class.getName(); 139 } 140 } 141 142 public Object convert(Object filePath) { 143 try { 144 return getInstanceCookie(filePath).instanceCreate(); 145 } catch (Exception e) { 146 LOG.log(Level.WARNING, "Can't create instance from '" + filePath + "'", e); return DeadMarker.THIS; 148 } 149 } 150 151 private InstanceCookie getInstanceCookie(Object filePath) throws IOException { 152 FileObject file = Repository.getDefault().getDefaultFileSystem().findResource((String ) filePath); 153 if (file == null) { 154 throw new IOException ("The file does not exist '" + filePath + "'"); } 157 158 DataObject d = DataObject.find(file); 159 InstanceCookie cookie = (InstanceCookie) d.getCookie(InstanceCookie.class); 160 if (cookie != null) { 161 return cookie; 162 } else { 163 throw new IOException ("Can't find InstanceCookie for '" + filePath + "'"); } 166 } 167 } 169 private static final class DeadMarker { 170 public static final DeadMarker THIS = new DeadMarker(); 171 } } 173 | Popular Tags |