/* QualifierTextArea.java
 *
 * created: Tue Oct 23 2001
 *
 * This file is part of Artemis
 * 
 * Copyright (C) 2000  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.
 *
 * $Header: /nfs/pathsoft/repository/powmap/diana/components/QualifierTextArea.java,v 1.3 2002/07/26 15:41:14 kmr Exp $
 */

package diana.components;

import diana.Options;

import uk.ac.sanger.pathogens.embl.QualifierParseException;
import uk.ac.sanger.pathogens.embl.QualifierVector;
import uk.ac.sanger.pathogens.embl.EntryInformation;
import uk.ac.sanger.pathogens.embl.EmblStreamFeature;

import java.awt.*;
import java.io.*;

/**
 *  This component is a TextArea that understands qualifiers.
 *
 *  @author Kim Rutherford <kmr@sanger.ac.uk>
 *  @version $Id: QualifierTextArea.java,v 1.3 2002/07/26 15:41:14 kmr Exp $
 **/

public class QualifierTextArea extends TextArea {
  /**
   *  Create a new QualifierTextArea containing no text.
   **/
  public QualifierTextArea () {
    super ("",
           (Options.getOptions ().getPropertyTruthValue ("alicat_mode") ?
            40 :
            18),
           81, TextArea.SCROLLBARS_VERTICAL_ONLY);

    setBackground (Color.white);
  }

  /**
   *  Parse and return the qualifiers in this TextArea in a QualifierVector.
   **/
  public QualifierVector
    getParsedQualifiers (final EntryInformation entry_information)
      throws QualifierParseException {
    final String qualifier_string = getText ();
    return getQualifiersFromString (qualifier_string,
                                    entry_information);
  }

  /**
   *  Return a QualifierVector containing the qualifiers from a String.
   *  @param qual_string contains the qualifiers to parse
   */
  private static QualifierVector
    getQualifiersFromString (final String qual_string,
                             final EntryInformation entry_information)
      throws QualifierParseException {

    try {
      final StringReader string_reader = new StringReader (qual_string);

      final QualifierVector embl_qualifiers =
        EmblStreamFeature.readQualifiers (string_reader,
                                          entry_information);

      string_reader.close ();

      return embl_qualifiers;
    } catch (IOException exception) {
      throw (new QualifierParseException (exception.getMessage ()));
    }
  }
}
