Skip to content
Snippets Groups Projects
RunMenu.java 8.33 KiB
Newer Older
  • Learn to ignore specific revisions
  • tjc's avatar
    tjc committed
    /* RunMenu.java
     *
     * created: Fri Jan 22 1999
     *
     * This file is part of Artemis
     *
     * Copyright(C) 1998,1999,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/RunMenu.java,v 1.3 2004-06-09 13:04:16 tjc Exp $
    
    tjc's avatar
    tjc committed
     **/
    
    package uk.ac.sanger.artemis.components;
    
    import uk.ac.sanger.artemis.*;
    import uk.ac.sanger.artemis.util.ReadOnlyException;
    import uk.ac.sanger.artemis.util.StringVector;
    import uk.ac.sanger.artemis.io.EntryInformationException;
    import uk.ac.sanger.artemis.io.Qualifier;
    import uk.ac.sanger.artemis.io.InvalidKeyException;
    import uk.ac.sanger.artemis.io.InvalidRelationException;
    
    import java.io.IOException;
    import java.awt.event.*;
    import java.util.Vector;
    
    import javax.swing.*;
    
    /**
     *  A JMenu of external commands/functions.
     *
     *  @author Kim Rutherford
    
    tjc's avatar
    tjc committed
     *  @version $Id: RunMenu.java,v 1.3 2004-06-09 13:04:16 tjc Exp $
    
    tjc's avatar
    tjc committed
     **/
    
    public class RunMenu extends SelectionMenu 
    {
      /**
       *  Create a new RunMenu object.
       *  @param frame The JFrame that owns this JMenu.
       *  @param selection The Selection that the commands in the menu will
       *    operate on.
       *  @param menu_name The name of the new menu.
       **/
      public RunMenu(final JFrame frame, final Selection selection,
                     final String menu_name) 
      {
        super(frame, menu_name, selection);
    
        final ExternalProgramVector external_programs =
          Options.getOptions().getExternalPrograms();
    
        for(int i = 0 ; i < external_programs.size() ; ++i) 
          makeMenuItem(external_programs.elementAt(i));
    
        addSeparator();
    
        for(int i = 0 ; i < external_programs.size() ; ++i) 
          makeOptionsMenuItem(external_programs.elementAt(i));
    
    
    tjc's avatar
    tjc committed
    //  if(Options.getOptions().getProperty("jcon_min_jobs") != null) 
    //  {
    //    addSeparator();
    //    final JMenuItem jcon_status = new JMenuItem("Show Job Status ...");
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
    //    jcon_status.addActionListener(new ActionListener() 
    //    {
    //      public void actionPerformed(ActionEvent event) 
    //      {
    //        try 
    //        {
    //          final int ids[] = getIds();
    //          TaskViewerFrame tvf = new TaskViewerFrame(ids);
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
    //          tvf.setSize(400, 600);
    //          tvf.setVisible(true);
    //        }
    //        catch(Exception e) 
    //        {
    //          e.printStackTrace();
    //          new MessageDialog(frame, "unable to view job status: " + e);
    //        }
    //      }
    //    });
    
    tjc's avatar
    tjc committed
    
    
    tjc's avatar
    tjc committed
    //    add(jcon_status);
    //  }
    
    tjc's avatar
    tjc committed
      }
    
      /**
       *  Create a new RunMenu object.
       *  @param frame The JFrame that owns this JMenu.
       *  @param selection The Selection that the commands in the menu will
       *    operate on.
       **/
      public RunMenu(final JFrame frame,
                     final Selection selection) 
      {
        this(frame, selection, "Run");
      }
    
      /**
       *  Make a new menu item for running the given ExternalProgram object.
       *  @param program Create two menu items for this program.
       **/
      private void makeMenuItem(final ExternalProgram program) 
      {
        final JMenuItem new_menu;
    
        if(program.getType() == ExternalProgram.AA_PROGRAM ||
           program.getType() == ExternalProgram.DNA_PROGRAM &&
           Options.getOptions().getPropertyTruthValue("sanger_options")) 
        {
          final String options_string = program.getProgramOptions();
    
          if(options_string.length() > 0) 
            new_menu = new JMenuItem("Run " + program.getName() + " (" +
                            options_string + ") on selected features");
          else 
            new_menu =
              new JMenuItem("Run " + program.getName() + " on selected features");
        } 
        else 
          new_menu =
            new JMenuItem("Run " + program.getName() + " on selected features");
    
        new_menu.addActionListener(new ActionListener() 
        {
          public void actionPerformed(ActionEvent event) 
          {
            if(!checkForSelectionFeatures()) 
              return;
    
            final FeatureVector selection_features =
              getSelection().getAllFeatures();
    
            try
            {
              final ExternalProgramMonitor monitor =
                            program.run(selection_features, Splash.getLogger());
    
              monitor.addExternalProgramListener(new ExternalProgramListener() 
              {
                public void statusChanged(final ExternalProgramEvent e) 
                {
                  if(e.getType() == ExternalProgramEvent.FINISHED) 
                    new MessageFrame(e.getMessage()).setVisible(true);
                }
              });
              new Thread(monitor).start();
            } 
            catch(InvalidKeyException e) 
            {
              new MessageDialog(getParentFrame(),
                                 "execution failed: " + e.getMessage());
            }
            catch(EntryInformationException e)
            {
              new MessageDialog(getParentFrame(),
                                 "execution of " + program.getName() +
                                 " failed because: " + e.getMessage());
            }
            catch(ReadOnlyException e)
            {
              new MessageDialog(getParentFrame(),
                                 "execution of " + program.getName() +
                                 " failed because one of the features is " +
                                 "read only");
            }
            catch(IOException e)
            {
              new MessageDialog(getParentFrame(),
                                 "execution of " + program.getName() +
                                 " failed because of an I/O error: " +
                                 e);
            }
            catch(ExternalProgramException e) 
            {
              new MessageDialog(getParentFrame(),
                                "execution of " + program.getName() +
                                " failed: " + e.getMessage());
            }
          }
        });
    
        add(new_menu);
      }
    
      /**
       *  Make a new options menu item for the given ExternalProgram object.
       *  @param program Create two menu items for this program.
       **/
      private void makeOptionsMenuItem(final ExternalProgram program)
      {
        if(!(program.getType() == ExternalProgram.AA_PROGRAM ||
             program.getType() == ExternalProgram.DNA_PROGRAM)) 
          return;
    
        final JMenuItem new_options_menu =
          new JMenuItem("Set " + program.getName() + " options");
    
        new_options_menu.addActionListener(new ActionListener() 
        {
          public void actionPerformed(ActionEvent event)
          {
            new ExternalProgramOptions(program);
          }
        });
    
        add(new_options_menu);
      }
    
      /**
       *  
       **/
      private int[] getIds() 
      {
        final FeatureVector selected_features = getSelection().getAllFeatures();
        final Vector ids_vector = new Vector();
    
        for(int feature_index = 0; feature_index < selected_features.size();
            ++feature_index) 
        {
          final Feature this_feature = selected_features.elementAt(feature_index);
    
          try
          {
            final Qualifier job_qualifier =
              this_feature.getQualifierByName("job");
            
            final StringVector values = job_qualifier.getValues();
    
            if(values != null && values.size() > 0) 
            {
              for(int value_index=0; value_index<values.size();
                  ++value_index) 
              {
                final String job_value = values.elementAt(value_index);
                final StringVector bits = StringVector.getStrings(job_value);
    
                if(bits.size() > 4 && bits.elementAt(2).equals("task:")) 
                {
                  try 
                  {
                    final Integer task_id = Integer.valueOf(bits.elementAt(3));
    
                    if(!ids_vector.contains(task_id)) 
                      ids_vector.add(task_id);
                  }
                  catch(NumberFormatException e) {}
                }
              }
            }
          } 
          catch(InvalidRelationException e) {}
        }
    
        final int[] ids = new int[ids_vector.size()];
    
        for(int i=0 ; i<ids.length ; ++i) 
          ids[i] =((Integer)ids_vector.elementAt(i)).intValue();
    
        return ids;
      }
    }