Skip to content
Snippets Groups Projects
EditMenu.java 82.5 KiB
Newer Older
  • Learn to ignore specific revisions
  • tjc's avatar
    tjc committed
    /* EditMenu.java
     *
     * created: Thu Dec  3 1998
     *
     * This file is part of Artemis
     *
     * Copyright (C) 1998,1999,2000,2001,2002  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/EditMenu.java,v 1.2 2004-12-03 18:11:28 tjc Exp $
    
    tjc's avatar
    tjc committed
     **/
    
    package uk.ac.sanger.artemis.components;
    
    import uk.ac.sanger.artemis.*;
    import uk.ac.sanger.artemis.sequence.*;
    import uk.ac.sanger.artemis.util.*;
    import uk.ac.sanger.artemis.io.Range;
    import uk.ac.sanger.artemis.io.RangeVector;
    import uk.ac.sanger.artemis.io.Key;
    import uk.ac.sanger.artemis.io.Location;
    import uk.ac.sanger.artemis.io.Qualifier;
    import uk.ac.sanger.artemis.io.QualifierInfo;
    import uk.ac.sanger.artemis.io.QualifierInfoVector;
    import uk.ac.sanger.artemis.io.QualifierVector;
    import uk.ac.sanger.artemis.io.QualifierParseException;
    import uk.ac.sanger.artemis.io.InvalidQualifierException;
    import uk.ac.sanger.artemis.io.InvalidRelationException;
    import uk.ac.sanger.artemis.io.EntryInformation;
    import uk.ac.sanger.artemis.io.EntryInformationException;
    import uk.ac.sanger.artemis.io.OutOfDateException;
    
    import java.awt.*;
    import java.awt.event.*;
    import java.io.IOException;
    
    import javax.swing.*;
    
    /**
     *  A menu with editing commands.
     *
     *  @author Kim Rutherford
    
     *  @version $Id: EditMenu.java,v 1.2 2004-12-03 18:11:28 tjc Exp $
    
    tjc's avatar
    tjc committed
    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 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
     **/
    
    public class EditMenu extends SelectionMenu
        implements EntryGroupChangeListener, EntryChangeListener {
      /**
       *  Create a new EditMenu object.
       *  @param frame The JFrame that owns this JMenu.
       *  @param selection The Selection that the commands in the menu will
       *    operate on.
       *  @param goto_event_source The object the we will call makeBaseVisible ()
       *    on.
       *  @param entry_group The EntryGroup object where new features/entries will
       *    be added.
       *  @param base_plot_group The BasePlotGroup associated with this JMenu -
       *    needed to call getCodonUsageAlgorithm()
       *  @param menu_name The name of the new menu.
       **/
      public EditMenu (final JFrame frame,
                       final Selection selection,
                       final GotoEventSource goto_event_source,
                       final EntryGroup entry_group,
                       final BasePlotGroup base_plot_group,
                       final String menu_name) {
        super (frame, menu_name, selection);
    
        this.entry_group = entry_group;
        this.goto_event_source = goto_event_source;
        this.base_plot_group = base_plot_group;
    
        getEntryGroup ().addEntryGroupChangeListener (this);
        getEntryGroup ().addEntryChangeListener (this);
    
        refreshMenu ();
      }
    
      /**
       *  Create a new EditMenu object and use "Edit" as the menu name.
       *  @param frame The JFrame that owns this JMenu.
       *  @param selection The Selection that the commands in the menu will
       *    operate on.
       *  @param goto_event_source The object the we will call makeBaseVisible ()
       *    on.
       *  @param entry_group The EntryGroup object where new features/entries will
       *    be added.
       *  @param base_plot_group The BasePlotGroup associated with this JMenu -
       *    needed to call getCodonUsageAlgorithm()
       **/
      public EditMenu (final JFrame frame,
                       final Selection selection,
                       final GotoEventSource goto_event_source,
                       final EntryGroup entry_group,
                       final BasePlotGroup base_plot_group) {
        this (frame, selection, goto_event_source, entry_group,
              base_plot_group, "Edit");
      }
    
      /**
       *  The shortcut for Edit Selected Features.
       **/
      final static KeyStroke EDIT_FEATURES_KEY =
        KeyStroke.getKeyStroke (KeyEvent.VK_E, InputEvent.CTRL_MASK);
      final static public int EDIT_FEATURES_KEY_CODE = KeyEvent.VK_E;
    
      /**
       *  The shortcut for Merge Selected Features.
       **/
      final static KeyStroke MERGE_FEATURES_KEY =
        KeyStroke.getKeyStroke (KeyEvent.VK_M, InputEvent.CTRL_MASK);
      final static public int MERGE_FEATURES_KEY_CODE = KeyEvent.VK_M;
    
      /**
       *  The shortcut for Duplicate Selected Features.
       **/
      final static KeyStroke DUPLICATE_KEY =
        KeyStroke.getKeyStroke (KeyEvent.VK_D, InputEvent.CTRL_MASK);
      final static public int DUPLICATE_KEY_CODE = KeyEvent.VK_D;
    
      /**
       *  The shortcut for Delete Selected Features.
       **/
      final static KeyStroke DELETE_FEATURES_KEY =
        KeyStroke.getKeyStroke (KeyEvent.VK_DELETE, InputEvent.CTRL_MASK);
      final static public int DELETE_FEATURES_KEY_CODE = KeyEvent.VK_DELETE;
    
      /**
       *  The shortcut for Trim Selected Features.
       **/
      final static KeyStroke TRIM_FEATURES_KEY =
        KeyStroke.getKeyStroke (KeyEvent.VK_T, InputEvent.CTRL_MASK);
      final static public int TRIM_FEATURES_KEY_CODE = KeyEvent.VK_T;
    
      /**
       *  The shortcut for Trim Selected Features To Next Any.
       **/
      final static KeyStroke TRIM_FEATURES_TO_NEXT_ANY_KEY =
        KeyStroke.getKeyStroke (KeyEvent.VK_Y, InputEvent.CTRL_MASK);
      final static public int TRIM_FEATURES_TO_NEXT_ANY_KEY_CODE = KeyEvent.VK_Y;
    
      /**
       *  The shortcut for Extend to Previous Stop Codon.
       **/
      final static public int EXTEND_TO_PREVIOUS_STOP_CODON_KEY_CODE =
        KeyEvent.VK_Q;
      final static KeyStroke EXTEND_TO_PREVIOUS_STOP_CODON_KEY =
        makeMenuKeyStroke (EXTEND_TO_PREVIOUS_STOP_CODON_KEY_CODE);
    
      /**
       *  The shortcut for Undo.
       **/
      final static public int UNDO_KEY_CODE = KeyEvent.VK_U;
      final static KeyStroke UNDO_KEY =
        KeyStroke.getKeyStroke (UNDO_KEY_CODE, InputEvent.CTRL_MASK);
    
      /**
       *  Implementation of the EntryGroupChangeListener interface.  We listen to
       *  EntryGroupChange events so that we can update the display if entries
       *  are added or deleted.
       **/
      public void entryGroupChanged (final EntryGroupChangeEvent event) {
        switch (event.getType ()) {
        case EntryGroupChangeEvent.ENTRY_ADDED:
        case EntryGroupChangeEvent.ENTRY_DELETED:
        case EntryGroupChangeEvent.ENTRY_INACTIVE:
        case EntryGroupChangeEvent.ENTRY_ACTIVE:
        case EntryGroupChangeEvent.NEW_DEFAULT_ENTRY:
          refreshMenu ();
          break;
        }
      }
    
      /**
       *  Implementation of the EntryChangeListener interface.
       **/
      public void entryChanged (final EntryChangeEvent event) {
        if (event.getType () == EntryChangeEvent.NAME_CHANGED) {
          refreshMenu ();
        }
      }
    
      /**
       *  Update the menus to the reflect the current contents of the EntryGroup.
       **/
      private void refreshMenu () {
        removeAll ();
    
        undo_item = new JMenuItem ("Undo");
        undo_item.setAccelerator (UNDO_KEY);
        undo_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            undo (getParentFrame (), getSelection (), getEntryGroup ());
          }
        });
    
        edit_feature_item = new JMenuItem ("Edit Selected Features");
        edit_feature_item.setAccelerator (EDIT_FEATURES_KEY);
        edit_feature_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            editSelectedFeatures (getParentFrame (), getEntryGroup (),
                                  getSelection (), getGotoEventSource ());
          }
        });
    
        edit_subsequence_item = new JMenuItem ("Edit Subsequence (and Features)");
        edit_subsequence_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            editSubSequence ();
          }
        });
    
        add_qualifiers_item = new JMenuItem ("Change Qualifiers Of Selected ...");
        add_qualifiers_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            addQualifiers (getParentFrame (), getSelection ());
          }
        });
    
        remove_qualifier_item = new JMenuItem ("Remove Qualifier Of Selected ...");
        remove_qualifier_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            removeQualifier (getParentFrame (), getSelection ());
          }
        });
    
    
        merge_features_item = new JMenuItem ("Merge Selected Features");
        merge_features_item.setAccelerator (MERGE_FEATURES_KEY);
        merge_features_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            mergeFeatures (getParentFrame (), getSelection (), getEntryGroup ());
          }
        });
    
        unmerge_feature_item = new JMenuItem ("Unmerge Selected Feature");
        unmerge_feature_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            unmergeFeature (getParentFrame (), getSelection (), getEntryGroup ());
          }
        });
    
        duplicate_item = new JMenuItem ("Duplicate Selected Features");
        duplicate_item.setAccelerator (DUPLICATE_KEY);
        duplicate_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            duplicateFeatures (getParentFrame (), getSelection (),
                               getEntryGroup ());
          }
        });
    
        delete_features_item = new JMenuItem ("Delete Selected Features");
        delete_features_item.setAccelerator (DELETE_FEATURES_KEY);
        delete_features_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            deleteSelectedFeatures (getParentFrame (), getSelection (),
                                    getEntryGroup ());
          }
        });
    
        delete_segments_item = new JMenuItem ("Delete Selected Exons");
        delete_segments_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            deleteSelectedSegments ();
          }
        });
    
        delete_introns_item =
          new JMenuItem ("Remove Introns of Selected Features");
        delete_introns_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            removeIntrons ();
          }
        });
    
        edit_header_item = new JMenuItem ("Edit Header Of Default Entry");
        edit_header_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            editHeader ();
          }
        });
    
        move_features_menu = new JMenu ("Move Selected Features To");
        copy_features_menu = new JMenu ("Copy Selected Features To");
    
        if (entry_group == null || getEntryGroup ().size () == 0) {
          move_features_menu.add (new JMenuItem ("(No Entries Currently)"));
          copy_features_menu.add (new JMenuItem ("(No Entries Currently)"));
        } else {
          for (int i = 0 ; i < getEntryGroup ().size () ; ++i) {
            final Entry this_entry = getEntryGroup ().elementAt (i);
    
            String entry_name = this_entry.getName ();
    
            if (entry_name == null) {
              entry_name = "no name";
            }
    
            final JMenuItem move_to_item = new JMenuItem (entry_name);
    
            move_to_item.addActionListener (new ActionListener () {
              public void actionPerformed (ActionEvent event) {
                // unselect, move, then reselect (for speed)
                final FeatureVector selected_features =
                  getSelection ().getAllFeatures ();
                getSelection ().clear ();
                moveFeatures (selected_features, this_entry);
                getSelection ().set (selected_features);
              }
            });
            move_features_menu.add (move_to_item);
    
            final JMenuItem copy_to_item = new JMenuItem (entry_name);
    
            copy_to_item.addActionListener (new ActionListener () {
              public void actionPerformed (ActionEvent event) {
                copyFeatures (getSelection ().getAllFeatures (), this_entry);
              }
            });
            copy_features_menu.add (copy_to_item);
          }
        }
    
        trim_to_any_item = new JMenuItem ("Trim Selected Features To Any");
        trim_to_any_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            EditMenu.trimSelected (getParentFrame (), getSelection (),
                                   getEntryGroup (), true, false);
          }
        });
    
        trim_item = new JMenuItem ("Trim Selected Features To Met");
        trim_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            EditMenu.trimSelected (getParentFrame (), getSelection (),
                                   getEntryGroup (), false, false);
          }
        });
    
        trim_to_next_any_item =
          new JMenuItem ("Trim Selected Features To Next Any");
        trim_to_next_any_item.setAccelerator (TRIM_FEATURES_TO_NEXT_ANY_KEY);
        trim_to_next_any_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            EditMenu.trimSelected (getParentFrame (), getSelection (),
                                   getEntryGroup (), true, true);
          }
        });
    
        trim_to_next_item = new JMenuItem ("Trim Selected Features To Next Met");
        trim_to_next_item.setAccelerator (TRIM_FEATURES_KEY);
        trim_to_next_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            EditMenu.trimSelected (getParentFrame (), getSelection (),
                                   getEntryGroup (), false, true);
          }
        });
    
        extend_to_prev_stop_item =
          new JMenuItem ("Extend to Previous Stop Codon");
        extend_to_prev_stop_item.setAccelerator (EXTEND_TO_PREVIOUS_STOP_CODON_KEY);
        extend_to_prev_stop_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            extendToORF (getParentFrame (), getSelection (),
                         getEntryGroup (), false);
          }
        });
    
        extend_to_next_stop_item = new JMenuItem ("Extend to Next Stop Codon");
        extend_to_next_stop_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            extendToORF (getParentFrame (), getSelection (),
                         getEntryGroup (), true);
          }
        });
    
        fix_stop_codons_item = new JMenuItem ("Fix Stop Codons");
        fix_stop_codons_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            fixStopCodons ();
          }
        });
    
        auto_gene_name_item = new JMenuItem ("Automatically Create Gene Names");
        auto_gene_name_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            autoGeneName ();
          }
        });
    
        fix_gene_names_item = new JMenuItem ("Fix Gene Names");
        fix_gene_names_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            fixGeneNames (getParentFrame (), getEntryGroup (),
                          getSelection ());
          }
        });
    
        reverse_complement_item = new JMenuItem ("Reverse And Complement");
        reverse_complement_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            reverseAndComplement ();
          }
        });
    
        delete_bases_item = new JMenuItem ("Delete Selected Bases");
        delete_bases_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            deleteSelectedBases ();
          }
        });
    
        add_bases_item = new JMenuItem ("Add Bases At Selection");
        add_bases_item.addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent event) {
            addBases ();
          }
        });
    
        if (Options.readWritePossible ()) {
          // only the standalone version can save or read
          add_bases_from_file_item = new JMenuItem ("Add Bases From File ...");
          add_bases_from_file_item.addActionListener (new ActionListener () {
            public void actionPerformed (ActionEvent event) {
              addBasesFromFile ();
            }
          });
        }
    
        if (Options.getOptions ().getPropertyTruthValue ("val_mode")) {
          add (edit_feature_item);
          add (edit_subsequence_item);
          addSeparator ();
          add (edit_header_item);
          addSeparator ();
        }
    
        if (Options.getOptions ().getUndoLevels () > 0) {
          add (undo_item);
          addSeparator ();
        }
    
        if (!Options.getOptions ().getPropertyTruthValue ("val_mode")) {
          add (edit_feature_item);
          add (edit_subsequence_item);
          addSeparator ();
          add (edit_header_item);
          addSeparator ();
        }
    
        add (add_qualifiers_item);
        add (remove_qualifier_item);
        add (duplicate_item);
        add (merge_features_item);
        add (unmerge_feature_item);
        add (delete_features_item);
        add (delete_segments_item);
        add (delete_introns_item);
        addSeparator ();
        add (move_features_menu);
        add (copy_features_menu);
        addSeparator ();
        add (trim_item);
        add (trim_to_any_item);
        add (trim_to_next_item);
        add (trim_to_next_any_item);
        add (extend_to_prev_stop_item);
        add (extend_to_next_stop_item);
        add (fix_stop_codons_item);
        addSeparator ();
        add (auto_gene_name_item);
        add (fix_gene_names_item);
        add (reverse_complement_item);
        add (delete_bases_item);
        add (add_bases_item);
        if (Options.readWritePossible ()) {
          // only the standalone version can save or read
          add (add_bases_from_file_item);
        }
      }
    
      /**
       *  Undo the last change by calling ActionController.undo().
       *  @param frame The Frame to use for MessageDialog components.
       *  @param selection The current Selection - needs to be cleared before undo
       *  @param entry_group Used to get the ActionController for calling
       *    ActionController.undo().
       **/
      public static void undo (final JFrame frame,
                               final Selection selection,
                               final EntryGroup entry_group) {
        if (Options.getOptions ().getUndoLevels () == 0) {
          // undo disabled
          return;
        }
    
        if (entry_group.getActionController ().canUndo ()) {
          // clear the selection because something in the selection might
          // disappear after the undo() eg. create a feature, select it then undo
          selection.clear ();
        }
    
        if (!entry_group.getActionController ().undo ()) {
          new MessageDialog (frame, "sorry - no further undo information");
        }
      }
    
      /**
       *  editSelectedFeatures () etc. will only edit this many features.
       **/
      private static final int MAXIMUM_SELECTED_FEATURES = 25;
    
      /**
       *  Open an edit window (FeatureEdit) for each of the selected features.
       *  The edit component will listen for feature change events and update
       *  itself.
       *  @param frame The JFrame to use for MessageDialog components.
       *  @param selection The selected features to edit.
       *  @param entry_group Used to get the ActionController for calling
       *    startAction() and endAction().
       **/
      static void editSelectedFeatures (final JFrame frame,
                                        final EntryGroup entry_group,
                                        final Selection selection,
                                        final GotoEventSource goto_event_source) {
        final FeatureVector features_to_edit = selection.getAllFeatures ();
    
        if (features_to_edit.size () > MAXIMUM_SELECTED_FEATURES) {
          new MessageDialog (frame, "warning: only editing the first " +
                             MAXIMUM_SELECTED_FEATURES +
                             " selected features");
        }
    
        for (int i = 0 ;
             i < features_to_edit.size () && i < MAXIMUM_SELECTED_FEATURES ;
             ++i) {
          final Feature selection_feature = features_to_edit.elementAt (i);
    
          new FeatureEdit (selection_feature, entry_group, selection,
    
                           goto_event_source).setVisible(true);
    
    tjc's avatar
    tjc committed
        }
    
        selection.set (features_to_edit);
      }
    
      /**
       *  Create a new EntryEdit component that contains only the selected
       *  sequence and the features in the selected range.
       **/
      private void editSubSequence () {
        if (getSelection ().isEmpty ()) {
          new MessageDialog (getParentFrame (), "nothing selected");
        }
    
        final Range range = getSelection ().getSelectionRange ();
    
        final EntryGroup new_entry_group = getEntryGroup ().truncate (range);
    
    
        new EntryEdit (new_entry_group).setVisible(true);
    
    tjc's avatar
    tjc committed
    573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
      }
    
      /**
       *  Open a EntryHeaderEdit window for the default entry.
       **/
      private void editHeader () {
        final Entry default_entry = getEntryGroup ().getDefaultEntry ();
    
        if (default_entry == null) {
          final String message =
            "there is no default entry";
          new MessageDialog (getParentFrame (), message);
        } else {
          if (default_entry.isReadOnly ()) {
            new MessageDialog (getParentFrame (),
                               "the default entry is read-only " +
                               "- cannot continue");
            return;
          }
    
          new EntryHeaderEdit (entry_group, default_entry);
        }
      }
    
      /**
       *  Merge the selected features into one Feature.  If there are selected
       *  segments then the owning Feature of each segment will be the Feature
       *  that is merged.  This method will create a new Feature.
       *  @param frame The JFrame to use for MessageDialog components.
       *  @param selection The Selection containing the features to merge.
       *  @param entry_group Used to get the ActionController for calling
       *    startAction() and endAction().
       **/
      static void mergeFeatures (final JFrame frame,
                                 final Selection selection,
                                 final EntryGroup entry_group) {
        try {
          entry_group.getActionController ().startAction ();
    
          if (!checkForSelectionFeatures (frame, selection,
                                          10, "really merge all (>10) " +
                                          "selected features?")) {
            return;
          }
    
          final FeatureVector features_to_merge = selection.getAllFeatures ();
    
          if (features_to_merge.size () < 2) {
            new MessageDialog (frame,
                               "nothing to merge - select more than one feature");
            return;
          }
    
          final Feature merge_feature = features_to_merge.elementAt (0);
    
          // make sure all the features are on the same strand
          for (int i = 1 ; i < features_to_merge.size () ; ++i) {
            final Feature this_feature = features_to_merge.elementAt (i);
    
            if (this_feature.isForwardFeature () !=
                merge_feature.isForwardFeature ()) {
              new MessageDialog (frame,
                                 "all the features in a merge must be on the " +
                                 "same strand");
              return;
            }
    
            if (! this_feature.getKey ().equals (merge_feature.getKey ())) {
              new MessageDialog (frame,
                                 "all the features in a merge must have the " +
                                 "same key");
              return;
            }
          }
    
          if (Options.getOptions ().isNoddyMode ()) {
            final YesNoDialog dialog =
              new YesNoDialog (frame,
                               "Are you sure you want to merge the selected " +
                               "features?");
    
            if (!dialog.getResult ()) {
              return;
            }
          }
    
          final Feature new_feature;
    
          try {
            new_feature = merge_feature.duplicate ();
          } catch (ReadOnlyException e) {
            final String message =
              "one or more of the features is read-only or is in a " +
              "read-only entry - cannot continue";
            new MessageDialog (frame, message);
            return;
          }
    
          for (int i = 1 ; i < features_to_merge.size () ; ++i) {
            final Feature this_feature = features_to_merge.elementAt (i);
    
            final QualifierVector qualifiers = this_feature.getQualifiers ();
    
            for (int qualifier_index = 0 ;
                 qualifier_index < qualifiers.size () ;
                 ++qualifier_index) {
              final Qualifier this_qualifier =
                qualifiers.elementAt (qualifier_index);
    
              try {
                new_feature.addQualifierValues (this_qualifier);
              } catch (EntryInformationException e) {
                try {
                  new_feature.removeFromEntry ();
                } catch (ReadOnlyException _) {
                  // give up ...
                }
                final String message =
                  "destination entry does not support all the qualifiers " +
                  "needed by " + this_feature.getIDString ();
                new MessageDialog (frame, message);
              } catch (ReadOnlyException e) {
                final String message = "the new feature is read-only so " +
                  "some qualifiers have been lost";
                new MessageDialog (frame, message);
              }
            }
    
            final FeatureSegmentVector segments = this_feature.getSegments ();
    
            for (int segment_index = 0 ;
                 segment_index < segments.size () ;
                 ++segment_index) {
              final FeatureSegment this_segment =
                segments.elementAt (segment_index);
    
              final Range this_range = this_segment.getRawRange ();
    
              try {
                new_feature.addSegment (this_range);
              } catch (ReadOnlyException e) {
                final String message =
                  "merging failed because the entry is read-only";
                new MessageDialog (frame, message);
                try {
                  new_feature.removeFromEntry ();
                } catch (ReadOnlyException _) {
                  // oh dear ...
                }
              }
            }
          }
    
          // this is set to true when a merge is done in the next (inner) loop
          boolean keep_looping = true;
    
          // this is a bit inefficient, but there aren't normally many segments
      LOOP:
          while (keep_looping) {
            final FeatureSegmentVector feature_segments =
              new_feature.getSegments ();
    
            keep_looping = false;
    
            // now merge overlapping ranges
            for (int segment_index = 0 ;
                 segment_index < feature_segments.size () - 1;
                 ++segment_index) {
    
              final FeatureSegment this_segment =
                feature_segments.elementAt (segment_index);
              final MarkerRange this_range = this_segment.getMarkerRange ();
    
              final FeatureSegment next_segment =
                feature_segments.elementAt (segment_index + 1);
              final MarkerRange next_range = next_segment.getMarkerRange ();
    
              // if it overlaps the next Range then merge it
              if (this_range.overlaps (next_range) &&
                  this_segment.getFrameID () == next_segment.getFrameID ()) {
                try {
                  final Range new_range =
                    this_range.combineRanges (next_range, false).getRawRange ();
                  new_feature.addSegment (new_range);
                  new_feature.removeSegment (this_segment);
                  new_feature.removeSegment (next_segment);
    
                  // start again
                  keep_looping = true;
                  continue LOOP;
                } catch (ReadOnlyException e) {
                  final String message =
                    "merging failed because the entry is read-only";
                  new MessageDialog (frame, message);
                } catch (LastSegmentException e) {
                  throw new Error ("internal error - tried to remove " +
                                   "last segment: " + e);
                }
              }
            }
          }
    
          boolean delete_old_features;
    
          if (Options.getOptions ().isNoddyMode ()) {
            final YesNoDialog delete_old_dialog =
              new YesNoDialog (frame, "delete old features?");
    
            delete_old_features = delete_old_dialog.getResult ();
          } else {
            delete_old_features = true;
          }
    
          if (delete_old_features) {
            if (getReadOnlyFeatures (features_to_merge).size () > 0) {
              new MessageDialog (frame, "deletion failed because the features " +
                                 "are read-only");
           } else {
              for (int i = 0 ; i < features_to_merge.size () ; ++i) {
                try {
                  features_to_merge.elementAt (i).removeFromEntry ();
                } catch (ReadOnlyException e) {
                  new MessageDialog (frame, "deletion failed one or more of the " +
                                     "features are read-only");
                }
              }
            }
          }
    
          selection.set (new_feature);
        } finally {
          entry_group.getActionController ().endAction ();
        }
      }
    
      /**
       *  If the selection contains exactly two segments and those segments are
       *  adjacent in the same feature, split the feature into two pieces.  The
       *  orignal feature is truncated and a new feature is created.  The
       *  qualifiers of the old feature are copied to new feature.
       *  @param frame The JFrame to use for MessageDialog components.
       *  @param selection The Selection containing the segments to unmerge.
       *  @param entry_group Used to get the ActionController for calling
       *    startAction() and endAction().
       **/
      static void unmergeFeature (final JFrame frame,
                                  final Selection selection,
                                  final EntryGroup entry_group) {
        try {
          entry_group.getActionController ().startAction ();
    
          final FeatureSegmentVector selected_segments =
            selection.getSelectedSegments ();
    
          if (selected_segments.size () != 2) {
            final String message =
              "you need to select exactly two exons use unmerge";
            new MessageDialog (frame, message);
            return;
          }
    
          FeatureSegment first_segment = selected_segments.elementAt (0);
          FeatureSegment second_segment = selected_segments.elementAt (1);
    
          if (first_segment.getFeature () != second_segment.getFeature ()) {
            final String message =
              "you need to select two exons from the same feature to use unmerge";
            new MessageDialog (frame, message);
            return;
          }
    
          final Feature segment_feature = first_segment.getFeature ();
    
          final FeatureSegmentVector all_feature_segments =
            segment_feature.getSegments ();
    
          int index_of_first_segment =
            all_feature_segments.indexOf (first_segment);
          int index_of_second_segment =
            all_feature_segments.indexOf (second_segment);
    
          if (index_of_first_segment - index_of_second_segment < -1 ||
              index_of_first_segment - index_of_second_segment > 1) {
            final String message =
              "you need to select two adjacent exons to use unmerge";
            new MessageDialog (frame, message);
            return;
          }
    
          if (index_of_second_segment <  index_of_first_segment) {
            // swap the segments for consistency
            final FeatureSegment temp_segment = first_segment;
            final int temp_segment_index = index_of_first_segment;
    
            first_segment = second_segment;
            index_of_first_segment = index_of_second_segment;
    
            second_segment = temp_segment;
            index_of_second_segment = temp_segment_index;
          }
    
          try {
            final Feature new_feature = segment_feature.duplicate ();
    
            // we set the Selection later
            selection.clear ();
    
            // delete the segments starting at index_of_second_segment from
            // segment_feature and delete the segments up to (and including)
            // index_of_first_segment from new_feature
    
            for (int i = all_feature_segments.size () - 1 ;
                 i >= index_of_second_segment ;
                 --i) {
              segment_feature.getSegments ().elementAt (i).removeFromFeature ();
            }
    
            // remove the first segment of new_feature index_of_first_segment times
            for (int i = 0 ; i <= index_of_first_segment ; ++i) {
              new_feature.getSegments ().elementAt (0).removeFromFeature ();
            }
    
            selection.set (segment_feature.getSegments ().lastElement ());
            selection.add (new_feature.getSegments ().elementAt (0));
          } catch (ReadOnlyException e) {
            final String message =
              "the selected exons (in " +
              segment_feature.getIDString () +
              ") are in a read only entry - cannot continue";
            new MessageDialog (frame, message);
          } catch (LastSegmentException e) {
            throw new Error ("internal error - unexpected exception: " + e);
          }
        } finally {
          entry_group.getActionController ().endAction ();
        }
      }
    
      /**
       *  Create a QualifierEditor JFrame that acts on the selected features.
       *  @param frame The JFrame to use for MessageDialog components.
       **/
      private void addQualifiers (final JFrame frame, final Selection selection) {
        if (!checkForSelectionFeatures (frame, selection)) {
          return;
        }
    
        final FeatureVector selected_features = selection.getAllFeatures ();
    
        if (getReadOnlyFeatures (selected_features).size () > 0) {
          new MessageDialog (frame,
                             "one or more of the selected features is read-only " +
                             "- cannot continue");
          return;
        }
    
        final QualifierEditor qualifier_editor =
          new QualifierEditor (selected_features, getEntryGroup ());
    
        qualifier_editor.setVisible (true);
      }
    
      /**
       *  Offer the user a choice of qualifier to remove from the selected features
       **/
      private void removeQualifier (final JFrame frame, final Selection selection) {
        if (!checkForSelectionFeatures (frame, selection)) {
          return;
        }
    
        final FeatureVector selected_features = selection.getAllFeatures ();
    
        final StringVector qualifier_names =
          Feature.getAllQualifierNames (selected_features);
    
        if (qualifier_names.size () == 0) {
          new MessageDialog (getParentFrame (), "feature has no qualifiers");
          return;
        }
    
        final ChoiceFrame choice_frame =
          new ChoiceFrame ("Select a qualifer name", qualifier_names);
    
        final JComboBox choice = choice_frame.getChoice ();
    
    
        final int MAX_VISIBLE_ROWS = 30;
        
        choice.setMaximumRowCount (MAX_VISIBLE_ROWS);
        
        choice.addItemListener (new ItemListener () {
          public void itemStateChanged (ItemEvent _) {
            removeQualifierFromFeatures (selected_features,
                                         (String) choice.getSelectedItem ());
            choice_frame.setVisible (false);
            choice_frame.dispose ();
          }
        });
    
        choice_frame.getOKButton ().addActionListener (new ActionListener () {
          public void actionPerformed (ActionEvent _) {
            removeQualifierFromFeatures (selected_features,
                                         (String) choice.getSelectedItem ());
          }
        });
    
        choice_frame.setVisible (true);
      }
    
      /**
       *  Remove the qualifier given by qualifier_name from the given features.
       *  Silently ignore features that don't have that qualifier.  Warn the user
       *  about read-only features.
       **/
      private void removeQualifierFromFeatures (final FeatureVector features,
                                                final String qualifier_name) {
        boolean found_read_only = false;
    
        try {
          entry_group.getActionController ().startAction ();
    
          for (int i = 0 ; i < features.size () ; ++i) {
            final Feature this_feature = features.elementAt (i);
            try {
              this_feature.removeQualifierByName (qualifier_name);
            } catch (OutOfDateException _) {
              // ignore
            } catch (EntryInformationException _) {