package jalview.appletgui; import java.awt.*; import java.applet.*; import java.awt.event.*; import java.util.*; public class Tooltip extends Canvas implements MouseListener, MouseMotionListener { private String [] tip; 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) { g.drawRect(0,0,getSize().width -1, getSize().height -1); int lindex, x; for(int i=0; i0) { 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); } else g.drawString(tip[i], 3, (i+1)*fontHeight - 3); } } private void addToolTip() { mainContainer.setLayout(null); mainContainer.add(this, 0); mainContainer.validate(); repaint(); shown = true; } void setTip(String tip) { 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); } void setTipLocation(int x, int y) { setLocation((owner.getLocationOnScreen().x - mainContainer.getLocationOnScreen().x) +x, (owner.getLocationOnScreen().y - mainContainer.getLocationOnScreen().y + VERTICAL_OFFSET)+y); // 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) { } 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) setTipLocation(me.getX(), me.getY()); else { findMainContainer(); addToolTip(); setTipLocation(me.getX(), me.getY()); } } public void mouseDragged(MouseEvent me) {} }