PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/gui/EnhancedMenuItem.java

#
Java | 137 lines | 93 code | 15 blank | 29 comment | 20 complexity | f395d0d2e572415490a21f052ffbe5f4 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. * Copyright (C) 1999, 2000, 2001 Slava Pestov
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package org.gjt.sp.jedit.gui;
  20. import javax.swing.*;
  21. import java.awt.event.*;
  22. import java.awt.*;
  23. import org.gjt.sp.jedit.textarea.JEditTextArea;
  24. import org.gjt.sp.jedit.*;
  25. /**
  26. * jEdit's custom menu item. It adds support for multi-key shortcuts.
  27. */
  28. public class EnhancedMenuItem extends JMenuItem
  29. {
  30. /**
  31. * Creates a new menu item. Most plugins should call
  32. * GUIUtilities.loadMenuItem() instead.
  33. * @param label The menu item label
  34. * @param action The edit action
  35. * @param actionCommand The action command
  36. */
  37. public EnhancedMenuItem(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. }
  48. else
  49. setEnabled(false);
  50. }
  51. public Dimension getPreferredSize()
  52. {
  53. Dimension d = super.getPreferredSize();
  54. String shortcut = getShortcut();
  55. if(shortcut != null)
  56. {
  57. d.width += (getFontMetrics(acceleratorFont)
  58. .stringWidth(shortcut) + 10);
  59. }
  60. return d;
  61. }
  62. public void paint(Graphics g)
  63. {
  64. super.paint(g);
  65. String shortcut = getShortcut();
  66. if(shortcut != null)
  67. {
  68. g.setFont(acceleratorFont);
  69. g.setColor(getModel().isArmed() ?
  70. acceleratorSelectionForeground :
  71. acceleratorForeground);
  72. FontMetrics fm = g.getFontMetrics();
  73. Insets insets = getInsets();
  74. g.drawString(shortcut,getWidth() - (fm.stringWidth(
  75. shortcut) + insets.right + insets.left),
  76. getFont().getSize() + (insets.top - 1)
  77. /* XXX magic number */);
  78. }
  79. }
  80. // private members
  81. private String shortcutProp1;
  82. private String shortcutProp2;
  83. private EditAction action;
  84. private static Font acceleratorFont;
  85. private static Color acceleratorForeground;
  86. private static Color acceleratorSelectionForeground;
  87. private String getShortcut()
  88. {
  89. if(action == null)
  90. return null;
  91. else
  92. {
  93. String shortcut1 = jEdit.getProperty(shortcutProp1);
  94. String shortcut2 = jEdit.getProperty(shortcutProp2);
  95. if(shortcut1 == null || shortcut1.length() == 0)
  96. {
  97. if(shortcut2 == null || shortcut2.length() == 0)
  98. return null;
  99. else
  100. return shortcut2;
  101. }
  102. else
  103. {
  104. if(shortcut2 == null || shortcut2.length() == 0)
  105. return shortcut1;
  106. else
  107. return shortcut1 + " or " + shortcut2;
  108. }
  109. }
  110. }
  111. static
  112. {
  113. acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont");
  114. acceleratorFont = new Font("Monospaced",
  115. acceleratorFont.getStyle(),
  116. acceleratorFont.getSize());
  117. acceleratorForeground = UIManager
  118. .getColor("MenuItem.acceleratorForeground");
  119. acceleratorSelectionForeground = UIManager
  120. .getColor("MenuItem.acceleratorSelectionForeground");
  121. }
  122. }