KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > gjt > sp > jedit > gui > HistoryTextField


1 /*
2  * HistoryTextField.java - Text field with a history
3  * :tabSize=8:indentSize=8:noTabs=false:
4  * :folding=explicit:collapseFolds=1:
5  *
6  * Copyright (C) 1999, 2000, 2001 Slava Pestov
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21  */

22
23 package org.gjt.sp.jedit.gui;
24
25 //{{{ Imports
26
import javax.swing.*;
27 import javax.swing.border.Border JavaDoc;
28 import javax.swing.border.AbstractBorder JavaDoc;
29 import javax.swing.border.CompoundBorder JavaDoc;
30 import javax.swing.event.MouseInputAdapter JavaDoc;
31 import java.awt.*;
32 import java.awt.event.*;
33 import org.gjt.sp.jedit.*;
34 //}}}
35

36 /**
37  * Text field with an arrow-key accessable history.
38  * @author Slava Pestov
39  * @version $Id: HistoryTextField.java 5176 2005-01-21 23:04:14Z spestov $
40  */

41 public class HistoryTextField extends JTextField
42 {
43     //{{{ HistoryTextField constructor
44
/**
45      * Creates a new history text field.
46      * @since jEdit 3.2pre5
47      */

48     public HistoryTextField()
49     {
50         this(null);
51     } //}}}
52

53     //{{{ HistoryTextField constructor
54
/**
55      * Creates a new history text field.
56      * @param name The history model name
57      */

58     public HistoryTextField(String JavaDoc name)
59     {
60         this(name,false,true);
61     } //}}}
62

63     //{{{ HistoryTextField constructor
64
/**
65      * Creates a new history text field.
66      * @param name The history model name
67      * @param instantPopups If true, selecting a value from the history
68      * popup will immediately fire an ActionEvent. If false, the user
69      * will have to press 'Enter' first
70      *
71      * @since jEdit 2.2pre5
72      */

73     public HistoryTextField(String JavaDoc name, boolean instantPopups)
74     {
75         this(name,instantPopups,true);
76     } //}}}
77

78     //{{{ HistoryTextField constructor
79
/**
80      * Creates a new history text field.
81      * @param name The history model name
82      * @param instantPopups If true, selecting a value from the history
83      * popup will immediately fire an ActionEvent. If false, the user
84      * will have to press 'Enter' first
85      * @param enterAddsToHistory If true, pressing the Enter key will
86      * automatically add the currently entered text to the history.
87      *
88      * @since jEdit 2.6pre5
89      */

90     public HistoryTextField(String JavaDoc 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     } //}}}
109

110     //{{{ setInstantPopups() method
111
/**
112      * Sets if selecting a value from the popup should immediately fire
113      * an ActionEvent.
114      * @since jEdit 4.0pre3
115      */

116     public void setInstantPopups(boolean instantPopups)
117     {
118         controller.setInstantPopups(instantPopups);
119     } //}}}
120

121     //{{{ getInstantPopups() method
122
/**
123      * Returns if selecting a value from the popup should immediately fire
124      * an ActionEvent.
125      * @since jEdit 4.0pre3
126      */

127     public boolean getInstantPopups()
128     {
129         return controller.getInstantPopups();
130     } //}}}
131

132     //{{{ setEnterAddsToHistory() method
133
/**
134      * Sets if pressing Enter should automatically add the currently
135      * entered text to the history.
136      * @since jEdit 4.0pre3
137      */

138     public void setEnterAddsToHistory(boolean enterAddsToHistory)
139     {
140         this.enterAddsToHistory = enterAddsToHistory;
141     } //}}}
142

143     //{{{ getEnterAddsToHistory() method
144
/**
145      * Returns if pressing Enter should automatically add the currently
146      * entered text to the history.
147      * @since jEdit 4.0pre3
148      */

149     public boolean setEnterAddsToHistory()
150     {
151         return enterAddsToHistory;
152     } //}}}
153

154     //{{{ setSelectAllOnFocus() method
155
/**
156      * Sets if all text should be selected when the field gets focus.
157      * @since jEdit 4.0pre3
158      */

159     public void setSelectAllOnFocus(boolean selectAllOnFocus)
160     {
161         this.selectAllOnFocus = selectAllOnFocus;
162     } //}}}
163

164     //{{{ getSelectAllOnFocus() method
165
/**
166      * Returns if all text should be selected when the field gets focus.
167      * @since jEdit 4.0pre3
168      */

169     public boolean setSelectAllOnFocus()
170     {
171         return selectAllOnFocus;
172     } //}}}
173

174     //{{{ getModel() method
175
/**
176      * Returns the underlying history model.
177      */

178     public HistoryModel getModel()
179     {
180         return controller.getModel();
181     } //}}}
182

183     //{{{ setModel() method
184
/**
185      * Sets the history list model.
186      * @param name The model name
187      * @since jEdit 2.3pre3
188      */

189     public void setModel(String JavaDoc name)
190     {
191         controller.setModel(name);
192
193         Border JavaDoc 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 JavaDoc(textFieldBorder,
205                     new HistoryBorder()));
206             }
207         }
208         repaint();
209     } //}}}
210

211     //{{{ addCurrentToHistory() method
212
/**
213      * Adds the currently entered item to the history.
214      */

215     public void addCurrentToHistory()
216     {
217         controller.addCurrentToHistory();
218     } //}}}
219

220     //{{{ setText() method
221
/**
222      * Sets the displayed text.
223      */

224     public void setText(String JavaDoc text)
225     {
226         super.setText(text);
227         controller.setIndex(-1);
228     } //}}}
229

230     //{{{ fireActionPerformed() method
231
/**
232      * Make it public.
233      */

234     public void fireActionPerformed()
235     {
236         super.fireActionPerformed();
237     } //}}}
238

239     //{{{ Protected members
240

241     //{{{ processKeyEvent() method
242
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     } //}}}
293

294     //{{{ processMouseEvent() method
295
protected void processMouseEvent(MouseEvent evt)
296     {
297         if(!isEnabled())
298             return;
299
300         switch(evt.getID())
301         {
302         case MouseEvent.MOUSE_PRESSED:
303             Border JavaDoc 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     } //}}}
324

325     //}}}
326

327     //{{{ Private members
328

329     //{{{ Instance variables
330
private HistoryText controller;
331     private boolean enterAddsToHistory;
332     private boolean selectAllOnFocus;
333     //}}}
334

335     //}}}
336

337     //{{{ Inner classes
338

339     //{{{ MouseHandler class
340
class MouseHandler extends MouseInputAdapter JavaDoc
341     {
342         boolean selectAll;
343
344         //{{{ mousePressed() method
345
public void mousePressed(MouseEvent evt)
346         {
347             selectAll = (!hasFocus() && selectAllOnFocus);
348         } //}}}
349

350         //{{{ mouseReleased() method
351
public void mouseReleased(MouseEvent evt)
352         {
353             SwingUtilities.invokeLater(new Runnable JavaDoc()
354             {
355                 public void run()
356                 {
357                     if(selectAll)
358                         selectAll();
359                 }
360             });
361         } //}}}
362

363         //{{{ mouseMoved() method
364
public void mouseMoved(MouseEvent evt)
365         {
366             Border JavaDoc 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         } //}}}
375

376         //{{{ mouseDragged() method
377
public void mouseDragged(MouseEvent evt)
378         {
379             selectAll = false;
380         } //}}}
381
} //}}}
382

383     //{{{ HistoryBorder class
384
static class HistoryBorder extends AbstractBorder JavaDoc
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             //if(c.isEnabled())
394
//{
395
// // vertical separation line
396
// g.setColor(UIManager.getColor("controlDkShadow"));
397
// g.drawLine(0,0,0,h);
398
//}
399

400             // down arrow
401
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     } //}}}
420

421     //}}}
422
}
423
Popular Tags