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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2006-2013 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.MainFrame;
  24. import com.dmdirc.addons.ui_swing.SelectionListener;
  25. import com.dmdirc.addons.ui_swing.SwingController;
  26. import com.dmdirc.addons.ui_swing.SwingWindowListener;
  27. import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
  28. import com.dmdirc.config.ConfigManager;
  29. import com.dmdirc.interfaces.ConfigChangeListener;
  30. import java.awt.event.ActionEvent;
  31. import java.awt.event.ActionListener;
  32. import javax.swing.JPanel;
  33. import javax.swing.SwingUtilities;
  34. import net.miginfocom.swing.MigLayout;
  35. /**
  36. * Provides an MDI style bar for closing frames.
  37. */
  38. public class MDIBar extends JPanel implements SwingWindowListener,
  39. SelectionListener, ActionListener, ConfigChangeListener {
  40. /**
  41. * A version number for this class. It should be changed whenever the class
  42. * structure is changed (or anything else that would prevent serialized
  43. * objects being unserialized with the new class).
  44. */
  45. private static final long serialVersionUID = -8028057596226636245L;
  46. /** Icon size for the close button. */
  47. private static final int ICON_SIZE = 12;
  48. /** Button to close frames. */
  49. private final NoFocusButton closeButton;
  50. /** Main frame component bieng shown in. */
  51. private final MainFrame mainFrame;
  52. /** Config manager to get settings from. */
  53. private final ConfigManager config;
  54. /** Option domain. */
  55. private final String configDomain;
  56. /** Are we meant to be visible? */
  57. private boolean visibility;
  58. /**
  59. * Instantiates a new MDI bar.
  60. *
  61. * @param controller The controller that owns this MDI bar
  62. * @param mainFrame Main frame instance
  63. */
  64. public MDIBar(final SwingController controller, final MainFrame mainFrame) {
  65. super();
  66. this.mainFrame = mainFrame;
  67. config = controller.getGlobalConfig();
  68. configDomain = controller.getDomain();
  69. visibility = config.getOptionBool(configDomain, "mdiBarVisibility");
  70. closeButton = new NoFocusButton(controller.getIconManager()
  71. .getScaledIcon("close-12", ICON_SIZE,ICON_SIZE));
  72. setOpaque(false);
  73. setLayout(new MigLayout("hmax 17, ins 1 0 0 0, fill"));
  74. add(closeButton, "w 17!, h 17!, right");
  75. controller.getWindowFactory().addWindowListener(this);
  76. mainFrame.addSelectionListener(this);
  77. closeButton.addActionListener(this);
  78. config.addChangeListener(configDomain, "mdiBarVisibility", this);
  79. check();
  80. }
  81. /** {@inheritDoc} */
  82. @Override
  83. public void setEnabled(final boolean enabled) {
  84. closeButton.setEnabled(enabled);
  85. }
  86. /**
  87. * Checks whether this MDI bar should be visible and active and sets itself
  88. * accordingly.
  89. */
  90. private void check() {
  91. SwingUtilities.invokeLater(new Runnable() {
  92. @Override
  93. public void run() {
  94. setVisible(visibility);
  95. setEnabled(mainFrame.getActiveFrame() != null);
  96. }
  97. });
  98. }
  99. /** {@inheritDoc} */
  100. @Override
  101. public void windowAdded(final TextFrame parent, final TextFrame window) {
  102. check();
  103. }
  104. /** {@inheritDoc} */
  105. @Override
  106. public void windowDeleted(final TextFrame parent, final TextFrame window) {
  107. check();
  108. }
  109. /**
  110. * {@inheritDoc}
  111. *
  112. * @param e Action event
  113. */
  114. @Override
  115. public void actionPerformed(final ActionEvent e) {
  116. if (mainFrame.getActiveFrame() == null) {
  117. return;
  118. }
  119. if (closeButton.equals(e.getSource())) {
  120. mainFrame.getActiveFrame().getContainer().close();
  121. }
  122. }
  123. /** {@inheritDoc} */
  124. @Override
  125. public void configChanged(final String domain, final String key) {
  126. visibility = config.getOptionBool(configDomain, "mdiBarVisibility");
  127. check();
  128. }
  129. /** {@inheritDoc} */
  130. @Override
  131. public void selectionChanged(final TextFrame window) {
  132. check();
  133. }
  134. }