Skip to content
Snippets Groups Projects
QualifierChoice.java 5.15 KiB
Newer Older
  • Learn to ignore specific revisions
  • tjc's avatar
    tjc committed
    /* QualifierChoice.java
     *
     * created: Tue Sep  7 1999
     *
     * This file is part of Artemis
     *
     * Copyright (C) 1999  Genome Research Limited
     *
     * This program is free software; you can redistribute it and/or
     * modify it under the terms of the GNU General Public License
     * as published by the Free Software Foundation; either version 2
     * of the License, or (at your option) any later version.
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with this program; if not, write to the Free Software
     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
     *
    
    tjc's avatar
    tjc committed
     * $Header: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/components/QualifierChoice.java,v 1.6 2008-01-14 10:05:50 tjc Exp $
    
    tjc's avatar
    tjc committed
     */
    
    package uk.ac.sanger.artemis.components;
    
    
    import java.awt.Dimension;
    import java.awt.Toolkit;
    
    tjc's avatar
    tjc committed
    
    
    import uk.ac.sanger.artemis.Options;
    
    tjc's avatar
    tjc committed
    import uk.ac.sanger.artemis.util.StringVector;
    
    tjc's avatar
    tjc committed
    import uk.ac.sanger.artemis.io.GFFEntryInformation;
    
    tjc's avatar
    tjc committed
    import uk.ac.sanger.artemis.io.Key;
    import uk.ac.sanger.artemis.io.EntryInformation;
    
    
    
    import javax.swing.JComboBox;
    
    
    tjc's avatar
    tjc committed
    
    /**
     *  This is a Choice component that shows only the qualifier names for a given
     *  key.
     *
     *  @author Kim Rutherford
    
    tjc's avatar
    tjc committed
     *  @version $Id: QualifierChoice.java,v 1.6 2008-01-14 10:05:50 tjc Exp $
    
    tjc's avatar
    tjc committed
     **/
    
    
    public class QualifierChoice extends JComboBox 
    {
      /** */
      private static final long serialVersionUID = 1L;
    
      /** The Key that was passed to the constructor. */
      private Key key = null;
    
      /** The qualifier name that was passed to the constructor. */
      private String default_qualifier = null;
    
      /** The EntryInformation object that was passed to the constructor. */
      private EntryInformation entry_information;
    
    tjc's avatar
    tjc committed
      
      private boolean isGFF;
    
    tjc's avatar
    tjc committed
      /**
       *  Create a new QualifierChoice component for the given Key with the given
       *  qualifier as the default.
       *  @param entry_information The object to get the list of possible
       *    qualifiers from.
       *  @param default_qualifier The name of the Qualifier that should be shown
       *    initially.  If null the first (alphabetically) is selected.
       **/
      public QualifierChoice (final EntryInformation entry_information,
    
    tjc's avatar
    tjc committed
                              final Key key, final String default_qualifier,
                              final boolean isGFF) 
    
    tjc's avatar
    tjc committed
        this.entry_information = entry_information;
        this.key = key;
    
    tjc's avatar
    tjc committed
        this.isGFF = isGFF;
    
    tjc's avatar
    tjc committed
    
        if (default_qualifier != null &&
    
            entry_information.isValidQualifier (key, default_qualifier))
    
    tjc's avatar
    tjc committed
          this.default_qualifier = default_qualifier;
    
        else
    
    tjc's avatar
    tjc committed
          this.default_qualifier = null;
    
    
    tjc's avatar
    tjc committed
        final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int screen_height = screen.height;
    
        final int MAX_VISIBLE_ROWS;
        if(screen_height < 1024)
          MAX_VISIBLE_ROWS = 20;
        else
          MAX_VISIBLE_ROWS = 30;
    
    tjc's avatar
    tjc committed
        
        setMaximumRowCount (MAX_VISIBLE_ROWS);
        setEditable(true);
        update ();
      }
    
      /**
       *  Change the qualifiers shown in this component to be those of the given
       *  Key.
       **/
    
      public void setKey (final Key key) 
      {
        if (this.key != key) 
        {
    
    tjc's avatar
    tjc committed
          this.key = key;
          update ();
        }
      }
    
      /**
       *  Select the given qualifier_name.
       **/
    
      private void setSelectedQualifierByName (final String qualifier_name) 
      {
    
    tjc's avatar
    tjc committed
        final int index = indexOf (qualifier_name);
    
    
        if (index == -1) 
        {
    
    tjc's avatar
    tjc committed
          // add the key
          addItem (qualifier_name);
          setSelectedItem (qualifier_name);
    
    tjc's avatar
    tjc committed
          setSelectedIndex (index);
      }
    
      /**
       *  Return the index in the Choice component of the given qualifier_name.
       **/
    
      private int indexOf (final String qualifier_name) 
      {
        for (int i = 0 ; i < getItemCount () ; ++i) 
        {
          if (getItemAt (i).equals (qualifier_name))
    
    tjc's avatar
    tjc committed
            return i;
        }
        return -1;
      }
    
      /**
       *  Update the Choice to refect the current Key.
       **/
    
      private void update () 
      {
    
    tjc's avatar
    tjc committed
        removeAllItems ();
        StringVector qualifier_names =
          entry_information.getValidQualifierNames (key);
    
    
        if (qualifier_names == null) 
    
    tjc's avatar
    tjc committed
          qualifier_names = new StringVector ("note");
    
        if (default_qualifier != null &&
    
            !qualifier_names.contains (default_qualifier)) 
        {
    
    tjc's avatar
    tjc committed
          qualifier_names.add (default_qualifier);
        }
    
        qualifier_names.sort ();
    
    tjc's avatar
    tjc committed
        
    
        final StringVector invisible_qualifiers =
    
    tjc's avatar
    tjc committed
          Options.getOptions ().getInvisibleQualifiers (isGFF);
    
    tjc's avatar
    tjc committed
    
    
        for (int i = 0 ; i < qualifier_names.size () ; ++i) 
        {
    
    tjc's avatar
    tjc committed
          final String qualifier_name = (String)qualifier_names.elementAt (i);
    
          if (!invisible_qualifiers.contains (qualifier_name)) 
    
    tjc's avatar
    tjc committed
            addItem (qualifier_name);
        }
    
    
        if (default_qualifier == null) 
        {
          if (indexOf ("note") != -1) 
    
    tjc's avatar
    tjc committed
            setSelectedQualifierByName ("note");
    
          else 
          {
            if (indexOf ("locus_tag") != -1) 
    
    tjc's avatar
    tjc committed
              setSelectedQualifierByName ("locus_tag");
    
    tjc's avatar
    tjc committed
            else if( this.getModel().getSize() > 0)
    
    tjc's avatar
    tjc committed
              setSelectedIndex (0);
          }
    
    tjc's avatar
    tjc committed
          setSelectedQualifierByName (default_qualifier);
      }
    
    }