KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > InputDialog


1 /*
2  * InputDialog.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: InputDialog.java,v 1.3 2003/07/23 16:13:51 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.awt.BorderLayout JavaDoc;
25 import java.awt.event.InputEvent JavaDoc;
26 import java.awt.event.KeyEvent JavaDoc;
27 import java.awt.event.KeyListener JavaDoc;
28 import java.util.List JavaDoc;
29 import javax.swing.BoxLayout JavaDoc;
30 import javax.swing.JDialog JavaDoc;
31 import javax.swing.JPanel JavaDoc;
32 import javax.swing.border.EmptyBorder JavaDoc;
33
34 public class InputDialog extends JDialog JavaDoc implements KeyListener JavaDoc
35 {
36     protected final Editor editor;
37
38     protected HistoryTextField textField;
39
40     private String JavaDoc defaultValue;
41     private History history;
42     private String JavaDoc input;
43     private List JavaDoc completions;
44     private int index;
45
46     public InputDialog(Editor editor, String JavaDoc prompt, String JavaDoc title,
47         String JavaDoc defaultValue)
48     {
49         super(editor.getFrame(), title, true);
50         this.editor = editor;
51         this.defaultValue = defaultValue;
52         JPanel JavaDoc panel = new JPanel JavaDoc();
53         panel.setLayout(new BoxLayout JavaDoc(panel, BoxLayout.Y_AXIS));
54         panel.setBorder(new EmptyBorder JavaDoc(5, 5, 5, 5));
55         panel.add(new Label(prompt));
56         textField = new HistoryTextField(20);
57         textField.addKeyListener(this);
58         panel.add(textField);
59         getContentPane().add(panel, BorderLayout.CENTER);
60         pack();
61         textField.setFocusTraversalKeysEnabled(false);
62     }
63
64     public static String JavaDoc showInputDialog(Editor editor, String JavaDoc prompt,
65         String JavaDoc title, String JavaDoc defaultValue)
66     {
67         InputDialog d = new InputDialog(editor, prompt, title, defaultValue);
68         editor.centerDialog(d);
69         d.show();
70         return d.input;
71     }
72
73     public static String JavaDoc showInputDialog(Editor editor, String JavaDoc prompt,
74         String JavaDoc title)
75     {
76         return showInputDialog(editor, prompt, title, null);
77     }
78
79     public void show()
80     {
81         if (defaultValue != null && defaultValue.length() > 0) {
82             textField.setText(defaultValue);
83             textField.selectAll();
84         }
85         textField.requestFocus();
86         super.show();
87     }
88
89     public final void setDefaultValue(String JavaDoc s)
90     {
91         defaultValue = s;
92     }
93
94     public final String JavaDoc getInput()
95     {
96         return input;
97     }
98
99     public void setHistory(History history)
100     {
101         this.history = history;
102         textField.setHistory(history);
103     }
104
105     protected void enter()
106     {
107         input = textField.getText();
108         if (history != null) {
109             history.append(input);
110             history.save();
111         }
112         dispose();
113     }
114
115     protected void escape()
116     {
117         input = null;
118         dispose();
119     }
120
121     public void keyPressed(KeyEvent JavaDoc e)
122     {
123         final int keyCode = e.getKeyCode();
124         final int modifiers = e.getModifiers();
125         switch (keyCode) {
126             case KeyEvent.VK_TAB: {
127                 String JavaDoc s = null;
128                 if (modifiers == InputEvent.SHIFT_MASK)
129                     s = previousGuess();
130                 else
131                     s = guess(textField.getText());
132                 e.consume();
133                 if (s != null) {
134                     textField.setText(s);
135                     textField.setCaretPosition(s.length());
136                 }
137                 return;
138             }
139             case KeyEvent.VK_ENTER:
140                 enter();
141                 return;
142             case KeyEvent.VK_ESCAPE:
143                 escape();
144                 return;
145             case KeyEvent.VK_SHIFT:
146             case KeyEvent.VK_META:
147             case KeyEvent.VK_ALT:
148                 // Ignore modifers.
149
return;
150             default:
151                 // Anything but tab, start over.
152
completions = null;
153                 return;
154         }
155     }
156
157     public void keyReleased(KeyEvent JavaDoc e) {}
158
159     public void keyTyped(KeyEvent JavaDoc e) {}
160
161     public void dispose()
162     {
163         super.dispose();
164         editor.restoreFocus();
165     }
166
167     private String JavaDoc guess(String JavaDoc prefix)
168     {
169         if (completions == null) {
170             completions = getCompletions(prefix);
171             if (completions == null)
172                 return null;
173             index = 0;
174         } else if (index >= completions.size())
175             index = 0; // Start over.
176
if (index < completions.size())
177             return (String JavaDoc) completions.get(index++);
178         return null;
179     }
180
181     private String JavaDoc previousGuess()
182     {
183         if (completions != null) {
184             if (completions.size() > 1) {
185                 index -= 2;
186                 if (index < 0)
187                     index += completions.size();
188                 return (String JavaDoc) completions.get(index++);
189             }
190         }
191         return null;
192     }
193
194     // Derived classes can override this method to provide completion
195
// functionality.
196
protected List JavaDoc getCompletions(String JavaDoc prefix)
197     {
198         return null;
199     }
200 }
201
Popular Tags