001: /*
002: * InputDialog.java
003: *
004: * Copyright (C) 1998-2003 Peter Graves
005: * $Id: InputDialog.java,v 1.3 2003/07/23 16:13:51 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.awt.BorderLayout;
025: import java.awt.event.InputEvent;
026: import java.awt.event.KeyEvent;
027: import java.awt.event.KeyListener;
028: import java.util.List;
029: import javax.swing.BoxLayout;
030: import javax.swing.JDialog;
031: import javax.swing.JPanel;
032: import javax.swing.border.EmptyBorder;
033:
034: public class InputDialog extends JDialog implements KeyListener {
035: protected final Editor editor;
036:
037: protected HistoryTextField textField;
038:
039: private String defaultValue;
040: private History history;
041: private String input;
042: private List completions;
043: private int index;
044:
045: public InputDialog(Editor editor, String prompt, String title,
046: String defaultValue) {
047: super (editor.getFrame(), title, true);
048: this .editor = editor;
049: this .defaultValue = defaultValue;
050: JPanel panel = new JPanel();
051: panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
052: panel.setBorder(new EmptyBorder(5, 5, 5, 5));
053: panel.add(new Label(prompt));
054: textField = new HistoryTextField(20);
055: textField.addKeyListener(this );
056: panel.add(textField);
057: getContentPane().add(panel, BorderLayout.CENTER);
058: pack();
059: textField.setFocusTraversalKeysEnabled(false);
060: }
061:
062: public static String showInputDialog(Editor editor, String prompt,
063: String title, String defaultValue) {
064: InputDialog d = new InputDialog(editor, prompt, title,
065: defaultValue);
066: editor.centerDialog(d);
067: d.show();
068: return d.input;
069: }
070:
071: public static String showInputDialog(Editor editor, String prompt,
072: String title) {
073: return showInputDialog(editor, prompt, title, null);
074: }
075:
076: public void show() {
077: if (defaultValue != null && defaultValue.length() > 0) {
078: textField.setText(defaultValue);
079: textField.selectAll();
080: }
081: textField.requestFocus();
082: super .show();
083: }
084:
085: public final void setDefaultValue(String s) {
086: defaultValue = s;
087: }
088:
089: public final String getInput() {
090: return input;
091: }
092:
093: public void setHistory(History history) {
094: this .history = history;
095: textField.setHistory(history);
096: }
097:
098: protected void enter() {
099: input = textField.getText();
100: if (history != null) {
101: history.append(input);
102: history.save();
103: }
104: dispose();
105: }
106:
107: protected void escape() {
108: input = null;
109: dispose();
110: }
111:
112: public void keyPressed(KeyEvent e) {
113: final int keyCode = e.getKeyCode();
114: final int modifiers = e.getModifiers();
115: switch (keyCode) {
116: case KeyEvent.VK_TAB: {
117: String s = null;
118: if (modifiers == InputEvent.SHIFT_MASK)
119: s = previousGuess();
120: else
121: s = guess(textField.getText());
122: e.consume();
123: if (s != null) {
124: textField.setText(s);
125: textField.setCaretPosition(s.length());
126: }
127: return;
128: }
129: case KeyEvent.VK_ENTER:
130: enter();
131: return;
132: case KeyEvent.VK_ESCAPE:
133: escape();
134: return;
135: case KeyEvent.VK_SHIFT:
136: case KeyEvent.VK_META:
137: case KeyEvent.VK_ALT:
138: // Ignore modifers.
139: return;
140: default:
141: // Anything but tab, start over.
142: completions = null;
143: return;
144: }
145: }
146:
147: public void keyReleased(KeyEvent e) {
148: }
149:
150: public void keyTyped(KeyEvent e) {
151: }
152:
153: public void dispose() {
154: super .dispose();
155: editor.restoreFocus();
156: }
157:
158: private String guess(String prefix) {
159: if (completions == null) {
160: completions = getCompletions(prefix);
161: if (completions == null)
162: return null;
163: index = 0;
164: } else if (index >= completions.size())
165: index = 0; // Start over.
166: if (index < completions.size())
167: return (String) completions.get(index++);
168: return null;
169: }
170:
171: private String previousGuess() {
172: if (completions != null) {
173: if (completions.size() > 1) {
174: index -= 2;
175: if (index < 0)
176: index += completions.size();
177: return (String) completions.get(index++);
178: }
179: }
180: return null;
181: }
182:
183: // Derived classes can override this method to provide completion
184: // functionality.
185: protected List getCompletions(String prefix) {
186: return null;
187: }
188: }
|