PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 187 lines | 122 code | 22 blank | 43 comment | 23 complexity | 5e2bdaf986b9df7d1fba4abdbdbbef54 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, 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. //}}}
  30. /**
  31. * jEdit's custom menu item. It adds support for multi-key shortcuts.
  32. */
  33. public class EnhancedMenuItem extends JMenuItem
  34. {
  35. //{{{ EnhancedMenuItem 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 actionCommand The action command
  42. */
  43. public EnhancedMenuItem(String label, EditAction action)
  44. {
  45. super(label);
  46. this.action = action;
  47. if(action != null)
  48. {
  49. setEnabled(true);
  50. addActionListener(new EditAction.Wrapper(action));
  51. shortcutProp1 = action.getName() + ".shortcut";
  52. shortcutProp2 = action.getName() + ".shortcut2";
  53. addMouseListener(new MouseHandler());
  54. }
  55. else
  56. setEnabled(false);
  57. } //}}}
  58. //{{{ getPreferredSize() method
  59. public Dimension getPreferredSize()
  60. {
  61. Dimension d = super.getPreferredSize();
  62. String shortcut = getShortcut();
  63. if(shortcut != null)
  64. {
  65. d.width += (getFontMetrics(acceleratorFont)
  66. .stringWidth(shortcut) + 15);
  67. }
  68. return d;
  69. } //}}}
  70. //{{{ paint() method
  71. public void paint(Graphics g)
  72. {
  73. super.paint(g);
  74. String shortcut = getShortcut();
  75. if(shortcut != null)
  76. {
  77. g.setFont(acceleratorFont);
  78. g.setColor(getModel().isArmed() ?
  79. acceleratorSelectionForeground :
  80. acceleratorForeground);
  81. FontMetrics fm = g.getFontMetrics();
  82. Insets insets = getInsets();
  83. g.drawString(shortcut,getWidth() - (fm.stringWidth(
  84. shortcut) + insets.right + insets.left + 5),
  85. getFont().getSize() + (insets.top -
  86. (OperatingSystem.isMacOSLF() ? 0 : 1))
  87. /* XXX magic number */);
  88. }
  89. } //}}}
  90. //{{{ Private members
  91. //{{{ Instance variables
  92. private String shortcutProp1;
  93. private String shortcutProp2;
  94. private EditAction action;
  95. private static Font acceleratorFont;
  96. private static Color acceleratorForeground;
  97. private static Color acceleratorSelectionForeground;
  98. //}}}
  99. //{{{ getShortcut() method
  100. private String getShortcut()
  101. {
  102. if(action == null)
  103. return null;
  104. else
  105. {
  106. String shortcut1 = jEdit.getProperty(shortcutProp1);
  107. String shortcut2 = jEdit.getProperty(shortcutProp2);
  108. if(shortcut1 == null || shortcut1.length() == 0)
  109. {
  110. if(shortcut2 == null || shortcut2.length() == 0)
  111. return null;
  112. else
  113. return shortcut2;
  114. }
  115. else
  116. {
  117. if(shortcut2 == null || shortcut2.length() == 0)
  118. return shortcut1;
  119. else
  120. return shortcut1 + " or " + shortcut2;
  121. }
  122. }
  123. } //}}}
  124. //{{{ Class initializer
  125. static
  126. {
  127. String shortcutFont;
  128. if (OperatingSystem.isMacOSLF())
  129. shortcutFont = "Lucida Grande";
  130. else
  131. shortcutFont = "Monospaced";
  132. acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont");
  133. acceleratorFont = new Font(shortcutFont,
  134. acceleratorFont.getStyle(),
  135. acceleratorFont.getSize());
  136. acceleratorForeground = UIManager
  137. .getColor("MenuItem.acceleratorForeground");
  138. acceleratorSelectionForeground = UIManager
  139. .getColor("MenuItem.acceleratorSelectionForeground");
  140. } //}}}
  141. //}}}
  142. //{{{ MouseHandler class
  143. class MouseHandler extends MouseAdapter
  144. {
  145. public void mouseReleased(MouseEvent evt)
  146. {
  147. GUIUtilities.getView((Component)evt.getSource())
  148. .getStatus().setMessage(null);
  149. }
  150. public void mouseEntered(MouseEvent evt)
  151. {
  152. String msg = action.getMouseOverText();
  153. if(msg != null)
  154. {
  155. GUIUtilities.getView((Component)evt.getSource())
  156. .getStatus().setMessage(msg);
  157. }
  158. }
  159. public void mouseExited(MouseEvent evt)
  160. {
  161. GUIUtilities.getView((Component)evt.getSource())
  162. .getStatus().setMessage(null);
  163. }
  164. } //}}}
  165. }