KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > editor > mimelookup > impl > FolderPathLookup


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.editor.mimelookup.impl;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.logging.Level JavaDoc;
32 import java.util.logging.Logger JavaDoc;
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 /**
42  *
43  * @author vita
44  */

45 public final class FolderPathLookup extends AbstractLookup {
46     
47     private static final Logger JavaDoc 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 JavaDoc LOCK = new String JavaDoc("InstanceProviderLookup.LOCK"); //NOI18N
55

56     private final InstanceConvertor CONVERTOR = new InstanceConvertor();
57     
58     /** Creates a new instance of InstanceProviderLookup */
59     public FolderPathLookup(String JavaDoc [] paths) {
60         this(paths, new InstanceContent());
61     }
62     
63     private FolderPathLookup(String JavaDoc [] 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 JavaDoc files = children.getChildren();
76         ArrayList JavaDoc instanceFiles = new ArrayList JavaDoc();
77         
78         for (Iterator JavaDoc 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 JavaDoc e) {
88                 LOG.log(Level.WARNING, "Can't create DataObject", e); //NOI18N
89
}
90         }
91
92 // System.out.println("Setting instanceFiles for FolderPathLookup@" + System.identityHashCode(this) + " {");
93
// for (Iterator i = instanceFiles.iterator(); i.hasNext(); ) {
94
// String filePath = (String) i.next();
95
// System.out.println(" '" + filePath);
96
// }
97
// System.out.println("} End of Setting instanceFiles for FolderPathLookup@" + System.identityHashCode(this) + " -----------------------------");
98

99         content.set(instanceFiles, CONVERTOR);
100     }
101     
102     private class PCL implements PropertyChangeListener JavaDoc {
103         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
104             rebuild();
105         }
106     } // End of PCL class
107

108     private static final class InstanceConvertor implements InstanceContent.Convertor {
109         private HashMap JavaDoc types = new HashMap JavaDoc();
110         
111         public Class JavaDoc type(Object JavaDoc filePath) {
112             synchronized (types) {
113                 WeakReference JavaDoc ref = (WeakReference JavaDoc) types.get(filePath);
114                 Class JavaDoc type = ref == null ? null : (Class JavaDoc) ref.get();
115                 if (type == null) {
116                     try {
117                         type = getInstanceCookie(filePath).instanceClass();
118                         types.put(filePath, new WeakReference JavaDoc(type));
119                     } catch (Exception JavaDoc e) {
120                         LOG.log(Level.WARNING, "Can't determine instance class from '" + filePath + "'", e); //NOI18N
121
return DeadMarker.class; // Something nobody will ever find
122
}
123                 }
124                 
125                 return type;
126             }
127         }
128
129         public String JavaDoc id(Object JavaDoc filePath) {
130             return (String JavaDoc) filePath;
131         }
132
133         public String JavaDoc displayName(Object JavaDoc filePath) {
134             try {
135                 return getInstanceCookie(filePath).instanceName();
136             } catch (Exception JavaDoc e) {
137                 LOG.log(Level.WARNING, "Can't determine instance name from '" + filePath + "'", e); //NOI18N
138
return DeadMarker.class.getName();
139             }
140         }
141
142         public Object JavaDoc convert(Object JavaDoc filePath) {
143             try {
144                 return getInstanceCookie(filePath).instanceCreate();
145             } catch (Exception JavaDoc e) {
146                 LOG.log(Level.WARNING, "Can't create instance from '" + filePath + "'", e); //NOI18N
147
return DeadMarker.THIS;
148             }
149         }
150         
151         private InstanceCookie getInstanceCookie(Object JavaDoc filePath) throws IOException JavaDoc {
152             FileObject file = Repository.getDefault().getDefaultFileSystem().findResource((String JavaDoc) filePath);
153             if (file == null) {
154                 // Should not occure
155
throw new IOException JavaDoc("The file does not exist '" + filePath + "'"); //NOI18N
156
}
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                 // Should not occure
164
throw new IOException JavaDoc("Can't find InstanceCookie for '" + filePath + "'"); //NOI18N
165
}
166         }
167     } // End of InstanceConvertor class
168

169     private static final class DeadMarker {
170         public static final DeadMarker THIS = new DeadMarker();
171     } // End of DeadMarker class
172
}
173
Popular Tags