KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > enode > ExtensibleIconsImpl


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 Nokia. Portions Copyright 2003-2004 Nokia.
17  * All Rights Reserved.
18  */

19
20 package org.netbeans.modules.enode;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.beans.PropertyChangeEvent JavaDoc;
27 import java.beans.PropertyChangeListener JavaDoc;
28 import java.beans.PropertyChangeSupport JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import javax.swing.ImageIcon JavaDoc;
32
33 import org.openide.ErrorManager;
34 import org.openide.util.WeakListeners;
35 import org.openide.util.RequestProcessor;
36
37 import org.netbeans.api.enode.*;
38 import org.netbeans.spi.enode.IconSet;
39 import org.netbeans.api.registry.*;
40
41 /**
42  * ExtensibleIcons implementation.
43  * @author David Strupl
44  */

45 public class ExtensibleIconsImpl extends ExtensibleIcons {
46     
47     private static ErrorManager log = ErrorManager.getDefault().getInstance(ExtensibleIconsImpl.class.getName());
48     private static boolean LOGGABLE = log.isLoggable(ErrorManager.INFORMATIONAL);
49     
50     private PropertyChangeSupport JavaDoc pcs = new PropertyChangeSupport JavaDoc(this);
51     
52     /**
53      * Our paths.
54      */

55     private String JavaDoc[] paths;
56     
57     /**
58      *
59      */

60     private IconSet iconSet;
61     
62     /**
63      * We hold a reference to the listener for preventing
64      * the garbage collection.
65      */

66     private Listener JavaDoc listener;
67     
68     /**
69      * Prevent the listeners to be attached more than once.
70      */

71     private boolean listenersAttached = false;
72     
73     /**
74      * To prevent garbage collection of context where we attached
75      * listeners. We just add items to the set and never do anything
76      * with them. But that is the reason why it is here - to hold
77      * strong references to the Context objects.
78      */

79     private Set JavaDoc listenersAttachedTo = new HashSet JavaDoc();
80     
81     /**
82      * Just remember the parameter enode.
83      */

84     public ExtensibleIconsImpl(String JavaDoc[] paths) {
85         this.paths = paths;
86     }
87     
88         /**
89      * Returns the default icon size of this <tt>IconSet</tt>.
90      *
91      * @return The default icon size.
92      */

93     public int getDefaultSize( ) {
94         return getIconSet().getDefaultSize();
95     }
96     
97     
98     /**
99      * Returns the icon defined by the name and icon size.
100      *
101      * @param name The name of the icon.
102      * @param size The size of the icon.
103      *
104      * @return The icon defined by the name or size or a default
105      * icon with the given size.
106      */

107     public ImageIcon JavaDoc getIcon( String JavaDoc name, int size ) {
108         return getIconSet().getIcon(name, size);
109     }
110     
111     
112     /**
113      * Returns the default icon for the given size.
114      *
115      * @return The default icon for the given size. If no default icon
116      * is defined a default icon with the given size is returned.
117      */

118     public ImageIcon JavaDoc getDefaultIcon( int size ) {
119         return getIconSet().getDefaultIcon(size);
120     }
121     
122     
123     /**
124      * Returns the default icon with the default size.
125      *
126      * @return The default icon for the default size. If no default icon
127      * is defined a default icon with the default size is returned.
128      *
129      * qsee #getDefaultSize
130      */

131     public ImageIcon JavaDoc getDefaultIcon( ) {
132         return getIconSet().getDefaultIcon();
133     }
134     
135     /**
136      * Returns the description of this <tt>IconSet</tt>.
137      *
138      * @return The description of this <tt>IconSet</tt>.
139      */

140     public String JavaDoc getDescription( ) {
141         return getIconSet().getDescription();
142     }
143     
144     /**
145      * provides the display name of the icon taken from the bundle file
146      * configured for the <tt>IconSet</tt>. If no bundle file was defined
147      * or if no entry was found the internal name is returned and an
148      * exception is logged.
149      *
150      * @param name The internal name of the icon.
151      *
152      * @return The localized display name of the icon.
153      */

154     public String JavaDoc getIconDisplayName( String JavaDoc name ) {
155         return getIconSet().getIconDisplayName(name);
156     }
157     
158     
159     /**
160      * Returns the names of all icons configured in this <tt>IconSet</tt>
161      * that match the given size.
162      *
163      * @param size The icon size.
164      *
165      * @return The names of all icons with the given size.
166      */

167     public String JavaDoc[] getAllIconNames( int size ) {
168         return getIconSet().getAllIconNames(size);
169     }
170
171     public void addPropertyChangeListener(PropertyChangeListener JavaDoc pcl) {
172         pcs.addPropertyChangeListener(pcl);
173     }
174     
175     public void removePropertyChangeListener(PropertyChangeListener JavaDoc pcl) {
176         pcs.removePropertyChangeListener(pcl);
177     }
178     
179     /**
180      *
181      */

182     IconSet getIconSet() {
183         if (LOGGABLE) log.log("getIconSet() called on " + this);
184         if (iconSet != null) {
185             if (LOGGABLE) log.log("getIconSet() returning cached value");
186             return iconSet;
187         }
188         ArrayList JavaDoc arr = new ArrayList JavaDoc ();
189         for (int i = 0; i < paths.length; i++) {
190             String JavaDoc path = ExtensibleNode.E_NODE_ICONS + paths[i];
191             try {
192                 boolean exists = true;
193                 Context con = Context.getDefault().getSubcontext(path);
194                 if (con == null) {
195                     con = ExtensibleLookupImpl.findExistingContext(path);
196                     exists = false;
197                 }
198                 if (!listenersAttached) {
199                     ContextListener l1 = getContextListener(con);
200                     con.addContextListener(l1);
201                     listenersAttachedTo.add(con);
202                 }
203                 if (! exists) {
204                     if (LOGGABLE) log.log("getIconSet() path " + path + " does not exist.");
205                     continue;
206                 }
207                 List JavaDoc objects = con.getOrderedObjects();
208                 Iterator JavaDoc it = objects.iterator();
209                 if (LOGGABLE) log.log("getIconSet() examining object on path " + path);
210                 while (it.hasNext()) {
211                     Object JavaDoc obj = it.next();
212                     if (LOGGABLE) log.log("getIconSet() trying to add " + obj);
213                     if (obj instanceof IconSet) {
214                         arr.add(obj);
215                     } else {
216                         if (LOGGABLE) log.log(obj + " is not icon set!");
217                     }
218                 }
219             } catch (Exception JavaDoc ce) {
220                 log.notify(ErrorManager.INFORMATIONAL, ce); // NOI18N
221
}
222         }
223         listenersAttached = true;
224         if (arr.isEmpty()) {
225             return new IconSet();
226         }
227         
228         IconSet previous = null;
229         for (Iterator JavaDoc i = arr.iterator(); i.hasNext(); ) {
230             IconSet next = (IconSet) i.next();
231             if (LOGGABLE) log.log("getIconSet() next " + next);
232             if (previous != null) {
233                 if (previous.getDelegate() == null) {
234                     if (LOGGABLE) log.log("getIconSet() setting " + next + " as delegate for " + previous);
235                     previous.setDelegate(next);
236                 }
237             }
238             previous = next;
239         }
240         
241         iconSet = (IconSet)arr.get(0);
242         return iconSet;
243     }
244
245     /**
246      * Lazy initialization of the listener variable. This method
247      * will return a weak listener according to the type argument.
248      * In both cases the weak listener references the object hold
249      * by the <code> listener </code> variable.
250      * @param type ObjectChangeListener or NamespaceChangeListener
251      */

252     private ContextListener getContextListener(Object JavaDoc source) {
253         if (listener == null) {
254             listener = new Listener JavaDoc();
255         }
256         return (ContextListener)WeakListeners.create(ContextListener.class, listener, source);
257     }
258     
259     /**
260      * If something has changed on the system file system, force
261      * the icon reload.
262      */

263     private void changeIcon() {
264         // clear our cache first
265
iconSet = null;
266         // fire in separate thread
267
RequestProcessor.getDefault().post(new Runnable JavaDoc() {
268             public void run() {
269                 pcs.firePropertyChange("icons", null, null);
270             }
271         });
272     }
273     
274     /**
275      * Whatever happens in the selected context this listener only calls
276      * changeIcon.
277      */

278     private class Listener implements ContextListener {
279         public void attributeChanged(AttributeEvent evt) {
280             changeIcon();
281         }
282         
283         public void bindingChanged(BindingEvent evt) {
284             changeIcon();
285         }
286         
287         public void subcontextChanged(SubcontextEvent evt) {
288             changeIcon();
289         }
290     }
291 }
292
Popular Tags