選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CheckBoxMenuItem.java 4.8KB

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