2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.ui;
18 import com.intellij.openapi.fileChooser.FileTextField;
19 import com.intellij.openapi.ui.ComboBox;
22 import javax.swing.event.DocumentListener;
23 import java.awt.event.KeyEvent;
24 import java.awt.event.KeyListener;
25 import java.util.ArrayList;
26 import java.util.List;
28 public class TextFieldWithHistory extends ComboBox {
29 private int myHistorySize = 5;
30 private final MyModel myModel;
32 public TextFieldWithHistory() {
33 myModel = new MyModel();
38 // API compatibility with 7.0.1
39 @SuppressWarnings({"UnusedDeclaration"})
40 public TextFieldWithHistory(boolean cropList) {
44 public void addDocumentListener(DocumentListener listener) {
45 getTextEditor().getDocument().addDocumentListener(listener);
48 public void removeDocumentListener(DocumentListener listener) {
49 getTextEditor().getDocument().removeDocumentListener(listener);
52 public void addKeyboardListener(final KeyListener listener) {
53 getTextEditor().addKeyListener(listener);
57 * @param aHistorySize -1 means unbounded
59 public void setHistorySize(int aHistorySize) {
60 myHistorySize = aHistorySize;
63 public void setHistory(List<String> aHistory) {
64 myModel.setItems(aHistory);
67 public List<String> getHistory() {
68 final int itemsCount = myModel.getSize();
69 List<String> history = new ArrayList<String>(itemsCount);
70 for (int i = 0; i < itemsCount; i++) {
71 history.add((String)myModel.getElementAt(i));
76 public void setText(String aText) {
77 getTextEditor().setText(aText);
80 public String getText() {
81 return getTextEditor().getText();
84 public void removeNotify() {
89 public void setTextAndAddToHistory(String text) {
91 addCurrentTextToHistory();
94 public void addCurrentTextToHistory() {
95 final String item = getText();
96 myModel.addElement(item);
99 public void selectText() {
100 getTextEditor().selectAll();
103 public JTextField getTextEditor() {
104 return (JTextField)getEditor().getEditorComponent();
108 public void setPopupVisible(boolean v) {
110 final FileTextField fileTextField = (FileTextField)getTextEditor().getClientProperty(FileTextField.KEY);
111 // don't allow showing combobox popup when file completion popup is displayed (IDEA-68711)
112 if (fileTextField != null && fileTextField.isPopupDisplayed()) {
116 super.setPopupVisible(v);
119 public class MyModel extends AbstractListModel implements ComboBoxModel{
120 private List<String> myFullList = new ArrayList<String>();
122 private Object mySelectedItem;
124 public Object getElementAt(int index) {
125 return myFullList.get(index);
128 public int getSize() {
129 return Math.min(myHistorySize == -1 ? Integer.MAX_VALUE : myHistorySize, myFullList.size());
132 public void addElement(Object obj) {
133 String newItem = ((String)obj).trim();
135 if (0 == newItem.length()) {
139 if (!contains(newItem)) {
140 // set newly added item as selected.
141 // otherwise current selection will be set to editor
142 mySelectedItem = newItem;
143 insertElementAt(newItem, 0);
147 public void insertElementAt(Object obj, int index) {
148 myFullList.add(index, (String)obj);
149 fireContentsChanged();
152 public Object getSelectedItem() {
153 return mySelectedItem;
156 public void setSelectedItem(Object anItem) {
157 mySelectedItem = anItem;
158 fireContentsChanged();
161 public void fireContentsChanged() {
162 fireContentsChanged(this, -1, -1);
165 public boolean contains(String aNewValue) {
166 return myFullList.contains(aNewValue);
169 public void setItems(List<String> aList) {
170 myFullList = new ArrayList<String>(aList);
171 fireContentsChanged();
175 protected static class TextFieldWithProcessing extends JTextField {
176 public void processKeyEvent(KeyEvent e) {
177 super.processKeyEvent(e);