PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/gui/RolloverButton.java

#
Java | 159 lines | 80 code | 15 blank | 64 comment | 8 complexity | ba6b1ccbf4078a34443015e15d7a5042 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 java.lang.reflect.Method;
  27. import javax.swing.*;
  28. import javax.swing.border.*;
  29. import org.gjt.sp.jedit.OperatingSystem;
  30. import org.gjt.sp.util.Log;
  31. //}}}
  32. /**
  33. * If you wish to have rollovers on your buttons, use this class.
  34. *
  35. * Unlike the Swing rollover support, this class works outside of
  36. * <code>JToolBar</code>s, and does not require undocumented client
  37. * property hacks or JDK1.4-specific API calls.<p>
  38. *
  39. * Note: You should not call <code>setBorder()</code> on your buttons,
  40. * as they probably won't work properly.
  41. */
  42. public class RolloverButton extends JButton
  43. {
  44. //{{{ RolloverButton constructor
  45. /**
  46. * Setup the border (invisible initially)
  47. */
  48. public RolloverButton()
  49. {
  50. if(method != null)
  51. {
  52. try
  53. {
  54. method.invoke(this,new Boolean[] { Boolean.TRUE });
  55. }
  56. catch(Exception e)
  57. {
  58. Log.log(Log.ERROR,this,e);
  59. }
  60. }
  61. else
  62. {
  63. setBorder(new EtchedBorder());
  64. setBorderPainted(false);
  65. setMargin(new Insets(0,0,0,0));
  66. setRequestFocusEnabled(false);
  67. addMouseListener(new MouseOverHandler());
  68. }
  69. } //}}}
  70. //{{{ RolloverButton constructor
  71. /**
  72. * Setup the border (invisible initially)
  73. */
  74. public RolloverButton(Icon icon)
  75. {
  76. this();
  77. setIcon(icon);
  78. } //}}}
  79. //{{{ isOpaque() method
  80. public boolean isOpaque()
  81. {
  82. return false;
  83. } //}}}
  84. //{{{ setEnabled() method
  85. public void setEnabled(boolean b)
  86. {
  87. super.setEnabled(b);
  88. if(method == null)
  89. {
  90. setBorderPainted(false);
  91. repaint();
  92. }
  93. } //}}}
  94. //{{{ paint() method
  95. public void paint(Graphics g)
  96. {
  97. if(method != null || isEnabled())
  98. super.paint(g);
  99. else
  100. {
  101. Graphics2D g2 = (Graphics2D)g;
  102. g2.setComposite(c);
  103. super.paint(g2);
  104. }
  105. } //}}}
  106. //{{{ Private members
  107. private static AlphaComposite c = AlphaComposite.getInstance(
  108. AlphaComposite.SRC_OVER, 0.5f);
  109. private static Method method;
  110. static
  111. {
  112. /* if(OperatingSystem.hasJava14())
  113. {
  114. try
  115. {
  116. method = RolloverButton.class
  117. .getMethod("setRolloverEnabled",new Class[]
  118. { boolean.class });
  119. Log.log(Log.DEBUG,RolloverButton.class,
  120. "Java 1.4 setRolloverEnabled() method enabled");
  121. }
  122. catch(Exception e)
  123. {
  124. Log.log(Log.ERROR,RolloverButton.class,e);
  125. }
  126. } */
  127. } //}}}
  128. //{{{ MouseHandler class
  129. /**
  130. * Make the border visible/invisible on rollovers
  131. */
  132. class MouseOverHandler extends MouseAdapter
  133. {
  134. public void mouseEntered(MouseEvent e)
  135. {
  136. if (isEnabled())
  137. setBorderPainted(true);
  138. }
  139. public void mouseExited(MouseEvent e)
  140. {
  141. setBorderPainted(false);
  142. }
  143. } //}}}
  144. }