/jEdit/tags/jedit-4-1-final/org/gjt/sp/jedit/gui/EnhancedMenuItem.java

# · Java · 186 lines · 121 code · 22 blank · 43 comment · 23 complexity · 799a03be4bc383cf92848a4dd620f8cd MD5 · raw file

  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, 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.*;
  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 actionCommand The action command
  41. */
  42. public EnhancedMenuItem(String label, EditAction action)
  43. {
  44. super(label);
  45. this.action = action;
  46. if(action != null)
  47. {
  48. setEnabled(true);
  49. addActionListener(new EditAction.Wrapper(action));
  50. shortcutProp1 = action.getName() + ".shortcut";
  51. shortcutProp2 = action.getName() + ".shortcut2";
  52. addMouseListener(new MouseHandler());
  53. }
  54. else
  55. setEnabled(false);
  56. } //}}}
  57. //{{{ getPreferredSize() method
  58. public Dimension getPreferredSize()
  59. {
  60. Dimension d = super.getPreferredSize();
  61. String shortcut = getShortcut();
  62. if(shortcut != null)
  63. {
  64. d.width += (getFontMetrics(acceleratorFont)
  65. .stringWidth(shortcut) + 15);
  66. }
  67. return d;
  68. } //}}}
  69. //{{{ paint() method
  70. public void paint(Graphics g)
  71. {
  72. super.paint(g);
  73. String shortcut = getShortcut();
  74. if(shortcut != null)
  75. {
  76. g.setFont(acceleratorFont);
  77. g.setColor(getModel().isArmed() ?
  78. acceleratorSelectionForeground :
  79. acceleratorForeground);
  80. FontMetrics fm = g.getFontMetrics();
  81. Insets insets = getInsets();
  82. g.drawString(shortcut,getWidth() - (fm.stringWidth(
  83. shortcut) + insets.right + insets.left + 5),
  84. getFont().getSize() + (insets.top -
  85. (OperatingSystem.isMacOSLF() ? 0 : 1))
  86. /* XXX magic number */);
  87. }
  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. String shortcutFont;
  127. if (OperatingSystem.isMacOSLF())
  128. shortcutFont = "Lucida Grande";
  129. else
  130. shortcutFont = "Monospaced";
  131. acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont");
  132. acceleratorFont = new Font(shortcutFont,
  133. acceleratorFont.getStyle(),
  134. acceleratorFont.getSize());
  135. acceleratorForeground = UIManager
  136. .getColor("MenuItem.acceleratorForeground");
  137. acceleratorSelectionForeground = UIManager
  138. .getColor("MenuItem.acceleratorSelectionForeground");
  139. } //}}}
  140. //}}}
  141. //{{{ MouseHandler class
  142. class MouseHandler extends MouseAdapter
  143. {
  144. public void mouseReleased(MouseEvent evt)
  145. {
  146. GUIUtilities.getView((Component)evt.getSource())
  147. .getStatus().setMessage(null);
  148. }
  149. public void mouseEntered(MouseEvent evt)
  150. {
  151. String msg = action.getMouseOverText();
  152. if(msg != null)
  153. {
  154. GUIUtilities.getView((Component)evt.getSource())
  155. .getStatus().setMessage(msg);
  156. }
  157. }
  158. public void mouseExited(MouseEvent evt)
  159. {
  160. GUIUtilities.getView((Component)evt.getSource())
  161. .getStatus().setMessage(null);
  162. }
  163. } //}}}
  164. }