001: package isql;
002:
003: import javax.swing.*;
004: import javax.swing.text.*;
005: import javax.swing.event.*;
006: import java.util.*;
007: import java.awt.event.*;
008: import java.awt.Component;
009:
010: // currently each time constrcutor is called and cache is set. would
011: // like to do this once and call only show and match method
012: /** Pops up with a list which user can select from. If user enters
013: * characters or backspace then the search result changes accordingly.
014: * HOwever, if you use anythig other than arrow keys then the list loses
015: * keyboard focus.
016: * @author rahul kumar 2001, $Author
017: * $Id: SqlPopupMenu.java,v 1.1 2001/12/31 19:51:37 rahul_kumar Exp $
018: */
019:
020: public class SqlPopupMenu {
021:
022: JPopupMenu jp; // that which will popup
023: JTextComponent _jt;
024: ActionListener alis;
025: JMenuItem ji;
026: String[] cache; // list of words to search in
027: String _patt; // current pattern
028: String originalpatt; // required for knowing length of what we are replacing
029:
030: public SqlPopupMenu(JTextComponent jt) {
031:
032: _jt = jt;
033: jp = new JPopupMenu();
034: jp.addKeyListener(new PopupKeyListener(this ));
035: alis = new ActionListener() {
036: public void actionPerformed(ActionEvent event) {
037: if (_jt instanceof JTextArea) {
038: // replace current word with what user selected
039: ((JTextArea) _jt).replaceRange(event
040: .getActionCommand(), _jt.getCaretPosition()
041: - originalpatt.length(), _jt
042: .getCaretPosition());
043: _jt.requestFocus(); // return focus back to caller
044: } else
045: System.out.println("selected: "
046: + event.getActionCommand());
047: }
048: };
049:
050: }
051:
052: public JTextComponent getTextComponent() {
053: return _jt;
054: }
055:
056: public void show(int x, int y) {
057: if (ji == null)
058: return;
059: jp.show(_jt, x, y);
060: jp.requestFocus();
061: }
062:
063: public void show() {
064: if (ji == null)
065: return;
066: jp.show(_jt, 0, 0);
067: jp.requestFocus();
068: }
069:
070: public void set(Collection coll) {
071: cache = new String[coll.size()];
072: coll.toArray(cache);
073: }
074:
075: public void set(String[] arr) {
076: // we need to clear the jmenu also
077: cache = arr;
078: }
079:
080: public void setMatching(String patt) {
081: if (cache == null || cache.length == 0)
082: return;
083: int len = patt.length();
084: if (len == 0)
085: return;
086: if (_patt == null) // otherwise replace will not work QUICK DIRTY
087: originalpatt = patt;
088: _patt = patt;
089: // clean out existing elements
090: Component comps[] = jp.getComponents();
091: for (int i = 0; i < comps.length; i++) {
092: jp.remove(comps[i]);
093: }
094: for (int i = 0; i < cache.length; i++) {
095: // check if patt is not longer than array else subst throws
096: // up
097: if (patt.length() <= cache[i].length()
098: && cache[i].substring(0, len).equals(patt)) {
099: ji = jp.add(cache[i]);
100: /*
101: if (len < cache[i].length())
102: ji.setMnemonic(cache[i].toUpperCase().charAt(len));
103: */
104: ji.addActionListener(alis);
105: ji.addKeyListener(new PopupKeyListener(this ));
106: }//if matching
107:
108: } // for
109:
110: } // setMatching
111:
112: public String getPattern() {
113: return _patt;
114: }
115:
116: public JPopupMenu getPopupMenu() {
117: return jp;
118: }
119: } // class SqlPopupMenu
120:
121: //
122: class PopupKeyListener implements KeyListener {
123: private SqlPopupMenu jp;
124:
125: public PopupKeyListener(SqlPopupMenu jpm) {
126: jp = jpm;
127: }
128:
129: public void keyTyped(KeyEvent e) {
130: }
131:
132: public void keyPressed(KeyEvent e) {
133: char ch = e.getKeyChar();
134: String patt = jp.getPattern(); // could be done once in constructor
135: String oldpatt = patt;
136: if (Character.isJavaIdentifierPart(ch))
137: patt += ch;
138: if (e.getKeyCode() == java.awt.event.KeyEvent.VK_BACK_SPACE)
139: patt = patt.substring(0, patt.length() - 2);
140: if (!oldpatt.equals(patt)) {
141: System.out.println("patt:" + patt);
142: jp.setMatching(patt);
143: jp.getPopupMenu().pack();
144: jp.getPopupMenu().invalidate();
145: jp.getPopupMenu().revalidate();
146: jp.getPopupMenu().repaint();
147: jp.getPopupMenu().requestFocus();
148: //jp.getPopupMenu().grabFocus();
149: try {
150: //jp.getPopupMenu().getComponentAtIndex(0).requestFocus();
151: //((JComponent)jp.getPopupMenu().getComponentAtIndex(0)).grabFocus();
152: jp.getPopupMenu().setSelected(
153: jp.getPopupMenu().getComponentAtIndex(0));
154: } catch (Exception exc) {
155: }
156: }
157:
158: }
159:
160: public void keyReleased(KeyEvent e) {
161: }
162: }
|