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 addCurrentTextToHistory() {
90 final String item = getText();
91 myModel.addElement(item);
94 public void selectText() {
95 getTextEditor().selectAll();
98 public JTextField getTextEditor() {
99 return (JTextField)getEditor().getEditorComponent();
103 public void setPopupVisible(boolean v) {
105 final FileTextField fileTextField = (FileTextField)getTextEditor().getClientProperty(FileTextField.KEY);
106 // don't allow showing combobox popup when file completion popup is displayed (IDEA-68711)
107 if (fileTextField != null && fileTextField.isPopupDisplayed()) {
111 super.setPopupVisible(v);
114 public class MyModel extends AbstractListModel implements ComboBoxModel{
115 private List<String> myFullList = new ArrayList<String>();
117 private Object mySelectedItem;
119 public Object getElementAt(int index) {
120 return myFullList.get(index);
123 public int getSize() {
124 return Math.min(myHistorySize == -1 ? Integer.MAX_VALUE : myHistorySize, myFullList.size());
127 public void addElement(Object obj) {
128 String newItem = ((String)obj).trim();
130 if (0 == newItem.length()) {
134 if (!contains(newItem)) {
135 // set newly added item as selected.
136 // otherwise current selection will be set to editor
137 mySelectedItem = newItem;
138 insertElementAt(newItem, 0);
142 public void insertElementAt(Object obj, int index) {
143 myFullList.add(index, (String)obj);
144 fireContentsChanged();
147 public Object getSelectedItem() {
148 return mySelectedItem;
151 public void setSelectedItem(Object anItem) {
152 mySelectedItem = anItem;
153 fireContentsChanged();
156 public void fireContentsChanged() {
157 fireContentsChanged(this, -1, -1);
160 public boolean contains(String aNewValue) {
161 return myFullList.contains(aNewValue);
164 public void setItems(List<String> aList) {
165 myFullList = new ArrayList<String>(aList);
166 fireContentsChanged();
170 protected static class TextFieldWithProcessing extends JTextField {
171 public void processKeyEvent(KeyEvent e) {
172 super.processKeyEvent(e);