Skip to content
Snippets Groups Projects
GraphMenu.java 13.2 KiB
Newer Older
  • Learn to ignore specific revisions
  • tjc's avatar
    tjc committed
    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    /* GraphMenu.java
     *
     * created: Tue Sep 18 2001
     *
     * This file is part of Artemis
     *
     * Copyright (C) 2001  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: //tmp/pathsoft/artemis/uk/ac/sanger/artemis/components/GraphMenu.java,v 1.1 2004-06-09 09:46:57 tjc Exp $
     */
    
    package uk.ac.sanger.artemis.components;
    
    import uk.ac.sanger.artemis.*;
    import uk.ac.sanger.artemis.plot.*;
    import uk.ac.sanger.artemis.sequence.*;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.io.IOException;
    import java.io.File;
    import java.io.FileReader;
    import java.util.Vector;
    
    import javax.swing.*;
    
    /**
     *  This menu controls one particular BasePlotGroup.
     *
     *  @author Kim Rutherford <kmr@sanger.ac.uk>
     *  @version $Id: GraphMenu.java,v 1.1 2004-06-09 09:46:57 tjc Exp $
     **/
    
    public class GraphMenu extends JMenu {
      /**
       *  Create a new GraphMenu object and all it's menu items.
       *  @param frame The JFrame that owns this JMenu.
       *  @param entry_group The EntryGroup containing the sequence to plot.
       *  @param base_plot_group The BasePlotGroup that this menu controls.
       *  @param view_menu This ViewMenu is updated when a usage plot is added.
       *  @param add_menu This AddMenu is updated when a usage plot is added.
       *  @param menu_name The name of the new menu.
       **/
      public GraphMenu (final JFrame frame,
                        final EntryGroup entry_group,
                        final BasePlotGroup base_plot_group,
                        final FeatureDisplay feature_display,
                        final String menu_name) {
        super (menu_name);
        this.frame = frame;
        this.entry_group = entry_group;
        this.base_plot_group = base_plot_group;
        this.view_menu = view_menu;
        this.add_menu = add_menu;
        this.feature_display = feature_display;
    
        final BaseAlgorithm [] orig_algorithms =
          base_plot_group.getPlotAlgorithms ();
    
        final JMenuItem hide_all_graphs_item = new JMenuItem ("Hide All Graphs");
        hide_all_graphs_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            final BaseAlgorithm [] current_algorithms =
              base_plot_group.getPlotAlgorithms ();
    
            for (int i = 0 ; i < current_algorithms.length ; ++i) {
              final BaseAlgorithm this_algorithm = current_algorithms[i];
    
              base_plot_group.setVisibleByAlgorithm (this_algorithm, false);
    
              final JCheckBoxMenuItem this_menu_item =
                (JCheckBoxMenuItem) algorithm_menu_items.elementAt (i);
    
              this_menu_item.setState (false);
            }
            if (getParent () != null) {
              // XXX change to revalidate().
              frame.validate ();
            }
          }
        });
        add (hide_all_graphs_item);
    
        addSeparator ();
    
        if (Options.readWritePossible ()) {
          final JMenuItem usage_plot_item = new JMenuItem ("Add Usage Plots ...");
          usage_plot_item.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent event) {
              addUsagePlot ();
            }
          });
          add (usage_plot_item);
    
          final JMenuItem user_plot_item = new JMenuItem ("Add User Plot ...");
          user_plot_item.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent event) {
              addUserPlot ();
            }
          });
    
          add (user_plot_item);
    
          addSeparator ();
        }
    
        for (int i = 0 ; i < orig_algorithms.length ; ++i) {
          final BaseAlgorithm this_algorithm = orig_algorithms[i];
    
          addAlgorithm (this_algorithm, false);
        }
    
        if (Options.getOptions ().getProperty ("codon_usage_file") != null) {
          final String codon_usage_file_name =
            Options.getOptions ().getProperty ("codon_usage_file");
    
          try {
            addUsagePlot (new File (codon_usage_file_name), true, false);
            addUsagePlot (new File (codon_usage_file_name), false, false);
    
            if (getParent () != null) {
              // XXX change to revalidate().
              frame.validate ();
            }
          } catch (IOException e) {
            new MessageDialog (frame, "error while reading usage data: " + e);
          }
        }
      }
    
      /**
       *  Create a new GraphMenu object and all it's menu items.
       *  @param frame The JFrame that owns this JMenu.
       *  @param entry_group The EntryGroup containing the sequence to plot.
       *  @param base_plot_group The BasePlotGroup that this menu controls.
       *  @param view_menu This ViewMenu is updated when a usage plot is added.
       *  @param add_menu This AddMenu is updated when a usage plot is added.
       *  @param menu_name The name of the new menu.
       **/
      public GraphMenu (final JFrame frame,
                        final EntryGroup entry_group,
                        final BasePlotGroup base_plot_group,
                        final FeatureDisplay feature_display) {
        this (frame, entry_group, base_plot_group, feature_display, "Graph");
      }
    
      /**
       *  Add a menu item for the given algorithm to the Display menu.
       *  @param is_visible The JCheckBoxMenuItem starts in the true state if and
       *    only if this is true.
       *  @return The new JCheckBoxMenuItem
       **/
      public JCheckBoxMenuItem addAlgorithm (final BaseAlgorithm algorithm,
                                            final boolean is_visible) {
    
        final JCheckBoxMenuItem new_item =
          new JCheckBoxMenuItem (algorithm.getAlgorithmName ());
    
        new_item.setState (is_visible);
    
        new_item.addItemListener (new ItemListener () {
          public void itemStateChanged(ItemEvent event) {
            base_plot_group.setVisibleByAlgorithm (algorithm,
                                                   new_item.getState ());
          }
        });
    
        add (new_item);
    
        algorithm_menu_items.addElement (new_item);
    
        return new_item;
      }
    
      /**
       *  Ask the user for a file name, then read the codon usage data from that
       *  file, then make and add forward and a reverse BasePlot component using
       *  the data.
       **/
      public void addUsagePlot () {
        final JFrame frame = Utilities.getComponentFrame (base_plot_group);
    
        final StickyFileChooser dialog = new StickyFileChooser ();
    
        dialog.setDialogTitle ("Select a codon usage data file name ...");
        dialog.setDialogType (JFileChooser.OPEN_DIALOG);
    
        final int status = dialog.showOpenDialog (frame);
    
        if (status != JFileChooser.APPROVE_OPTION ||
            dialog.getSelectedFile () == null) {
          return;
        }
    
        final File file =
          new File (dialog.getCurrentDirectory (),
                    dialog.getSelectedFile ().getName ());
    
        if (file.length () != 0) {
          try {
            final BasePlot new_forward_plot =
              addUsagePlot (file, true, true);
            final BasePlot new_reverse_plot =
              addUsagePlot (file, false, true);
    
            final Algorithm forward_algorithm = new_forward_plot.getAlgorithm ();
            final Algorithm reverse_algorithm = new_reverse_plot.getAlgorithm ();
    
            base_plot_group.setVisibleByAlgorithm (forward_algorithm, true);
            base_plot_group.setVisibleByAlgorithm (reverse_algorithm, true);
          } catch (IOException e) {
            new MessageDialog (Utilities.getComponentFrame (base_plot_group),
                               "error while reading usage data: " + e);
          }
        }
      }
    
      /**
       *  Read the codon usage data from the given File, then make and add a
       *  BasePlot component using the data.
       *  @param use_forward_strand The plot will be a forward plot if and only if
       *    this is true.
       *  @param is_visible The plot will start off visible if and only if this is
       *    true.
       *  @return The BasePlot that was added.
       **/
      private BasePlot addUsagePlot (final File codon_usage_file,
                                     final boolean use_forward_strand,
                                     final boolean is_visible)
          throws IOException {
        final CodonUsageAlgorithm codon_usage_algorithm;
    
        if (use_forward_strand) {
          final Strand forward_strand =
            entry_group.getBases ().getForwardStrand ();
    
          final CodonUsageWeight usage_weights =
            new CodonUsageWeight (codon_usage_file, forward_strand);
    
          codon_usage_algorithm =
            new CodonUsageAlgorithm (forward_strand, usage_weights);
        } else {
          final Strand backward_strand =
            entry_group.getBases ().getReverseStrand ();
    
          final CodonUsageWeight usage_weights =
            new CodonUsageWeight (codon_usage_file, backward_strand);
    
          codon_usage_algorithm =
            new CodonUsageAlgorithm (backward_strand, usage_weights);
        }
    
        addAlgorithm (codon_usage_algorithm, is_visible);
    
        final BasePlot new_plot =
          base_plot_group.addAlgorithm (codon_usage_algorithm);
    
        base_plot_group.setVisibleByAlgorithm (codon_usage_algorithm, is_visible);
    
        // XXX hack to force the BasePlot to initialise
        final DisplayAdjustmentEvent event =
          new DisplayAdjustmentEvent (this,
                                      feature_display.getFirstVisibleForwardBase (),
                                      feature_display.getLastVisibleForwardBase (),
                                      feature_display.getMaxVisibleBases (),
                                      feature_display.getScaleValue (),
                                      feature_display.getScaleFactor (),
                                      feature_display.isRevCompDisplay (),
                                      DisplayAdjustmentEvent.ALL_CHANGE_ADJUST_EVENT);
    
        base_plot_group.displayAdjustmentValueChanged (event);
    
        return new_plot;
      }
    
      /**
       *  Add a UserDataAlgorithm to the display.
       **/
      private void addUserPlot () {
        final JFrame frame = Utilities.getComponentFrame (base_plot_group);
    
        final StickyFileChooser dialog = new StickyFileChooser ();
    
        dialog.setDialogTitle ("Select a data file name ...");
        dialog.setDialogType (JFileChooser.OPEN_DIALOG);
    
        final int status = dialog.showOpenDialog (frame);
    
        if (status != JFileChooser.APPROVE_OPTION ||
            dialog.getSelectedFile () == null) {
          return;
        }
    
        final File file =
          new File (dialog.getCurrentDirectory (),
                    dialog.getSelectedFile ().getName ());
    
        if (file.length () != 0) {
          final uk.ac.sanger.artemis.util.Document document =
            new uk.ac.sanger.artemis.util.FileDocument (file);
    
          final Strand forward_strand =
            getEntryGroup ().getBases ().getForwardStrand ();
    
          try {
            final UserDataAlgorithm new_algorithm =
              new UserDataAlgorithm (forward_strand, document);
    
            final BasePlot new_base_plot =
              base_plot_group.addAlgorithm (new_algorithm);
    
            base_plot_group.setVisibleByAlgorithm (new_algorithm, true);
    
            addAlgorithm (new_algorithm, true);
    
            // XXX hack to force the BasePlot to initialise
            final DisplayAdjustmentEvent event =
              new DisplayAdjustmentEvent (this,
                                          feature_display.getFirstVisibleForwardBase (),
                                          feature_display.getLastVisibleForwardBase (),
                                          feature_display.getMaxVisibleBases (),
                                          feature_display.getScaleValue (),
                                          feature_display.getScaleFactor (),
                                          feature_display.isRevCompDisplay (),
                                          DisplayAdjustmentEvent.ALL_CHANGE_ADJUST_EVENT);
    
            base_plot_group.displayAdjustmentValueChanged (event);
    
            if (getParent () != null) {
              // XXX change to revalidate().
              frame.validate ();
            }
          } catch (IOException e) {
            new MessageDialog (Utilities.getComponentFrame (base_plot_group),
                               "error while reading user data: " + e);
          }
        }
      }
    
      /**
       *  Return the JFrame that was passed to the constructor.
       **/
      public JFrame getParentFrame () {
        return frame;
      }
    
      /**
       *  Return the EntryGroup that was passed to the constructor.
       **/
      private EntryGroup getEntryGroup () {
        return entry_group;
      }
    
      /**
       *  The JFrame reference that was passed to the constructor.
       **/
      private JFrame frame;
    
      /**
       *  The BasePlotGroup that was passed to the constructor.
       **/
      private BasePlotGroup base_plot_group;
    
      /**
       *  The EntryGroup object that was passed to the constructor.
       **/
      private EntryGroup entry_group = null;
    
      /**
       *  This ViewMenu is updated when a usage plot is added.
       **/
      private ViewMenu view_menu;
    
      /**
       *  This AddMenu is updated when a usage plot is added.
       **/
      private AddMenu add_menu;
    
      /**
       *  The FeatureDisplay that was passed to the constructor.
       **/
      private FeatureDisplay feature_display;
    
      /**
       *  This list of the CheckboxMenuItems for the graphs is stored so that we
       *  can turn them all off with "Hide All Graphs".
       **/
      private Vector algorithm_menu_items = new Vector ();
    }