1 21 22 package org.armedbear.j; 23 24 import java.awt.BorderLayout ; 25 import java.awt.event.InputEvent ; 26 import java.awt.event.KeyEvent ; 27 import java.awt.event.KeyListener ; 28 import java.util.List ; 29 import javax.swing.BoxLayout ; 30 import javax.swing.JDialog ; 31 import javax.swing.JPanel ; 32 import javax.swing.border.EmptyBorder ; 33 34 public class InputDialog extends JDialog implements KeyListener 35 { 36 protected final Editor editor; 37 38 protected HistoryTextField textField; 39 40 private String defaultValue; 41 private History history; 42 private String input; 43 private List completions; 44 private int index; 45 46 public InputDialog(Editor editor, String prompt, String title, 47 String defaultValue) 48 { 49 super(editor.getFrame(), title, true); 50 this.editor = editor; 51 this.defaultValue = defaultValue; 52 JPanel panel = new JPanel (); 53 panel.setLayout(new BoxLayout (panel, BoxLayout.Y_AXIS)); 54 panel.setBorder(new EmptyBorder (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 showInputDialog(Editor editor, String prompt, 65 String title, String 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 showInputDialog(Editor editor, String prompt, 74 String 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 s) 90 { 91 defaultValue = s; 92 } 93 94 public final String 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 e) 122 { 123 final int keyCode = e.getKeyCode(); 124 final int modifiers = e.getModifiers(); 125 switch (keyCode) { 126 case KeyEvent.VK_TAB: { 127 String 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 return; 150 default: 151 completions = null; 153 return; 154 } 155 } 156 157 public void keyReleased(KeyEvent e) {} 158 159 public void keyTyped(KeyEvent e) {} 160 161 public void dispose() 162 { 163 super.dispose(); 164 editor.restoreFocus(); 165 } 166 167 private String guess(String 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; if (index < completions.size()) 177 return (String ) completions.get(index++); 178 return null; 179 } 180 181 private String 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 ) completions.get(index++); 189 } 190 } 191 return null; 192 } 193 194 protected List getCompletions(String prefix) 197 { 198 return null; 199 } 200 } 201 | Popular Tags |