1 package rero.gui.input; 2 3 import rero.config.ClientDefaults; 4 import rero.config.ClientState; 5 import rero.config.ClientStateListener; 6 import text.AttributedString; 7 import text.AttributedText; 8 import text.ModifyColorMapDialog; 9 import text.TextSource; 10 11 import javax.swing.*; 12 import javax.swing.border.Border ; 13 import javax.swing.border.CompoundBorder ; 14 import javax.swing.text.AttributeSet ; 15 import javax.swing.text.BadLocationException ; 16 import javax.swing.text.Document ; 17 import javax.swing.text.PlainDocument ; 18 import java.awt.*; 19 import java.awt.event.*; 20 import java.util.ArrayList ; 21 import java.util.LinkedList ; 22 import java.util.ListIterator ; 23 24 public class InputField extends JTextField implements KeyListener, ActionListener, MouseListener, ClientStateListener { 25 protected Border defaultBorder; 27 protected LinkedList listeners; 28 protected UserInputEvent event; 29 30 private ArrayList commandHistory; 34 35 private ListIterator commandIterator; 38 39 private int maxCommands = 30; 42 43 private boolean previousKeyUpArrow = true; 46 47 protected InputBorder indent; 48 49 public void mouseClicked(MouseEvent ev) { 50 if (ev.getButton() == MouseEvent.BUTTON1 && ev.isShiftDown() && indent != null) { 51 AttributedText temp = 52 indent.getAttributes().getAttributesAt(ev.getX() - defaultBorder.getBorderInsets(this).left); 53 54 if (temp != null) { 55 if (temp.backIndex != -1 && ev.isControlDown()) { 56 ModifyColorMapDialog.showModifyColorMapDialog((JComponent) ev.getSource(), temp.backIndex); 57 } else { 58 ModifyColorMapDialog.showModifyColorMapDialog((JComponent) ev.getSource(), temp.foreIndex); 59 } 60 repaint(); 61 } 62 } 63 } 64 65 public boolean isFocusable() { 66 return true; 67 } 68 69 public void mouseEntered(MouseEvent ev) { 70 } 71 72 public void mouseExited(MouseEvent ev) { 73 } 74 75 public void mousePressed(MouseEvent ev) { 76 } 77 78 public void mouseReleased(MouseEvent ev) { 79 } 80 81 public InputField() { 82 setUI(new javax.swing.plaf.basic.BasicTextFieldUI ()); 83 84 setOpaque(false); 85 86 defaultBorder = 87 BorderFactory.createEmptyBorder(1, TextSource.UNIVERSAL_TWEAK, 1, 1); setBorder(defaultBorder); 89 93 94 addActionListener(this); 95 addKeyListener(this); 96 97 listeners = new LinkedList (); 98 99 event = new UserInputEvent(); 100 event.source = this; 101 102 indent = null; 103 104 addMouseListener(this); 105 106 rehashColors(); 107 108 ClientState.getClientState().addClientStateListener("ui.editcolor", this); 109 ClientState.getClientState().addClientStateListener("ui.font", this); 110 111 this.commandHistory = new ArrayList (maxCommands + 1); 113 this.commandIterator = commandHistory.listIterator(); 114 115 } 116 117 public void propertyChanged(String name, String parms) { 118 rehashColors(); 119 } 120 121 public void rehashColors() { 122 Color temp = ClientState.getClientState().getColor("ui.editcolor", ClientDefaults.ui_editcolor); 123 124 setForeground(temp); 125 setCaretColor(temp.brighter()); 126 127 setFont(ClientState.getClientState().getFont("ui.font", ClientDefaults.ui_font)); 128 129 revalidate(); 130 } 131 132 public void actionPerformed(ActionEvent ev) { 133 event.text = ev.getActionCommand(); 134 135 if (event.text.length() <= 0) { 136 fireInputEvent(); return; 138 } 139 140 fireInputEvent(); 141 } 142 143 public void addInputListener(InputListener l) { 144 152 listeners.addFirst(l); 153 } 154 155 public void fireInputEvent() { 156 ListIterator i = listeners.listIterator(); 157 158 this.addToHistory(event.text); 160 161 setText(""); 163 while (i.hasNext()) { 164 InputListener temp = (InputListener) i.next(); 165 temp.onInput(event); 166 } 167 168 this.resetIterator(false); 169 170 event.reset(); 171 } 172 173 public String getIndent() { 174 if (indent != null) { 175 return indent.getText(); 176 } 177 178 return ""; 179 } 180 181 public void setIndent(String text) { 182 if (text != null) { 183 indent = new InputBorder(text); 184 setBorder(new CompoundBorder (defaultBorder, indent)); 185 } else { 186 setBorder(defaultBorder); 187 indent = null; 188 } 189 } 190 191 public void keyTyped(KeyEvent e) { 192 if (e.getKeyChar() == KeyEvent.VK_BACK_SPACE && getText().length() == 0) { 196 e.consume(); 197 } 198 } 199 200 public String getCurrentText() { 201 return getText(); 202 } 203 204 205 public void keyPressed(KeyEvent e) { 206 if (e.getModifiers() == 2) { 210 int caretpos = getCaretPosition() + 1; 211 212 switch (e.getKeyCode()) { 213 case 75: setText(getText().substring(0, getCaretPosition()) + AttributedString.color + 215 getText().substring(getCaretPosition(), getText().length())); 216 setCaretPosition(caretpos); 217 e.consume(); 218 return; 219 case 85: setText(getText().substring(0, getCaretPosition()) + AttributedString.underline + 221 getText().substring(getCaretPosition(), getText().length())); 222 setCaretPosition(caretpos); 223 e.consume(); 224 return; 225 case 66: setText(getText().substring(0, getCaretPosition()) + AttributedString.bold + 227 getText().substring(getCaretPosition(), getText().length())); 228 setCaretPosition(caretpos); 229 e.consume(); 230 return; 231 case 79: setText(getText().substring(0, getCaretPosition()) + AttributedString.cancel + 233 getText().substring(getCaretPosition(), getText().length())); 234 setCaretPosition(caretpos); 235 e.consume(); 236 return; 237 case 82: setText(getText().substring(0, getCaretPosition()) + AttributedString.reverse + 239 getText().substring(getCaretPosition(), getText().length())); 240 setCaretPosition(caretpos); 241 e.consume(); 242 return; 243 default: 244 } 245 } 246 247 if (e.getKeyCode() == KeyEvent.VK_ENTER && e.getModifiers() != 0) { 248 this.resetIterator(); 249 event.text = getText(); 250 fireInputEvent(); 251 e.consume(); 252 return; 253 } 254 255 if (e.getKeyCode() == KeyEvent.VK_UP) { 259 260 if (commandIterator.hasPrevious()) { 262 263 String previous = String.valueOf(commandIterator.previous()); 265 266 if (!previousKeyUpArrow && commandIterator.hasPrevious()) { 268 setText(String.valueOf(commandIterator.previous())); 269 } 270 271 else { 273 setText(previous); 274 } 275 276 } else { 277 e.consume(); 278 } 279 280 this.previousKeyUpArrow = true; 282 } 283 284 if (e.getKeyCode() == KeyEvent.VK_DOWN) { 286 287 String next; 289 290 if (commandIterator.hasNext()) { 292 293 if (commandIterator.hasNext() && 296 commandIterator.nextIndex() + 1 == commandHistory.size() && 297 this.previousKeyUpArrow) { 298 resetIterator(false); 300 this.previousKeyUpArrow = true; 301 setText(""); 302 } 303 304 else { 306 next = String.valueOf(commandIterator.next()); 308 309 if (previousKeyUpArrow && commandIterator.hasNext()) { 312 setText(String.valueOf(commandIterator.next())); 313 } 314 315 else { 317 setText(next); 318 } 319 320 this.previousKeyUpArrow = false; 322 } 323 } 324 325 else { 327 setText(""); 328 next = null; 329 this.previousKeyUpArrow = true; 330 331 } 332 333 e.consume(); 335 } 336 337 } 339 340 private void resetIterator() { 342 this.resetIterator(true); 343 } 344 345 private void resetIterator(boolean resetToBeginning) { 347 if (resetToBeginning) { 348 349 while (this.commandIterator.hasPrevious()) { 351 this.commandIterator.previous(); 352 } 353 354 } else { 355 356 while (this.commandIterator.hasNext()) { 358 this.commandIterator.next(); 359 } 360 361 } 362 } 363 364 365 private void addToHistory(String text) { 366 367 this.commandExistsInHistory(text, true); this.resetIterator(false); this.commandIterator.add(text); 372 if (this.commandHistory.size() == (maxCommands + 1) 374 && (maxCommands > 0)) { 375 376 this.resetIterator(); this.commandIterator.remove(); this.resetIterator(false); } 380 } 381 382 383 private boolean commandExistsInHistory(String cmd, boolean delete) { 387 388 this.resetIterator(); 390 391 while (this.commandIterator.hasNext()) { 393 if (this.commandIterator.next().equals(cmd)) { 394 395 if (delete) { 397 398 this.commandIterator.remove(); 400 } 401 402 return true; 404 } 405 } 406 407 return false; 409 } 410 411 public void keyReleased(KeyEvent e) { 412 } 413 414 public void paint(Graphics g) { 415 TextSource.initGraphics(g); 416 super.paint(g); 417 } 418 419 protected Document createDefaultModel() { 420 return new InputDocument(); 421 } 422 423 class InputDocument extends PlainDocument { 424 public void insertString(int offs, String str, AttributeSet a) throws BadLocationException { 425 if (str.indexOf('\n') == -1) { 426 super.insertString(offs, str, a); 428 return; 429 } 430 431 while (str.indexOf('\n') > -1) { 432 event.text = str.substring(0, str.indexOf('\n')); 433 if (getCurrentText().length() > 0) { 438 super.insertString(offs, event.text, a); 439 event.text = getCurrentText(); 440 } 441 442 fireInputEvent(); 443 444 if (str.indexOf('\n') == str.length()) { 445 return; 446 } else { 447 str = str.substring(str.indexOf('\n') + 1, str.length()); 448 } 449 } 450 451 if (str.length() > 0) { 452 event.text = str; 453 fireInputEvent(); 454 } 455 } 456 } 457 } 458 | Popular Tags |