PageRenderTime 38ms CodeModel.GetById 29ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/bundles/plugins-trunk/CommonControls/common/gui/HistoryComboBox.java

#
Java | 105 lines | 66 code | 11 blank | 28 comment | 11 complexity | a8898014f87808391862a8ccb6e27c84 MD5 | raw file
  1package common.gui;
  2
  3import java.awt.event.*;
  4import javax.swing.*;
  5import java.util.*;
  6
  7import org.gjt.sp.jedit.*;
  8import org.gjt.sp.util.*;
  9
 10/**
 11 * A Combo Box that remembers a given number of previous entries, even between jEdit runs.
 12 *
 13 * @author    mace
 14 * @version   $Revision: 1347 $ modified $Date: 2004-02-09 03:49:01 +0100 (Mon, 09 Feb 2004) $ by $Author: bemace $
 15 */
 16public class HistoryComboBox extends JComboBox {
 17	private Stack entries;
 18	private int entryCount;
 19	private DefaultComboBoxModel model;
 20	private String propertyName;
 21
 22	/**
 23	 * Build a combo box which will remember the given the given number
 24	 * of previous entries, and store them in the given property.
 25	 *
 26	 * @param entryCount  Description of the Parameter
 27	 * @param propName    Description of the Parameter
 28	 */
 29	public HistoryComboBox(int entryCount, String propName) {
 30		propertyName = propName;
 31		setEditable(true);
 32		model = new DefaultComboBoxModel();
 33		setModel(model);
 34		this.entryCount = entryCount;
 35		entries = new Stack();
 36		if (propertyName != null)
 37			loadFromProperty(propertyName);
 38	}
 39
 40	/**
 41	 * Creates a combo box that remembers the given number of entries
 42	 * but doesn't not remember them between sessions.
 43	 */
 44	public HistoryComboBox(int entryCount) {
 45		this(entryCount, null);
 46	}
 47
 48	protected void addEntry() {
 49		model.addElement(getSelectedItem().toString());
 50		if (model.getSize() > entryCount) {
 51			model.removeElementAt(0);
 52		}
 53		setSelectedItem("");
 54		repaint();
 55	}
 56
 57	public void processKeyEvent(KeyEvent e) {
 58		super.processKeyEvent(e);
 59		if (e.getKeyCode() == KeyEvent.VK_ENTER) {
 60			fireActionEvent();
 61		}
 62	}
 63
 64	public void actionPerformed(ActionEvent e) {
 65		super.actionPerformed(e);
 66		addEntry();
 67		if (propertyName != null)
 68			storeToProperty(propertyName);
 69	}
 70
 71	/**
 72	 * Stores this box's elements in the given property.
 73	 *
 74	 * @param name  Description of the Parameter
 75	 */
 76	protected void storeToProperty(String name) {
 77		int i = 1;
 78		while (i < getItemCount()) {
 79			//Log.log(Log.DEBUG,this,"Setting '"+name+"."+i+"' to '"+getItemAt(i)+"'");
 80			jEdit.setProperty(name + "." + i, getItemAt(i).toString());
 81			i++;
 82		}
 83		jEdit.unsetProperty(name + "." + i);
 84	}
 85
 86	/**
 87	 * Loads elements into this box from the given property.
 88	 *
 89	 * @param name  Description of the Parameter
 90	 */
 91	protected void loadFromProperty(String name) {
 92		removeAllItems();
 93		int i = 1;
 94		String query = jEdit.getProperty(name + "." + i);
 95		while (query != null) {
 96			if (!query.equals("")) {
 97				addItem(query);
 98			}
 99			query = jEdit.getProperty(name + "." + i);
100			i++;
101		}
102		setSelectedItem("");
103	}
104}
105