1 /* KeyChoice.java
2  *
3  * created: Mon Sep  6 1999
4  *
5  * This file is part of Artemis
6  *
7  * Copyright (C) 1999  Genome Research Limited
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  *
23  * $Header: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/components/KeyChoice.java,v 1.5 2008-07-21 15:43:16 tjc Exp $
24  */
25 
26 package uk.ac.sanger.artemis.components;
27 
28 import uk.ac.sanger.artemis.Options;
29 import uk.ac.sanger.artemis.io.Key;
30 import uk.ac.sanger.artemis.io.KeyVector;
31 import uk.ac.sanger.artemis.io.EntryInformation;
32 
33 import java.awt.Font;
34 import java.awt.event.ItemListener;
35 
36 import javax.swing.JComboBox;
37 import javax.swing.JPanel;
38 
39 /**
40  *  This component is a Choice component that shows the possible feature keys.
41  *
42  *  @author Kim Rutherford
43  *  @version $Id: KeyChoice.java,v 1.5 2008-07-21 15:43:16 tjc Exp $
44  **/
45 public class KeyChoice extends JPanel
46 {
47   private static final long serialVersionUID = 1L;
48 
49   /** The JComboBox component that will show the feature keys. */
50   private JComboBox key_chooser = null;
51 
52   /**
53    *  Create a new KeyChoice component with CDS as the default key.
54    *  @param entry_information The object to get the list of possible
55    *    keys from.
56    **/
KeyChoice(final EntryInformation entry_information)57   public KeyChoice (final EntryInformation entry_information)
58   {
59     this (entry_information, Key.CDS);
60   }
61 
62   /**
63    *  Create a new KeyChoice component with the given key as the default.
64    *  @param entry_information The object to get the list of possible
65    *    keys from.
66    **/
KeyChoice(final EntryInformation entry_information, final Key default_key)67   public KeyChoice (final EntryInformation entry_information,
68                     final Key default_key)
69   {
70     final Font font = Options.getOptions ().getFont ();
71     setFont (font);
72 
73     key_chooser = new JComboBox ();
74     key_chooser.setEditable(true);
75 
76     final int MAX_VISIBLE_ROWS = 30;
77     key_chooser.setMaximumRowCount (MAX_VISIBLE_ROWS);
78 
79     addChoice (entry_information, default_key);
80   }
81 
82   /**
83    *  Return the currently selected key.
84    **/
getSelectedItem()85   public Key getSelectedItem ()
86   {
87     return new Key ((String) key_chooser.getSelectedItem ());
88   }
89 
90   /**
91    *  Set the selected Key.
92    **/
setKey(final Key new_key)93   public void setKey (final Key new_key)
94   {
95     final int key_index = keyIndex (new_key);
96 
97     if (key_index == -1)
98     {
99       // add the key
100       key_chooser.addItem (new_key.toString ());
101       key_chooser.setSelectedItem (new_key.toString ());
102     }
103     else
104       key_chooser.setSelectedIndex (key_index);
105   }
106 
107   /**
108    *  Adds the specified item listener to receive item events from the Choice
109    *  component of this KeyChoice.
110    *  @param l The item listener.
111    **/
addItemListener(ItemListener l)112   public void addItemListener(ItemListener l)
113   {
114     key_chooser.addItemListener (l);
115   }
116 
117 
118   /**
119    *  Removes the specified item listener so that it no longer receives item
120    *  events from the Choice component of this KeyChoice.
121    *  @param l The item listener.
122    **/
removeItemListener(ItemListener l)123   public void removeItemListener(ItemListener l)
124   {
125     key_chooser.removeItemListener (l);
126   }
127 
128   /**
129    *  Add the key_chooser.
130    **/
addChoice(final EntryInformation entry_information, final Key default_key)131   private void addChoice (final EntryInformation entry_information,
132                           final Key default_key)
133   {
134     final Font font = Options.getOptions ().getFont ();
135     key_chooser.setFont (font);
136 
137     KeyVector keys = entry_information.getSortedValidKeys();
138 
139     if (keys == null)
140     {
141       keys = new KeyVector ();
142       keys.add (Key.CDS);
143     }
144 
145     for (int i = 0 ; i < keys.size () ; ++i)
146       key_chooser.addItem ( ((Key)keys.get(i)).toString ());
147 
148     if (keyIndex (default_key) != -1)
149       setKey (default_key);
150     add (key_chooser);
151   }
152 
153   /**
154    *  Return the index in the key_chooser component of the given Key.
155    **/
keyIndex(final Key key)156   private int keyIndex (final Key key)
157   {
158     for (int i = 0 ; i < key_chooser.getItemCount () ; ++i)
159     {
160       if (key.toString ().equals ((String)key_chooser.getItemAt (i)))
161         return i;
162     }
163     return -1;
164   }
165 
setEnabled(final boolean isEnabled)166   public void setEnabled(final boolean isEnabled)
167   {
168     key_chooser.setEnabled(isEnabled);
169     repaint();
170   }
171 }