KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javahelp > HelpAction


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.javahelp;
21
22 import java.awt.*;
23 import java.awt.event.*;
24 import javax.swing.*;
25
26 import org.openide.util.*;
27 import org.openide.util.actions.SystemAction;
28 import org.netbeans.api.javahelp.Help;
29 import org.openide.awt.StatusDisplayer;
30
31 /**
32  * Shows help for the currently focused component
33  * @author Jesse Glick
34  */

35 public class HelpAction extends SystemAction
36 {
37     private static final long serialVersionUID = 4658008202517094416L;
38
39     public String JavaDoc getName() {
40         return NbBundle.getMessage(HelpAction.class, "LBL_HelpAction");
41     }
42
43     public HelpCtx getHelpCtx() {
44         return HelpCtx.DEFAULT_HELP;
45     }
46
47     protected String JavaDoc iconResource() {
48         return "org/netbeans/modules/javahelp/resources/show-help.gif"; // NOI18N
49
}
50
51     protected void initialize() {
52         super.initialize();
53         Installer.log.fine("HelpAction.initialize");
54
55         // Cf. org.netbeans.core.windows.frames.NbFocusManager and
56
// org.netbeans.core.windows.frames.ShortcutAndMenuKeyEventProcessor
57
putProperty("OpenIDE-Transmodal-Action", Boolean.TRUE); // NOI18N
58
}
59
60     static class WindowActivatedDetector implements AWTEventListener {
61         private static java.lang.ref.WeakReference JavaDoc<Window> currentWindowRef;
62         private static WindowActivatedDetector detector = null;
63
64         static synchronized void install() {
65             if (detector == null) {
66                 detector = new WindowActivatedDetector();
67                 Toolkit.getDefaultToolkit ().addAWTEventListener(detector, AWTEvent.WINDOW_EVENT_MASK);
68             }
69         }
70         
71         static synchronized void uninstall() {
72             if (detector != null) {
73                 Toolkit.getDefaultToolkit().removeAWTEventListener(detector);
74                 detector = null;
75             }
76         }
77         
78         static synchronized Window getCurrentActivatedWindow() {
79             if (currentWindowRef != null) {
80                 return currentWindowRef.get();
81             }
82             else {
83                 return null;
84             }
85         }
86
87         private static synchronized void setCurrentActivatedWindow(Window w) {
88             currentWindowRef = new java.lang.ref.WeakReference JavaDoc<Window>(w);
89         }
90
91         public void eventDispatched (AWTEvent ev) {
92             if (ev.getID() != WindowEvent.WINDOW_ACTIVATED)
93                 return;
94             setCurrentActivatedWindow(((WindowEvent) ev).getWindow());
95         }
96     }
97     
98     private static HelpCtx findHelpCtx() {
99         Window w = WindowActivatedDetector.getCurrentActivatedWindow();
100         Component focused = (w != null) ? SwingUtilities.findFocusOwner(w) : null;
101         HelpCtx help = (focused == null) ? HelpCtx.DEFAULT_HELP : HelpCtx.findHelp(focused);
102
103         Installer.log.fine(help.toString() + " from " + focused);
104         return help;
105     }
106     
107     public void actionPerformed(ActionEvent ev) {
108         Help h = (Help)Lookup.getDefault().lookup(Help.class);
109         if (h == null) {
110             Toolkit.getDefaultToolkit().beep();
111             return;
112         }
113
114         HelpCtx help;
115         
116         final MenuElement[] path =
117             MenuSelectionManager.defaultManager().getSelectedPath();
118
119         if (path != null
120             && path.length > 0
121             && !(path[0].getComponent() instanceof javax.swing.plaf.basic.ComboPopup JavaDoc)
122             ) {
123             help = HelpCtx.findHelp(path[path.length - 1].getComponent());
124             
125             SwingUtilities.invokeLater(new Runnable JavaDoc() {
126                 public void run() {
127                     MenuElement[] newPath =
128                         MenuSelectionManager.defaultManager().getSelectedPath();
129
130                     if (newPath.length != path.length)
131                         return;
132                     for (int i = 0; i < newPath.length; i++) {
133                         if (newPath[i] != path[i])
134                             return;
135                     }
136                     MenuSelectionManager.defaultManager().clearSelectedPath();
137                 }
138             });
139         }
140         else {
141             help = findHelpCtx();
142         }
143         
144         StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(HelpAction.class, "CTL_OpeningHelp"));
145         h.showHelp (help);
146     }
147 }
148
Popular Tags