Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CheckBoxMenuItem.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.components;
  23. import javax.swing.Action;
  24. import javax.swing.Icon;
  25. import javax.swing.JCheckBoxMenuItem;
  26. import javax.swing.MenuElement;
  27. import javax.swing.MenuSelectionManager;
  28. import javax.swing.event.ChangeEvent;
  29. import javax.swing.event.ChangeListener;
  30. /**
  31. * Extention of a normal JCheckboxMenuItem that stays open when clicked.
  32. */
  33. public class CheckBoxMenuItem extends JCheckBoxMenuItem {
  34. /**
  35. * A version number for this class. It should be changed whenever the class
  36. * structure is changed (or anything else that would prevent serialized
  37. * objects being unserialized with the new class).
  38. */
  39. private static final long serialVersionUID = 1;
  40. /** Menu path to use when clicked. */
  41. private static MenuElement[] path;
  42. /**
  43. * Constructs a new checkbox menu item.
  44. *
  45. * @see JCheckBoxMenuItem#JCheckBoxMenuItem()
  46. */
  47. public CheckBoxMenuItem() {
  48. super();
  49. getModel().addChangeListener(new StayOpenListener());
  50. }
  51. /**
  52. * Constructs a new checkbox menu item with the specified action.
  53. *
  54. * @param a Action to use
  55. *
  56. * @see JCheckBoxMenuItem#JCheckBoxMenuItem(Action)
  57. */
  58. public CheckBoxMenuItem(final Action a) {
  59. super(a);
  60. getModel().addChangeListener(new StayOpenListener());
  61. }
  62. /**
  63. * Constructs a new checkbox menu item with the specified action.
  64. *
  65. * @param icon Icon to use
  66. *
  67. * @see JCheckBoxMenuItem#JCheckBoxMenuItem(Icon)
  68. */
  69. public CheckBoxMenuItem(final Icon icon) {
  70. super(icon);
  71. getModel().addChangeListener(new StayOpenListener());
  72. }
  73. /**
  74. * Constructs a new checkbox menu item with the specified text.
  75. *
  76. * @param text Text to use
  77. *
  78. * @see JCheckBoxMenuItem#JCheckBoxMenuItem(String)
  79. */
  80. public CheckBoxMenuItem(final String text) {
  81. super(text);
  82. getModel().addChangeListener(new StayOpenListener());
  83. }
  84. /**
  85. * Constructs a new checkbox menu item with the specified text and selected
  86. * state.
  87. *
  88. * @param text Text to use
  89. * @param selected Initial selection state
  90. *
  91. * @see JCheckBoxMenuItem#JCheckBoxMenuItem(String, boolean)
  92. */
  93. public CheckBoxMenuItem(final String text, final boolean selected) {
  94. super(text, selected);
  95. getModel().addChangeListener(new StayOpenListener());
  96. }
  97. /**
  98. * Constructs a new checkbox menu item with the specified text and icon.
  99. *
  100. * @param text Text to use
  101. * @param icon Icon to use
  102. *
  103. * @see JCheckBoxMenuItem#JCheckBoxMenuItem(String, Icon)
  104. */
  105. public CheckBoxMenuItem(final String text, final Icon icon) {
  106. super(text, icon);
  107. getModel().addChangeListener(new StayOpenListener());
  108. }
  109. /**
  110. * Constructs a new checkbox menu item with the specified text icon and
  111. * initial selected state.
  112. *
  113. * @param text Text to use
  114. * @param icon Icon to use
  115. * @param selected Initial selection state
  116. *
  117. * @see JCheckBoxMenuItem#JCheckBoxMenuItem(String, Icon, boolean)
  118. */
  119. public CheckBoxMenuItem(final String text, final Icon icon,
  120. final boolean selected) {
  121. super(text, icon, selected);
  122. getModel().addChangeListener(new StayOpenListener());
  123. }
  124. /**
  125. * Overridden to reopen the menu.
  126. *
  127. * @param pressTime the time to "hold down" the button, in milliseconds
  128. */
  129. @Override
  130. public void doClick(final int pressTime) {
  131. super.doClick(pressTime);
  132. MenuSelectionManager.defaultManager().setSelectedPath(path);
  133. }
  134. /**
  135. * Listener to restore the saved path when clicked.
  136. */
  137. private class StayOpenListener implements ChangeListener {
  138. /** {@inheritDoc} */
  139. @Override
  140. public void stateChanged(final ChangeEvent e) {
  141. if (getModel().isArmed() && isShowing()) {
  142. path = MenuSelectionManager.defaultManager().getSelectedPath();
  143. }
  144. }
  145. }
  146. }