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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.components;
  23. import com.dmdirc.ClientModule.GlobalConfig;
  24. import com.dmdirc.addons.ui_swing.EdtHandlerInvocation;
  25. import com.dmdirc.addons.ui_swing.SwingController;
  26. import com.dmdirc.addons.ui_swing.events.SwingEventBus;
  27. import com.dmdirc.addons.ui_swing.events.SwingWindowAddedEvent;
  28. import com.dmdirc.addons.ui_swing.events.SwingWindowDeletedEvent;
  29. import com.dmdirc.addons.ui_swing.events.SwingWindowSelectedEvent;
  30. import com.dmdirc.addons.ui_swing.interfaces.ActiveFrameManager;
  31. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  32. import com.dmdirc.interfaces.config.ConfigChangeListener;
  33. import com.dmdirc.plugins.PluginDomain;
  34. import com.dmdirc.ui.IconManager;
  35. import javax.inject.Inject;
  36. import javax.inject.Singleton;
  37. import javax.swing.JPanel;
  38. import javax.swing.SwingUtilities;
  39. import net.miginfocom.swing.MigLayout;
  40. import net.engio.mbassy.listener.Handler;
  41. /**
  42. * Provides an MDI style bar for closing frames.
  43. */
  44. @Singleton
  45. public class MDIBar extends JPanel implements ConfigChangeListener {
  46. /** A version number for this class. */
  47. private static final long serialVersionUID = -8028057596226636245L;
  48. /** Icon size for the close button. */
  49. private static final int ICON_SIZE = 12;
  50. /** Button to close frames. */
  51. private final NoFocusButton closeButton;
  52. /** Active frame manager. */
  53. private final ActiveFrameManager activeFrameManager;
  54. /** Config manager to get settings from. */
  55. private final AggregateConfigProvider config;
  56. /** Option domain. */
  57. private final String configDomain;
  58. /** Are we meant to be visible? */
  59. private boolean visibility;
  60. /**
  61. * Instantiates a new MDI bar.
  62. *
  63. * @param globalConfig The config to read settings from.
  64. * @param iconManager The manager to use to retrieve icons.
  65. * @param domain The domain to read settings from under.
  66. * @param activeFrameManager Active frame manager.
  67. * @param eventBus The event bus to listen to events on.
  68. */
  69. @Inject
  70. public MDIBar(
  71. @GlobalConfig final AggregateConfigProvider globalConfig,
  72. @GlobalConfig final IconManager iconManager,
  73. @PluginDomain(SwingController.class) final String domain,
  74. final ActiveFrameManager activeFrameManager,
  75. final SwingEventBus eventBus) {
  76. this.activeFrameManager = activeFrameManager;
  77. config = globalConfig;
  78. configDomain = domain;
  79. visibility = config.getOptionBool(configDomain, "mdiBarVisibility");
  80. closeButton = new NoFocusButton(iconManager.getScaledIcon("close-12", ICON_SIZE, ICON_SIZE));
  81. setOpaque(false);
  82. setLayout(new MigLayout("hmax 17, ins 1 0 0 0, fill"));
  83. add(closeButton, "w 17!, h 17!, right");
  84. eventBus.subscribe(this);
  85. closeButton.addActionListener(e -> activeFrameManager.getActiveFrame()
  86. .ifPresent(f -> f.getContainer().close()));
  87. config.addChangeListener(configDomain, "mdiBarVisibility", this);
  88. check();
  89. }
  90. @Override
  91. public void setEnabled(final boolean enabled) {
  92. closeButton.setEnabled(enabled);
  93. }
  94. /**
  95. * Checks whether this MDI bar should be visible and active and sets itself accordingly.
  96. */
  97. private void check() {
  98. SwingUtilities.invokeLater(() -> {
  99. setVisible(visibility);
  100. setEnabled(activeFrameManager.getActiveFrame().isPresent());
  101. });
  102. }
  103. @Handler
  104. public void windowAdded(final SwingWindowAddedEvent event) {
  105. check();
  106. }
  107. @Handler
  108. public void windowDeleted(final SwingWindowDeletedEvent event) {
  109. check();
  110. }
  111. @Override
  112. public void configChanged(final String domain, final String key) {
  113. visibility = config.getOptionBool(configDomain, "mdiBarVisibility");
  114. check();
  115. }
  116. @Handler(invocation = EdtHandlerInvocation.class)
  117. public void selectionChanged(final SwingWindowSelectedEvent event) {
  118. check();
  119. }
  120. }