FloatingButtonEnabler.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* ========================================================================
  2. * JCommon : a free general purpose class library for the Java(tm) platform
  3. * ========================================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jcommon/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU Lesser General Public License as published by
  11. * the Free Software Foundation; either version 2.1 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  17. * License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
  22. * USA.
  23. *
  24. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  25. * in the United States and other countries.]
  26. *
  27. * -----------------
  28. * DetailEditor.java
  29. * -----------------
  30. * (C) Copyright 2004, by Thomas Morgner and Contributors.
  31. *
  32. * Original Author: Thomas Morgner;
  33. * Contributor(s): David Gilbert (for Object Refinery Limited);
  34. *
  35. * $Id: FloatingButtonEnabler.java,v 1.4 2007/11/02 17:50:36 taqua Exp $
  36. *
  37. * Changes
  38. * -------
  39. * 07-Jun-2004 : Added JCommon header (DG);
  40. *
  41. */
  42. package org.jfree.ui;
  43. import java.awt.event.MouseAdapter;
  44. import java.awt.event.MouseEvent;
  45. import javax.swing.AbstractButton;
  46. /**
  47. * Enables a button to have a simple floating effect. The border of the button is only visible,
  48. * when the mouse pointer is floating over the button.
  49. *
  50. * @author Thomas Morgner
  51. */
  52. public final class FloatingButtonEnabler extends MouseAdapter {
  53. /** A single instance. */
  54. private static FloatingButtonEnabler singleton;
  55. /**
  56. * Default constructor.
  57. */
  58. private FloatingButtonEnabler() {
  59. // nothing required
  60. }
  61. /**
  62. * Returns a default instance of this enabler.
  63. *
  64. * @return a shared instance of this class.
  65. */
  66. public static FloatingButtonEnabler getInstance() {
  67. if (singleton == null) {
  68. singleton = new FloatingButtonEnabler();
  69. }
  70. return singleton;
  71. }
  72. /**
  73. * Adds a button to this enabler.
  74. *
  75. * @param button the button.
  76. */
  77. public void addButton(final AbstractButton button) {
  78. button.addMouseListener(this);
  79. button.setBorderPainted(false);
  80. }
  81. /**
  82. * Removes a button from the enabler.
  83. *
  84. * @param button the button.
  85. */
  86. public void removeButton(final AbstractButton button) {
  87. button.addMouseListener(this);
  88. button.setBorderPainted(true);
  89. }
  90. /**
  91. * Triggers the drawing of the border when the mouse entered the button area.
  92. *
  93. * @param e the mouse event.
  94. */
  95. public void mouseEntered(final MouseEvent e) {
  96. if (e.getSource() instanceof AbstractButton) {
  97. final AbstractButton button = (AbstractButton) e.getSource();
  98. if (button.isEnabled()) {
  99. button.setBorderPainted(true);
  100. }
  101. }
  102. }
  103. /**
  104. * Disables the drawing of the border when the mouse leaves the button area.
  105. *
  106. * @param e the mouse event.
  107. */
  108. public void mouseExited(final MouseEvent e) {
  109. if (e.getSource() instanceof AbstractButton) {
  110. final AbstractButton button = (AbstractButton) e.getSource();
  111. button.setBorderPainted(false);
  112. if (button.getParent() != null)
  113. {
  114. // button.getParent().repaint(button.getX(), button.getY(),
  115. // button.getWidth(), button.getHeight());
  116. button.getParent().repaint();
  117. }
  118. }
  119. }
  120. }