PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 190 lines | 100 code | 19 blank | 71 comment | 10 complexity | 5f286b30b7a8055bc6aae3f99dfd15a5 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 java.lang.reflect.Method;
  28. import javax.swing.*;
  29. import javax.swing.border.*;
  30. import org.gjt.sp.jedit.OperatingSystem;
  31. import org.gjt.sp.util.Log;
  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. */
  43. public class RolloverButton extends JButton
  44. {
  45. //{{{ RolloverButton constructor
  46. /**
  47. * Setup the border (invisible initially)
  48. */
  49. public RolloverButton()
  50. {
  51. if(OperatingSystem.hasJava15())
  52. setContentAreaFilled(false);
  53. if(method != null)
  54. {
  55. try
  56. {
  57. method.invoke(this,new Boolean[] { Boolean.TRUE });
  58. }
  59. catch(Exception e)
  60. {
  61. Log.log(Log.ERROR,this,e);
  62. }
  63. }
  64. else
  65. {
  66. setBorder(new EtchedBorder());
  67. setBorderPainted(false);
  68. setMargin(new Insets(1,1,1,1));
  69. setRequestFocusEnabled(false);
  70. addMouseListener(new MouseOverHandler());
  71. }
  72. } //}}}
  73. //{{{ RolloverButton constructor
  74. /**
  75. * Setup the border (invisible initially)
  76. */
  77. public RolloverButton(Icon icon)
  78. {
  79. this();
  80. setIcon(icon);
  81. } //}}}
  82. //{{{ isOpaque() method
  83. public boolean isOpaque()
  84. {
  85. return false;
  86. } //}}}
  87. //{{{ setEnabled() method
  88. public void setEnabled(boolean b)
  89. {
  90. super.setEnabled(b);
  91. if(method == null)
  92. {
  93. setBorderPainted(false);
  94. repaint();
  95. }
  96. } //}}}
  97. //{{{ setBorderPainted() method
  98. public void setBorderPainted(boolean b)
  99. {
  100. try
  101. {
  102. revalidateBlocked = true;
  103. super.setBorderPainted(b);
  104. }
  105. finally
  106. {
  107. revalidateBlocked = false;
  108. }
  109. } //}}}
  110. //{{{ revalidate() method
  111. /**
  112. * We block calls to revalidate() from a setBorderPainted(), for
  113. * performance reasons.
  114. */
  115. public void revalidate()
  116. {
  117. if(!revalidateBlocked)
  118. super.revalidate();
  119. } //}}}
  120. //{{{ paint() method
  121. public void paint(Graphics g)
  122. {
  123. if(method != null || isEnabled())
  124. super.paint(g);
  125. else
  126. {
  127. Graphics2D g2 = (Graphics2D)g;
  128. g2.setComposite(c);
  129. super.paint(g2);
  130. }
  131. } //}}}
  132. //{{{ Private members
  133. private static AlphaComposite c = AlphaComposite.getInstance(
  134. AlphaComposite.SRC_OVER, 0.5f);
  135. private static Method method;
  136. private boolean revalidateBlocked;
  137. static
  138. {
  139. /* if(OperatingSystem.hasJava14())
  140. {
  141. try
  142. {
  143. method = RolloverButton.class
  144. .getMethod("setRolloverEnabled",new Class[]
  145. { boolean.class });
  146. Log.log(Log.DEBUG,RolloverButton.class,
  147. "Java 1.4 setRolloverEnabled() method enabled");
  148. }
  149. catch(Exception e)
  150. {
  151. Log.log(Log.ERROR,RolloverButton.class,e);
  152. }
  153. } */
  154. } //}}}
  155. //{{{ MouseHandler class
  156. /**
  157. * Make the border visible/invisible on rollovers
  158. */
  159. class MouseOverHandler extends MouseAdapter
  160. {
  161. public void mouseEntered(MouseEvent e)
  162. {
  163. if (isEnabled())
  164. setBorderPainted(true);
  165. }
  166. public void mouseExited(MouseEvent e)
  167. {
  168. setBorderPainted(false);
  169. }
  170. } //}}}
  171. }