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

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

#
Java | 159 lines | 118 code | 19 blank | 22 comment | 21 complexity | 544059dde9eb0e0d226daefb77223372 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. * 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. import org.gjt.sp.util.Log;
  26. /**
  27. * jEdit's custom menu item. It adds support for multi-key shortcuts.
  28. */
  29. public class EnhancedCheckBoxMenuItem extends JCheckBoxMenuItem
  30. {
  31. public EnhancedCheckBoxMenuItem(String label, EditAction action)
  32. {
  33. super(label);
  34. this.action = action;
  35. if(action != null)
  36. {
  37. setEnabled(true);
  38. addActionListener(new EditAction.Wrapper(action));
  39. shortcutProp1 = action.getName() + ".shortcut";
  40. shortcutProp2 = action.getName() + ".shortcut2";
  41. }
  42. else
  43. setEnabled(false);
  44. setModel(new Model());
  45. }
  46. public Dimension getPreferredSize()
  47. {
  48. Dimension d = super.getPreferredSize();
  49. String shortcut = getShortcut();
  50. if(shortcut != null)
  51. {
  52. d.width += (getFontMetrics(acceleratorFont)
  53. .stringWidth(shortcut) + 10);
  54. }
  55. return d;
  56. }
  57. public void paint(Graphics g)
  58. {
  59. super.paint(g);
  60. String shortcut = getShortcut();
  61. if(shortcut != null)
  62. {
  63. g.setFont(acceleratorFont);
  64. g.setColor(getModel().isArmed() ?
  65. acceleratorSelectionForeground :
  66. acceleratorForeground);
  67. FontMetrics fm = g.getFontMetrics();
  68. Insets insets = getInsets();
  69. g.drawString(shortcut,getWidth() - (fm.stringWidth(
  70. shortcut) + insets.right + insets.left),
  71. getFont().getSize() + (insets.top - 1)
  72. /* XXX magic number */);
  73. }
  74. }
  75. public String getActionCommand()
  76. {
  77. return getModel().getActionCommand();
  78. }
  79. // private members
  80. private String shortcutProp1;
  81. private String shortcutProp2;
  82. private EditAction action;
  83. private static Font acceleratorFont;
  84. private static Color acceleratorForeground;
  85. private static Color acceleratorSelectionForeground;
  86. private String getShortcut()
  87. {
  88. if(action == null)
  89. return null;
  90. else
  91. {
  92. String shortcut1 = jEdit.getProperty(shortcutProp1);
  93. String shortcut2 = jEdit.getProperty(shortcutProp2);
  94. if(shortcut1 == null || shortcut1.length() == 0)
  95. {
  96. if(shortcut2 == null || shortcut2.length() == 0)
  97. return null;
  98. else
  99. return shortcut2;
  100. }
  101. else
  102. {
  103. if(shortcut2 == null || shortcut2.length() == 0)
  104. return shortcut1;
  105. else
  106. return shortcut1 + " or " + shortcut2;
  107. }
  108. }
  109. }
  110. static
  111. {
  112. acceleratorFont = UIManager.getFont("MenuItem.acceleratorFont");
  113. acceleratorFont = new Font("Monospaced",
  114. acceleratorFont.getStyle(),
  115. acceleratorFont.getSize());
  116. acceleratorForeground = UIManager
  117. .getColor("MenuItem.acceleratorForeground");
  118. acceleratorSelectionForeground = UIManager
  119. .getColor("MenuItem.acceleratorSelectionForeground");
  120. }
  121. class Model extends DefaultButtonModel
  122. {
  123. public boolean isSelected()
  124. {
  125. if(!isShowing())
  126. return false;
  127. try
  128. {
  129. return action.isSelected(GUIUtilities.getView(
  130. EnhancedCheckBoxMenuItem.this));
  131. }
  132. catch(Throwable t)
  133. {
  134. Log.log(Log.ERROR,this,t);
  135. return false;
  136. }
  137. }
  138. public void setSelected(boolean b) {}
  139. }
  140. }