CompletionMode.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.xnap.gui.component |
![]() |
![]() |
XNap 3 |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* XNap - A P2P framework and client.
*
* See the file AUTHORS for copyright information.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.xnap.gui.component;
import org.apache.log4j.Logger;
import javax.swing.text.BadLocationException;
import javax.swing.text.JTextComponent;
import javax.swing.text.Utilities;
/**
* This class handles the way completion is actually done.
*
* It decides when and how to offer completion to the user. Completion may be
* triggered using a special key, it may be visible all the time, it may use a
* {@link org.xnap.gui.component.CompletionPopup} to present all possibilities
* or may insert the text directly.
*/
public abstract class CompletionMode
{
private static Logger logger = Logger.getLogger(CompletionMode.class);
//--- Constant(s) ---
//--- Data field(s) ---
private CompletionModel model;
private JTextComponent jtc;
private boolean wholeText;
private boolean enabled = false;
//--- Constructor(s) ---
public CompletionMode(JTextComponent textComponent, CompletionModel model,
boolean wholeText)
{
this.model = model;
this.jtc = textComponent;
this.wholeText = wholeText;
}
public CompletionMode(JTextComponent textComponent, boolean wholeText)
{
this(textComponent, new DefaultCompletionModel(), wholeText);
}
public CompletionMode(JTextComponent textComponent, CompletionModel model)
{
this(textComponent, model, true);
}
public CompletionMode(JTextComponent textComponent)
{
this(textComponent, true);
}
//--- Method(s) ---
/**
* Returns the name of this completion mode.
*
* This should be localized since it's shown to the user.
*/
public abstract String getName();
/**
* Enables the completion mode letting it add its listeners to the text
* component.
*/
protected abstract void enable();
/**
* Disables the completion mode letting it remove its listeners from the
* text component.
*/
protected abstract void disable();
/**
* Enables or disables the completion mode.
*
* @param enabled true to enable the completion mode, false to disable it
*/
public final void setEnabled(boolean enabled)
{
this.enabled = enabled;
if (enabled) {
enable();
}
else {
disable();
}
}
/**
* Returns true if the completion mode is enabled.
*/
public final boolean isEnabled()
{
return enabled;
}
/**
* Sets the completion model.
*
* If the completion mode is currently enabled, it will be disabled before
* the model is set and enabled afterwards. Thus subclasses have a chance
* of being notified of the model change in their {@link #disable()}
* {@link #enable()} methods.
*
* @param model the new completion model which will be used henceforth
*/
public void setModel(CompletionModel model)
{
if (isEnabled()) {
disable();
this.model = model;
enable();
}
else {
this.model = model;
}
}
/**
* Returns the currently used model.
*/
public CompletionModel getModel()
{
return model;
}
/**
* Sets a new text component for this completion mode.
*
* If the completion mode is currently enabled {@link disable()} and
* {@link enable()} are called to give listeners a chance to remove
* themselves from the old text component add themselves to the new text
* component.
*/
public void setTextComponent(JTextComponent jtc)
{
if (isEnabled()) {
// remove listeners
disable();
this.jtc = jtc;
// add listeners
enable();
}
else {
this.jtc = jtc;
}
}
/**
* Returns the text component this completion mode is responsible for
*/
public JTextComponent getTextComponent()
{
return jtc;
}
/**
* Returns whether this completion mode is supposed to complete the whole
* text of its text component made availabe through {@link
* JTextComponent#getText()} or just the last word before the cursor.
*/
public final boolean isWholeTextCompletion()
{
return wholeText;
}
/**
* Convenience wrapper for {@link setText(String, int, int)}.
*
* Sets the text without any selection and setts the cursor to the end
* of the set text.
*
* @param text the text to set
*/
protected void setText(String text)
{
setText(text, text.length(), text.length());
}
/**
* Sets the given text honoring the whole text mode.
*
* @param text the text to set
* @param selectionStart the offset of the selection start relative to the
* beginning of the set text. Can be greater than <code>selectionEnd</code>.
* @param selectionEnd the offset of the selection end relative
* to the beginning of the set text. This is where the cursor is afterwards.
*/
protected void setText(String text, int selectionStart,
int selectionEnd)
{
if (isWholeTextCompletion()) {
jtc.setText(text);
jtc.setCaretPosition(selectionStart);
jtc.moveCaretPosition(selectionEnd);
}
else {
int offs = -1;
try {
offs = Utilities.getPreviousWord(jtc, jtc.getCaretPosition());
}
catch (BadLocationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
jtc.moveCaretPosition(offs);
jtc.replaceSelection(text);
jtc.setCaretPosition(offs + selectionStart);
jtc.moveCaretPosition(offs + selectionEnd);
}
}
// TODO
// FIX
private String getPreviousWord()
{
int start = jtc.getCaretPosition() + 1;
// System.out.println("current pos " + start);
try {
start = Utilities.getPreviousWord(jtc, start);
// System.out.println("start " + start);
String ret = jtc.getText(start, jtc.getCaretPosition() - start + 1);
// System.out.println(ret);
return ret;
}
catch (BadLocationException ble) {
return "";
}
}
/**
* Returns the text which should be completed.
*
* @return Returns {@link JTextComponent#getText()} if wholeText is true
* otherwise the word before the cursor.
*/
protected String getText()
{
if (wholeText) {
return jtc.getText();
}
else {
return getPreviousWord();
}
}
}
The table below shows all metrics for CompletionMode.java.




