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.javahelp;
043:
044: import java.awt.*;
045: import java.awt.event.*;
046: import javax.swing.*;
047:
048: import org.openide.util.*;
049: import org.openide.util.actions.SystemAction;
050: import org.netbeans.api.javahelp.Help;
051: import org.openide.awt.StatusDisplayer;
052:
053: /**
054: * Shows help for the currently focused component
055: * @author Jesse Glick
056: */
057: public class HelpAction extends SystemAction {
058: private static final long serialVersionUID = 4658008202517094416L;
059:
060: public String getName() {
061: return NbBundle.getMessage(HelpAction.class, "LBL_HelpAction");
062: }
063:
064: public HelpCtx getHelpCtx() {
065: return HelpCtx.DEFAULT_HELP;
066: }
067:
068: protected String iconResource() {
069: return "org/netbeans/modules/javahelp/resources/show-help.gif"; // NOI18N
070: }
071:
072: protected void initialize() {
073: super .initialize();
074: Installer.log.fine("HelpAction.initialize");
075:
076: // Cf. org.netbeans.core.windows.frames.NbFocusManager and
077: // org.netbeans.core.windows.frames.ShortcutAndMenuKeyEventProcessor
078: putProperty("OpenIDE-Transmodal-Action", Boolean.TRUE); // NOI18N
079: }
080:
081: static class WindowActivatedDetector implements AWTEventListener {
082: private static java.lang.ref.WeakReference<Window> currentWindowRef;
083: private static WindowActivatedDetector detector = null;
084:
085: static synchronized void install() {
086: if (detector == null) {
087: detector = new WindowActivatedDetector();
088: Toolkit.getDefaultToolkit().addAWTEventListener(
089: detector, AWTEvent.WINDOW_EVENT_MASK);
090: }
091: }
092:
093: static synchronized void uninstall() {
094: if (detector != null) {
095: Toolkit.getDefaultToolkit().removeAWTEventListener(
096: detector);
097: detector = null;
098: }
099: }
100:
101: static synchronized Window getCurrentActivatedWindow() {
102: if (currentWindowRef != null) {
103: return currentWindowRef.get();
104: } else {
105: return null;
106: }
107: }
108:
109: private static synchronized void setCurrentActivatedWindow(
110: Window w) {
111: currentWindowRef = new java.lang.ref.WeakReference<Window>(
112: w);
113: }
114:
115: public void eventDispatched(AWTEvent ev) {
116: if (ev.getID() != WindowEvent.WINDOW_ACTIVATED)
117: return;
118: setCurrentActivatedWindow(((WindowEvent) ev).getWindow());
119: }
120: }
121:
122: private static HelpCtx findHelpCtx() {
123: Window w = WindowActivatedDetector.getCurrentActivatedWindow();
124: Component focused = (w != null) ? SwingUtilities
125: .findFocusOwner(w) : null;
126: HelpCtx help = (focused == null) ? HelpCtx.DEFAULT_HELP
127: : HelpCtx.findHelp(focused);
128:
129: Installer.log.fine(help.toString() + " from " + focused);
130: return help;
131: }
132:
133: public void actionPerformed(ActionEvent ev) {
134: Help h = (Help) Lookup.getDefault().lookup(Help.class);
135: if (h == null) {
136: Toolkit.getDefaultToolkit().beep();
137: return;
138: }
139:
140: HelpCtx help;
141:
142: final MenuElement[] path = MenuSelectionManager
143: .defaultManager().getSelectedPath();
144:
145: if (path != null
146: && path.length > 0
147: && !(path[0].getComponent() instanceof javax.swing.plaf.basic.ComboPopup)) {
148: help = HelpCtx.findHelp(path[path.length - 1]
149: .getComponent());
150:
151: SwingUtilities.invokeLater(new Runnable() {
152: public void run() {
153: MenuElement[] newPath = MenuSelectionManager
154: .defaultManager().getSelectedPath();
155:
156: if (newPath.length != path.length)
157: return;
158: for (int i = 0; i < newPath.length; i++) {
159: if (newPath[i] != path[i])
160: return;
161: }
162: MenuSelectionManager.defaultManager()
163: .clearSelectedPath();
164: }
165: });
166: } else {
167: help = findHelpCtx();
168: }
169:
170: StatusDisplayer.getDefault()
171: .setStatusText(
172: NbBundle.getMessage(HelpAction.class,
173: "CTL_OpeningHelp"));
174: h.showHelp(help);
175: }
176: }
|