/bundles/plugins-trunk/CommonControls/common/gui/HistoryComboBox.java
Java | 105 lines | 66 code | 11 blank | 28 comment | 11 complexity | a8898014f87808391862a8ccb6e27c84 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
- package common.gui;
- import java.awt.event.*;
- import javax.swing.*;
- import java.util.*;
- import org.gjt.sp.jedit.*;
- import org.gjt.sp.util.*;
- /**
- * A Combo Box that remembers a given number of previous entries, even between jEdit runs.
- *
- * @author mace
- * @version $Revision: 1347 $ modified $Date: 2004-02-09 03:49:01 +0100 (Mon, 09 Feb 2004) $ by $Author: bemace $
- */
- public class HistoryComboBox extends JComboBox {
- private Stack entries;
- private int entryCount;
- private DefaultComboBoxModel model;
- private String propertyName;
- /**
- * Build a combo box which will remember the given the given number
- * of previous entries, and store them in the given property.
- *
- * @param entryCount Description of the Parameter
- * @param propName Description of the Parameter
- */
- public HistoryComboBox(int entryCount, String propName) {
- propertyName = propName;
- setEditable(true);
- model = new DefaultComboBoxModel();
- setModel(model);
- this.entryCount = entryCount;
- entries = new Stack();
- if (propertyName != null)
- loadFromProperty(propertyName);
- }
- /**
- * Creates a combo box that remembers the given number of entries
- * but doesn't not remember them between sessions.
- */
- public HistoryComboBox(int entryCount) {
- this(entryCount, null);
- }
- protected void addEntry() {
- model.addElement(getSelectedItem().toString());
- if (model.getSize() > entryCount) {
- model.removeElementAt(0);
- }
- setSelectedItem("");
- repaint();
- }
- public void processKeyEvent(KeyEvent e) {
- super.processKeyEvent(e);
- if (e.getKeyCode() == KeyEvent.VK_ENTER) {
- fireActionEvent();
- }
- }
- public void actionPerformed(ActionEvent e) {
- super.actionPerformed(e);
- addEntry();
- if (propertyName != null)
- storeToProperty(propertyName);
- }
- /**
- * Stores this box's elements in the given property.
- *
- * @param name Description of the Parameter
- */
- protected void storeToProperty(String name) {
- int i = 1;
- while (i < getItemCount()) {
- //Log.log(Log.DEBUG,this,"Setting '"+name+"."+i+"' to '"+getItemAt(i)+"'");
- jEdit.setProperty(name + "." + i, getItemAt(i).toString());
- i++;
- }
- jEdit.unsetProperty(name + "." + i);
- }
- /**
- * Loads elements into this box from the given property.
- *
- * @param name Description of the Parameter
- */
- protected void loadFromProperty(String name) {
- removeAllItems();
- int i = 1;
- String query = jEdit.getProperty(name + "." + i);
- while (query != null) {
- if (!query.equals("")) {
- addItem(query);
- }
- query = jEdit.getProperty(name + "." + i);
- i++;
- }
- setSelectedItem("");
- }
- }