KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > search > FindActionManager


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.search;
21
22
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.lang.ref.Reference JavaDoc;
26 import java.lang.ref.WeakReference JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import javax.swing.Action JavaDoc;
29 import javax.swing.ActionMap JavaDoc;
30 import org.openide.ErrorManager;
31
32 import org.openide.actions.FindAction;
33 import org.openide.text.CloneableEditorSupport;
34 import org.openide.util.SharedClassObject;
35 import org.openide.util.WeakSet;
36 import org.openide.util.Mutex;
37 import org.openide.windows.TopComponent;
38
39
40 /**
41  * Manages <em>FindAction</em> - enables and disables it by current set of
42  * selected nodes.
43  *
44  * @author Petr Kuzel
45  * @author Marian Petras
46  * @see org.openide.actions.FindAction
47  * @see org.openide.windows.TopComponent.Registry
48  */

49 final class FindActionManager implements PropertyChangeListener JavaDoc, Runnable JavaDoc {
50     
51     /** */
52     private static final String JavaDoc MAPPED_FIND_ACTION
53             = FindActionManager.class.getName() + " - FindActionImpl"; //NOI18N
54

55     /**
56      */

57     private static FindActionManager instance;
58     /** Search perfomer. */
59     private final SearchPerformer performer;
60     /** holds set of windows for which their ActionMap was modified */
61     private final WeakSet activatedOnWindows = new WeakSet(8);
62     
63     /** */
64     private Object JavaDoc findActionMapKey;
65     
66     /**
67      */

68     private FindActionManager() {
69         performer = (SearchPerformer)
70                     SharedClassObject.findObject(SearchPerformer.class, true);
71     }
72     
73     /**
74      */

75     static FindActionManager getInstance() {
76         if (instance == null) {
77             instance = new FindActionManager();
78         }
79         return instance;
80     }
81
82     /**
83      */

84     void init() {
85         TopComponent.getRegistry().addPropertyChangeListener(this);
86         
87         Mutex.EVENT.writeAccess(this);
88     }
89
90     /**
91      */

92     void cleanup() {
93         //System.out.println("cleanup");
94
TopComponent.getRegistry().removePropertyChangeListener(this);
95         
96         /*
97          * We just need to run method 'cleanupWindowRegistry' in the AWT event
98          * dispatching thread. We use Mutex.EVENT for this task.
99          *
100          * We use Mutex.Action rather than Runnable. The reason is that
101          * Runnable could be run asynchronously which is undesirable during
102          * uninstallation (we do not want any instance/class from this module to
103          * be in use by the time ModuleInstall.uninstalled() returns).
104          */

105         Mutex.EVENT.readAccess(new Mutex.Action() {
106             public Object JavaDoc run() {
107                 cleanupWindowRegistry();
108                 return null;
109             }
110         });
111     }
112     
113     /**
114      */

115     public void run() {
116         someoneActivated();
117     }
118     
119     /**
120      */

121     private void cleanupWindowRegistry() {
122         //System.out.println("Utilities: Cleaning window registry");
123
final Object JavaDoc findActionKey = getFindActionMapKey();
124         
125         for (Iterator JavaDoc i = activatedOnWindows.iterator(); i.hasNext(); ) {
126             TopComponent tc = (TopComponent) i.next();
127             //System.out.println(" ** " + tc.getName());
128

129             Action JavaDoc origFindAction = null, currFindAction = null;
130             
131             Object JavaDoc origFindActionRef = tc.getClientProperty(MAPPED_FIND_ACTION);
132             if (origFindActionRef instanceof Reference JavaDoc) {
133                 Object JavaDoc origFindActionObj = ((Reference JavaDoc)origFindActionRef).get();
134                 if (origFindActionObj instanceof Action JavaDoc) {
135                     origFindAction = (Action JavaDoc) origFindActionObj;
136                 }
137             }
138             
139             if (origFindAction != null) {
140                 currFindAction = tc.getActionMap().get(findActionKey);
141             }
142             
143             if ((currFindAction != null) && (currFindAction == origFindAction)){
144                 tc.getActionMap().put(findActionKey, null);
145                 //System.out.println(" - successfully cleared");
146
} else {
147                 //System.out.println(" - DID NOT MATCH");
148
ErrorManager.getDefault().log(
149                         ErrorManager.WARNING,
150                         "ActionMap mapping of FindAction changed" + //NOI18N
151
" for window " + tc.getName()); //NOI18N
152
}
153             
154             if (origFindActionRef != null) {
155                 tc.putClientProperty(MAPPED_FIND_ACTION, null);
156             }
157         }
158         activatedOnWindows.clear();
159     }
160
161     /**
162      */

163     private void someoneActivated() {
164         TopComponent window = TopComponent.getRegistry().getActivated();
165
166         if ((window == null) || (window instanceof CloneableEditorSupport.Pane)) {
167             return;
168         }
169             
170         Object JavaDoc key = getFindActionMapKey();
171         ActionMap JavaDoc actionMap = window.getActionMap();
172
173         if ((actionMap.get(key) == null) && activatedOnWindows.add(window)) {
174             //System.out.println("Utilities: Registered window " + window.getName());
175

176             Action JavaDoc a = performer.createContextAwareInstance(window.getLookup());
177
178             actionMap.put(key, a);
179             window.putClientProperty(MAPPED_FIND_ACTION, new WeakReference JavaDoc(a));
180         }
181     }
182     
183     /** Implements <code>PropertyChangeListener</code>. Be interested in current_nodes property change. */
184     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
185         if (TopComponent.Registry.PROP_ACTIVATED.equals(evt.getPropertyName())){
186             someoneActivated();
187         }
188     }
189     
190     /**
191      */

192     private Object JavaDoc getFindActionMapKey() {
193         if (findActionMapKey == null) {
194             SharedClassObject findAction =
195                     SharedClassObject.findObject(FindAction.class);
196             assert findAction != null;
197             
198             findActionMapKey = ((FindAction) findAction).getActionMapKey();
199         }
200         return findActionMapKey;
201     }
202
203 }
204
Popular Tags