You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CloseActiveWindowMenuItem.java 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  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.framemanager.windowmenu;
  23. import com.dmdirc.ClientModule;
  24. import com.dmdirc.addons.ui_swing.EdtHandlerInvocation;
  25. import com.dmdirc.addons.ui_swing.events.SwingEventBus;
  26. import com.dmdirc.addons.ui_swing.events.SwingWindowAddedEvent;
  27. import com.dmdirc.addons.ui_swing.events.SwingWindowDeletedEvent;
  28. import com.dmdirc.addons.ui_swing.interfaces.ActiveFrameManager;
  29. import com.dmdirc.ui.IconManager;
  30. import com.dmdirc.ui.WindowManager;
  31. import java.awt.event.ActionEvent;
  32. import java.awt.event.ActionListener;
  33. import javax.inject.Inject;
  34. import javax.swing.JMenuItem;
  35. import net.engio.mbassy.listener.Handler;
  36. import net.engio.mbassy.listener.Invoke;
  37. /**
  38. * A Menu item that closes the active window when triggered.
  39. */
  40. public class CloseActiveWindowMenuItem extends JMenuItem implements ActionListener {
  41. private final ActiveFrameManager activeFrameManager;
  42. private final SwingEventBus eventBus;
  43. private final WindowManager windowManager;
  44. @Inject
  45. public CloseActiveWindowMenuItem(final ActiveFrameManager activeFrameManager,
  46. @ClientModule.GlobalConfig final IconManager iconManager,
  47. final SwingEventBus eventBus,
  48. final WindowManager windowManager) {
  49. super(iconManager.getIcon("close"));
  50. this.activeFrameManager = activeFrameManager;
  51. this.eventBus = eventBus;
  52. this.windowManager = windowManager;
  53. setMnemonic('c');
  54. setText("Close");
  55. setActionCommand("Close");
  56. }
  57. /**
  58. * Initialises the menu item adding listeners as required.
  59. */
  60. public void init() {
  61. addActionListener(this);
  62. eventBus.subscribe(this);
  63. }
  64. @Override
  65. public void actionPerformed(final ActionEvent e) {
  66. activeFrameManager.getActiveFrame().getContainer().close();
  67. }
  68. @Handler(invocation = EdtHandlerInvocation.class, delivery = Invoke.Asynchronously)
  69. public void windowAdded(final SwingWindowAddedEvent event) {
  70. setEnabled(true);
  71. }
  72. @Handler(invocation = EdtHandlerInvocation.class, delivery = Invoke.Asynchronously)
  73. public void windowDeleted(final SwingWindowDeletedEvent event) {
  74. setEnabled(!windowManager.getRootWindows().isEmpty());
  75. }
  76. }