Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 12/21/2008
   *
   * DefaultCompletionProvider.java - A basic completion provider implementation.
   * Copyright (C) 2008 Robert Futrell
   * robert_futrell at users.sourceforge.net
   * http://fifesoft.com/rsyntaxtextarea
   *
   * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA.
  */
 package org.fife.ui.autocomplete;
 
 import java.awt.Point;
 import java.io.File;
 import java.util.List;
A basic completion provider implementation. This provider has no understanding of language semantics. It simply checks the text entered up to the caret position for a match against known completions. This is all that is needed in the majority of cases.

Author(s):
Robert Futrell
Version:
1.0
 
 
 	protected Segment seg;

 
Used to speed up getCompletionsAt(javax.swing.text.JTextComponent,java.awt.Point), since this may be called multiple times in succession (this is usually called by JTextComponent.getToolTipText(), and if the user wiggles the mouse while a tool tip is displayed, this method gets repeatedly called. It can be costly so we try to speed it up a tad).
 
Constructor. The returned provider will not be aware of any completions.

 
 		init();
 	}


Creates a completion provider that provides completion for a simple list of words.

Parameters:
words The words to offer as completion suggestions. If this is null, no completions will be known.
See also:
BasicCompletion
 
 	public DefaultCompletionProvider(String[] words) {
 		init();
 	}


Returns the text just before the current caret position that could be the start of something auto-completable.

This method returns all characters before the caret that are matched by isValidChar(char).

		Document doc = comp.getDocument();
		int dot = comp.getCaretPosition();
		int index = root.getElementIndex(dot);
		Element elem = root.getElement(index);
		int start = elem.getStartOffset();
		int len = dot-start;
		try {
			doc.getText(startlen);
catch (BadLocationException ble) {
			return ;
		}
		int segEnd = . + len;
		start = segEnd - 1;
		while (start>=. && isValidChar(.[start])) {
			start--;
		}
		start++;
		len = segEnd - start;
		return len==0 ?  : new String(.startlen);
	}


		int offset = tc.viewToModel(p);
		if (offset<0 || offset>=tc.getDocument().getLength()) {
		}
		Segment s = new Segment();
		Document doc = tc.getDocument();
		int line = root.getElementIndex(offset);
		Element elem = root.getElement(line);
		int start = elem.getStartOffset();
		int end = elem.getEndOffset() - 1;
		try {
			doc.getText(startend-starts);
			// Get the valid chars before the specified offset.
			int startOffs = s.offset + (offset-start) - 1;
			while (startOffs>=s.offset && isValidChar(s.array[startOffs])) {
				startOffs--;
			}
			// Get the valid chars at and after the specified offset.
			int endOffs = s.offset + (offset-start);
			while (endOffs<s.offset+s.count && isValidChar(s.array[endOffs])) {
				endOffs++;
			}
			int len = endOffs - startOffs - 1;
			if (len<=0) {
			}
			String text = new String(s.arraystartOffs+1, len);
			}
			// Get a list of all Completions matching the text.
catch (BadLocationException ble) {
			ble.printStackTrace(); // Never happens
		}
	}


		List list = null;
		// If this provider doesn't support parameterized completions,
		// bail out now.
		char paramListStart = getParameterListStart();
		if (paramListStart==0) {
			return list// null
		}
		int dot = tc.getCaretPosition();
		Segment s = new Segment();
		Document doc = tc.getDocument();
		int line = root.getElementIndex(dot);
		Element elem = root.getElement(line);
		int offs = elem.getStartOffset();
		int len = dot - offs - 1/*paramListStart.length()*/;
		if (len<=0) { // Not enough chars on line for a method.
			return list// null
		}
		try {
			doc.getText(offslens);
			// Get the identifier preceding the '(', ignoring any whitespace
			// between them.
			offs = s.offset + len - 1;
			while (offs>=s.offset && Character.isWhitespace(s.array[offs])) {
				offs--;
			}
			int end = offs;
			while (offs>=s.offset && isValidChar(s.array[offs])) {
				offs--;
			}
			String text = new String(s.arrayoffs+1, end-offs);
			// Get a list of all Completions matching the text, but then
			// narrow it down to just the ParameterizedCompletions.
			if (l!=null && !l.isEmpty()) {
				for (int i=0; i<l.size(); i++) {
					Object o = l.get(i);
					if (o instanceof ParameterizedCompletion) {
						if (list==null) {
							list = new ArrayList(1);
						}
						list.add(o);
					}
				}
			}
catch (BadLocationException ble) {
			ble.printStackTrace(); // Never happens
		}
		return list;
	}


Initializes this completion provider.
	protected void init() {
		 = new Segment();
	}


Returns whether the specified character is valid in an auto-completion. The default implementation is equivalent to "Character.isLetterOrDigit(ch) || ch=='_'". Subclasses can override this method to change what characters are matched.

Parameters:
ch The character.
Returns:
Whether the character is valid.
	protected boolean isValidChar(char ch) {
		return Character.isLetterOrDigit(ch) || ch=='_';
	}


Loads completions from an XML file. The XML should validate against the completion XML schema.

Parameters:
file An XML file to load from.
Throws:
java.io.IOException If an IO error occurs.
	public void loadFromXML(File filethrows IOException {
										new FileInputStream(file));
		try {
finally {
			bin.close();
		}
	}


Loads completions from an XML input stream. The XML should validate against the completion XML schema.

Parameters:
in The input stream to read from.
Throws:
java.io.IOException If an IO error occurs.
	public void loadFromXML(InputStream inthrows IOException {
		//long start = System.currentTimeMillis();
		SAXParserFactory factory = SAXParserFactory.newInstance();
		try {
			SAXParser saxParser = factory.newSAXParser();
			saxParser.parse(binhandler);
			List completions =  handler.getCompletions();
			addCompletions(completions);
			char startChar = handler.getParamStartChar();
			if (startChar!=0) {
				char endChar = handler.getParamEndChar();
				String sep = handler.getParamSeparator();
				if (endChar!=0 && sep!=null && sep.length()>0) { // Sanity
					setParameterizedCompletionParams(startCharsependChar);
				}
			}
catch (SAXException se) {
			throw new IOException(se.toString());
catch (ParserConfigurationException pce) {
			throw new IOException(pce.toString());
finally {
			//long time = System.currentTimeMillis() - start;
			//System.out.println("XML loaded in: " + time + "ms");
			bin.close();
		}
	}


Loads completions from an XML file. The XML should validate against the completion XML schema.

Parameters:
resource A resource the current ClassLoader can get to.
Throws:
java.io.IOException If an IO error occurs.
	public void loadFromXML(String resourcethrows IOException {
		InputStream in = cl.getResourceAsStream(resource);
		if (in==null) {
			throw new IOException("No such resource: " + resource);
		}
		try {
finally {
			bin.close();
		}
	}
New to GrepCode? Check out our FAQ X