PageRenderTime 14ms CodeModel.GetById 1ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/gui/HistoryTextArea.java

#
Java | 206 lines | 124 code | 18 blank | 64 comment | 11 complexity | 3cfa2becc65b674ae398019e1db18b24 MD5 | raw file
  1/*
  2 * HistoryTextArea.java - Text area with a history
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2004 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
 23package org.gjt.sp.jedit.gui;
 24
 25//{{{ Imports
 26import javax.swing.*;
 27import javax.swing.border.Border;
 28import javax.swing.border.AbstractBorder;
 29import javax.swing.border.CompoundBorder;
 30import javax.swing.event.MouseInputAdapter;
 31import java.awt.*;
 32import java.awt.event.*;
 33import java.util.Set;
 34import java.util.TreeSet;
 35import org.gjt.sp.jedit.*;
 36//}}}
 37
 38/**
 39 * Text area with a history.
 40 * @author Slava Pestov
 41 * @version $Id: HistoryTextArea.java 5136 2004-10-17 04:36:32Z spestov $
 42 */
 43public class HistoryTextArea extends JTextArea
 44{
 45	//{{{ HistoryTextArea constructor
 46	public HistoryTextArea(String name)
 47	{
 48		super(3,15);
 49		controller = new HistoryText(this,name);
 50		Set focusForwardTraversalKeys = new TreeSet();
 51		focusForwardTraversalKeys.add(
 52			KeyStroke.getKeyStroke(KeyEvent.VK_TAB,0));
 53		Set focusBackwardTraversalKeys = new TreeSet();
 54		focusBackwardTraversalKeys.add(
 55			KeyStroke.getKeyStroke(KeyEvent.VK_TAB,
 56			InputEvent.SHIFT_MASK));
 57		setFocusTraversalKeys(
 58			KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
 59			focusForwardTraversalKeys);
 60		setFocusTraversalKeys(
 61			KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
 62			focusBackwardTraversalKeys);
 63	} //}}}
 64
 65	//{{{ getModel() method
 66	/**
 67	 * Returns the underlying history controller.
 68	 * @since jEdit 4.3pre1
 69	 */
 70	public HistoryModel getModel()
 71	{
 72		return controller.getModel();
 73	} //}}}
 74
 75	//{{{ setModel() method
 76	/**
 77	 * Sets the history list controller.
 78	 * @param name The model name
 79	 * @since jEdit 4.3pre1
 80	 */
 81	public void setModel(String name)
 82	{
 83		controller.setModel(name);
 84	} //}}}
 85
 86	//{{{ setInstantPopups() method
 87	/**
 88	 * Sets if selecting a value from the popup should immediately fire
 89	 * an ActionEvent.
 90	 */
 91	public void setInstantPopups(boolean instantPopups)
 92	{
 93		controller.setInstantPopups(instantPopups);
 94	} //}}}
 95
 96	//{{{ getInstantPopups() method
 97	/**
 98	 * Returns if selecting a value from the popup should immediately fire
 99	 * an ActionEvent.
100	 */
101	public boolean getInstantPopups()
102	{
103		return controller.getInstantPopups();
104	} //}}}
105
106	//{{{ addCurrentToHistory() method
107	/**
108	 * Adds the currently entered item to the history.
109	 */
110	public void addCurrentToHistory()
111	{
112		controller.addCurrentToHistory();
113	} //}}}
114
115	//{{{ setText() method
116	/**
117	 * Sets the displayed text.
118	 */
119	public void setText(String text)
120	{
121		super.setText(text);
122		controller.setIndex(-1);
123	} //}}}
124
125	//{{{ Protected members
126
127	//{{{ processKeyEvent() method
128	protected void processKeyEvent(KeyEvent evt)
129	{
130		if(!isEnabled())
131			return;
132
133		if(evt.getID() == KeyEvent.KEY_PRESSED)
134		{
135			switch(evt.getKeyCode())
136			{
137			case KeyEvent.VK_ENTER:
138				if(evt.isControlDown())
139				{
140					replaceSelection("\n");
141					evt.consume();
142				}
143				break;
144			case KeyEvent.VK_TAB:
145				if(evt.isControlDown())
146				{
147					replaceSelection("\t");
148					evt.consume();
149				}
150				break;
151			case KeyEvent.VK_PAGE_UP:
152				if(evt.isShiftDown())
153					controller.doBackwardSearch();
154				else
155					controller.historyPrevious();
156				evt.consume();
157				break;
158			case KeyEvent.VK_PAGE_DOWN:
159				if(evt.isShiftDown())
160					controller.doForwardSearch();
161				else
162					controller.historyNext();
163				evt.consume();
164				break;
165			case KeyEvent.VK_UP:
166				if(evt.isAltDown())
167				{
168					controller.showPopupMenu(
169						evt.isShiftDown());
170					evt.consume();
171				}
172				break;
173			}
174		}
175
176		if(!evt.isConsumed())
177			super.processKeyEvent(evt);
178	} //}}}
179
180	//{{{ processMouseEvent() method
181	protected void processMouseEvent(MouseEvent evt)
182	{
183		if(!isEnabled())
184			return;
185
186		switch(evt.getID())
187		{
188		case MouseEvent.MOUSE_PRESSED:
189			if(GUIUtilities.isPopupTrigger(evt))
190				controller.showPopupMenu(evt.isShiftDown());
191			else
192				super.processMouseEvent(evt);
193
194			break;
195		default:
196			super.processMouseEvent(evt);
197			break;
198		}
199	} //}}}
200	
201	//}}}
202
203	//{{{ Private variables
204	private HistoryText controller;
205	//}}}
206}