HistoryComboBox.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 java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.Vector;
import javax.help.CSH;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.event.ListDataEvent;
import javax.swing.text.JTextComponent;
import org.apache.log4j.Logger;
import org.xnap.XNap;
import org.xnap.gui.action.ClearHistoriesAction;
import org.xnap.gui.action.Clearable;
import org.xnap.gui.event.PopupListener;
import org.xnap.gui.menu.TextFieldMenu;
import org.xnap.gui.util.IconHelper;
import org.xnap.util.Preferences;
import org.xnap.util.PreferencesProvider;
/**
* Provides a <code>JComboBox</code> with auto completion and a history.
*/
public class HistoryComboBox extends JComboBox implements Clearable, Completable
{
//--- Constant(s) ---
//--- Data field(s) ---
private static Logger logger = Logger.getLogger(HistoryComboBox.class);
private boolean itemsOnly;
private JTextField jtf;
private TextFieldMenu menu;
/**
* Holds the current completion mode.
*/
private CompletionMode mode = null;
//--- Constructor(s) ---
/**
* Constructs a new HistoryComboBox with a history and specified width.
*/
public HistoryComboBox(Vector history, int columns)
{
super(history);
initialize(columns);
}
/**
* Constructs a new HistoryComboBox that takes it's items from an existing
* ComboBoxModel.
*/
public HistoryComboBox(ComboBoxModel model, int columns)
{
super(model);
initialize(columns);
}
/**
* Constructs a new HistoryComboBox with specified width.
*/
public HistoryComboBox(int columns)
{
initialize(columns);
}
public Object[] getHistoryItems()
{
int count = getItemCount();
Object[] array = new Object[count];
for (int i = 0; (i < array.length) && (i < getItemCount()); i++) {
array[i] = getItemAt(i);
}
return array;
}
public boolean isItemSelected()
{
return isItem(jtf.getText());
}
public void setItemsOnly(boolean newValue)
{
this.itemsOnly = newValue;
}
public boolean getItemsOnly()
{
return itemsOnly;
}
public Dimension getMaximumSize()
{
return jtf.getMaximumSize();
}
public Dimension getMinimumSize()
{
return jtf.getMinimumSize();
}
public Dimension getPreferredSize()
{
return jtf.getPreferredSize();
}
/**
* Returns the selected object if autoComplete is true or editable is
* false.
* Otherwise you only get a string.
* @return the selected object (if autoComplete is true or editable is
* false).
*/
public Object getSelectedItem()
{
ComboBoxModel boxModel = getModel();
Object item = boxModel.getSelectedItem();
if (item != null) {
if (item instanceof String) {
Object o = getItem((String)item);
if (o != null) {
return o;
}
else {
return jtf.getText();
}
}
else {
return item;
}
}
else {
return jtf.getText();
}
}
public void setText(String newValue)
{
jtf.setText(newValue);
}
public String getText()
{
return jtf.getText();
}
public JTextComponent getTextComponent()
{
return jtf;
}
public JTextField getTextField()
{
return jtf;
}
public void setCompletionModel(CompletionModel model)
{
mode.setModel(model);
}
public CompletionModel getCompletionModel()
{
return mode.getModel();
}
public CompletionMode getCompletionMode()
{
return mode;
}
public void setPreferences(String prefsKey, PreferencesProvider prefs)
{
menu.setPreferences(prefsKey, prefs);
}
public void setPreferences(String prefsKey)
{
setPreferences(prefsKey, Preferences.getInstance());
}
/**
* Overwritten to insert the new elements in the history into the
* completion model.
*/
public void intervalAdded(ListDataEvent event)
{
super.intervalAdded(event);
for (int i = event.getIndex0(); i <= event.getIndex1(); i++) {
mode.getModel().insert(getModel().getElementAt(i));
}
}
private void setCompletionMode(CompletionMode newMode)
{
if (mode != null) {
if (mode.isEnabled()) {
mode.setEnabled(false);
newMode.setModel(mode.getModel());
mode = newMode;
mode.setEnabled(true);
}
else {
newMode.setModel(mode.getModel());
mode = newMode;
}
}
else {
mode = newMode;
mode.setEnabled(true);
}
}
public void setCompletionMode(String mode)
{
CompletionMode m = CompletionModeFactory.createCompletionMode(mode,
jtf, true);
setCompletionMode(m);
}
/**
* Adds item if not already in list.
*/
public void addDistinctItemAtTop(Object item)
{
for (int i = getItemCount() - 1; i >= 0; i--) {
if (item.toString().equals(getItemAt(i).toString())) {
removeItemAt(i);
}
}
insertItemAt(item, 0);
setSelectedIndex(0);
}
/**
* Implements {@link Clearable} interface.
*/
public void clear()
{
removeAllItems();
}
/**
* Returns the first object which corresponds to name.
* @param name The name of the object to return.
* @return the object whose name is name.
*/
private Object getItem(String name)
{
ComboBoxModel boxModel = getModel();
for (int i = 0; i < boxModel.getSize(); i++) {
// shouldn't it be toString().equals(name) ??
if (boxModel.getElementAt(i).equals(name)) {
return boxModel.getElementAt(i);
}
}
return null;
}
private boolean isItem(String name)
{
return getItem(name) != null;
}
//--- Method(s) ---
private void initialize(int columns)
{
jtf = (JTextField)getEditor().getEditorComponent();
jtf.setColumns(columns);
jtf.addFocusListener(new FocusHandler());
setEditable(true);
// register standard help text for component
CSH.setHelpIDString(this, "historycombobox");
// popup menu
menu = new TextFieldMenu(this, null);
menu.addSeparator();
menu.add(new XNapMenuItem(new ClearHistoryAction()));
jtf.addMouseListener(new PopupListener(menu));
// clear all histories action
ClearHistoriesAction.add(this);
for (int i = 0; i < getModel().getSize(); i++) {
mode.getModel().insert(getModel().getElementAt(i));
}
}
private boolean selectFirstMatchingItem(String string)
{
ComboBoxModel boxModel = getModel();
int matchingItemIndex = -1;
string = string.toLowerCase();
for (int i = 0; i < boxModel.getSize(); i++) {
if (boxModel.getElementAt(i).toString().toLowerCase().
startsWith(string)) {
matchingItemIndex = i;
break;
}
}
//logger.debug(string + ":" + 1);
if (matchingItemIndex != -1) {
/* Each selection is being cached, therefore the string in the
editor won't be updated when selecting the same item twice. */
setSelectedItem(null);
setSelectedIndex(matchingItemIndex);
return true;
}
else {
return false;
}
}
/**
* Make sure a valid item is selected when focus is lost.
*/
public class FocusHandler extends FocusAdapter
{
public void focusGained(FocusEvent e)
{
// jtf.setCaretPosition(jtf.getText().length());
// jtf.moveCaretPosition(0);
}
public void focusLost(FocusEvent e)
{
int caretPos = jtf.getCaretPosition();
if (getItemsOnly() && !isItemSelected()) {
HistoryComboBox.this.requestFocus();
selectFirstMatchingItem(jtf.getText());
jtf.setCaretPosition(jtf.getText().length());
jtf.moveCaretPosition(caretPos);
}
}
}
private class ClearHistoryAction extends AbstractAction
{
public ClearHistoryAction()
{
putValue(Action.NAME, XNap.tr("Clear History"));
putValue(IconHelper.XNAP_ICON, "eraser.png");
}
public void actionPerformed(ActionEvent event)
{
clear();
}
}
}
The table below shows all metrics for HistoryComboBox.java.




