001: /*
002: * HistoryTextField.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.gui.components;
013:
014: import java.util.ArrayList;
015: import java.util.List;
016: import javax.swing.DefaultComboBoxModel;
017: import javax.swing.JComboBox;
018: import workbench.interfaces.PropertyStorage;
019: import workbench.resource.Settings;
020: import workbench.util.StringUtil;
021:
022: /**
023: * @author support@sql-workbench.net
024: */
025: public class HistoryTextField extends JComboBox {
026: private String propName;
027: private List<String> historyValues = new ArrayList<String>();
028: private int maxHistorySize = 25;
029:
030: public HistoryTextField(String prop) {
031: super ();
032: setEditable(true);
033: this .propName = prop;
034: this .maxHistorySize = Settings.getInstance().getIntProperty(
035: "workbench.history." + propName + ".size", 25);
036: }
037:
038: public String getText() {
039: Object item = getSelectedItem();
040: if (item == null)
041: item = getEditor().getItem();
042: if (item == null)
043: return "";
044: return (String) item;
045: }
046:
047: public void setText(String s) {
048: // this.getEditor().setItem(s);
049: this .setSelectedItem(s);
050: }
051:
052: public void saveSettings(PropertyStorage props, String prefix) {
053: props.setProperty(prefix + propName + ".history", StringUtil
054: .listToString(historyValues, ';', true));
055: props.setProperty(prefix + propName + ".lastvalue", this
056: .getText());
057: }
058:
059: public void restoreSettings(PropertyStorage props, String prefix) {
060: String s = props
061: .getProperty(prefix + propName + ".history", "");
062: List<String> l = StringUtil.stringToList(s, ";", true, true);
063: this .setText("");
064: this .historyValues.clear();
065: this .historyValues.addAll(l);
066: this .updateModel();
067: String lastValue = props.getProperty(prefix + propName
068: + ".lastvalue", null);
069:
070: if (lastValue != null)
071: this .setText(lastValue);
072: }
073:
074: public void restoreSettings() {
075: restoreSettings(Settings.getInstance(),
076: "workbench.quickfilter." + propName + ".");
077: }
078:
079: public void saveSettings() {
080: saveSettings(Settings.getInstance(), "workbench.quickfilter."
081: + propName + ".");
082: }
083:
084: public void addToHistory(String s) {
085: if (StringUtil.isEmptyString(s))
086: return;
087: s = s.trim();
088: Object item = getSelectedItem();
089: int index = historyValues.indexOf(s);
090: if (index > -1) {
091: this .historyValues.remove(index);
092: } else {
093: while (this .historyValues.size() >= this .maxHistorySize) {
094: this .historyValues.remove(historyValues.size() - 1);
095: }
096: }
097: this .historyValues.add(0, s);
098: this .updateModel();
099: setSelectedItem(item);
100: }
101:
102: private void updateModel() {
103: DefaultComboBoxModel model = new DefaultComboBoxModel(
104: this.historyValues.toArray());
105: setModel(model);
106: }
107: }
|