001: /*
002: * HistoryTextArea.java - Text area with a history
003: * :tabSize=8:indentSize=8:noTabs=false:
004: * :folding=explicit:collapseFolds=1:
005: *
006: * Copyright (C) 2004 Slava Pestov
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022:
023: package org.gjt.sp.jedit.gui;
024:
025: //{{{ Imports
026: import javax.swing.*;
027: import javax.swing.border.Border;
028: import javax.swing.border.AbstractBorder;
029: import javax.swing.border.CompoundBorder;
030: import javax.swing.event.MouseInputAdapter;
031: import java.awt.*;
032: import java.awt.event.*;
033: import java.util.Set;
034: import java.util.TreeSet;
035: import org.gjt.sp.jedit.*;
036:
037: //}}}
038:
039: /**
040: * Text area with a history.
041: * @author Slava Pestov
042: * @version $Id: HistoryTextArea.java 5136 2004-10-17 04:36:32Z spestov $
043: */
044: public class HistoryTextArea extends JTextArea {
045: //{{{ HistoryTextArea constructor
046: public HistoryTextArea(String name) {
047: super (3, 15);
048: controller = new HistoryText(this , name);
049: Set focusForwardTraversalKeys = new TreeSet();
050: focusForwardTraversalKeys.add(KeyStroke.getKeyStroke(
051: KeyEvent.VK_TAB, 0));
052: Set focusBackwardTraversalKeys = new TreeSet();
053: focusBackwardTraversalKeys.add(KeyStroke.getKeyStroke(
054: KeyEvent.VK_TAB, InputEvent.SHIFT_MASK));
055: setFocusTraversalKeys(
056: KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
057: focusForwardTraversalKeys);
058: setFocusTraversalKeys(
059: KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
060: focusBackwardTraversalKeys);
061: } //}}}
062:
063: //{{{ getModel() method
064: /**
065: * Returns the underlying history controller.
066: * @since jEdit 4.3pre1
067: */
068: public HistoryModel getModel() {
069: return controller.getModel();
070: } //}}}
071:
072: //{{{ setModel() method
073: /**
074: * Sets the history list controller.
075: * @param name The model name
076: * @since jEdit 4.3pre1
077: */
078: public void setModel(String name) {
079: controller.setModel(name);
080: } //}}}
081:
082: //{{{ setInstantPopups() method
083: /**
084: * Sets if selecting a value from the popup should immediately fire
085: * an ActionEvent.
086: */
087: public void setInstantPopups(boolean instantPopups) {
088: controller.setInstantPopups(instantPopups);
089: } //}}}
090:
091: //{{{ getInstantPopups() method
092: /**
093: * Returns if selecting a value from the popup should immediately fire
094: * an ActionEvent.
095: */
096: public boolean getInstantPopups() {
097: return controller.getInstantPopups();
098: } //}}}
099:
100: //{{{ addCurrentToHistory() method
101: /**
102: * Adds the currently entered item to the history.
103: */
104: public void addCurrentToHistory() {
105: controller.addCurrentToHistory();
106: } //}}}
107:
108: //{{{ setText() method
109: /**
110: * Sets the displayed text.
111: */
112: public void setText(String text) {
113: super .setText(text);
114: controller.setIndex(-1);
115: } //}}}
116:
117: //{{{ Protected members
118:
119: //{{{ processKeyEvent() method
120: protected void processKeyEvent(KeyEvent evt) {
121: if (!isEnabled())
122: return;
123:
124: if (evt.getID() == KeyEvent.KEY_PRESSED) {
125: switch (evt.getKeyCode()) {
126: case KeyEvent.VK_ENTER:
127: if (evt.isControlDown()) {
128: replaceSelection("\n");
129: evt.consume();
130: }
131: break;
132: case KeyEvent.VK_TAB:
133: if (evt.isControlDown()) {
134: replaceSelection("\t");
135: evt.consume();
136: }
137: break;
138: case KeyEvent.VK_PAGE_UP:
139: if (evt.isShiftDown())
140: controller.doBackwardSearch();
141: else
142: controller.historyPrevious();
143: evt.consume();
144: break;
145: case KeyEvent.VK_PAGE_DOWN:
146: if (evt.isShiftDown())
147: controller.doForwardSearch();
148: else
149: controller.historyNext();
150: evt.consume();
151: break;
152: case KeyEvent.VK_UP:
153: if (evt.isAltDown()) {
154: controller.showPopupMenu(evt.isShiftDown());
155: evt.consume();
156: }
157: break;
158: }
159: }
160:
161: if (!evt.isConsumed())
162: super .processKeyEvent(evt);
163: } //}}}
164:
165: //{{{ processMouseEvent() method
166: protected void processMouseEvent(MouseEvent evt) {
167: if (!isEnabled())
168: return;
169:
170: switch (evt.getID()) {
171: case MouseEvent.MOUSE_PRESSED:
172: if (GUIUtilities.isPopupTrigger(evt))
173: controller.showPopupMenu(evt.isShiftDown());
174: else
175: super .processMouseEvent(evt);
176:
177: break;
178: default:
179: super .processMouseEvent(evt);
180: break;
181: }
182: } //}}}
183:
184: //}}}
185:
186: //{{{ Private variables
187: private HistoryText controller;
188: //}}}
189: }
|