001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.search;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.lang.ref.Reference;
047: import java.lang.ref.WeakReference;
048: import java.util.Set;
049: import java.util.logging.Logger;
050: import javax.swing.Action;
051: import javax.swing.ActionMap;
052: import org.openide.ErrorManager;
053:
054: import org.openide.actions.FindAction;
055: import org.openide.text.CloneableEditorSupport;
056: import org.openide.util.SharedClassObject;
057: import org.openide.util.WeakSet;
058: import org.openide.util.Mutex;
059: import org.openide.windows.TopComponent;
060: import static java.util.logging.Level.FINER;
061:
062: /**
063: * Manages <em>FindAction</em> - enables and disables it by current set of
064: * selected nodes.
065: *
066: * @author Petr Kuzel
067: * @author Marian Petras
068: * @see org.openide.actions.FindAction
069: * @see org.openide.windows.TopComponent.Registry
070: */
071: final class FindActionManager implements PropertyChangeListener,
072: Runnable {
073:
074: /** */
075: private static final Logger LOG = Logger
076: .getLogger(FindActionManager.class.getName());
077:
078: /** */
079: private static final String MAPPED_FIND_ACTION = FindActionManager.class
080: .getName()
081: + " - FindActionImpl"; //NOI18N
082:
083: /**
084: */
085: private static FindActionManager instance;
086: /** Search perfomer. */
087: private final FindInFilesAction findAction;
088: /** holds set of windows for which their ActionMap was modified */
089: private final Set<TopComponent> activatedOnWindows = new WeakSet<TopComponent>(
090: 8);
091:
092: /** */
093: private Object findActionMapKey;
094:
095: /**
096: */
097: private FindActionManager() {
098: findAction = SharedClassObject.findObject(
099: FindInFilesAction.class, true);
100: }
101:
102: /**
103: */
104: static FindActionManager getInstance() {
105: LOG.finer("getInstance()");
106: if (instance == null) {
107: instance = new FindActionManager();
108: }
109: return instance;
110: }
111:
112: /**
113: */
114: void init() {
115: TopComponent.getRegistry().addPropertyChangeListener(this );
116:
117: Mutex.EVENT.writeAccess(this );
118: }
119:
120: /**
121: */
122: void cleanup() {
123: //System.out.println("cleanup");
124: TopComponent.getRegistry().removePropertyChangeListener(this );
125:
126: /*
127: * We just need to run method 'cleanupWindowRegistry' in the AWT event
128: * dispatching thread. We use Mutex.EVENT for this task.
129: *
130: * We use Mutex.Action rather than Runnable. The reason is that
131: * Runnable could be run asynchronously which is undesirable during
132: * uninstallation (we do not want any instance/class from this module to
133: * be in use by the time ModuleInstall.uninstalled() returns).
134: */
135: Mutex.EVENT.readAccess(new Mutex.Action<Object>() {
136: public Object run() {
137: cleanupWindowRegistry();
138: return null;
139: }
140: });
141: }
142:
143: /**
144: */
145: public void run() {
146: someoneActivated();
147: }
148:
149: /**
150: */
151: private void cleanupWindowRegistry() {
152: //System.out.println("Utilities: Cleaning window registry");
153: final Object findActionKey = getFindActionMapKey();
154:
155: for (TopComponent tc : activatedOnWindows) {
156: //System.out.println(" ** " + tc.getName());
157:
158: Action origFindAction = null, currFindAction = null;
159:
160: Object origFindActionRef = tc
161: .getClientProperty(MAPPED_FIND_ACTION);
162: if (origFindActionRef instanceof Reference) {
163: Object origFindActionObj = ((Reference) origFindActionRef)
164: .get();
165: if (origFindActionObj instanceof Action) {
166: origFindAction = (Action) origFindActionObj;
167: }
168: }
169:
170: if (origFindAction != null) {
171: currFindAction = tc.getActionMap().get(findActionKey);
172: }
173:
174: if ((currFindAction != null)
175: && (currFindAction == origFindAction)) {
176: tc.getActionMap().put(findActionKey, null);
177: //System.out.println(" - successfully cleared");
178: } else {
179: //System.out.println(" - DID NOT MATCH");
180: ErrorManager.getDefault().log(ErrorManager.WARNING,
181: "ActionMap mapping of FindAction changed" + //NOI18N
182: " for window " + tc.getName()); //NOI18N
183: }
184:
185: if (origFindActionRef != null) {
186: tc.putClientProperty(MAPPED_FIND_ACTION, null);
187: }
188: }
189: activatedOnWindows.clear();
190: }
191:
192: /**
193: */
194: private void someoneActivated() {
195: TopComponent window = TopComponent.getRegistry().getActivated();
196: if (LOG.isLoggable(FINER)) {
197: String windowId;
198: if (window == null) {
199: windowId = "<null>";
200: } else {
201: String windowName = window.getDisplayName();
202: if (windowName == null) {
203: windowName = window.getHtmlDisplayName();
204: }
205: if (windowName == null) {
206: windowName = window.getName();
207: }
208: if (windowName != null) {
209: windowName = '"' + windowName + '"';
210: } else {
211: windowName = "<noname>";
212: }
213: windowId = windowName + '('
214: + window.getClass().getName() + ')';
215: }
216: LOG.finer("someoneActivated (" + windowId + ')');
217: }
218:
219: if ((window == null)
220: || (window instanceof CloneableEditorSupport.Pane)) {
221: return;
222: }
223:
224: Object key = getFindActionMapKey();
225: ActionMap actionMap = window.getActionMap();
226:
227: if ((actionMap.get(key) == null)
228: && activatedOnWindows.add(window)) {
229: //System.out.println("Utilities: Registered window " + window.getName());
230:
231: Action a = findAction.createContextAwareInstance(window
232: .getLookup(), true);
233:
234: actionMap.put(key, a);
235: window.putClientProperty(MAPPED_FIND_ACTION,
236: new WeakReference<Action>(a));
237: }
238: }
239:
240: /** Implements <code>PropertyChangeListener</code>. Be interested in current_nodes property change. */
241: public void propertyChange(PropertyChangeEvent evt) {
242: if (TopComponent.Registry.PROP_ACTIVATED.equals(evt
243: .getPropertyName())) {
244: someoneActivated();
245: }
246: }
247:
248: /**
249: */
250: private Object getFindActionMapKey() {
251: if (findActionMapKey == null) {
252: FindAction systemFindAction = SharedClassObject.findObject(
253: FindAction.class, true);
254: assert systemFindAction != null;
255:
256: findActionMapKey = systemFindAction.getActionMapKey();
257: }
258: return findActionMapKey;
259: }
260:
261: }
|