1 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 35 public class HelpAction extends SystemAction 36 { 37 private static final long serialVersionUID = 4658008202517094416L; 38 39 public String 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 iconResource() { 48 return "org/netbeans/modules/javahelp/resources/show-help.gif"; } 50 51 protected void initialize() { 52 super.initialize(); 53 Installer.log.fine("HelpAction.initialize"); 54 55 putProperty("OpenIDE-Transmodal-Action", Boolean.TRUE); } 59 60 static class WindowActivatedDetector implements AWTEventListener { 61 private static java.lang.ref.WeakReference <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 <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 ) 122 ) { 123 help = HelpCtx.findHelp(path[path.length - 1].getComponent()); 124 125 SwingUtilities.invokeLater(new Runnable () { 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 |