merge from 2_4_Release branch
[jalview.git] / src / jalview / appletgui / APopupMenu.java
index 7c1ea3f..f7ecfda 100755 (executable)
@@ -1,22 +1,21 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer
- * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
- *
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
+ * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
+ * 
  * 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
  */
-
 package jalview.appletgui;
 
 import java.util.*;
@@ -31,60 +30,97 @@ import jalview.schemes.*;
 import jalview.util.UrlLink;
 import jalview.io.AppletFormatAdapter;
 
-public class APopupMenu
-    extends java.awt.PopupMenu implements ActionListener, ItemListener
+public class APopupMenu extends java.awt.PopupMenu implements
+        ActionListener, ItemListener
 {
   Menu groupMenu = new Menu();
+
   MenuItem editGroupName = new MenuItem();
+
   protected MenuItem clustalColour = new MenuItem();
+
   protected MenuItem zappoColour = new MenuItem();
+
   protected MenuItem taylorColour = new MenuItem();
+
   protected MenuItem hydrophobicityColour = new MenuItem();
+
   protected MenuItem helixColour = new MenuItem();
+
   protected MenuItem strandColour = new MenuItem();
+
   protected MenuItem turnColour = new MenuItem();
+
   protected MenuItem buriedColour = new MenuItem();
+
   protected CheckboxMenuItem abovePIDColour = new CheckboxMenuItem();
+
   protected MenuItem userDefinedColour = new MenuItem();
+
   protected MenuItem PIDColour = new MenuItem();
+
   protected MenuItem BLOSUM62Colour = new MenuItem();
+
   MenuItem noColourmenuItem = new MenuItem();
+
   protected CheckboxMenuItem conservationMenuItem = new CheckboxMenuItem();
 
   final AlignmentPanel ap;
+
   MenuItem unGroupMenuItem = new MenuItem();
+
   MenuItem nucleotideMenuItem = new MenuItem();
+
   Menu colourMenu = new Menu();
+
   CheckboxMenuItem showBoxes = new CheckboxMenuItem();
+
   CheckboxMenuItem showText = new CheckboxMenuItem();
+
   CheckboxMenuItem showColourText = new CheckboxMenuItem();
+
   Menu editMenu = new Menu("Edit");
+
   MenuItem copy = new MenuItem("Copy (Jalview Only)");
+
   MenuItem cut = new MenuItem("Cut (Jalview Only)");
+
   MenuItem toUpper = new MenuItem("To Upper Case");
+
   MenuItem toLower = new MenuItem("To Lower Case");
+
   MenuItem toggleCase = new MenuItem("Toggle Case");
+
   Menu outputmenu = new Menu();
+
   Menu seqMenu = new Menu();
+
   MenuItem pdb = new MenuItem();
+
   MenuItem hideSeqs = new MenuItem();
+
   MenuItem repGroup = new MenuItem();
+
   MenuItem sequenceName = new MenuItem("Edit Name/Description");
+
   MenuItem sequenceFeature = new MenuItem("Create Sequence Feature");
+
   MenuItem editSequence = new MenuItem("Edit Sequence");
 
   Sequence seq;
+
   MenuItem revealAll = new MenuItem();
+
   Menu menu1 = new Menu();
 
   public APopupMenu(AlignmentPanel apanel, final Sequence seq, Vector links)
   {
-    ///////////////////////////////////////////////////////////
+    // /////////////////////////////////////////////////////////
     // If this is activated from the sequence panel, the user may want to
     // edit or annotate a particular residue. Therefore display the residue menu
     //
     // If from the IDPanel, we must display the sequence menu
-    //////////////////////////////////////////////////////////
+    // ////////////////////////////////////////////////////////
 
     this.ap = apanel;
     this.seq = seq;
@@ -92,17 +128,15 @@ public class APopupMenu
     try
     {
       jbInit();
-    }
-    catch (Exception e)
+    } catch (Exception e)
     {
       e.printStackTrace();
     }
 
-    for (int i = 0; i < jalview.io.AppletFormatAdapter.WRITEABLE_FORMATS.length;
-         i++)
+    for (int i = 0; i < jalview.io.AppletFormatAdapter.WRITEABLE_FORMATS.length; i++)
     {
-      MenuItem item = new MenuItem(jalview.io.AppletFormatAdapter.
-                                   WRITEABLE_FORMATS[i]);
+      MenuItem item = new MenuItem(
+              jalview.io.AppletFormatAdapter.WRITEABLE_FORMATS[i]);
 
       item.addActionListener(this);
       outputmenu.add(item);
@@ -128,7 +162,7 @@ public class APopupMenu
       remove(groupMenu);
     }
 
-    if (links != null && links.size()>0)
+    if (links != null && links.size() > 0)
     {
       Menu linkMenu = new Menu("Link");
       String link;
@@ -141,92 +175,82 @@ public class APopupMenu
           System.err.println(urlLink.getInvalidMessage());
           continue;
         }
-        final String target = urlLink.getTarget(); // link.substring(0, link.indexOf("|"));
+        final String target = urlLink.getTarget(); // link.substring(0,
+                                                    // link.indexOf("|"));
         final String label = urlLink.getLabel();
         if (urlLink.isDynamic())
         {
 
           // collect matching db-refs
-          DBRefEntry[] dbr = jalview.util.DBRefUtils.selectRefs(seq.getDBRef(), new String[]{target});
+          DBRefEntry[] dbr = jalview.util.DBRefUtils.selectRefs(seq
+                  .getDBRef(), new String[]
+          { target });
           // collect id string too
           String id = seq.getName();
-          if (dbr!=null)
+          if (dbr != null)
           {
-            for (int r=0;r<dbr.length; r++)
+            for (int r = 0; r < dbr.length; r++)
             {
-              if (id!=null && dbr[r].getAccessionId().equals(id))
+              if (id != null && dbr[r].getAccessionId().equals(id))
               {
-                // suppress duplicate link creation for the bare sequence ID string with this link
+                // suppress duplicate link creation for the bare sequence ID
+                // string with this link
                 id = null;
               }
               // create Bare ID link for this RUL
-              String[] urls = urlLink.makeUrls(dbr[r].getAccessionId(), true);
-              if (urls!=null)
+              String[] urls = urlLink.makeUrls(dbr[r].getAccessionId(),
+                      true);
+              if (urls != null)
               {
-                for (int u=0; u<urls.length; u+=2)
+                for (int u = 0; u < urls.length; u += 2)
                 {
-                  addshowLink(linkMenu, label+"|"+urls[u],urls[u+1]);
+                  addshowLink(linkMenu, label + "|" + urls[u], urls[u + 1]);
                 }
               }
             }
           }
-          if (id!=null)
+          if (id != null)
           {
             // create Bare ID link for this RUL
             String[] urls = urlLink.makeUrls(id, true);
-            if (urls!=null)
+            if (urls != null)
             {
-              for (int u=0; u<urls.length; u+=2)
+              for (int u = 0; u < urls.length; u += 2)
               {
-                addshowLink(linkMenu, label,urls[u+1]);
+                addshowLink(linkMenu, label, urls[u + 1]);
               }
             }
-            // addshowLink(linkMenu, target,  url_pref + id + url_suff);
+            // addshowLink(linkMenu, target, url_pref + id + url_suff);
           }
-        } else {
-          addshowLink(linkMenu, target, urlLink.getUrl_prefix()); // link.substring(link.lastIndexOf("|")+1));
         }
-        /*final String url;
-        
-        if (link.indexOf("$SEQUENCE_ID$") > -1)
+        else
         {
-          // Substitute SEQUENCE_ID string and any matching database reference accessions
-          String url_pref = link.substring(link.indexOf("|") + 1,
-                  link.indexOf("$SEQUENCE_ID$"));
-          
-          String url_suff = link.substring(link.indexOf("$SEQUENCE_ID$") + 13); 
-
-          // collect matching db-refs
-          DBRefEntry[] dbr = jalview.util.DBRefUtils.selectRefs(seq.getDBRef(), new String[]{target});
-          // collect id string too
-          String id = seq.getName();
-          if (id.indexOf("|") > -1)
-          {
-            id = id.substring(id.lastIndexOf("|") + 1);
-          }
-          if (dbr!=null)
-          {
-            for (int r=0;r<dbr.length; r++)
-            {
-              if (dbr[r].getAccessionId().equals(id))
-              {
-                // suppress duplicate link creation for the bare sequence ID string with this link
-                id = null;
-              }
-              addshowLink(linkMenu, dbr[r].getSource()+"|"+dbr[r].getAccessionId(), target,
-                      url_pref+dbr[r].getAccessionId()+url_suff);
-            }
-          }
-          if (id!=null)
-          {
-            // create Bare ID link for this RUL
-            addshowLink(linkMenu, target,  url_pref + id + url_suff);
-          }
-        } else {
-          addshowLink(linkMenu, target, link.substring(link.lastIndexOf("|")+1));
-        } */
+          addshowLink(linkMenu, target, urlLink.getUrl_prefix()); // link.substring(link.lastIndexOf("|")+1));
+        }
+        /*
+         * final String url;
+         * 
+         * if (link.indexOf("$SEQUENCE_ID$") > -1) { // Substitute SEQUENCE_ID
+         * string and any matching database reference accessions String url_pref =
+         * link.substring(link.indexOf("|") + 1, link.indexOf("$SEQUENCE_ID$"));
+         * 
+         * String url_suff = link.substring(link.indexOf("$SEQUENCE_ID$") + 13);
+         *  // collect matching db-refs DBRefEntry[] dbr =
+         * jalview.util.DBRefUtils.selectRefs(seq.getDBRef(), new
+         * String[]{target}); // collect id string too String id =
+         * seq.getName(); if (id.indexOf("|") > -1) { id =
+         * id.substring(id.lastIndexOf("|") + 1); } if (dbr!=null) { for (int
+         * r=0;r<dbr.length; r++) { if (dbr[r].getAccessionId().equals(id)) { //
+         * suppress duplicate link creation for the bare sequence ID string with
+         * this link id = null; } addshowLink(linkMenu,
+         * dbr[r].getSource()+"|"+dbr[r].getAccessionId(), target,
+         * url_pref+dbr[r].getAccessionId()+url_suff); } } if (id!=null) { //
+         * create Bare ID link for this RUL addshowLink(linkMenu, target,
+         * url_pref + id + url_suff); } } else { addshowLink(linkMenu, target,
+         * link.substring(link.lastIndexOf("|")+1)); }
+         */
       }
-    
+
       if (seq != null)
       {
         seqMenu.add(linkMenu);
@@ -251,24 +275,35 @@ public class APopupMenu
       remove(revealAll);
     }
   }
+
   /**
    * add a show URL menu item to the given linkMenu
+   * 
    * @param linkMenu
-   * @param target - menu label string
-   * @param url - url to open
+   * @param target -
+   *                menu label string
+   * @param url -
+   *                url to open
    */
-  private void addshowLink(Menu linkMenu, final String target, final String url)
+  private void addshowLink(Menu linkMenu, final String target,
+          final String url)
   {
     addshowLink(linkMenu, target, target, url);
   }
+
   /**
    * add a show URL menu item to the given linkMenu
+   * 
    * @param linkMenu
-   * @param target - URL target window
-   * @param label - menu label string
-   * @param url - url to open
+   * @param target -
+   *                URL target window
+   * @param label -
+   *                menu label string
+   * @param url -
+   *                url to open
    */
-  private void addshowLink(Menu linkMenu, final String target, final String label, final String url)
+  private void addshowLink(Menu linkMenu, final String target,
+          final String label, final String url)
   {
     MenuItem item = new MenuItem(label);
     item.addActionListener(new java.awt.event.ActionListener()
@@ -280,7 +315,6 @@ public class APopupMenu
     });
     linkMenu.add(item);
   }
 
   public void itemStateChanged(ItemEvent evt)
   {
@@ -390,14 +424,10 @@ public class APopupMenu
 
     else if (source == editGroupName)
     {
-      EditNameDialog dialog = new EditNameDialog(
-          getGroup().getName(),
-          getGroup().getDescription(),
-          "       Group Name",
-          "Group Description",
-          ap.alignFrame,
-          "Edit Group Name / Description",
-          500,100, true);
+      EditNameDialog dialog = new EditNameDialog(getGroup().getName(),
+              getGroup().getDescription(), "       Group Name",
+              "Group Description", ap.alignFrame,
+              "Edit Group Name / Description", 500, 100, true);
 
       if (dialog.accept)
       {
@@ -414,39 +444,33 @@ public class APopupMenu
     {
       ap.alignFrame.cut_actionPerformed();
     }
-    else if(source == editSequence)
+    else if (source == editSequence)
     {
       SequenceGroup sg = ap.av.getSelectionGroup();
 
-      if(sg!=null)
+      if (sg != null)
       {
         if (seq == null)
           seq = (Sequence) sg.getSequenceAt(0);
 
         EditNameDialog dialog = new EditNameDialog(seq.getSequenceAsString(
-            sg.getStartRes(),
-            sg.getEndRes() + 1),
-                                                   null,
-                                                   "Edit Sequence ",
-                                                   null,
+                sg.getStartRes(), sg.getEndRes() + 1), null,
+                "Edit Sequence ", null,
 
-                                                   ap.alignFrame,
-                                                   "Edit Sequence",
-                                                   500, 100, true);
+                ap.alignFrame, "Edit Sequence", 500, 100, true);
 
         if (dialog.accept)
         {
-          EditCommand editCommand = new EditCommand(
-              "Edit Sequences", EditCommand.REPLACE,
-              dialog.getName().replace(' ', ap.av.getGapCharacter()),
-              sg.getSequencesAsArray(ap.av.hiddenRepSequences),
-              sg.getStartRes(), sg.getEndRes()+1, ap.av.alignment
-              );
+          EditCommand editCommand = new EditCommand("Edit Sequences",
+                  EditCommand.REPLACE, dialog.getName().replace(' ',
+                          ap.av.getGapCharacter()), sg
+                          .getSequencesAsArray(ap.av.hiddenRepSequences),
+                  sg.getStartRes(), sg.getEndRes() + 1, ap.av.alignment);
 
           ap.alignFrame.addHistoryItem(editCommand);
 
-          ap.av.firePropertyChange("alignment", null,
-                                 ap.av.getAlignment().getSequences());
+          ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
+                  .getSequences());
         }
       }
     }
@@ -480,15 +504,14 @@ public class APopupMenu
           }
 
           regions.addElement(new int[]
-                             {start, end});
+          { start, end });
 
           if (ap.av.hasHiddenColumns)
           {
             start = ap.av.colSel.adjustForHiddenColumns(end);
             start = ap.av.colSel.getHiddenBoundaryLeft(start) + 1;
           }
-        }
-        while (end < sg.getEndRes());
+        } while (end < sg.getEndRes());
 
         int[][] startEnd = new int[regions.size()][2];
         for (int i = 0; i < regions.size(); i++)
@@ -515,19 +538,18 @@ public class APopupMenu
           caseChange = ChangeCaseCommand.TO_LOWER;
         }
 
-        ChangeCaseCommand caseCommand = new ChangeCaseCommand(
-            description, sg.getSequencesAsArray(ap.av.hiddenRepSequences),
-            startEnd, caseChange
-            );
+        ChangeCaseCommand caseCommand = new ChangeCaseCommand(description,
+                sg.getSequencesAsArray(ap.av.hiddenRepSequences), startEnd,
+                caseChange);
 
         ap.alignFrame.addHistoryItem(caseCommand);
 
-        ap.av.firePropertyChange("alignment", null,
-                                 ap.av.getAlignment().getSequences());
+        ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
+                .getSequences());
 
       }
     }
-    else if(source == sequenceFeature)
+    else if (source == sequenceFeature)
     {
       SequenceGroup sg = ap.av.getSelectionGroup();
       if (sg == null)
@@ -545,11 +567,11 @@ public class APopupMenu
         int start = sg.getSequenceAt(i).findPosition(sg.getStartRes());
         int end = sg.findEndRes(sg.getSequenceAt(i));
         features[i] = new SequenceFeature(null, null, null, start, end,
-                                          "Jalview");
+                "Jalview");
       }
 
-      if (ap.seqPanel.seqCanvas.getFeatureRenderer()
-          .amendFeatures(seqs, features, true, ap))
+      if (ap.seqPanel.seqCanvas.getFeatureRenderer().amendFeatures(seqs,
+              features, true, ap))
       {
         ap.alignFrame.sequenceFeatures.setState(true);
         ap.av.showSequenceFeatures(true);
@@ -569,27 +591,21 @@ public class APopupMenu
 
     Frame frame = new Frame();
     frame.add(cap);
-    jalview.bin.JalviewLite.addFrame(frame,
-                                     "Selection output - " + e.getActionCommand(),
-                                     600, 500);
+    jalview.bin.JalviewLite.addFrame(frame, "Selection output - "
+            + e.getActionCommand(), 600, 500);
 
-    cap.setText(new jalview.io.AppletFormatAdapter().formatSequences(
-        e.getActionCommand(),
-        new Alignment(ap.av.getSelectionAsNewSequence()),
-        ap.av.showJVSuffix));
+    cap.setText(new jalview.io.AppletFormatAdapter().formatSequences(e
+            .getActionCommand(), new Alignment(ap.av
+            .getSelectionAsNewSequence()), ap.av.showJVSuffix));
 
   }
 
   void editName()
   {
-    EditNameDialog dialog = new EditNameDialog(
-        seq.getName(),
-        seq.getDescription(),
-        "       Sequence Name",
-        "Sequence Description",
-        ap.alignFrame,
-        "Edit Sequence Name / Description",
-        500,100, true);
+    EditNameDialog dialog = new EditNameDialog(seq.getName(), seq
+            .getDescription(), "       Sequence Name",
+            "Sequence Description", ap.alignFrame,
+            "Edit Sequence Name / Description", 500, 100, true);
 
     if (dialog.accept)
     {
@@ -601,22 +617,16 @@ public class APopupMenu
 
   void addPDB()
   {
-    if(seq.getPDBId()!=null)
+    if (seq.getPDBId() != null)
     {
-      PDBEntry entry = (PDBEntry)seq.getPDBId().firstElement();
+      PDBEntry entry = (PDBEntry) seq.getPDBId().firstElement();
 
-      if ( ap.av.applet.jmolAvailable )
-       new jalview.appletgui.AppletJmol(entry,
-                                        new Sequence[]{seq},
-                                        null,
-                                        ap,
-                                        AppletFormatAdapter.URL);
-     else
-       new MCview.AppletPDBViewer(entry,
-                                  new Sequence[]{seq},
-                                  null,
-                                  ap,
-                                  AppletFormatAdapter.URL);
+      if (ap.av.applet.jmolAvailable)
+        new jalview.appletgui.AppletJmol(entry, new Sequence[]
+        { seq }, null, ap, AppletFormatAdapter.URL);
+      else
+        new MCview.AppletPDBViewer(entry, new Sequence[]
+        { seq }, null, ap, AppletFormatAdapter.URL);
 
     }
     else
@@ -630,8 +640,7 @@ public class APopupMenu
     }
   }
 
-  private void jbInit()
-      throws Exception
+  private void jbInit() throws Exception
   {
     groupMenu.setLabel("Group");
     groupMenu.setLabel("Selection");
@@ -754,8 +763,9 @@ public class APopupMenu
   protected void clustalColour_actionPerformed()
   {
     SequenceGroup sg = getGroup();
-    sg.cs = new ClustalxColourScheme(sg.getSequences(ap.av.hiddenRepSequences),
-                                     ap.av.alignment.getWidth());
+    sg.cs = new ClustalxColourScheme(sg
+            .getSequences(ap.av.hiddenRepSequences), ap.av.alignment
+            .getWidth());
     refresh();
   }
 
@@ -817,18 +827,19 @@ public class APopupMenu
 
     if (abovePIDColour.getState())
     {
-      sg.cs.setConsensus(AAFrequency.calculate(sg.getSequences(ap.av.
-          hiddenRepSequences), 0,
-                                               ap.av.alignment.getWidth()));
-      int threshold = SliderPanel.setPIDSliderSource(ap, sg.cs,
-          getGroup().getName());
+      sg.cs.setConsensus(AAFrequency.calculate(sg
+              .getSequences(ap.av.hiddenRepSequences), 0, ap.av.alignment
+              .getWidth()));
+      int threshold = SliderPanel.setPIDSliderSource(ap, sg.cs, getGroup()
+              .getName());
 
       sg.cs.setThreshold(threshold, ap.av.getIgnoreGapsConsensus());
 
       SliderPanel.showPIDSlider();
 
     }
-    else // remove PIDColouring
+    else
+    // remove PIDColouring
     {
       sg.cs.setThreshold(0, ap.av.getIgnoreGapsConsensus());
     }
@@ -846,9 +857,9 @@ public class APopupMenu
   {
     SequenceGroup sg = getGroup();
     sg.cs = new PIDColourScheme();
-    sg.cs.setConsensus(AAFrequency.calculate(sg.getSequences(ap.av.
-        hiddenRepSequences), 0,
-                                             ap.av.alignment.getWidth()));
+    sg.cs.setConsensus(AAFrequency.calculate(sg
+            .getSequences(ap.av.hiddenRepSequences), 0, ap.av.alignment
+            .getWidth()));
     refresh();
   }
 
@@ -858,9 +869,9 @@ public class APopupMenu
 
     sg.cs = new Blosum62ColourScheme();
 
-    sg.cs.setConsensus(AAFrequency.calculate(sg.getSequences(ap.av.
-        hiddenRepSequences), 0,
-                                             ap.av.alignment.getWidth()));
+    sg.cs.setConsensus(AAFrequency.calculate(sg
+            .getSequences(ap.av.hiddenRepSequences), 0, ap.av.alignment
+            .getWidth()));
 
     refresh();
   }
@@ -883,10 +894,9 @@ public class APopupMenu
     {
 
       Conservation c = new Conservation("Group",
-                                        ResidueProperties.propHash, 3,
-                                        sg.getSequences(ap.av.
-          hiddenRepSequences), 0,
-                                        ap.av.alignment.getWidth());
+              ResidueProperties.propHash, 3, sg
+                      .getSequences(ap.av.hiddenRepSequences), 0,
+              ap.av.alignment.getWidth());
 
       c.calculate();
       c.verdict(false, ap.av.ConsPercGaps);
@@ -896,7 +906,8 @@ public class APopupMenu
       SliderPanel.setConservationSlider(ap, sg.cs, sg.getName());
       SliderPanel.showConservationSlider();
     }
-    else // remove ConservationColouring
+    else
+    // remove ConservationColouring
     {
       sg.cs.setConservation(null);
     }
@@ -949,7 +960,7 @@ public class APopupMenu
     if (sg == null || sg.getSize() < 1)
     {
       ap.av.hideSequence(new SequenceI[]
-                         {seq});
+      { seq });
       return;
     }