KickJava   Java API By Example, From Geeks To Geeks.

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


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 import javax.swing.Action JavaDoc;
24 import org.netbeans.modules.enode.ExtensibleActionsImpl;
25 import org.netbeans.modules.enode.TimedSoftReference;
26
27 /**
28  * This class computes list of actions from the content of the configuration
29  * storage (system file system, registry). It is used from ExtensibleNode to
30  * keep track of its actions. You can obtain a reference to an initialized
31  * object of this class using the static factory methods getInstance.
32  * <p>
33  * The folders (contexts) where the actions are stored begin with prefix
34  * "/ExtensibleNode/Actions/".
35  *
36  * @author David Strupl
37  */

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

44     private static Map cache = new HashMap();
45     
46     /**
47      * It is impossible to create instances of this class. Please use
48      * the static factory methods instead.
49      */

50     protected ExtensibleActions() {
51         if (! getClass().equals(ExtensibleActionsImpl.class)) {
52             throw new IllegalStateException JavaDoc("You cannot create a subclass of this class. Please read the JavaDoc comment"); // NOI18N
53
}
54     }
55     
56     /**
57      * This method computes the list (array) of actions configured
58      * using the paths (or path) parameter to getInstance.
59      * @return array of actions
60      */

61     public abstract Action JavaDoc[] getActions();
62
63     /**
64      * This method finds/creates an instance of ExtensibleActions bound to given
65      * path. Please use this method instead of constructor - it allows
66      * caching/sharing the instances of this class.
67      *
68      * @param path Path to the folder where the configured actions
69      * are stored.
70      * @param recurse If set to true the parent folders contents
71      * are also added to the result.
72      * @return Fully initialized ExtensibleActions object
73      * configured using the parameters provided.
74      */

75     public static ExtensibleActions getInstance(String JavaDoc path, boolean recurse) {
76         String JavaDoc[] paths = null;
77         if (recurse) {
78             paths = ExtensibleNode.computeHierarchicalPaths(path);
79         } else {
80             paths = new String JavaDoc[] { path };
81         }
82         return getInstance(paths);
83     }
84
85     /**
86      * This method finds/creates an instance of ExtensibleActions bound to given
87      * paths. Please use this method instead of constructor - it allows
88      * caching/sharing the instances of this class.
89      *
90      * @param paths Paths to the folder where the configured actions
91      * are stored.
92      * @return Fully initialized ExtensibleActions object
93      * configured using the parameters provided.
94      */

95     public static ExtensibleActions getInstance(String JavaDoc[] paths) {
96         // We use list as the key. It ensures that the hashCode will
97
// be same for the arrays of equals String instances.
98
Object JavaDoc key = Arrays.asList(paths);
99         
100         TimedSoftReference ref = null;
101         synchronized (cache) {
102             ref = (TimedSoftReference)cache.get(key);
103         }
104         ExtensibleActions instance = null;
105         if (ref != null) {
106             instance = (ExtensibleActions)ref.get();
107         }
108         if (instance == null) {
109             instance = new ExtensibleActionsImpl(paths);
110             synchronized (cache) {
111                 cache.put(key, new TimedSoftReference(instance, cache, key));
112             }
113         }
114         return instance;
115     }
116 }
117
118
Popular Tags