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

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

#
Java | 209 lines | 142 code | 21 blank | 46 comment | 34 complexity | b583830faf045335de57b9494fbba8af 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. * EnhancedMenuItem.java - Menu item with user-specified accelerator string
  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. //}}}
  29. /**
  30. * jEdit's custom menu item. It adds support for multi-key shortcuts.
  31. */
  32. public class EnhancedMenuItem extends JMenuItem
  33. {
  34. //{{{ EnhancedMenuItem constructor
  35. /**
  36. * Creates a new menu item. Most plugins should call
  37. * GUIUtilities.loadMenuItem() instead.
  38. * @param label The menu item label
  39. * @param action The edit action
  40. * @param context An action context
  41. * @since jEdit 4.2pre1
  42. */
  43. public EnhancedMenuItem(String label, String action, ActionContext context)
  44. {
  45. this.action = action;
  46. this.shortcut = getShortcut();
  47. if(OperatingSystem.hasScreenMenuBar() && shortcut != null)
  48. {
  49. setText(label + " (" + shortcut + ")");
  50. shortcut = null;
  51. }
  52. else
  53. setText(label);
  54. if(action != null)
  55. {
  56. setEnabled(true);
  57. addActionListener(new EditAction.Wrapper(context,action));
  58. addMouseListener(new MouseHandler());
  59. }
  60. else
  61. setEnabled(false);
  62. } //}}}
  63. //{{{ getPreferredSize() method
  64. public Dimension getPreferredSize()
  65. {
  66. Dimension d = super.getPreferredSize();
  67. if(shortcut != null)
  68. {
  69. d.width += (getFontMetrics(acceleratorFont)
  70. .stringWidth(shortcut) + 15);
  71. }
  72. return d;
  73. } //}}}
  74. //{{{ paint() method
  75. public void paint(Graphics g)
  76. {
  77. super.paint(g);
  78. if(shortcut != null)
  79. {
  80. g.setFont(acceleratorFont);
  81. g.setColor(getModel().isArmed() ?
  82. acceleratorSelectionForeground :
  83. acceleratorForeground);
  84. FontMetrics fm = g.getFontMetrics();
  85. Insets insets = getInsets();
  86. g.drawString(shortcut,getWidth() - (fm.stringWidth(
  87. shortcut) + insets.right + insets.left + 5),
  88. getFont().getSize() + (insets.top -
  89. (OperatingSystem.isMacOSLF() ? 0 : 1))
  90. /* XXX magic number */);
  91. }
  92. } //}}}
  93. //{{{ Package-private members
  94. static Font acceleratorFont;
  95. static Color acceleratorForeground;
  96. static Color acceleratorSelectionForeground;
  97. //}}}
  98. //{{{ Private members
  99. //{{{ Instance variables
  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. //{{{ Class initializer
  129. static
  130. {
  131. String shortcutFont;
  132. if (OperatingSystem.isMacOSLF())
  133. shortcutFont = "Lucida Grande";
  134. else
  135. shortcutFont = "Monospaced";
  136. acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont");
  137. if(acceleratorFont == null)
  138. acceleratorFont = new Font(shortcutFont,Font.PLAIN,12);
  139. else
  140. {
  141. acceleratorFont = new Font(shortcutFont,
  142. acceleratorFont.getStyle(),
  143. acceleratorFont.getSize());
  144. }
  145. acceleratorForeground = UIManager
  146. .getColor("MenuItem.acceleratorForeground");
  147. if(acceleratorForeground == null)
  148. acceleratorForeground = Color.black;
  149. acceleratorSelectionForeground = UIManager
  150. .getColor("MenuItem.acceleratorSelectionForeground");
  151. if(acceleratorSelectionForeground == null)
  152. acceleratorSelectionForeground = Color.black;
  153. } //}}}
  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. }