KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > enode > ExtensibleIcons


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.api.enode;
21
22 import java.util.*;
23
24 import javax.swing.ImageIcon JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26
27 import org.netbeans.modules.enode.ExtensibleIconsImpl;
28 import org.netbeans.modules.enode.TimedSoftReference;
29
30 /**
31  * This object allows reading information about icons from the
32  * configuration storage (system file system, registry). If you need an instance
33  * of this class please use one of the provided static factory methods.
34  * @author David Strupl
35  */

36 public abstract class ExtensibleIcons {
37     /**
38      * Maps List<String> --> ExtensibleIcons. The key is list of
39      * folders passed as paths parameter or computed by
40      * ExtensibleNode.computeHierarchicalPaths().
41      */

42     private static Map cache = new HashMap();
43     
44     /**
45      * This constructor will throw an exception if you attempt to call
46      * it from your code. Please DO NOT create instances of this class -
47      * use static factory methods getInstance.
48      */

49     protected ExtensibleIcons() {
50         if (! getClass().equals(ExtensibleIconsImpl.class)) {
51             throw new IllegalStateException JavaDoc("You cannot create a subclass of this class. Please read the JavaDoc comment"); // NOI18N
52
}
53     }
54     
55     /**
56      * Returns the default icon size.
57      *
58      * @return The default icon size.
59      */

60     public abstract int getDefaultSize( );
61     
62     
63     /**
64      * Returns the icon defined by the name and icon size.
65      *
66      * @param name The name of the icon.
67      * @param size The size of the icon.
68      *
69      * @return The icon defined by the name or size or a default
70      * icon with the given size.
71      */

72     public abstract ImageIcon JavaDoc getIcon( String JavaDoc name, int size );
73     
74     
75     /**
76      * Returns the default icon for the given size.
77      *
78      * @return The default icon for the given size. If no default icon
79      * is defined a default icon with the given size is returned.
80      */

81     public abstract ImageIcon JavaDoc getDefaultIcon( int size );
82     
83     
84     /**
85      * Returns the default icon with the default size.
86      *
87      * @return The default icon for the default size. If no default icon
88      * is defined a default icon with the default size is returned.
89      *
90      * qsee #getDefaultSize
91      */

92     public abstract ImageIcon JavaDoc getDefaultIcon( );
93     
94     /**
95      * Returns the description of the icon.
96      *
97      * @return The description of the icon.
98      */

99     public abstract String JavaDoc getDescription( );
100     
101     /**
102      * Provides the display name of the icon taken from the bundle file.
103      * If no bundle file was defined
104      * or if no entry was found the internal name is returned and an
105      * exception is logged.
106      *
107      * @param name The internal name of the icon.
108      *
109      * @return The localized display name of the icon.
110      */

111     public abstract String JavaDoc getIconDisplayName( String JavaDoc name );
112     
113     
114     /**
115      * Returns the names of all icons configured
116      * that match the given size.
117      *
118      * @param size The icon size.
119      *
120      * @return The names of all icons with the given size.
121      */

122     public abstract String JavaDoc[] getAllIconNames( int size );
123
124     /**
125      * This object will fire a propety change when the configuration is changed
126      * and the user should update the icons.
127      */

128     public abstract void addPropertyChangeListener(PropertyChangeListener JavaDoc pcl);
129     
130     /**
131      * This object will fire a propety change when the configuration is changed
132      * and the user should update the icons.
133      */

134     public abstract void removePropertyChangeListener(PropertyChangeListener JavaDoc pcl);
135     
136     /**
137      * This method finds/creates an instance of ExtensibleIcons bound to given
138      * path. Please use this method instead of constructor - it allows
139      * caching/sharing the instances of this class.
140      *
141      * @param path Path to the folder where the configured icons
142      * are stored.
143      * @param recurse If set to true the parent folders contents
144      * are also added to the result.
145      * @return Fully initialized ExtensibleIcons object
146      * configured using the parameters provided.
147      */

148     public static ExtensibleIcons getInstance(String JavaDoc path, boolean recurse) {
149         String JavaDoc[] paths = null;
150         if (recurse) {
151             paths = ExtensibleNode.computeHierarchicalPaths(path);
152         } else {
153             paths = new String JavaDoc[] { path };
154         }
155         return getInstance(paths);
156     }
157     
158     /**
159      * This method finds/creates an instance of ExtensibleIcons bound to given
160      * path. Please use this method instead of constructor - it allows
161      * caching/sharing the instances of this class.
162      *
163      * @param paths Paths to the folders where the configured icons
164      * are stored.
165      * @return Fully initialized ExtensibleIcons object
166      * configured using the parameters provided.
167      */

168     public static ExtensibleIcons getInstance(String JavaDoc[] paths) {
169         // We use list as the key. It ensures that the hashCode will
170
// be same for the arrays of equals String instances.
171
Object JavaDoc key = Arrays.asList(paths);
172         
173         TimedSoftReference ref = null;
174         synchronized (cache) {
175             ref = (TimedSoftReference)cache.get(key);
176         }
177         ExtensibleIcons instance = null;
178         if (ref != null) {
179             instance = (ExtensibleIcons)ref.get();
180         }
181         if (instance == null) {
182             instance = new ExtensibleIconsImpl(paths);
183             synchronized (cache) {
184                 cache.put(key, new TimedSoftReference(instance, cache, key));
185             }
186         }
187         return instance;
188     }
189 }
190
Popular Tags