/* * 12/22/2008 * VariableCompletion.java - A completion for a variable. * This library is distributed under a modified BSD license. See the included * AutoComplete.License.txt file for details. */ package org.fife.ui.autocomplete; import javax.swing.text.JTextComponent; /** * A completion for a variable (or constant) in a programming language. *
*
* This completion type uses its shortDescription property as part of
* its summary returned by {@link #getSummary()}; for this reason, it may be a
* little longer (even much longer), if desired, than what is recommended for
* BasicCompletions (where the shortDescription is used in
* {@link #toString()} for ListCellRenderers).
*
* @author Robert Futrell
* @version 1.0
*/
public class VariableCompletion extends BasicCompletion {
/**
* The variable's type.
*/
private String type;
/**
* What library (for example) this variable is defined in.
*/
private String definedIn;
/**
* Constructor.
*
* @param provider The parent provider.
* @param name The name of this variable.
* @param type The type of this variable (e.g. "int",
* "String", etc.).
*/
public VariableCompletion(CompletionProvider provider, String name, String type) {
super(provider, name);
this.type = type;
}
protected void addDefinitionString(StringBuilder sb) {
sb.append("").append(getDefinitionString()).append("");
}
public String getDefinitionString() {
StringBuilder sb = new StringBuilder();
// Add the return type if applicable (C macros like NULL have no type).
if (type != null) {
sb.append(type).append(' ');
}
// Add the item being described's name.
sb.append(getName());
return sb.toString();
}
/**
* Returns where this variable is defined.
*
* @return Where this variable is defined.
* @see #setDefinedIn(String)
*/
public String getDefinedIn() {
return definedIn;
}
/**
* Returns the name of this variable.
*
* @return The name.
*/
public String getName() {
return getReplacementText();
}
/**
* {@inheritDoc}
*/
@Override
public String getSummary() {
StringBuilder sb = new StringBuilder();
addDefinitionString(sb);
possiblyAddDescription(sb);
possiblyAddDefinedIn(sb);
return sb.toString();
}
/**
* Returns the tool tip text to display for mouse hovers over this completion.
*
*
* Note that for this functionality to be enabled, a JTextComponent
* must be registered with the ToolTipManager, and the text component
* must know to search for this value. In the case of an
* RSyntaxTextArea, this can
* be done with a org.fife.ui.rtextarea.ToolTipSupplier that calls into
* {@link CompletionProvider#getCompletionsAt(JTextComponent, java.awt.Point)}.
*
* @return The tool tip text for this completion, or null if none.
*/
@Override
public String getToolTipText() {
return getDefinitionString();
}
/**
* Returns the type of this variable.
*
* @return The type.
*/
public String getType() {
return type;
}
/**
* Adds some HTML describing where this variable is defined, if this information
* is known.
*
* @param sb The buffer to append to.
*/
protected void possiblyAddDefinedIn(StringBuilder sb) {
if (definedIn != null) {
sb.append("