PageRenderTime 143ms CodeModel.GetById 136ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/HistoryTextArea.java

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