PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/RolloverButton.java

#
Java | 155 lines | 78 code | 15 blank | 62 comment | 2 complexity | 0428a315378f63c01e729c71c647a249 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * RolloverButton.java - Class for buttons that implement rollovers
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2002 Kris Kopicki
  7. * Portions copyright (C) 2003 Slava Pestov
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. package org.gjt.sp.jedit.gui;
  24. //{{{ Imports
  25. import java.awt.*;
  26. import java.awt.event.*;
  27. import javax.swing.*;
  28. import javax.swing.border.*;
  29. import javax.swing.plaf.basic.BasicButtonUI;
  30. import javax.swing.plaf.basic.BasicBorders.ButtonBorder;
  31. import org.gjt.sp.jedit.OperatingSystem;
  32. //}}}
  33. /**
  34. * If you wish to have rollovers on your buttons, use this class.
  35. *
  36. * Unlike the Swing rollover support, this class works outside of
  37. * <code>JToolBar</code>s, and does not require undocumented client
  38. * property hacks or JDK1.4-specific API calls.<p>
  39. *
  40. * Note: You should not call <code>setBorder()</code> on your buttons,
  41. * as they probably won't work properly.
  42. * @version $Id: RolloverButton.java 12504 2008-04-22 23:12:43Z ezust $
  43. */
  44. public class RolloverButton extends JButton
  45. {
  46. //{{{ RolloverButton constructor
  47. /**
  48. * Setup the border (invisible initially)
  49. */
  50. public RolloverButton()
  51. {
  52. //setContentAreaFilled(true);
  53. addMouseListener(new MouseOverHandler());
  54. } //}}}
  55. //{{{ RolloverButton constructor
  56. /**
  57. * Setup the border (invisible initially)
  58. *
  59. * @param icon the icon of this button
  60. */
  61. public RolloverButton(Icon icon)
  62. {
  63. this();
  64. setIcon(icon);
  65. } //}}}
  66. //{{{ updateUI() method
  67. public void updateUI()
  68. {
  69. super.updateUI();
  70. //setBorder(originalBorder);
  71. setBorderPainted(false);
  72. setRequestFocusEnabled(false);
  73. setMargin(new Insets(1,1,1,1));
  74. } //}}}
  75. //{{{ setEnabled() method
  76. public void setEnabled(boolean b)
  77. {
  78. super.setEnabled(b);
  79. setBorderPainted(false);
  80. repaint();
  81. } //}}}
  82. //{{{ setBorderPainted() method
  83. public void setBorderPainted(boolean b)
  84. {
  85. try
  86. {
  87. revalidateBlocked = true;
  88. super.setBorderPainted(b);
  89. setContentAreaFilled(b);
  90. }
  91. finally
  92. {
  93. revalidateBlocked = false;
  94. }
  95. } //}}}
  96. //{{{ revalidate() method
  97. /**
  98. * We block calls to revalidate() from a setBorderPainted(), for
  99. * performance reasons.
  100. */
  101. public void revalidate()
  102. {
  103. if(!revalidateBlocked)
  104. super.revalidate();
  105. } //}}}
  106. //{{{ paint() method
  107. public void paint(Graphics g)
  108. {
  109. if(isEnabled())
  110. super.paint(g);
  111. else
  112. {
  113. Graphics2D g2 = (Graphics2D)g;
  114. g2.setComposite(c);
  115. super.paint(g2);
  116. }
  117. } //}}}
  118. //{{{ Private members
  119. private static final AlphaComposite c = AlphaComposite.getInstance(
  120. AlphaComposite.SRC_OVER, 0.5f);
  121. private boolean revalidateBlocked;
  122. //{{{ MouseHandler class
  123. /**
  124. * Make the border visible/invisible on rollovers
  125. */
  126. class MouseOverHandler extends MouseAdapter
  127. {
  128. public void mouseEntered(MouseEvent e)
  129. {
  130. setContentAreaFilled(true);
  131. setBorderPainted(isEnabled());
  132. }
  133. public void mouseExited(MouseEvent e)
  134. {
  135. setContentAreaFilled(false);
  136. setBorderPainted(false);
  137. }
  138. } //}}}
  139. //}}}
  140. }