PageRenderTime 61ms CodeModel.GetById 46ms app.highlight 12ms RepoModel.GetById 0ms app.codeStats 1ms

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

#
Java | 352 lines | 230 code | 38 blank | 84 comment | 47 complexity | 42b65a760404b20e16a933538b7e711f MD5 | raw file
  1/*
  2 * HistoryText.java - Common code for text components 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.text.*;
 28import javax.swing.event.MouseInputAdapter;
 29import java.awt.*;
 30import java.awt.event.*;
 31import org.gjt.sp.jedit.*;
 32//}}}
 33
 34/**
 35 * Text area with a history.
 36 * @author Slava Pestov
 37 * @version $Id: HistoryText.java 5148 2004-11-16 03:47:37Z spestov $
 38 */
 39public class HistoryText
 40{
 41	//{{{ HistoryText constructor
 42	public HistoryText(JTextComponent text, String name)
 43	{
 44		this.text = text;
 45		setModel(name);
 46		index = -1;
 47	} //}}}
 48
 49	//{{{ fireActionPerformed() method
 50	public void fireActionPerformed()
 51	{
 52	} //}}}
 53
 54	//{{{ getIndex() mehtod
 55	public int getIndex()
 56	{
 57		return index;
 58	} //}}}
 59
 60	//{{{ setIndex() mehtod
 61	public void setIndex(int index)
 62	{
 63		this.index = index;
 64	} //}}}
 65
 66	//{{{ getModel() method
 67	/**
 68	 * Returns the underlying history controller.
 69	 * @since jEdit 4.3pre1
 70	 */
 71	public HistoryModel getModel()
 72	{
 73		return historyModel;
 74	} //}}}
 75
 76	//{{{ setModel() method
 77	/**
 78	 * Sets the history list controller.
 79	 * @param name The model name
 80	 * @since jEdit 4.3pre1
 81	 */
 82	public void setModel(String name)
 83	{
 84		if(name == null)
 85			historyModel = null;
 86		else
 87			historyModel = HistoryModel.getModel(name);
 88		index = -1;
 89	} //}}}
 90
 91	//{{{ setInstantPopups() method
 92	/**
 93	 * Sets if selecting a value from the popup should immediately fire
 94	 * an ActionEvent.
 95	 */
 96	public void setInstantPopups(boolean instantPopups)
 97	{
 98		this.instantPopups = instantPopups;
 99	} //}}}
100
101	//{{{ getInstantPopups() method
102	/**
103	 * Returns if selecting a value from the popup should immediately fire
104	 * an ActionEvent.
105	 */
106	public boolean getInstantPopups()
107	{
108		return instantPopups;
109	} //}}}
110
111	//{{{ addCurrentToHistory() method
112	/**
113	 * Adds the currently entered item to the history.
114	 */
115	public void addCurrentToHistory()
116	{
117		if(historyModel != null)
118			historyModel.addItem(getText());
119		index = 0;
120	} //}}}
121
122	//{{{ doBackwardSearch() method
123	public void doBackwardSearch()
124	{
125		if(historyModel == null)
126			return;
127
128		if(text.getSelectionEnd() != getDocument().getLength())
129		{
130			text.setCaretPosition(getDocument().getLength());
131		}
132
133		int start = getInputStart();
134		String t = getText().substring(0,
135			text.getSelectionStart() - start);
136		if(t == null)
137		{
138			historyPrevious();
139			return;
140		}
141
142		for(int i = index + 1; i < historyModel.getSize(); i++)
143		{
144			String item = historyModel.getItem(i);
145			if(item.startsWith(t))
146			{
147				text.replaceSelection(item.substring(t.length()));
148				text.select(getInputStart() + t.length(),
149					getDocument().getLength());
150				index = i;
151				return;
152			}
153		}
154
155		text.getToolkit().beep();
156	} //}}}
157
158	//{{{ doForwardSearch() method
159	public void doForwardSearch()
160	{
161		if(historyModel == null)
162			return;
163
164		if(text.getSelectionEnd() != getDocument().getLength())
165		{
166			text.setCaretPosition(getDocument().getLength());
167		}
168
169		int start = getInputStart();
170		String t = getText().substring(0,
171			text.getSelectionStart() - start);
172		if(t == null)
173		{
174			historyNext();
175			return;
176		}
177
178		for(int i = index - 1; i >= 0; i--)
179		{
180			String item = historyModel.getItem(i);
181			if(item.startsWith(t))
182			{
183				text.replaceSelection(item.substring(t.length()));
184				text.select(getInputStart() + t.length(),
185					getDocument().getLength());
186				index = i;
187				return;
188			}
189		}
190
191		text.getToolkit().beep();
192	} //}}}
193
194	//{{{ historyPrevious() method
195	public void historyPrevious()
196	{
197		if(historyModel == null)
198			return;
199
200		if(index == historyModel.getSize() - 1)
201			text.getToolkit().beep();
202		else if(index == -1)
203		{
204			current = getText();
205			setText(historyModel.getItem(0));
206			index = 0;
207		}
208		else
209		{
210			// have to do this because setText() sets index to -1
211			int newIndex = index + 1;
212			setText(historyModel.getItem(newIndex));
213			index = newIndex;
214		}
215	} //}}}
216
217	//{{{ historyNext() method
218	public void historyNext()
219	{
220		if(historyModel == null)
221			return;
222
223		if(index == -1)
224			text.getToolkit().beep();
225		else if(index == 0)
226			setText(current);
227		else
228		{
229			// have to do this because setText() sets index to -1
230			int newIndex = index - 1;
231			setText(historyModel.getItem(newIndex));
232			index = newIndex;
233		}
234	} //}}}
235	
236	//{{{ getDocument() method
237	public Document getDocument()
238	{
239		return text.getDocument();
240	} //}}}
241	
242	//{{{ getText() method
243	/**
244	 * Subclasses can override this to provide funky history behavior,
245	 * for JTextPanes and such.
246	 */
247	public String getText()
248	{
249		return text.getText();
250	} //}}}
251	
252	//{{{ setText() method
253	/**
254	 * Subclasses can override this to provide funky history behavior,
255	 * for JTextPanes and such.
256	 */
257	public void setText(String text)
258	{
259		this.index = -1;
260		this.text.setText(text);
261	} //}}}
262
263	//{{{ getInputStart() method
264	/**
265	 * Subclasses can override this to provide funky history behavior,
266	 * for JTextPanes and such.
267	 */
268	public int getInputStart()
269	{
270		return 0;
271	} //}}}
272
273	//{{{ showPopupMenu() method
274	public void showPopupMenu(String t, int x, int y)
275	{
276		if(historyModel == null)
277			return;
278
279		text.requestFocus();
280
281		if(popup != null && popup.isVisible())
282		{
283			popup.setVisible(false);
284			return;
285		}
286
287		popup = new JPopupMenu();
288		JMenuItem caption = new JMenuItem(jEdit.getProperty(
289			"history.caption"));
290		caption.getModel().setEnabled(false);
291 		popup.add(caption);
292 		popup.addSeparator();
293
294		for(int i = 0; i < historyModel.getSize(); i++)
295		{
296			String item = historyModel.getItem(i);
297			if(item.startsWith(t))
298			{
299				JMenuItem menuItem = new JMenuItem(item);
300				menuItem.setActionCommand(String.valueOf(i));
301				menuItem.addActionListener(
302					new ActionHandler());
303				popup.add(menuItem);
304			}
305		}
306
307		GUIUtilities.showPopupMenu(popup,text,x,y,false);
308	} //}}}
309
310	//{{{ showPopupMenu() method
311	public void showPopupMenu(boolean search)
312	{
313		if(search)
314			showPopupMenu(getText().substring(getInputStart(),
315				text.getSelectionStart()),0,text.getHeight());
316		else
317			showPopupMenu("",0,text.getHeight());
318	} //}}}
319
320	//{{{ Private members
321	private JTextComponent text;
322	private HistoryModel historyModel;
323	private int index;
324	private String current;
325	private JPopupMenu popup;
326	private boolean instantPopups;
327	//}}}
328
329	//{{{ ActionHandler class
330	class ActionHandler implements ActionListener
331	{
332		public void actionPerformed(ActionEvent evt)
333		{
334			int ind = Integer.parseInt(evt.getActionCommand());
335			if(ind == -1)
336			{
337				if(index != -1)
338					setText(current);
339			}
340			else
341			{
342				setText(historyModel.getItem(ind));
343				index = ind;
344			}
345			if(instantPopups)
346			{
347				addCurrentToHistory();
348				fireActionPerformed();
349			}
350		}
351	} //}}}
352}