update author list in license for (JAL-826)
[jalview.git] / src / jalview / appletgui / Tooltip.java
index 76c0047..52b40e5 100755 (executable)
 /*\r
- * Jalview - A Sequence Alignment Editor and Viewer\r
- * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
- *\r
- * This program is free software; you can redistribute it and/or\r
- * modify it under the terms of the GNU General Public License\r
- * as published by the Free Software Foundation; either version 2\r
- * of the License, or (at your option) any later version.\r
- *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License\r
- * along with this program; if not, write to the Free Software\r
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)\r
+ * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle\r
+ * \r
+ * This file is part of Jalview.\r
+ * \r
+ * Jalview is free software: you can redistribute it and/or\r
+ * modify it under the terms of the GNU General Public License \r
+ * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.\r
+ * \r
+ * Jalview is distributed in the hope that it will be useful, but \r
+ * WITHOUT ANY WARRANTY; without even the implied warranty \r
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR \r
+ * PURPOSE.  See the GNU General Public License for more details.\r
+ * \r
+ * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.\r
  */\r
-\r
 package jalview.appletgui;\r
 \r
-import java.awt.*;\r
 import java.applet.*;\r
-import java.awt.event.*;\r
 import java.util.*;\r
 \r
+import java.awt.*;\r
+import java.awt.event.*;\r
+\r
 public class Tooltip extends Canvas implements MouseListener,\r
-    MouseMotionListener\r
+        MouseMotionListener\r
 {\r
+  private String[] tip;\r
 \r
-        private String [] tip;\r
-        protected Component owner;\r
-\r
-        private Container mainContainer;\r
-        private LayoutManager mainLayout;\r
-\r
-        private boolean shown;\r
-\r
-        private final int VERTICAL_OFFSET = 20;\r
-        private final int HORIZONTAL_ENLARGE = 10;\r
-\r
-        int fontHeight = 0;\r
-\r
-        Image linkImage;\r
-\r
-        FontMetrics fm;\r
-\r
-\r
-\r
-        public Tooltip(String tip, Component owner)\r
-        {\r
-          this.owner = owner;\r
-          owner.addMouseListener(this);\r
-          owner.addMouseMotionListener(this);\r
-          setBackground(new Color(255, 255, 220));\r
-          setTip(tip);\r
-          java.net.URL url = getClass().getResource("/images/link.gif");\r
-          if (url != null)\r
-          {\r
-            linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);\r
-          }\r
-        }\r
-\r
+  private String lastTip = "";\r
 \r
-        public void paint(Graphics g)\r
-        {\r
-                g.drawRect(0,0,getSize().width -1, getSize().height -1);\r
-                int lindex, x;\r
-                for(int i=0; i<tip.length; i++)\r
-                {\r
-                  x = 3;\r
-                  lindex = tip[i].indexOf("%LINK%");\r
-                  if(lindex!=-1)\r
-                  {\r
-                   if(lindex>0)\r
-                   {\r
-                     g.drawString(tip[i].substring(0, lindex), 3, (i+1)*fontHeight-3);\r
-                     x+=fm.stringWidth(tip[i].substring(0, lindex)+3);\r
-                   }\r
-                   g.drawImage(linkImage, x, i * fontHeight+1, this);\r
-                   if(lindex+6<tip[i].length())\r
-                   {\r
-                     g.drawString(tip[i].substring(lindex+6),\r
-                                  x + linkImage.getWidth(this),\r
-                                  (i+1)*fontHeight-3);\r
-                   }\r
-\r
-                  }\r
-                  else\r
-                  g.drawString(tip[i], 3, (i+1)*fontHeight - 3);\r
-                }\r
-        }\r
-\r
-        private void addToolTip()\r
-        {\r
-                mainContainer.setLayout(null);\r
-                mainContainer.add(this, 0);\r
-                mainContainer.validate();\r
-                repaint();\r
-                shown = true;\r
-        }\r
+  private boolean setPosition = false;\r
 \r
-        void setTip(String tip)\r
-        {\r
-          fm = getFontMetrics(owner.getFont());\r
-          fontHeight = fm.getHeight();\r
-\r
-          int longestLine = 0;\r
-          StringTokenizer st = new StringTokenizer(tip, "\n");\r
-          this.tip = new String[st.countTokens()];\r
-          int index = 0;\r
-          while(st.hasMoreElements())\r
-          {\r
-            this.tip[index] = st.nextToken();\r
-            if(fm.stringWidth(this.tip[index])>longestLine)\r
-              longestLine = fm.stringWidth(this.tip[index]);\r
-            index ++;\r
-          }\r
-\r
-          setSize(longestLine + HORIZONTAL_ENLARGE,\r
-                  fontHeight*this.tip.length);\r
-        }\r
-\r
-        void setTipLocation(int x, int y)\r
-        {\r
+  protected Component owner;\r
 \r
-          setLocation((owner.getLocationOnScreen().x - mainContainer.getLocationOnScreen().x) +x,\r
-           (owner.getLocationOnScreen().y - mainContainer.getLocationOnScreen().y + VERTICAL_OFFSET)+y);\r
+  private Container mainContainer;\r
 \r
+  private LayoutManager mainLayout;\r
 \r
+  private boolean shown;\r
 \r
-        // correction, whole tool tip must be visible\r
-          if (mainContainer.getSize().width < (getLocation().x + getSize().width))\r
-          {\r
-            setLocation(mainContainer.getSize().width - getSize().width,\r
-                        getLocation().y);\r
-          }\r
+  private final int VERTICAL_OFFSET = 20;\r
 \r
-        }\r
+  private final int HORIZONTAL_ENLARGE = 10;\r
 \r
+  int fontHeight = 0;\r
 \r
-        private void removeToolTip() {\r
-                if (shown) {\r
-                        mainContainer.remove(0);\r
-                        mainContainer.setLayout(mainLayout);\r
-                        mainContainer.validate();\r
-                }\r
-                shown = false;\r
-        }\r
+  Image linkImage;\r
 \r
-        private void findMainContainer() {\r
-                Container parent = owner.getParent();\r
-                while (true) {\r
-                        if ((parent instanceof Applet) || (parent instanceof Frame)) {\r
-                                mainContainer = parent;\r
-                                break;\r
-                        } else {\r
-                                parent = parent.getParent();\r
-                        }\r
-                }\r
-                mainLayout = mainContainer.getLayout();\r
-        }\r
+  FontMetrics fm;\r
 \r
-        public void mouseEntered(MouseEvent me)\r
-        {     }\r
+  public Tooltip(String tip, Component owner)\r
+  {\r
+    this.owner = owner;\r
+    owner.addMouseListener(this);\r
+    owner.addMouseMotionListener(this);\r
+    setBackground(new Color(255, 255, 220));\r
+    setTip(tip);\r
+    java.net.URL url = getClass().getResource("/images/link.gif");\r
+    if (url != null)\r
+    {\r
+      linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url);\r
+    }\r
+  }\r
 \r
-        public void mouseExited(MouseEvent me)\r
-        {\r
-          removeToolTip();\r
-        }\r
+  public void paint(Graphics g)\r
+  {\r
+    int w = getSize().width;\r
+    int h = getSize().height;\r
 \r
-        public void mousePressed(MouseEvent me)\r
+    g.drawRect(0, 0, w - 1, h - 1);\r
+    int lindex, x;\r
+    for (int i = 0; i < tip.length; i++)\r
+    {\r
+      x = 3;\r
+      lindex = tip[i].indexOf("%LINK%");\r
+      if (lindex != -1)\r
+      {\r
+        if (lindex > 0)\r
         {\r
-          removeToolTip();\r
+          g.drawString(tip[i].substring(0, lindex), 3, (i + 1) * fontHeight\r
+                  - 3);\r
+          x += fm.stringWidth(tip[i].substring(0, lindex) + 3);\r
         }\r
-\r
-        public void mouseReleased(MouseEvent me)\r
-        {}\r
-\r
-        public void mouseClicked(MouseEvent me)\r
-        {}\r
-\r
-        public void mouseMoved(MouseEvent me)\r
+        g.drawImage(linkImage, x, i * fontHeight + 1, this);\r
+        if (lindex + 6 < tip[i].length())\r
         {\r
-          if (shown)\r
-            setTipLocation(me.getX(), me.getY());\r
-          else\r
-          {\r
-            findMainContainer();\r
-            addToolTip();\r
-            setTipLocation(me.getX(), me.getY());\r
-          }\r
+          g.drawString(tip[i].substring(lindex + 6),\r
+                  x + linkImage.getWidth(this), (i + 1) * fontHeight - 3);\r
         }\r
-\r
-        public void mouseDragged(MouseEvent me)\r
-        {}\r
+      }\r
+      else\r
+      {\r
+        g.drawString(tip[i], 3, (i + 1) * fontHeight - 3);\r
+      }\r
+    }\r
+  }\r
+\r
+  synchronized void setTip(String tip)\r
+  {\r
+    if (tip == null)\r
+    {\r
+      setTip("");\r
+      return;\r
+    }\r
+\r
+    if (lastTip.equals(tip))\r
+    {\r
+      return;\r
+    }\r
+\r
+    lastTip = tip;\r
+    setPosition = true;\r
+\r
+    fm = getFontMetrics(owner.getFont());\r
+    fontHeight = fm.getHeight();\r
+\r
+    int longestLine = 0;\r
+    StringTokenizer st = new StringTokenizer(tip, "\n");\r
+    this.tip = new String[st.countTokens()];\r
+    int index = 0;\r
+    while (st.hasMoreElements())\r
+    {\r
+      this.tip[index] = st.nextToken();\r
+      if (fm.stringWidth(this.tip[index]) > longestLine)\r
+      {\r
+        longestLine = fm.stringWidth(this.tip[index]);\r
+      }\r
+      index++;\r
+    }\r
+\r
+    setSize(longestLine + HORIZONTAL_ENLARGE, fontHeight * this.tip.length);\r
+\r
+    repaint();\r
+\r
+  }\r
+\r
+  void setTipLocation(MouseEvent evt)\r
+  {\r
+    if (mainContainer == null || owner == null)\r
+    {\r
+      return;\r
+    }\r
+    setLocation(\r
+            (owner.getLocationOnScreen().x - mainContainer.getLocationOnScreen().x)\r
+                    + evt.getX(),\r
+            (owner.getLocationOnScreen().y\r
+                    - mainContainer.getLocationOnScreen().y + VERTICAL_OFFSET)\r
+                    + evt.getY());\r
+\r
+    // correction, whole tool tip must be visible\r
+    if (mainContainer.getSize().width < (getLocation().x + getSize().width))\r
+    {\r
+      setLocation(mainContainer.getSize().width - getSize().width,\r
+              getLocation().y);\r
+    }\r
+  }\r
+\r
+  private void removeToolTip()\r
+  {\r
+    if (shown)\r
+    {\r
+      mainContainer.remove(0);\r
+      mainContainer.setLayout(mainLayout);\r
+      mainContainer.validate();\r
+    }\r
+    shown = false;\r
+  }\r
+\r
+  private void findMainContainer()\r
+  {\r
+    Container parent = owner.getParent();\r
+    while (true)\r
+    {\r
+      if ((parent instanceof Applet) || (parent instanceof Frame))\r
+      {\r
+        mainContainer = parent;\r
+        break;\r
+      }\r
+      else\r
+      {\r
+        parent = parent.getParent();\r
+      }\r
+    }\r
+    mainLayout = mainContainer.getLayout();\r
+  }\r
+\r
+  public void mouseEntered(MouseEvent me)\r
+  {\r
+    setTipLocation(me);\r
+  }\r
+\r
+  public void mouseExited(MouseEvent me)\r
+  {\r
+    removeToolTip();\r
+  }\r
+\r
+  public void mousePressed(MouseEvent me)\r
+  {\r
+    removeToolTip();\r
+  }\r
+\r
+  public void mouseReleased(MouseEvent me)\r
+  {\r
+  }\r
+\r
+  public void mouseClicked(MouseEvent me)\r
+  {\r
+  }\r
+\r
+  public void mouseMoved(MouseEvent me)\r
+  {\r
+    if (!shown)\r
+    {\r
+      findMainContainer();\r
+      mainContainer.setLayout(null);\r
+      mainContainer.add(this, 0);\r
+      mainContainer.validate();\r
+      shown = true;\r
+      setTipLocation(me);\r
+    }\r
+    else if (setPosition)\r
+    {\r
+      setTipLocation(me);\r
+      setPosition = false;\r
+    }\r
+  }\r
+\r
+  public void mouseDragged(MouseEvent me)\r
+  {\r
+  }\r
 }\r