KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * HistoryTextField.java
3  *
4  * Copyright (C) 1998-2004 Peter Graves
5  * $Id: HistoryTextField.java,v 1.3 2004/09/13 00:11:28 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.Dimension JavaDoc;
25 import java.awt.Font JavaDoc;
26 import java.awt.Graphics JavaDoc;
27 import java.awt.event.FocusEvent JavaDoc;
28 import java.awt.event.FocusListener JavaDoc;
29 import java.awt.event.MouseEvent JavaDoc;
30 import java.awt.event.MouseListener JavaDoc;
31 import java.awt.event.TextListener JavaDoc;
32 import javax.swing.JTextField JavaDoc;
33 import javax.swing.SwingUtilities JavaDoc;
34
35 public class HistoryTextField extends JTextField JavaDoc implements FocusListener JavaDoc,
36     MouseListener JavaDoc
37 {
38     private History history;
39
40     private Object JavaDoc owner;
41
42     // Only one text listener is supported.
43
private TextListener JavaDoc textListener;
44
45     protected TextFieldHandler handler;
46
47     public HistoryTextField(Editor editor, int columns)
48     {
49         super(columns);
50         final Preferences preferences = Editor.preferences();
51         final String JavaDoc fontName =
52             preferences.getStringProperty(Property.TEXT_FIELD_FONT_NAME);
53         if (fontName != null) {
54             int fontSize =
55                 preferences.getIntegerProperty(Property.TEXT_FIELD_FONT_SIZE);
56             if (fontSize == 0)
57                 fontSize =
58                     preferences.getIntegerProperty(Property.DIALOG_FONT_SIZE);
59             setFont(new Font JavaDoc(fontName, Font.PLAIN, fontSize));
60         }
61         setAlignmentX(LEFT_ALIGNMENT);
62         setHandler(new DefaultTextFieldHandler(editor, this));
63         addFocusListener(this);
64         addMouseListener(this);
65     }
66
67     public HistoryTextField(int columns)
68     {
69         this(Editor.currentEditor(), columns);
70     }
71
72     public Dimension JavaDoc getPreferredSize() {
73         Dimension JavaDoc size = super.getPreferredSize();
74         size.width = getColumns() * 11;
75         return size;
76     }
77
78     public final Object JavaDoc getOwner()
79     {
80         return owner;
81     }
82
83     public final void setOwner(Object JavaDoc owner)
84     {
85         this.owner = owner;
86     }
87
88     public void addTextListener(TextListener JavaDoc textListener)
89     {
90         Debug.assertTrue(this.textListener == null);
91         this.textListener = textListener;
92     }
93
94     public final TextListener JavaDoc getTextListener()
95     {
96         return textListener;
97     }
98
99     public final TextFieldHandler getHandler()
100     {
101         return handler;
102     }
103
104     public final void setHandler(TextFieldHandler handler)
105     {
106         Debug.assertTrue(handler != null);
107         if (this.handler != null)
108             removeKeyListener(this.handler);
109         this.handler = handler;
110         addKeyListener(handler);
111     }
112
113     public void setHistory(History history)
114     {
115         this.history = history;
116         resetHistory();
117     }
118
119     public final History getHistory()
120     {
121         return history;
122     }
123
124     public final void resetHistory()
125     {
126         if (history != null)
127             history.reset();
128     }
129
130     public String JavaDoc getText()
131     {
132         String JavaDoc s = super.getText();
133         int length = s.length();
134         FastStringBuffer sb = new FastStringBuffer(length);
135         // Copy string, stripping control characters if any.
136
for (int i = 0; i < length; i++) {
137             char c = s.charAt(i);
138             if (!Character.isISOControl(c))
139                 sb.append(c);
140         }
141         return sb.toString();
142     }
143
144     public void previousHistory()
145     {
146         if (history != null) {
147             final String JavaDoc text = super.getText();
148             while (true) {
149                 String JavaDoc s = history.getPrevious();
150                 if (s == null)
151                     break;
152                 if (!s.equals(text)) {
153                     setText(s);
154                     selectAll();
155                     break;
156                 }
157             }
158         }
159     }
160
161     public void nextHistory()
162     {
163         if (history != null) {
164             final String JavaDoc text = super.getText();
165             while (true) {
166                 String JavaDoc s = history.getNext();
167                 if (s == null)
168                     break;
169                 if (!s.equals(text)) {
170                     setText(s);
171                     selectAll();
172                     break;
173                 }
174             }
175         }
176     }
177
178     public void recallLast()
179     {
180         if (history != null) {
181             String JavaDoc s = history.getPrevious();
182             setText(s != null ? s : "");
183         }
184     }
185
186     public void paintComponent(Graphics JavaDoc g)
187     {
188         Display.setRenderingHints(g);
189         super.paintComponent(g);
190     }
191
192     public void focusGained(FocusEvent JavaDoc e)
193     {
194         selectAll();
195     }
196
197     public void focusLost(FocusEvent JavaDoc e)
198     {
199         int length = getText().length();
200         select(length, length);
201     }
202
203     public void mouseClicked(MouseEvent JavaDoc e) {}
204
205     public void mouseEntered(MouseEvent JavaDoc e) {}
206
207     public void mouseExited(MouseEvent JavaDoc e) {}
208
209     public void mousePressed(MouseEvent JavaDoc e) {}
210
211     public void mouseReleased(MouseEvent JavaDoc e)
212     {
213         final int dot = getCaretPosition();
214         final int mark = getCaret().getMark();
215         Runnable JavaDoc r = new Runnable JavaDoc() {
216             public void run()
217             {
218                 setCaretPosition(mark);
219                 moveCaretPosition(dot);
220             }
221         };
222         SwingUtilities.invokeLater(r);
223     }
224 }
225
Popular Tags