PageRenderTime 65ms CodeModel.GetById 58ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/SideKick/sidekick/SideKickCompletion.java

#
Java | 188 lines | 93 code | 16 blank | 79 comment | 4 complexity | 6c008a623e5be0f56abaab67cb622f86 MD5 | raw file
  1/*
  2 * SideKickCompletion.java
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2003, 2005 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 sidekick;
 24
 25//{{{ Imports
 26import javax.swing.*;
 27import java.util.*;
 28import org.gjt.sp.jedit.*;
 29import org.gjt.sp.jedit.buffer.JEditBuffer;
 30import org.gjt.sp.jedit.textarea.*;
 31//}}}
 32
 33/**
 34 * A code completion instance.<p>
 35 *
 36 * This is a wrapper around a collection of possible completions, with callbacks
 37 * for inserting and displaying the completions in a popup menu.
 38 *
 39 * @author Slava Pestov
 40 * @version $Id: SideKickCompletion.java 10427 2007-08-22 22:43:15Z ezust $
 41 * @since SideKick 0.3
 42 */
 43public abstract class SideKickCompletion
 44{
 45	//{{{ SideKickCompletion constructor
 46	/**
 47	 * @deprecated Use the other constructor instead.
 48	 */
 49	public SideKickCompletion() {}
 50	//}}}
 51
 52	//{{{ SideKickCompletion constructor
 53	/**
 54	 * @since SideKick 0.3.2
 55	 */
 56	public SideKickCompletion(View view, String text)
 57	{
 58		this.view = view;
 59		textArea = view.getTextArea();
 60		this.text = text;
 61	} //}}}
 62
 63	//{{{ SideKickCompletion constructor
 64	/**
 65	 * @since SideKick 0.3.2
 66	 * @param items - a list of strings containing the possible completions.
 67	 */
 68	public SideKickCompletion(View view, String text, List items)
 69	{
 70		this(view,text);
 71		this.items = items;
 72	} //}}}
 73
 74	//{{{ SideKickCompletion constructor
 75	/**
 76	 * @since SideKick 0.3.2
 77	 */
 78	public SideKickCompletion(View view, String text, Object[] items)
 79	{
 80		this(view,text);
 81		this.items = Arrays.asList(items);
 82	} //}}}
 83
 84	//{{{ size() method
 85	public int size()
 86	{
 87		return items.size();
 88	} //}}}
 89
 90	//{{{ get() method
 91	public Object get(int index)
 92	{
 93		return items.get(index);
 94	} //}}}
 95
 96	//{{{ getCompletionDescription() method
 97	public String getCompletionDescription(int index)
 98	{
 99		return null;
100	} //}}}
101
102	//{{{ isCompletionSelectable() method
103	/**
104	 * @deprecated
105	 *   Do not return false from this method. Unselectable completion
106	 *   is not useful.
107	 */
108	@Deprecated
109	public boolean isCompletionSelectable(int index)
110	{
111		return true;
112	} //}}}
113
114	//{{{ updateInPlace() method
115	/**
116	 * @return If this returns false, then we create a new completion
117	 * object after user input.
118	 */
119	public boolean updateInPlace(EditPane editPane, int caret)
120	{
121		return false;
122	} //}}}
123	
124	//{{{ getRenderer() method
125	public ListCellRenderer getRenderer()
126	{
127		return new DefaultListCellRenderer();
128	} //}}}
129
130	//{{{ insert() method
131	public void insert(int index)
132	{
133		String selected = String.valueOf(get(index));
134		int caret = textArea.getCaretPosition();
135		Selection s = textArea.getSelectionAtOffset(caret);
136		int start = (s == null ? caret : s.getStart());
137		int end = (s == null ? caret : s.getEnd());
138		JEditBuffer buffer = textArea.getBuffer();
139		try
140		{
141			buffer.beginCompoundEdit();
142			buffer.remove(start - text.length(),text.length());
143			buffer.insert(start - text.length(),selected);
144		}
145		finally
146		{
147			buffer.endCompoundEdit();
148		}
149	} //}}}
150
151	//{{{ getTokenLength() method
152	/**
153	 * The length of the text being completed (popup will be positioned there).
154	 */
155	public int getTokenLength()
156	{
157		return text.length();
158	} //}}}
159
160	//{{{ handleKeystroke() method
161	/**
162	 * @param selectedIndex The index of the selected completion.
163	 * @param keyChar the character typed by the user.
164	 * @return True if completion should continue, false otherwise.
165	 * @since SideKick 0.3.2
166	 */
167	public boolean handleKeystroke(int selectedIndex, char keyChar)
168	{
169		// if(keyChar == '\t' || keyChar == '\n')
170		if(SideKickActions.acceptChars.indexOf(keyChar) > -1)
171		{
172			insert(selectedIndex);
173			if(SideKickActions.insertChars.indexOf(keyChar) > -1)
174				textArea.userInput(keyChar);
175			return false;
176		}
177		else
178		{
179			textArea.userInput(keyChar);
180			return true;
181		}
182	} //}}}
183
184	protected View view;
185	protected JEditTextArea textArea;
186	protected String text;
187	protected List items = new ArrayList();
188}