Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

MDIBar.java 5.0KB

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