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.

MDIBar.java 5.0KB

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