PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/gui/RolloverButton.java

#
Java | 124 lines | 53 code | 12 blank | 59 comment | 2 complexity | ec7b50567be061f4bffe1a944db52722 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. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.jedit.gui;
  23. //{{{ Imports
  24. import java.awt.*;
  25. import java.awt.event.*;
  26. import javax.swing.*;
  27. import javax.swing.border.*;
  28. import org.gjt.sp.jedit.OperatingSystem;
  29. //}}}
  30. /**
  31. * If you wish to have rollovers on your buttons, use this class.
  32. *
  33. * Unlike the Swing rollover support, this class works outside of
  34. * <code>JToolBars</code>, and does not require undocumented client
  35. * property hacks or JDK1.4-specific API calls.<p>
  36. *
  37. * Note: You should not call setBorder() on your buttons, as they probably
  38. * won't work properly.
  39. */
  40. public class RolloverButton extends JButton
  41. {
  42. //{{{ RolloverButton constructor
  43. /**
  44. * Setup the border (invisible initially)
  45. */
  46. public RolloverButton()
  47. {
  48. setBorder(new EtchedBorder());
  49. setBorderPainted(false);
  50. setMargin(new Insets(0,0,0,0));
  51. setRequestFocusEnabled(false);
  52. addMouseListener(new MouseOverHandler());
  53. } //}}}
  54. //{{{ RolloverButton constructor
  55. /**
  56. * Setup the border (invisible initially)
  57. */
  58. public RolloverButton(Icon icon)
  59. {
  60. this();
  61. setIcon(icon);
  62. } //}}}
  63. //{{{ setEnabled() method
  64. /**
  65. * @overrides javax.swing.AbstractButton.setEnabled()
  66. * @since jEdit 4.0pre3
  67. * @param b true to enable the button, otherwise false
  68. */
  69. public void setEnabled(boolean b)
  70. {
  71. super.setEnabled(b);
  72. setBorderPainted(false);
  73. repaint();
  74. } //}}}
  75. //{{{ paint() method
  76. /**
  77. * Custom paint method for fading the button when
  78. * it is disabled
  79. * @overrides javax.swing.JComponent.paint()
  80. * @since jEdit 4.0pre3
  81. * @param g the graphics context
  82. */
  83. public void paint(Graphics g)
  84. {
  85. if (isEnabled())
  86. super.paint(g);
  87. else
  88. {
  89. Graphics2D g2 = (Graphics2D)g;
  90. g2.setComposite(c);
  91. super.paint(g2);
  92. }
  93. } //}}}
  94. private static AlphaComposite c = AlphaComposite.getInstance(
  95. AlphaComposite.SRC_OVER, 0.5f);
  96. //{{{ MouseHandler class
  97. /**
  98. * Make the border visible/invisible on rollovers
  99. */
  100. class MouseOverHandler extends MouseAdapter
  101. {
  102. public void mouseEntered(MouseEvent e)
  103. {
  104. if (isEnabled())
  105. setBorderPainted(true);
  106. }
  107. public void mouseExited(MouseEvent e)
  108. {
  109. setBorderPainted(false);
  110. }
  111. } //}}}
  112. }