1 22 23 package org.gjt.sp.jedit.gui; 24 25 import javax.swing.*; 27 import javax.swing.border.Border ; 28 import javax.swing.border.AbstractBorder ; 29 import javax.swing.border.CompoundBorder ; 30 import javax.swing.event.MouseInputAdapter ; 31 import java.awt.*; 32 import java.awt.event.*; 33 import org.gjt.sp.jedit.*; 34 36 41 public class HistoryTextField extends JTextField 42 { 43 48 public HistoryTextField() 49 { 50 this(null); 51 } 53 58 public HistoryTextField(String name) 59 { 60 this(name,false,true); 61 } 63 73 public HistoryTextField(String name, boolean instantPopups) 74 { 75 this(name,instantPopups,true); 76 } 78 90 public HistoryTextField(String name, boolean instantPopups, 91 boolean enterAddsToHistory) 92 { 93 controller = new HistoryText(this,null) 94 { 95 public void fireActionPerformed() 96 { 97 HistoryTextField.this.fireActionPerformed(); 98 } 99 }; 100 101 setModel(name); 102 MouseHandler mouseHandler = new MouseHandler(); 103 addMouseListener(mouseHandler); 104 addMouseMotionListener(mouseHandler); 105 106 setInstantPopups(instantPopups); 107 setEnterAddsToHistory(enterAddsToHistory); 108 } 110 116 public void setInstantPopups(boolean instantPopups) 117 { 118 controller.setInstantPopups(instantPopups); 119 } 121 127 public boolean getInstantPopups() 128 { 129 return controller.getInstantPopups(); 130 } 132 138 public void setEnterAddsToHistory(boolean enterAddsToHistory) 139 { 140 this.enterAddsToHistory = enterAddsToHistory; 141 } 143 149 public boolean setEnterAddsToHistory() 150 { 151 return enterAddsToHistory; 152 } 154 159 public void setSelectAllOnFocus(boolean selectAllOnFocus) 160 { 161 this.selectAllOnFocus = selectAllOnFocus; 162 } 164 169 public boolean setSelectAllOnFocus() 170 { 171 return selectAllOnFocus; 172 } 174 178 public HistoryModel getModel() 179 { 180 return controller.getModel(); 181 } 183 189 public void setModel(String name) 190 { 191 controller.setModel(name); 192 193 Border textFieldBorder = UIManager.getBorder("TextField.border"); 194 195 if(name == null) 196 { 197 if(textFieldBorder != null) 198 setBorder(textFieldBorder); 199 } 200 else 201 { 202 if(textFieldBorder != null) 203 { 204 setBorder(new CompoundBorder (textFieldBorder, 205 new HistoryBorder())); 206 } 207 } 208 repaint(); 209 } 211 215 public void addCurrentToHistory() 216 { 217 controller.addCurrentToHistory(); 218 } 220 224 public void setText(String text) 225 { 226 super.setText(text); 227 controller.setIndex(-1); 228 } 230 234 public void fireActionPerformed() 235 { 236 super.fireActionPerformed(); 237 } 239 241 protected void processKeyEvent(KeyEvent evt) 243 { 244 if(!isEnabled()) 245 return; 246 247 if(evt.getID() == KeyEvent.KEY_PRESSED) 248 { 249 switch(evt.getKeyCode()) 250 { 251 case KeyEvent.VK_ENTER: 252 if(enterAddsToHistory) 253 addCurrentToHistory(); 254 255 if(evt.getModifiers() == 0) 256 { 257 fireActionPerformed(); 258 evt.consume(); 259 } 260 break; 261 case KeyEvent.VK_UP: 262 if(evt.isShiftDown()) 263 controller.doBackwardSearch(); 264 else 265 controller.historyPrevious(); 266 evt.consume(); 267 break; 268 case KeyEvent.VK_DOWN: 269 if(evt.isShiftDown()) 270 controller.doForwardSearch(); 271 else if(evt.isAltDown()) 272 { 273 controller.showPopupMenu( 274 evt.isShiftDown()); 275 } 276 else 277 controller.historyNext(); 278 evt.consume(); 279 break; 280 case KeyEvent.VK_TAB: 281 if(evt.isControlDown()) 282 { 283 controller.doBackwardSearch(); 284 evt.consume(); 285 } 286 break; 287 } 288 } 289 290 if(!evt.isConsumed()) 291 super.processKeyEvent(evt); 292 } 294 protected void processMouseEvent(MouseEvent evt) 296 { 297 if(!isEnabled()) 298 return; 299 300 switch(evt.getID()) 301 { 302 case MouseEvent.MOUSE_PRESSED: 303 Border border = getBorder(); 304 Insets insets = border.getBorderInsets(HistoryTextField.this); 305 306 if(evt.getX() >= getWidth() - insets.right 307 || GUIUtilities.isPopupTrigger(evt)) 308 { 309 controller.showPopupMenu(evt.isShiftDown()); 310 } 311 else 312 super.processMouseEvent(evt); 313 314 break; 315 case MouseEvent.MOUSE_EXITED: 316 setCursor(Cursor.getDefaultCursor()); 317 super.processMouseEvent(evt); 318 break; 319 default: 320 super.processMouseEvent(evt); 321 break; 322 } 323 } 325 327 329 private HistoryText controller; 331 private boolean enterAddsToHistory; 332 private boolean selectAllOnFocus; 333 335 337 339 class MouseHandler extends MouseInputAdapter 341 { 342 boolean selectAll; 343 344 public void mousePressed(MouseEvent evt) 346 { 347 selectAll = (!hasFocus() && selectAllOnFocus); 348 } 350 public void mouseReleased(MouseEvent evt) 352 { 353 SwingUtilities.invokeLater(new Runnable () 354 { 355 public void run() 356 { 357 if(selectAll) 358 selectAll(); 359 } 360 }); 361 } 363 public void mouseMoved(MouseEvent evt) 365 { 366 Border border = getBorder(); 367 Insets insets = border.getBorderInsets(HistoryTextField.this); 368 369 if(evt.getX() >= getWidth() - insets.right) 370 setCursor(Cursor.getDefaultCursor()); 371 else 372 setCursor(Cursor.getPredefinedCursor( 373 Cursor.TEXT_CURSOR)); 374 } 376 public void mouseDragged(MouseEvent evt) 378 { 379 selectAll = false; 380 } } 383 static class HistoryBorder extends AbstractBorder 385 { 386 static final int WIDTH = 16; 387 388 public void paintBorder(Component c, Graphics g, 389 int x, int y, int w, int h) 390 { 391 g.translate(x+w-WIDTH,y-1); 392 393 400 int w2 = WIDTH/2; 402 int h2 = h/2; 403 g.setColor(UIManager.getColor(c.isEnabled() 404 && ((HistoryTextField)c).getModel() != null 405 ? "TextField.foreground" : "TextField.disabledForeground")); 406 g.drawLine(w2-5,h2-2,w2+4,h2-2); 407 g.drawLine(w2-4,h2-1,w2+3,h2-1); 408 g.drawLine(w2-3,h2 ,w2+2,h2 ); 409 g.drawLine(w2-2,h2+1,w2+1,h2+1); 410 g.drawLine(w2-1,h2+2,w2 ,h2+2); 411 412 g.translate(-(x+w-WIDTH),-(y-1)); 413 } 414 415 public Insets getBorderInsets(Component c) 416 { 417 return new Insets(0,0,0,WIDTH); 418 } 419 } 421 } 423 | Popular Tags |