Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

MDIBar.java 5.2KB

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