/edu/uncc/parsets/parsets/Tooltip.java

https://code.google.com/p/parsets/ · Java · 85 lines · 43 code · 14 blank · 28 comment · 8 complexity · b55e410d2952f72d218d549b43160783 MD5 · raw file

  1. package edu.uncc.parsets.parsets;
  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.FontMetrics;
  5. import java.awt.Graphics2D;
  6. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
  7. * Copyright (c) 2009, Robert Kosara, Caroline Ziemkiewicz,
  8. * and others (see Authors.txt for full list)
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of UNC Charlotte nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY ITS AUTHORS ''AS IS'' AND ANY
  24. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
  27. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  30. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  32. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  34. public class Tooltip {
  35. private int x;
  36. private int y;
  37. private String text;
  38. public Tooltip(String text, int x, int y) {
  39. this.text = text;
  40. this.x = x;
  41. this.y = y;
  42. }
  43. public void paint(Graphics2D g, Font tooltipFont, FontMetrics tooltipFontMetrics, int width) {
  44. if (text == null)
  45. return;
  46. g.setColor(new Color(.8f, .8f, .8f, .8f));
  47. String tok[] = text.split("\n");
  48. int maxWidth = 0;
  49. for (int i=0; i<tok.length; i++) {
  50. if (tooltipFontMetrics.stringWidth(tok[i]) > maxWidth)
  51. maxWidth = tooltipFontMetrics.stringWidth(tok[i]);
  52. }
  53. if (x + maxWidth > width)
  54. x -= maxWidth;
  55. if (y - 13 < 0)
  56. y += 13;
  57. g.fillRect(x, y + 10, maxWidth + 5, tooltipFontMetrics.getHeight()*tok.length + 3);
  58. g.setFont(tooltipFont);
  59. g.setColor(Color.BLACK);
  60. if (text.length() > 0) {
  61. for (int i = 0; i < tok.length; i++) {
  62. g.drawString(tok[i], x + 2, y + tooltipFontMetrics.getHeight()*(i+1) + 8);
  63. }
  64. }
  65. }
  66. public void newValues(String newText, int newX, int newY) {
  67. text = newText;
  68. x = newX;
  69. y = newY;
  70. }
  71. }