JAL-2189 source formatting
[jalview.git] / src / jalview / gui / IdPanel.java
index deb1167..a65be7b 100755 (executable)
@@ -1,6 +1,6 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
@@ -26,7 +26,9 @@ import jalview.datamodel.SequenceGroup;
 import jalview.datamodel.SequenceI;
 import jalview.io.SequenceAnnotationReport;
 import jalview.util.MessageManager;
+import jalview.util.Platform;
 import jalview.util.UrlLink;
+import jalview.viewmodel.AlignmentViewport;
 
 import java.awt.BorderLayout;
 import java.awt.event.MouseEvent;
@@ -52,9 +54,9 @@ import javax.swing.ToolTipManager;
 public class IdPanel extends JPanel implements MouseListener,
         MouseMotionListener, MouseWheelListener
 {
-  protected IdCanvas idCanvas;
+  private IdCanvas idCanvas;
 
-  protected AlignViewport av;
+  protected AlignmentViewport av;
 
   protected AlignmentPanel alignPanel;
 
@@ -81,11 +83,11 @@ public class IdPanel extends JPanel implements MouseListener,
   {
     this.av = av;
     alignPanel = parent;
-    idCanvas = new IdCanvas(av);
+    setIdCanvas(new IdCanvas(av));
     linkImageURL = getClass().getResource("/images/link.gif").toString();
     seqAnnotReport = new SequenceAnnotationReport(linkImageURL);
     setLayout(new BorderLayout());
-    add(idCanvas, BorderLayout.CENTER);
+    add(getIdCanvas(), BorderLayout.CENTER);
     addMouseListener(this);
     addMouseMotionListener(this);
     addMouseWheelListener(this);
@@ -102,18 +104,17 @@ public class IdPanel extends JPanel implements MouseListener,
   @Override
   public void mouseMoved(MouseEvent e)
   {
-    SeqPanel sp = alignPanel.seqPanel;
+    SeqPanel sp = alignPanel.getSeqPanel();
     int seq = Math.max(0, sp.findSeq(e));
     if (seq > -1 && seq < av.getAlignment().getHeight())
     {
       SequenceI sequence = av.getAlignment().getSequenceAt(seq);
       StringBuffer tip = new StringBuffer(64);
-      seqAnnotReport
-              .createSequenceAnnotationReport(tip, sequence,
-                      av.isShowDbRefs(), av.isShowNpFeats(),
-                      sp.seqCanvas.fr.minmax);
-      setToolTipText("<html>" + sequence.getDisplayId(true) + " "
-              + tip.toString() + "</html>");
+      seqAnnotReport.createSequenceAnnotationReport(tip, sequence,
+              av.isShowDBRefs(), av.isShowNPFeats(),
+              sp.seqCanvas.fr.getMinMax());
+      setToolTipText(JvSwingUtils.wrapTooltip(true,
+              sequence.getDisplayId(true) + " " + tip.toString()));
     }
   }
 
@@ -128,7 +129,7 @@ public class IdPanel extends JPanel implements MouseListener,
   {
     mouseDragging = true;
 
-    int seq = Math.max(0, alignPanel.seqPanel.findSeq(e));
+    int seq = Math.max(0, alignPanel.getSeqPanel().findSeq(e));
 
     if (seq < lastid)
     {
@@ -193,6 +194,8 @@ public class IdPanel extends JPanel implements MouseListener,
      */
     if (e.getClickCount() < 2 || SwingUtilities.isRightMouseButton(e))
     {
+      // reinstate isRightMouseButton check to ignore mouse-related popup events
+      // note - this does nothing on default MacBookPro force-trackpad config!
       return;
     }
 
@@ -202,7 +205,7 @@ public class IdPanel extends JPanel implements MouseListener,
       return;
     }
 
-    int seq = alignPanel.seqPanel.findSeq(e);
+    int seq = alignPanel.getSeqPanel().findSeq(e);
     String url = null;
     int i = 0;
     String id = av.getAlignment().getSequenceAt(seq).getName();
@@ -244,11 +247,10 @@ public class IdPanel extends JPanel implements MouseListener,
       jalview.util.BrowserLauncher.openURL(url);
     } catch (Exception ex)
     {
-      JOptionPane
-              .showInternalMessageDialog(
-                      Desktop.desktop,
-                      MessageManager.getString("label.web_browser_not_found_unix"),
-                      MessageManager.getString("label.web_browser_not_found"), JOptionPane.WARNING_MESSAGE);
+      JOptionPane.showInternalMessageDialog(Desktop.desktop,
+              MessageManager.getString("label.web_browser_not_found_unix"),
+              MessageManager.getString("label.web_browser_not_found"),
+              JOptionPane.WARNING_MESSAGE);
       ex.printStackTrace();
     }
   }
@@ -315,39 +317,24 @@ public class IdPanel extends JPanel implements MouseListener,
       return;
     }
 
-    int seq = alignPanel.seqPanel.findSeq(e);
-
-    if (SwingUtilities.isRightMouseButton(e))
+    if (e.isPopupTrigger()) // Mac reports this in mousePressed
     {
-      Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq);
-      // build a new links menu based on the current links + any non-positional
-      // features
-      Vector nlinks = new Vector(Preferences.sequenceURLLinks);
-      SequenceFeature sf[] = sq == null ? null : sq.getDatasetSequence()
-              .getSequenceFeatures();
-      for (int sl = 0; sf != null && sl < sf.length; sl++)
-      {
-        if (sf[sl].begin == sf[sl].end && sf[sl].begin == 0)
-        {
-          if (sf[sl].links != null && sf[sl].links.size() > 0)
-          {
-            for (int l = 0, lSize = sf[sl].links.size(); l < lSize; l++)
-            {
-              nlinks.addElement(sf[sl].links.elementAt(l));
-            }
-          }
-        }
-      }
-
-      jalview.gui.PopupMenu pop = new jalview.gui.PopupMenu(alignPanel, sq,
-              nlinks, new Vector(Preferences.getGroupURLLinks()));
-      pop.show(this, e.getX(), e.getY());
+      showPopupMenu(e);
+      return;
+    }
 
+    /*
+     * defer right-mouse click handling to mouseReleased on Windows
+     * (where isPopupTrigger() will answer true)
+     * NB isRightMouseButton is also true for Cmd-click on Mac
+     */
+    if (SwingUtilities.isRightMouseButton(e) && !Platform.isAMac())
+    {
       return;
     }
 
     if ((av.getSelectionGroup() == null)
-            || ((!e.isControlDown() && !e.isShiftDown()) && av
+            || (!jalview.util.Platform.isControlDown(e) && !e.isShiftDown() && av
                     .getSelectionGroup() != null))
     {
       av.setSelectionGroup(new SequenceGroup());
@@ -355,6 +342,7 @@ public class IdPanel extends JPanel implements MouseListener,
       av.getSelectionGroup().setEndRes(av.getAlignment().getWidth() - 1);
     }
 
+    int seq = alignPanel.getSeqPanel().findSeq(e);
     if (e.isShiftDown() && (lastid != -1))
     {
       selectSeqs(lastid, seq);
@@ -363,10 +351,48 @@ public class IdPanel extends JPanel implements MouseListener,
     {
       selectSeq(seq);
     }
+
+    av.isSelectionGroupChanged(true);
+
     alignPanel.paintAlignment(true);
   }
 
   /**
+   * Build and show the popup-menu at the right-click mouse position
+   * 
+   * @param e
+   */
+  void showPopupMenu(MouseEvent e)
+  {
+    int seq2 = alignPanel.getSeqPanel().findSeq(e);
+    Sequence sq = (Sequence) av.getAlignment().getSequenceAt(seq2);
+    // build a new links menu based on the current links + any non-positional
+    // features
+    Vector<String> nlinks = new Vector<String>(Preferences.sequenceURLLinks);
+    SequenceFeature sfs[] = sq == null ? null : sq.getSequenceFeatures();
+    if (sfs != null)
+    {
+      for (SequenceFeature sf : sfs)
+      {
+        if (sf.begin == sf.end && sf.begin == 0)
+        {
+          if (sf.links != null && sf.links.size() > 0)
+          {
+            for (int l = 0, lSize = sf.links.size(); l < lSize; l++)
+            {
+              nlinks.addElement(sf.links.elementAt(l));
+            }
+          }
+        }
+      }
+    }
+
+    PopupMenu pop = new PopupMenu(alignPanel, sq, nlinks,
+            Preferences.getGroupURLLinks());
+    pop.show(this, e.getX(), e.getY());
+  }
+
+  /**
    * Toggle whether the sequence is part of the current selection group.
    * 
    * @param seq
@@ -433,6 +459,11 @@ public class IdPanel extends JPanel implements MouseListener,
     PaintRefresher.Refresh(this, av.getSequenceSetId());
     // always send selection message when mouse is released
     av.sendSelection();
+
+    if (e.isPopupTrigger()) // Windows reports this in mouseReleased
+    {
+      showPopupMenu(e);
+    }
   }
 
   /**
@@ -443,7 +474,7 @@ public class IdPanel extends JPanel implements MouseListener,
    */
   public void highlightSearchResults(List<SequenceI> list)
   {
-    idCanvas.setHighlighted(list);
+    getIdCanvas().setHighlighted(list);
 
     if (list == null)
     {
@@ -459,6 +490,16 @@ public class IdPanel extends JPanel implements MouseListener,
     }
   }
 
+  public IdCanvas getIdCanvas()
+  {
+    return idCanvas;
+  }
+
+  public void setIdCanvas(IdCanvas idCanvas)
+  {
+    this.idCanvas = idCanvas;
+  }
+
   // this class allows scrolling off the bottom of the visible alignment
   class ScrollThread extends Thread
   {