PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
Java | 204 lines | 141 code | 25 blank | 38 comment | 23 complexity | 6fcaae47c4805f9f80a712fb4ed979dc 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. * EnhancedCheckBoxMenuItem.java - Check box menu item
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1999, 2000, 2001, 2002 Slava Pestov
  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 javax.swing.*;
  25. import java.awt.event.*;
  26. import java.awt.*;
  27. import org.gjt.sp.jedit.textarea.JEditTextArea;
  28. import org.gjt.sp.jedit.*;
  29. import org.gjt.sp.util.Log;
  30. //}}}
  31. /**
  32. * jEdit's custom menu item. It adds support for multi-key shortcuts.
  33. */
  34. public class EnhancedCheckBoxMenuItem extends JCheckBoxMenuItem
  35. {
  36. //{{{ EnhancedCheckBoxMenuItem constructor
  37. public EnhancedCheckBoxMenuItem(String label, EditAction action)
  38. {
  39. super(label);
  40. this.action = action;
  41. if(action != null)
  42. {
  43. setEnabled(true);
  44. addActionListener(new EditAction.Wrapper(action));
  45. shortcutProp1 = action.getName() + ".shortcut";
  46. shortcutProp2 = action.getName() + ".shortcut2";
  47. addMouseListener(new MouseHandler());
  48. }
  49. else
  50. setEnabled(false);
  51. setModel(new Model());
  52. } //}}}
  53. //{{{ getPreferredSize() method
  54. public Dimension getPreferredSize()
  55. {
  56. Dimension d = super.getPreferredSize();
  57. String shortcut = getShortcut();
  58. if(shortcut != null)
  59. {
  60. d.width += (getFontMetrics(acceleratorFont)
  61. .stringWidth(shortcut) + 15);
  62. }
  63. return d;
  64. } //}}}
  65. //{{{ paint() method
  66. public void paint(Graphics g)
  67. {
  68. super.paint(g);
  69. String shortcut = getShortcut();
  70. if(shortcut != null)
  71. {
  72. g.setFont(acceleratorFont);
  73. g.setColor(getModel().isArmed() ?
  74. acceleratorSelectionForeground :
  75. acceleratorForeground);
  76. FontMetrics fm = g.getFontMetrics();
  77. Insets insets = getInsets();
  78. g.drawString(shortcut,getWidth() - (fm.stringWidth(
  79. shortcut) + insets.right + insets.left + 5),
  80. getFont().getSize() + (insets.top - 1)
  81. /* XXX magic number */);
  82. }
  83. } //}}}
  84. //{{{ getActionCommand() method
  85. public String getActionCommand()
  86. {
  87. return getModel().getActionCommand();
  88. } //}}}
  89. //{{{ Private members
  90. //{{{ Instance variables
  91. private String shortcutProp1;
  92. private String shortcutProp2;
  93. private EditAction action;
  94. private static Font acceleratorFont;
  95. private static Color acceleratorForeground;
  96. private static Color acceleratorSelectionForeground;
  97. //}}}
  98. //{{{ getShortcut() method
  99. private String getShortcut()
  100. {
  101. if(action == null)
  102. return null;
  103. else
  104. {
  105. String shortcut1 = jEdit.getProperty(shortcutProp1);
  106. String shortcut2 = jEdit.getProperty(shortcutProp2);
  107. if(shortcut1 == null || shortcut1.length() == 0)
  108. {
  109. if(shortcut2 == null || shortcut2.length() == 0)
  110. return null;
  111. else
  112. return shortcut2;
  113. }
  114. else
  115. {
  116. if(shortcut2 == null || shortcut2.length() == 0)
  117. return shortcut1;
  118. else
  119. return shortcut1 + " or " + shortcut2;
  120. }
  121. }
  122. } //}}}
  123. //{{{ Class initializer
  124. static
  125. {
  126. acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont");
  127. acceleratorFont = new Font("Monospaced",
  128. acceleratorFont.getStyle(),
  129. acceleratorFont.getSize());
  130. acceleratorForeground = UIManager
  131. .getColor("MenuItem.acceleratorForeground");
  132. acceleratorSelectionForeground = UIManager
  133. .getColor("MenuItem.acceleratorSelectionForeground");
  134. } //}}}
  135. //}}}
  136. //{{{ Model class
  137. class Model extends DefaultButtonModel
  138. {
  139. public boolean isSelected()
  140. {
  141. if(!isShowing())
  142. return false;
  143. try
  144. {
  145. return action.isSelected(GUIUtilities.getView(
  146. EnhancedCheckBoxMenuItem.this));
  147. }
  148. catch(Throwable t)
  149. {
  150. Log.log(Log.ERROR,this,t);
  151. return false;
  152. }
  153. }
  154. public void setSelected(boolean b) {}
  155. } //}}}
  156. //{{{ MouseHandler class
  157. class MouseHandler extends MouseAdapter
  158. {
  159. public void mouseReleased(MouseEvent evt)
  160. {
  161. GUIUtilities.getView((Component)evt.getSource())
  162. .getStatus().setMessage(null);
  163. }
  164. public void mouseEntered(MouseEvent evt)
  165. {
  166. String msg = action.getMouseOverText();
  167. if(msg != null)
  168. {
  169. GUIUtilities.getView((Component)evt.getSource())
  170. .getStatus().setMessage(msg);
  171. }
  172. }
  173. public void mouseExited(MouseEvent evt)
  174. {
  175. GUIUtilities.getView((Component)evt.getSource())
  176. .getStatus().setMessage(null);
  177. }
  178. } //}}}
  179. }