001: /*
002: * Created on Sep 10, 2007
003: *
004: * Copyright 2004, Projity Inc.
005: */
006: package com.projity.help;
007:
008: import java.awt.Component;
009: import java.awt.Container;
010: import java.awt.Frame;
011: import java.awt.KeyEventDispatcher;
012: import java.awt.KeyboardFocusManager;
013: import java.awt.MouseInfo;
014: import java.awt.Point;
015: import java.awt.event.KeyEvent;
016: import java.util.HashMap;
017:
018: import javax.swing.JComponent;
019: import javax.swing.JDialog;
020: import javax.swing.table.JTableHeader;
021:
022: import com.projity.configuration.Settings;
023: import com.projity.field.Field;
024: import com.projity.pm.graphic.IconManager;
025: import com.projity.pm.graphic.spreadsheet.SpreadSheet;
026: import com.projity.pm.graphic.spreadsheet.SpreadSheetModel;
027: import com.projity.util.BrowserControl;
028: import com.projity.util.Environment;
029:
030: public class HelpUtil implements KeyEventDispatcher {
031: private HashMap<Component, String> map = new HashMap<Component, String>();
032: private static HelpUtil instance = null;
033: private static final int DELAY_BETWEEN_HELPS = 5000;
034: private long lastHelpTime = 0L;
035: public boolean noHelp = false;
036:
037: private synchronized static HelpUtil getInstance() {
038: if (instance == null)
039: instance = new HelpUtil();
040: return instance;
041: }
042:
043: private HelpUtil() {
044: noHelp = Environment.getOs() == Environment.MAC
045: && Environment.isApplet();
046: if (noHelp)
047: System.out.print("no help");
048: if (noHelp)
049: return;
050: KeyboardFocusManager.getCurrentKeyboardFocusManager()
051: .addKeyEventDispatcher(this );
052:
053: }
054:
055: public static String helpTipImg = "<img src=\""
056: + IconManager.class
057: .getResource("/toolbarButtonGraphics/general/Information16.gif")
058: + "\"> F2";
059: // a tooltip with a help icon and F1
060: public static String helpTipHtml = "<html><img src=\""
061: + IconManager.class
062: .getResource("/toolbarButtonGraphics/general/Information16.gif")
063: + "\"> F2</html>";
064:
065: public static void addHelp(Component c, String address) {
066: if (getInstance().noHelp)
067: return;
068: if (c instanceof JComponent
069: && ((JComponent) c).getToolTipText() == null)
070: ((JComponent) c).setToolTipText(helpTipHtml);
071: getInstance().map.put(c, address);
072: }
073:
074: public static void addDocHelp(Component c, String address) {
075: addHelp(c, Settings.HELP_HOME + address);
076: }
077:
078: public static String findHelp(Component c) {
079: if (getInstance().noHelp)
080: return null;
081: String result = null;
082: while (c != null) {
083: if ((result = getInstance().map.get(c)) != null)
084: break;
085: c = c.getParent();
086: }
087: return result;
088: }
089:
090: public static boolean doHelp(Component c) {
091: if (getInstance().noHelp)
092: return false;
093: String help = findHelp(c);
094: if (help != null) {
095: long time = System.currentTimeMillis();
096: boolean doHelp = time - getInstance().lastHelpTime > DELAY_BETWEEN_HELPS;
097: getInstance().lastHelpTime = time;
098:
099: if (doHelp) { // prevents doubles which arrive for some reason
100: BrowserControl.displayURL(help);
101: return true;
102: }
103: }
104: return false;
105: }
106:
107: public static boolean doHelp() {
108: if (getInstance().noHelp)
109: return false;
110: Point pt = MouseInfo.getPointerInfo().getLocation();
111: Component owner = KeyboardFocusManager
112: .getCurrentKeyboardFocusManager().getFocusOwner();
113:
114: while (owner != null) { // get dialog or frame which is focus owner
115: if (owner instanceof JDialog || owner instanceof Frame) {
116: break;
117: }
118: owner = owner.getParent();
119: }
120: // Get component at point
121: Component c = null;
122: Point loc = owner.getLocationOnScreen();
123: Point offsetPoint = new Point(pt.x - loc.x, pt.y - loc.y);
124: c = ((Container) owner).findComponentAt(offsetPoint);
125:
126: if (c == null)
127: c = owner;
128:
129: // do help for most detailed item
130: while (c != null) {
131: if (c instanceof JTableHeader) { // if clicked on row header, see if there is help for the field
132: JTableHeader th = (JTableHeader) c;
133: loc = c.getLocationOnScreen();
134: offsetPoint = new Point(pt.x - loc.x, pt.y - loc.y);
135: int col = th.columnAtPoint(offsetPoint);
136: SpreadSheet ss = (SpreadSheet) th.getTable();
137: Field f = ((SpreadSheetModel) ss.getModel())
138: .getFieldInColumn(col + 1);
139: if (f.getHelp() != null) {
140: BrowserControl.displayURL(Settings.HELP_HOME
141: + f.getHelp());
142: return true;
143: }
144: }
145: if (doHelp(c))
146: return true;
147: c = c.getParent();
148: }
149: return false;
150:
151: }
152:
153: public boolean dispatchKeyEvent(KeyEvent e) {
154: if (noHelp)
155: return false;
156: if ((e.getKeyCode() == KeyEvent.VK_F2 || e.getKeyCode() == KeyEvent.VK_F1)
157: && !e.isConsumed())
158: if (doHelp()) {
159: e.consume();
160: return true;
161: }
162: return false;
163: }
164: }
|