X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fappletgui%2FTooltip.java;h=90f2e97263d148b64f34e1a8226f631a2a9f777b;hb=7bc226b58110fa26d9dbd3f0c78095d06909ffc3;hp=6766330c786cdd2f2a02030bd19e98fb4c41c180;hpb=4756b29312ca78d0975248c65e50157399e6ce39;p=jalview.git diff --git a/src/jalview/appletgui/Tooltip.java b/src/jalview/appletgui/Tooltip.java index 6766330..90f2e97 100755 --- a/src/jalview/appletgui/Tooltip.java +++ b/src/jalview/appletgui/Tooltip.java @@ -1,6 +1,6 @@ /* * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle + * Copyright (C) 2007 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 @@ -19,194 +19,206 @@ package jalview.appletgui; -import java.awt.*; import java.applet.*; -import java.awt.event.*; import java.util.*; -public class Tooltip extends Canvas implements MouseListener, +import java.awt.*; +import java.awt.event.*; + +public class Tooltip + extends Canvas implements MouseListener, MouseMotionListener { - private String [] tip; - private String lastTip=""; - private boolean setPosition = false; - protected Component owner; - - private Container mainContainer; - private LayoutManager mainLayout; - - private boolean shown; - - private final int VERTICAL_OFFSET = 20; - private final int HORIZONTAL_ENLARGE = 10; - - int fontHeight = 0; - - Image linkImage; - - FontMetrics fm; - - public Tooltip(String tip, Component owner) - { - this.owner = owner; - owner.addMouseListener(this); - owner.addMouseMotionListener(this); - setBackground(new Color(255, 255, 220)); - setTip(tip); - java.net.URL url = getClass().getResource("/images/link.gif"); - if (url != null) - { - linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url); - } - } - - public void paint(Graphics g) - { - int w = getSize().width; - int h = getSize().height; - - g.drawRect(0, 0, w - 1, h - 1); - int lindex, x; - for (int i = 0; i < tip.length; i++) - { - x = 3; - lindex = tip[i].indexOf("%LINK%"); - if (lindex != -1) - { - if (lindex > 0) - { - g.drawString(tip[i].substring(0, lindex), 3, (i + 1) * fontHeight - 3); - x += fm.stringWidth(tip[i].substring(0, lindex) + 3); - } - g.drawImage(linkImage, x, i * fontHeight + 1, this); - if (lindex + 6 < tip[i].length()) - { - g.drawString(tip[i].substring(lindex + 6), - x + linkImage.getWidth(this), - (i + 1) * fontHeight - 3); - } - } - else - { - g.drawString(tip[i], 3, (i + 1) * fontHeight - 3); - } - } - } - - synchronized void setTip(String tip) - { - if(tip==null) - { - setTip(""); - return; - } - - if(lastTip.equals(tip)) - return; - - lastTip = tip; - setPosition = true; - - fm = getFontMetrics(owner.getFont()); - fontHeight = fm.getHeight(); - - int longestLine = 0; - StringTokenizer st = new StringTokenizer(tip, "\n"); - this.tip = new String[st.countTokens()]; - int index = 0; - while(st.hasMoreElements()) - { - this.tip[index] = st.nextToken(); - if(fm.stringWidth(this.tip[index])>longestLine) - longestLine = fm.stringWidth(this.tip[index]); - index ++; - } - - setSize(longestLine + HORIZONTAL_ENLARGE, - fontHeight*this.tip.length); - - repaint(); - - } - - void setTipLocation(MouseEvent evt) - { - setLocation( (owner.getLocationOnScreen().x - - mainContainer.getLocationOnScreen().x) + evt.getX(), - (owner.getLocationOnScreen().y - - mainContainer.getLocationOnScreen().y - + VERTICAL_OFFSET) + evt.getY()); - - // correction, whole tool tip must be visible - if (mainContainer.getSize().width < (getLocation().x + getSize().width)) - { - setLocation(mainContainer.getSize().width - getSize().width, - getLocation().y); - } - } - - - private void removeToolTip() { - if (shown) { - mainContainer.remove(0); - mainContainer.setLayout(mainLayout); - mainContainer.validate(); - } - shown = false; - } - - private void findMainContainer() { - Container parent = owner.getParent(); - while (true) { - if ((parent instanceof Applet) || (parent instanceof Frame)) { - mainContainer = parent; - break; - } else { - parent = parent.getParent(); - } - } - mainLayout = mainContainer.getLayout(); - } - - public void mouseEntered(MouseEvent me) + private String[] tip; + private String lastTip = ""; + private boolean setPosition = false; + protected Component owner; + + private Container mainContainer; + private LayoutManager mainLayout; + + private boolean shown; + + private final int VERTICAL_OFFSET = 20; + private final int HORIZONTAL_ENLARGE = 10; + + int fontHeight = 0; + + Image linkImage; + + FontMetrics fm; + + public Tooltip(String tip, Component owner) + { + this.owner = owner; + owner.addMouseListener(this); + owner.addMouseMotionListener(this); + setBackground(new Color(255, 255, 220)); + setTip(tip); + java.net.URL url = getClass().getResource("/images/link.gif"); + if (url != null) + { + linkImage = java.awt.Toolkit.getDefaultToolkit().getImage(url); + } + } + + public void paint(Graphics g) + { + int w = getSize().width; + int h = getSize().height; + + g.drawRect(0, 0, w - 1, h - 1); + int lindex, x; + for (int i = 0; i < tip.length; i++) + { + x = 3; + lindex = tip[i].indexOf("%LINK%"); + if (lindex != -1) + { + if (lindex > 0) { - setTipLocation(me); + g.drawString(tip[i].substring(0, lindex), 3, (i + 1) * fontHeight - 3); + x += fm.stringWidth(tip[i].substring(0, lindex) + 3); } - - public void mouseExited(MouseEvent me) - { - removeToolTip(); - } - - public void mousePressed(MouseEvent me) - { - removeToolTip(); - } - - public void mouseReleased(MouseEvent me) - {} - - public void mouseClicked(MouseEvent me) - {} - - public void mouseMoved(MouseEvent me) + g.drawImage(linkImage, x, i * fontHeight + 1, this); + if (lindex + 6 < tip[i].length()) { - if(!shown) - { - findMainContainer(); - mainContainer.setLayout(null); - mainContainer.add(this, 0); - mainContainer.validate(); - shown = true; - setTipLocation(me); - } - else if (setPosition) - { - setTipLocation(me); - setPosition = false; - } + g.drawString(tip[i].substring(lindex + 6), + x + linkImage.getWidth(this), + (i + 1) * fontHeight - 3); } - - public void mouseDragged(MouseEvent me) - {} + } + else + { + g.drawString(tip[i], 3, (i + 1) * fontHeight - 3); + } + } + } + + synchronized void setTip(String tip) + { + if (tip == null) + { + setTip(""); + return; + } + + if (lastTip.equals(tip)) + { + return; + } + + lastTip = tip; + setPosition = true; + + fm = getFontMetrics(owner.getFont()); + fontHeight = fm.getHeight(); + + int longestLine = 0; + StringTokenizer st = new StringTokenizer(tip, "\n"); + this.tip = new String[st.countTokens()]; + int index = 0; + while (st.hasMoreElements()) + { + this.tip[index] = st.nextToken(); + if (fm.stringWidth(this.tip[index]) > longestLine) + { + longestLine = fm.stringWidth(this.tip[index]); + } + index++; + } + + setSize(longestLine + HORIZONTAL_ENLARGE, + fontHeight * this.tip.length); + + repaint(); + + } + + void setTipLocation(MouseEvent evt) + { + setLocation( (owner.getLocationOnScreen().x + - mainContainer.getLocationOnScreen().x) + evt.getX(), + (owner.getLocationOnScreen().y - + mainContainer.getLocationOnScreen().y + + VERTICAL_OFFSET) + evt.getY()); + + // correction, whole tool tip must be visible + if (mainContainer.getSize().width < (getLocation().x + getSize().width)) + { + setLocation(mainContainer.getSize().width - getSize().width, + getLocation().y); + } + } + + private void removeToolTip() + { + if (shown) + { + mainContainer.remove(0); + mainContainer.setLayout(mainLayout); + mainContainer.validate(); + } + shown = false; + } + + private void findMainContainer() + { + Container parent = owner.getParent(); + while (true) + { + if ( (parent instanceof Applet) || (parent instanceof Frame)) + { + mainContainer = parent; + break; + } + else + { + parent = parent.getParent(); + } + } + mainLayout = mainContainer.getLayout(); + } + + public void mouseEntered(MouseEvent me) + { + setTipLocation(me); + } + + public void mouseExited(MouseEvent me) + { + removeToolTip(); + } + + public void mousePressed(MouseEvent me) + { + removeToolTip(); + } + + public void mouseReleased(MouseEvent me) + {} + + public void mouseClicked(MouseEvent me) + {} + + public void mouseMoved(MouseEvent me) + { + if (!shown) + { + findMainContainer(); + mainContainer.setLayout(null); + mainContainer.add(this, 0); + mainContainer.validate(); + shown = true; + setTipLocation(me); + } + else if (setPosition) + { + setTipLocation(me); + setPosition = false; + } + } + + public void mouseDragged(MouseEvent me) + {} }