View Javadoc

1   /*
2    * $Id: JHistoryComboBox.java,v 1.1 2004/04/15 16:37:06 mch Exp $
3    *
4    * Copyright 2003 AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid Software License,
7    * a copy of which has been included with this distribution in the LICENSE.txt file.
8    *
9    */
10  
11  package org.astrogrid.ui;
12  
13  import java.awt.event.ActionEvent;
14  import java.awt.event.ActionListener;
15  import javax.swing.JComboBox;
16  
17  /***
18   *
19   * A history combo box that remembers previous input and allows you to pick
20   * from a list
21   *
22   * @author M Hill
23   */
24  
25  
26  public class JHistoryComboBox extends JComboBox
27  {
28     public JHistoryComboBox()
29     {
30        super();
31        setEditable(true);
32        
33        addActionListener(
34           new ActionListener()
35           {
36              public void actionPerformed(ActionEvent e)
37              {
38                 updateList(getText());
39              }
40           }
41        );
42     }
43  
44     public void setDefaultList(String[] list)
45     {
46        for (int i=0; i<list.length; i++)
47        {
48           addItem(list[i]);
49        }
50     }
51  
52     /*** Checks to see if the input is already in the list, and if not adds it
53      */
54     public void updateList(Object item)
55     {
56        for (int i=0;i<getItemCount();i++) {
57           if (item.equals(getItemAt(i))) {
58              return;
59           }
60        }
61  
62        addItem(item);
63     }
64     
65     /***
66      * Sets entry
67      */
68     public void setItem(Object newEntry)
69     {
70        updateList(newEntry);
71        getEditor().setItem(newEntry);
72     }
73     
74     public String getText()
75     {
76        return getEditor().getItem().toString();
77     }
78     
79  }
80  
81  /*
82  $Log: JHistoryComboBox.java,v $
83  Revision 1.1  2004/04/15 16:37:06  mch
84  More tidying
85  
86  Revision 1.1  2004/03/03 17:40:58  mch
87  Moved ui package
88  
89  Revision 1.1  2004/02/17 16:04:06  mch
90  New Desktop GUI
91  
92  Revision 1.1  2004/02/17 03:53:43  mch
93  Nughtily large number of fixes for demo
94  
95   */
96