HistoryComboBox.java revision 9883:903a2e023ffb
11638Srgrimes/*
250476Speter * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
31638Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4241823Smarcel *
5241823Smarcel * This code is free software; you can redistribute it and/or modify it
674856Sru * under the terms of the GNU General Public License version 2 only, as
71638Srgrimes * published by the Free Software Foundation.
8260013Sjmmv *
9241823Smarcel * This code is distributed in the hope that it will be useful, but WITHOUT
10241823Smarcel * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11241823Smarcel * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12241823Smarcel * version 2 for more details (a copy is included in the LICENSE file that
13241823Smarcel * accompanied this code).
1451090Ssheldonh *
1551090Ssheldonh * You should have received a copy of the GNU General Public License version
16124747Sru * 2 along with this work; if not, write to the Free Software Foundation,
17124747Sru * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18124747Sru *
19100574Stjr * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20124747Sru * or visit www.oracle.com if you need additional information or have any
21124747Sru * questions.
22124747Sru *
23124747Sru */
24124747Sru
25124747Srupackage sun.jvm.hotspot.ui;
26124747Sru
27124747Sru
2860261Ssheldonhimport java.awt.event.*;
29124747Sruimport javax.swing.*;
30124747Sruimport javax.swing.event.*;
31124747Sru
32124747Sru/** Provides an editable text field with history. */
33124747Sru
34124747Srupublic class HistoryComboBox extends JComboBox {
35124747Sru  static final int HISTORY_LENGTH = 15;
36124747Sru
37124747Sru  public HistoryComboBox() {
38124747Sru    setEditable(true);
39124747Sru    addActionListener(new ActionListener() {
40124747Sru        public void actionPerformed(ActionEvent e) {
41124747Sru          Object text = getSelectedItem();
42124747Sru          if (text != null) {
43124747Sru            setText((String)text);
44124747Sru          }
45124747Sru        }
46124747Sru      });
47124747Sru  }
48124747Sru
49124747Sru  public String getText() {
50124747Sru    Object text = getSelectedItem();
51124747Sru    if (text == null) {
52124747Sru      return "";
53124747Sru    }
54124747Sru    return (String)text;
55124747Sru  }
56124747Sru
57124747Sru  public void setText(String text) {
58124747Sru    removeItem(text);
59124747Sru    insertItemAt(text, 0);
60124747Sru    setSelectedItem(text);
61124747Sru    int length = getModel().getSize();
62124747Sru    while (length > HISTORY_LENGTH) {
63124747Sru      removeItemAt(--length);
6460261Ssheldonh    }
65124747Sru  }
66124747Sru}
67124747Sru