PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/menu/EnhancedCheckBoxMenuItem.java

#
Java | 211 lines | 144 code | 23 blank | 44 comment | 30 complexity | 50f7192e98e4214ca551475d526862c5 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, 2003 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.menu;
  23. //{{{ Imports
  24. import javax.swing.*;
  25. import java.awt.event.*;
  26. import java.awt.*;
  27. import org.gjt.sp.jedit.*;
  28. import org.gjt.sp.util.Log;
  29. //}}}
  30. /**
  31. * jEdit's custom menu item. It adds support for multi-key shortcuts.
  32. */
  33. public class EnhancedCheckBoxMenuItem extends JCheckBoxMenuItem
  34. {
  35. //{{{ EnhancedCheckBoxMenuItem constructor
  36. /**
  37. * Creates a new menu item. Most plugins should call
  38. * GUIUtilities.loadMenuItem() instead.
  39. * @param label The menu item label
  40. * @param action The edit action
  41. * @param context An action context
  42. * @since jEdit 4.2pre1
  43. */
  44. public EnhancedCheckBoxMenuItem(String label, String action,
  45. ActionContext context)
  46. {
  47. this.context = context;
  48. this.action = action;
  49. this.shortcut = getShortcut();
  50. if(OperatingSystem.hasScreenMenuBar() && shortcut != null)
  51. {
  52. setText(label + " (" + shortcut + ")");
  53. shortcut = null;
  54. }
  55. else
  56. setText(label);
  57. if(action != null)
  58. {
  59. setEnabled(true);
  60. addActionListener(new EditAction.Wrapper(context,action));
  61. addMouseListener(new MouseHandler());
  62. }
  63. else
  64. setEnabled(false);
  65. setModel(new Model());
  66. } //}}}
  67. //{{{ getPreferredSize() method
  68. public Dimension getPreferredSize()
  69. {
  70. Dimension d = super.getPreferredSize();
  71. if(shortcut != null)
  72. {
  73. d.width += (getFontMetrics(EnhancedMenuItem.acceleratorFont)
  74. .stringWidth(shortcut) + 15);
  75. }
  76. return d;
  77. } //}}}
  78. //{{{ paint() method
  79. public void paint(Graphics g)
  80. {
  81. super.paint(g);
  82. if(shortcut != null)
  83. {
  84. g.setFont(EnhancedMenuItem.acceleratorFont);
  85. g.setColor(getModel().isArmed() ?
  86. EnhancedMenuItem.acceleratorSelectionForeground :
  87. EnhancedMenuItem.acceleratorForeground);
  88. FontMetrics fm = g.getFontMetrics();
  89. Insets insets = getInsets();
  90. g.drawString(shortcut,getWidth() - (fm.stringWidth(
  91. shortcut) + insets.right + insets.left + 5),
  92. getFont().getSize() + (insets.top -
  93. (OperatingSystem.isMacOSLF() ? 0 : 1))
  94. /* XXX magic number */);
  95. }
  96. } //}}}
  97. //{{{ Private members
  98. //{{{ Instance variables
  99. private ActionContext context;
  100. private String shortcut;
  101. private String action;
  102. //}}}
  103. //{{{ getShortcut() method
  104. private String getShortcut()
  105. {
  106. if(action == null)
  107. return null;
  108. else
  109. {
  110. String shortcut1 = jEdit.getProperty(action + ".shortcut");
  111. String shortcut2 = jEdit.getProperty(action + ".shortcut2");
  112. if(shortcut1 == null || shortcut1.length() == 0)
  113. {
  114. if(shortcut2 == null || shortcut2.length() == 0)
  115. return null;
  116. else
  117. return shortcut2;
  118. }
  119. else
  120. {
  121. if(shortcut2 == null || shortcut2.length() == 0)
  122. return shortcut1;
  123. else
  124. return shortcut1 + " or " + shortcut2;
  125. }
  126. }
  127. } //}}}
  128. //}}}
  129. //{{{ Model class
  130. class Model extends DefaultButtonModel
  131. {
  132. public boolean isSelected()
  133. {
  134. if(!isShowing())
  135. return false;
  136. EditAction a = context.getAction(action);
  137. if(a == null)
  138. {
  139. Log.log(Log.WARNING,this,"Unknown action: "
  140. + action);
  141. return false;
  142. }
  143. try
  144. {
  145. return a.isSelected(EnhancedCheckBoxMenuItem.this);
  146. }
  147. catch(Throwable t)
  148. {
  149. Log.log(Log.ERROR,this,t);
  150. return false;
  151. }
  152. }
  153. public void setSelected(boolean b) {}
  154. } //}}}
  155. //{{{ MouseHandler class
  156. class MouseHandler extends MouseAdapter
  157. {
  158. boolean msgSet = false;
  159. public void mouseReleased(MouseEvent evt)
  160. {
  161. if(msgSet)
  162. {
  163. GUIUtilities.getView((Component)evt.getSource())
  164. .getStatus().setMessage(null);
  165. msgSet = false;
  166. }
  167. }
  168. public void mouseEntered(MouseEvent evt)
  169. {
  170. String msg = jEdit.getProperty(action + ".mouse-over");
  171. if(msg != null)
  172. {
  173. GUIUtilities.getView((Component)evt.getSource())
  174. .getStatus().setMessage(msg);
  175. msgSet = true;
  176. }
  177. }
  178. public void mouseExited(MouseEvent evt)
  179. {
  180. if(msgSet)
  181. {
  182. GUIUtilities.getView((Component)evt.getSource())
  183. .getStatus().setMessage(null);
  184. msgSet = false;
  185. }
  186. }
  187. } //}}}
  188. }