001: package org.acm.seguin.completer.popup;
002:
003: import org.acm.seguin.ide.jedit.Navigator;
004: import org.acm.seguin.completer.Completer;
005: import java.awt.Point;
006: import java.awt.event.KeyEvent;
007: import java.lang.reflect.Field;
008: import javax.swing.ImageIcon;
009: import javax.swing.SwingUtilities;
010: import org.gjt.sp.jedit.View;
011: import org.gjt.sp.jedit.gui.DefaultInputHandler;
012: import org.gjt.sp.jedit.gui.InputHandler;
013: import org.gjt.sp.jedit.jEdit;
014: import org.gjt.sp.jedit.textarea.JEditTextArea;
015:
016: /**
017:
018: * Utility functions for popups
019:
020: *
021:
022: * @author btateyama@yahoo.com
023:
024: * @created December 12, 2002
025:
026: */
027:
028: public class PopupUtils {
029: static final Navigator.NavigatorLogger logger = Completer
030: .getLogger(PopupUtils.class);
031: static final ImageIcon ICON_FIELD = new ImageIcon(Navigator.class
032: .getResource("icons/field.gif"));
033: static final ImageIcon ICON_METHOD = new ImageIcon(Navigator.class
034: .getResource("icons/method.gif"));
035: static final ImageIcon ICON_INNER = new ImageIcon(Navigator.class
036: .getResource("icons/inner-class.gif"));
037:
038: static final int ICON_WIDTH = 13;
039:
040: /**
041: * Translate a text position to point where popup should appear.
042: *
043: * @param argView Description of the Parameter
044: * @param argPrefix Description of the Parameter
045: * @return The pointOnTextArea value
046: */
047: public static Point getPointOnTextArea(View argView,
048: String argPrefix) {
049:
050: JEditTextArea textArea = argView.getTextArea();
051: int caretLine = textArea.getCaretLine();
052: int caret = textArea.getCaretPosition();
053: textArea.scrollToCaret(false);
054: Point location = textArea
055: .offsetToXY(caret - argPrefix.length());
056: location.y += textArea.getPainter().getFontMetrics()
057: .getHeight();
058: SwingUtilities.convertPointToScreen(location, textArea
059: .getPainter());
060: return location;
061: }
062:
063: public static void requestFocusOnTextArea(JEditTextArea argTextArea) {
064: SwingUtilities
065: .invokeLater(new TextAreaFocusHelper(argTextArea));
066: }
067:
068: static class TextAreaFocusHelper implements Runnable {
069: JEditTextArea _textArea = null;
070:
071: TextAreaFocusHelper(JEditTextArea argTextArea) {
072: _textArea = argTextArea;
073: }
074:
075: public void run() {
076: _textArea.requestFocus();
077: }
078: }
079:
080: public static void processJEditCommand(View argView,
081: BasePopup argPopup, KeyEvent argEvent) {
082: if (hasBinding(argView, argEvent)) {
083: argPopup.dispose();
084: //FIXME: argView.getInputHandler().keyPressed(argEvent);
085: }
086:
087: if (!argEvent.isConsumed() && argEvent.isActionKey()) {
088: argPopup.dispose();
089: argView.processKeyEvent(argEvent);
090: }
091: }
092:
093: private static boolean hasBinding(View argView, KeyEvent argEvent) {
094: boolean bHasBinding = false;
095: StringBuffer sbBinding = new StringBuffer();
096: sbBinding.append(DefaultInputHandler
097: .getModifierString(argEvent));
098: sbBinding.append("+");
099: String strSym = getSymbolicName(argEvent.getKeyCode());
100: if (strSym != null) {
101: sbBinding.append(strSym);
102: DefaultInputHandler inputHandler = (DefaultInputHandler) argView
103: .getInputHandler();
104: Object obj = inputHandler.getKeyBinding(sbBinding
105: .toString());
106: bHasBinding = obj != null;
107: }
108: return bHasBinding;
109: }
110:
111: private static String getSymbolicName(int keyCode) {
112: // Copied from org.gjt.sp.jedit.gui.GrabKeyDialog
113: if (keyCode == KeyEvent.VK_UNDEFINED)
114: return null;
115: /* else if(keyCode == KeyEvent.VK_OPEN_BRACKET)
116: return "[";
117: else if(keyCode == KeyEvent.VK_CLOSE_BRACKET)
118: return "]"; */
119:
120: if (keyCode >= KeyEvent.VK_A && keyCode <= KeyEvent.VK_Z)
121: return String
122: .valueOf(Character.toLowerCase((char) keyCode));
123:
124: try {
125: Field[] fields = KeyEvent.class.getFields();
126: for (int i = 0; i < fields.length; i++) {
127: Field field = fields[i];
128: String name = field.getName();
129: if (name.startsWith("VK_")
130: && field.getInt(null) == keyCode) {
131: return name.substring(3);
132: }
133: }
134: } catch (Exception e) {
135: logger.error("Error enum fields", e);
136: }
137:
138: return null;
139: } //}}}
140:
141: public static boolean isCompleteWord(KeyEvent argEvent) {
142: boolean bIsCompleteWord = false;
143: StringBuffer sbBinding = new StringBuffer();
144: sbBinding.append(DefaultInputHandler
145: .getModifierString(argEvent));
146: sbBinding.append("+");
147: String strSym = getSymbolicName(argEvent.getKeyCode());
148: if (strSym != null) {
149: sbBinding.append(strSym);
150: String strB = sbBinding.toString();
151: bIsCompleteWord = (strB.equals(jEdit
152: .getProperty("complete-word.shortcut")) || strB
153: .equals(jEdit
154: .getProperty("complete-word.shortcut2")));
155: // hmm..we could also do a cross ref of actions
156: }
157: return bIsCompleteWord;
158: }
159:
160: }
|