PageRenderTime 111ms CodeModel.GetById 99ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 370 lines | 248 code | 38 blank | 84 comment | 48 complexity | 1c2a56498cb5fe2a367000085d98c471 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 * Controller (manager of models) for HistoryTextArea.
 36 * @author Slava Pestov
 37 * @version $Id: HistoryText.java 16341 2009-10-14 10:05:51Z kpouer $
 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			popup = null;
285			return;
286		}
287
288		popup = new JPopupMenu()
289		{
290			@Override
291			public void setVisible(boolean b)
292			{
293				if (!b)
294				{
295					popup = null;
296				}
297				super.setVisible(b);
298			}
299		};
300		JMenuItem caption = new JMenuItem(jEdit.getProperty(
301			"history.caption"));
302		caption.addActionListener(new ActionListener()
303		{
304		  public void actionPerformed(ActionEvent e) 
305		  {
306		    new ListModelEditor().open(historyModel);
307		  }
308		});		
309 		popup.add(caption);
310 		popup.addSeparator();
311
312		for(int i = 0; i < historyModel.getSize(); i++)
313		{
314			String item = historyModel.getItem(i);
315			if(item.startsWith(t))
316			{
317				JMenuItem menuItem = new JMenuItem(item);
318				menuItem.setActionCommand(String.valueOf(i));
319				menuItem.addActionListener(
320					new ActionHandler());
321				popup.add(menuItem);
322			}
323		}
324
325		GUIUtilities.showPopupMenu(popup,text,x,y,false);
326	} //}}}
327
328	//{{{ showPopupMenu() method
329	public void showPopupMenu(boolean search)
330	{
331		if(search)
332			showPopupMenu(getText().substring(getInputStart(),
333				text.getSelectionStart()),0,text.getHeight());
334		else
335			showPopupMenu("",0,text.getHeight());
336	} //}}}
337
338	//{{{ Private members
339	private JTextComponent text;
340	private HistoryModel historyModel;
341	private int index;
342	private String current;
343	private JPopupMenu popup;
344	private boolean instantPopups;
345	//}}}
346
347	//{{{ ActionHandler class
348	class ActionHandler implements ActionListener
349	{
350		public void actionPerformed(ActionEvent evt)
351		{
352			int ind = Integer.parseInt(evt.getActionCommand());
353			if(ind == -1)
354			{
355				if(index != -1)
356					setText(current);
357			}
358			else
359			{
360				setText(historyModel.getItem(ind));
361				index = ind;
362			}
363			if(instantPopups)
364			{
365				addCurrentToHistory();
366				fireActionPerformed();
367			}
368		}
369	} //}}}
370}