001: /*
002: * HistoryTextField.java
003: *
004: * Copyright (C) 1998-2004 Peter Graves
005: * $Id: HistoryTextField.java,v 1.3 2004/09/13 00:11:28 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.awt.Dimension;
025: import java.awt.Font;
026: import java.awt.Graphics;
027: import java.awt.event.FocusEvent;
028: import java.awt.event.FocusListener;
029: import java.awt.event.MouseEvent;
030: import java.awt.event.MouseListener;
031: import java.awt.event.TextListener;
032: import javax.swing.JTextField;
033: import javax.swing.SwingUtilities;
034:
035: public class HistoryTextField extends JTextField implements
036: FocusListener, MouseListener {
037: private History history;
038:
039: private Object owner;
040:
041: // Only one text listener is supported.
042: private TextListener textListener;
043:
044: protected TextFieldHandler handler;
045:
046: public HistoryTextField(Editor editor, int columns) {
047: super (columns);
048: final Preferences preferences = Editor.preferences();
049: final String fontName = preferences
050: .getStringProperty(Property.TEXT_FIELD_FONT_NAME);
051: if (fontName != null) {
052: int fontSize = preferences
053: .getIntegerProperty(Property.TEXT_FIELD_FONT_SIZE);
054: if (fontSize == 0)
055: fontSize = preferences
056: .getIntegerProperty(Property.DIALOG_FONT_SIZE);
057: setFont(new Font(fontName, Font.PLAIN, fontSize));
058: }
059: setAlignmentX(LEFT_ALIGNMENT);
060: setHandler(new DefaultTextFieldHandler(editor, this ));
061: addFocusListener(this );
062: addMouseListener(this );
063: }
064:
065: public HistoryTextField(int columns) {
066: this (Editor.currentEditor(), columns);
067: }
068:
069: public Dimension getPreferredSize() {
070: Dimension size = super .getPreferredSize();
071: size.width = getColumns() * 11;
072: return size;
073: }
074:
075: public final Object getOwner() {
076: return owner;
077: }
078:
079: public final void setOwner(Object owner) {
080: this .owner = owner;
081: }
082:
083: public void addTextListener(TextListener textListener) {
084: Debug.assertTrue(this .textListener == null);
085: this .textListener = textListener;
086: }
087:
088: public final TextListener getTextListener() {
089: return textListener;
090: }
091:
092: public final TextFieldHandler getHandler() {
093: return handler;
094: }
095:
096: public final void setHandler(TextFieldHandler handler) {
097: Debug.assertTrue(handler != null);
098: if (this .handler != null)
099: removeKeyListener(this .handler);
100: this .handler = handler;
101: addKeyListener(handler);
102: }
103:
104: public void setHistory(History history) {
105: this .history = history;
106: resetHistory();
107: }
108:
109: public final History getHistory() {
110: return history;
111: }
112:
113: public final void resetHistory() {
114: if (history != null)
115: history.reset();
116: }
117:
118: public String getText() {
119: String s = super .getText();
120: int length = s.length();
121: FastStringBuffer sb = new FastStringBuffer(length);
122: // Copy string, stripping control characters if any.
123: for (int i = 0; i < length; i++) {
124: char c = s.charAt(i);
125: if (!Character.isISOControl(c))
126: sb.append(c);
127: }
128: return sb.toString();
129: }
130:
131: public void previousHistory() {
132: if (history != null) {
133: final String text = super .getText();
134: while (true) {
135: String s = history.getPrevious();
136: if (s == null)
137: break;
138: if (!s.equals(text)) {
139: setText(s);
140: selectAll();
141: break;
142: }
143: }
144: }
145: }
146:
147: public void nextHistory() {
148: if (history != null) {
149: final String text = super .getText();
150: while (true) {
151: String s = history.getNext();
152: if (s == null)
153: break;
154: if (!s.equals(text)) {
155: setText(s);
156: selectAll();
157: break;
158: }
159: }
160: }
161: }
162:
163: public void recallLast() {
164: if (history != null) {
165: String s = history.getPrevious();
166: setText(s != null ? s : "");
167: }
168: }
169:
170: public void paintComponent(Graphics g) {
171: Display.setRenderingHints(g);
172: super .paintComponent(g);
173: }
174:
175: public void focusGained(FocusEvent e) {
176: selectAll();
177: }
178:
179: public void focusLost(FocusEvent e) {
180: int length = getText().length();
181: select(length, length);
182: }
183:
184: public void mouseClicked(MouseEvent e) {
185: }
186:
187: public void mouseEntered(MouseEvent e) {
188: }
189:
190: public void mouseExited(MouseEvent e) {
191: }
192:
193: public void mousePressed(MouseEvent e) {
194: }
195:
196: public void mouseReleased(MouseEvent e) {
197: final int dot = getCaretPosition();
198: final int mark = getCaret().getMark();
199: Runnable r = new Runnable() {
200: public void run() {
201: setCaretPosition(mark);
202: moveCaretPosition(dot);
203: }
204: };
205: SwingUtilities.invokeLater(r);
206: }
207: }
|