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-2006 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.project.ui.actions;
043:
044: import java.awt.EventQueue;
045: import java.awt.event.ActionEvent;
046: import java.beans.PropertyChangeEvent;
047: import java.beans.PropertyChangeListener;
048: import java.util.logging.Level;
049: import java.util.logging.LogRecord;
050: import java.util.logging.Logger;
051: import javax.swing.Icon;
052: import javax.swing.JButton;
053: import javax.swing.JMenuItem;
054: import org.openide.nodes.Node;
055: import org.openide.util.Lookup;
056: import org.openide.util.LookupEvent;
057: import org.openide.util.LookupListener;
058: import org.openide.util.NbBundle;
059: import org.openide.util.WeakListeners;
060: import org.openide.util.lookup.ProxyLookup;
061: import org.openide.windows.TopComponent;
062:
063: /** Action sensitive to current project
064: * @author Petr Hrebejk
065: */
066: public abstract class LookupSensitiveAction extends BasicAction
067: implements LookupListener {
068: private static Logger UILOG = Logger
069: .getLogger("org.netbeans.ui.actions"); // NOI18N
070:
071: private Lookup lookup;
072: private Class<?>[] watch;
073: private Lookup.Result results[];
074: private boolean needsRefresh = true;
075: private boolean initialized = false;
076:
077: private boolean refreshing = false;
078:
079: /**
080: * Constructor for global actions. E.g. actions in main menu which
081: * listen to the global context.
082: */
083: public LookupSensitiveAction(Icon icon, Lookup lookup, Class[] watch) {
084: super (null, icon);
085: if (lookup == null) {
086: lookup = LastActivatedWindowLookup.INSTANCE;
087: }
088: this .lookup = lookup;
089: this .watch = watch;
090: }
091:
092: private void init() {
093: if (initialized) {
094: return;
095: }
096: assert EventQueue.isDispatchThread() : "Cannot be called outside EQ!";
097: this .results = new Lookup.Result[watch.length];
098: // Needs to listen on changes in results
099: for (int i = 0; i < watch.length; i++) {
100: results[i] = lookup.lookupResult(watch[i]);
101: results[i].allItems();
102: LookupListener resultListener = WeakListeners.create(
103: LookupListener.class, this , results[i]);
104: results[i].addLookupListener(resultListener);
105: }
106: initialized = true;
107: }
108:
109: /** Needs to override getValue in order to force refresh
110: */
111: public @Override
112: Object getValue(String key) {
113: init();
114: if (needsRefresh) {
115: doRefresh();
116: }
117: return super .getValue(key);
118: }
119:
120: /** Needs to override isEnabled in order to force refresh
121: */
122: public @Override
123: boolean isEnabled() {
124: init();
125: if (needsRefresh) {
126: doRefresh();
127: }
128: return super .isEnabled();
129: }
130:
131: public final void actionPerformed(ActionEvent e) {
132: init();
133:
134: if (UILOG.isLoggable(Level.FINE)) {
135: LogRecord r;
136: boolean isKey;
137: if (e.getSource() instanceof JMenuItem) {
138: isKey = false;
139: } else if (e.getSource() instanceof JButton) {
140: isKey = false;
141: } else {
142: isKey = true;
143: }
144:
145: if (!isKey) {
146: r = new LogRecord(Level.FINE, "UI_ACTION_BUTTON_PRESS"); // NOI18N
147: r.setResourceBundle(NbBundle
148: .getBundle(LookupSensitiveAction.class));
149: r.setParameters(new Object[] { e.getSource(),
150: e.getSource().getClass().getName(), this ,
151: getClass().getName(), getValue(NAME) });
152: r.setLoggerName(UILOG.getName());
153: UILOG.log(r);
154: }
155: }
156:
157: actionPerformed(lookup);
158: }
159:
160: protected final Lookup getLookup() {
161: return lookup;
162: }
163:
164: private void doRefresh() {
165: refreshing = true;
166: try {
167: refresh(lookup);
168: } finally {
169: refreshing = false;
170: }
171: needsRefresh = false;
172: }
173:
174: // Abstract methods --------------------------------------------------------
175:
176: /** Called when the action is performed
177: */
178: protected abstract void actionPerformed(Lookup context);
179:
180: /** Place where to change properties (enablement/name) when
181: * the set of current projects changes.
182: */
183: protected abstract void refresh(Lookup context);
184:
185: // Implementation of LookupListener ----------------------------------------
186:
187: public void resultChanged(LookupEvent e) {
188: if (refreshing) {
189: return;
190: } else if (getPropertyChangeListeners().length == 0) {
191: needsRefresh = true;
192: } else {
193: doRefresh();
194: }
195: }
196:
197: /**
198: * #120721: do not want to use Utilities.actionsGlobalContext since that does not survive focus change,
199: * and we would like to mimic the selection tracking behavior of Hacks.keepCurrentProjectNameUpdated.
200: */
201: private static final class LastActivatedWindowLookup extends
202: ProxyLookup implements PropertyChangeListener {
203:
204: static final Lookup INSTANCE = new LastActivatedWindowLookup();
205:
206: private final TopComponent.Registry reg = TopComponent
207: .getRegistry();
208:
209: LastActivatedWindowLookup() {
210: reg.addPropertyChangeListener(this );
211: updateLookups();
212: }
213:
214: private void updateLookups() {
215: Node[] nodes = reg.getActivatedNodes();
216: Lookup[] delegates = new Lookup[nodes.length];
217: for (int i = 0; i < nodes.length; i++) {
218: delegates[i] = nodes[i].getLookup();
219: }
220: setLookups(delegates);
221: }
222:
223: public void propertyChange(PropertyChangeEvent ev) {
224: if (TopComponent.Registry.PROP_ACTIVATED_NODES.equals(ev
225: .getPropertyName())) {
226: updateLookups();
227: }
228: }
229:
230: }
231:
232: }
|