Skip to content
Snippets Groups Projects
MarkerRangeRequesterEvent.java 6.22 KiB
Newer Older
  • Learn to ignore specific revisions
  • tjc's avatar
    tjc committed
    /* MarkerRangeRequesterEvent.java
     *
     * created: Mon Jul 10 2000
     *
     * 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.
     *
    
    tjc's avatar
    tjc committed
     * $Header: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/components/MarkerRangeRequesterEvent.java,v 1.2 2004-10-29 09:36:24 tjc Exp $
    
    tjc's avatar
    tjc committed
     */
    
    package uk.ac.sanger.artemis.components;
    
    import uk.ac.sanger.artemis.sequence.*;
    import uk.ac.sanger.artemis.util.OutOfRangeException;
    import uk.ac.sanger.artemis.io.Range;
    import uk.ac.sanger.artemis.io.LocationLexer;
    import uk.ac.sanger.artemis.io.LocationLexer.*;
    import uk.ac.sanger.artemis.util.*;
    
    import javax.swing.*;
    
    /**
     *  This event is sent when the user presses OK or Cancel in a
     *  MarkerRangeRequester component.
     *
     *  @author Kim Rutherford <kmr@sanger.ac.uk>
    
    tjc's avatar
    tjc committed
     *  @version $Id: MarkerRangeRequesterEvent.java,v 1.2 2004-10-29 09:36:24 tjc Exp $
    
    tjc's avatar
    tjc committed
     **/
    
    public class MarkerRangeRequesterEvent extends TextRequesterEvent {
      /**
       *  Create a new MarkerRangeRequesterEvent object.
       *  @param source The MarkerRangeRequester that generated the event.
       *  @param requester_text The contents of the TextField in the
       *    MarkerRangeRequester.
       *  @param type The type of event.
       **/
    
    tjc's avatar
    tjc committed
      public MarkerRangeRequesterEvent(final MarkerRangeRequester source,
                                       final String requester_text,
                                       final int type)
      {
        super(source, requester_text, type);
    
    tjc's avatar
    tjc committed
      }
    
    
      /**
       *  Parse a Range and return the start and end positions in an array that is
       *  two elements long.
       *  @return null if and only if there was a parse error.  If the range is on
       *    the forward strand the first element will be less than or equal to the
       *    second, otherwise the first will be greater than the second.
       **/
    
    tjc's avatar
    tjc committed
      private int[] getRangeInternal() 
      {
        if(getRequesterText().length() == 0) 
    
    tjc's avatar
    tjc committed
          return null;
    
    
    tjc's avatar
    tjc committed
        final LocationLexer lexer = new LocationLexer(getRequesterText());
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        final TokenEnumeration enumTk = lexer.getTokens();
    
    tjc's avatar
    tjc committed
    
        boolean complement_flag = false;
    
    
    tjc's avatar
    tjc committed
        if(enumTk.peekElement() instanceof String &&
            ((String)enumTk.peekElement()).equals("complement")) 
        {
    
    tjc's avatar
    tjc committed
          complement_flag = true;
    
    tjc's avatar
    tjc committed
          enumTk.nextElement();
    
    tjc's avatar
    tjc committed
        }
    
    
    tjc's avatar
    tjc committed
        enumTk.eatToken('(');
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        if(enumTk.peekElement() instanceof Integer) 
        {
          int first_base = ((Integer)enumTk.nextElement()).intValue();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
          if(enumTk.peekElement() instanceof Integer ||
             (enumTk.eatToken("..") || enumTk.eatToken('.') ||
              enumTk.eatToken("-")) &&
              enumTk.peekElement() instanceof Integer)
          {
            int last_base = ((Integer)enumTk.nextElement()).intValue();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
            enumTk.eatToken(')');
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
            if(enumTk.peekElement() == null) 
            {
              if(complement_flag) 
              {
    
    tjc's avatar
    tjc committed
                final int temp = first_base;
                first_base = last_base;
                last_base = temp;
              }
    
    
    tjc's avatar
    tjc committed
              return new int[]
              {
    
    tjc's avatar
    tjc committed
                first_base, last_base
              };
    
    tjc's avatar
    tjc committed
            }
            else
            {
              new MessageDialog((JFrame) getSource(),
                                "garbage at the end of the range: " + enumTk);
    
    tjc's avatar
    tjc committed
              return null;
            }
          }
        }
    
        // if we get to here then there was a parse error
    
    tjc's avatar
    tjc committed
        new MessageDialog((JFrame) getSource(),
                          "error in range: the range should have this " +
                          "form: 100..200 - error at: " + enumTk);
    
    tjc's avatar
    tjc committed
    
        return null;
      }
    
      /**
       *  Parse the contents of the String that was passed to the constructor as a
       *  Range and return it.
       *  @param bases The Bases object to use to find the Strand that is passed
       *    to the MarkerRange constructor.
       *  @return The Range or null if the Range can't be parsed.
       **/
    
    tjc's avatar
    tjc committed
      public MarkerRange getMarkerRange(final Bases bases) 
      {
        try 
        {
    
    tjc's avatar
    tjc committed
          final MarkerRange marker_range;
    
    
    tjc's avatar
    tjc committed
          final int [] return_values = getRangeInternal();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
          if(return_values == null) 
    
    tjc's avatar
    tjc committed
            return null;
    
          final int first_base = return_values[0];
    
    tjc's avatar
    tjc committed
          final int last_base  = return_values[1];
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
          if(first_base <= last_base) 
          {
    
    tjc's avatar
    tjc committed
            // forward strand
            final Strand strand;
    
    tjc's avatar
    tjc committed
            strand = bases.getForwardStrand();
    
    tjc's avatar
    tjc committed
    
            marker_range =
    
    tjc's avatar
    tjc committed
              strand.makeMarkerRangeFromPositions(first_base,
                                                  last_base);
          }
          else
          {
    
    tjc's avatar
    tjc committed
            // reverse strand
    
    tjc's avatar
    tjc committed
            final Strand strand = bases.getReverseStrand();
            final int raw_first = bases.getComplementPosition(first_base);
            final int raw_last  = bases.getComplementPosition(last_base);
    
    tjc's avatar
    tjc committed
    
            marker_range =
    
    tjc's avatar
    tjc committed
              strand.makeMarkerRangeFromPositions(raw_last,
                                                  raw_first);
    
    tjc's avatar
    tjc committed
          }
    
          return marker_range;
    
    tjc's avatar
    tjc committed
        } 
        catch(OutOfRangeException e)
        {
          new MessageDialog((JFrame)getSource(),
    
    tjc's avatar
    tjc committed
                             "the bases are out of range for this " +
                             "sequence");
          return null;
        }
      }
    
      /**
       *  Parse and return a raw Range object from the text.
       *  @return The Range or null if the range could not be parsed.
       **/
    
    tjc's avatar
    tjc committed
      public Range getRawRange()
      {
        final int [] return_values = getRangeInternal();
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
        if(return_values == null) 
    
    tjc's avatar
    tjc committed
          return null;
    
        final int first_base = return_values[0];
    
    tjc's avatar
    tjc committed
        final int last_base  = return_values[1];
    
        try 
        {
          if(first_base < last_base) 
            return new Range(first_base, last_base);
          else 
            return new Range(last_base, first_base);
        } 
        catch(OutOfRangeException e) 
        {
          throw new Error("internal error - unexpected exception: " + e);
    
    tjc's avatar
    tjc committed
        }
      }
    }