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 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. *
  3. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. package com.dmdirc.addons.ui_swing.components;
  24. import com.dmdirc.addons.ui_swing.MainFrame;
  25. import com.dmdirc.addons.ui_swing.UIUtilities;
  26. import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
  27. import com.dmdirc.FrameContainer;
  28. import com.dmdirc.config.ConfigManager;
  29. import com.dmdirc.config.IdentityManager;
  30. import com.dmdirc.interfaces.ConfigChangeListener;
  31. import com.dmdirc.interfaces.SelectionListener;
  32. import com.dmdirc.ui.IconManager;
  33. import com.dmdirc.ui.WindowManager;
  34. import com.dmdirc.ui.interfaces.Window;
  35. import com.dmdirc.ui.interfaces.FrameListener;
  36. import java.awt.event.ActionEvent;
  37. import java.awt.event.ActionListener;
  38. import java.beans.PropertyChangeEvent;
  39. import java.beans.PropertyChangeListener;
  40. import javax.swing.JInternalFrame;
  41. import javax.swing.JPanel;
  42. import net.miginfocom.swing.MigLayout;
  43. /**
  44. * Provides an MDI style bar for restore/minimise/close.
  45. */
  46. public class MDIBar extends JPanel implements FrameListener, SelectionListener,
  47. PropertyChangeListener, ActionListener, ConfigChangeListener {
  48. private static final long serialVersionUID = -8028057596226636245L;
  49. private static final int ICON_SIZE = 12;
  50. private NoFocusButton closeButton;
  51. private NoFocusButton minimiseButton;
  52. private NoFocusButton restoreButton;
  53. private MainFrame mainFrame;
  54. private ConfigManager config;
  55. private String visibility;
  56. /** Active frame. */
  57. private Window activeFrame;
  58. /**
  59. * Instantiates a new MDI bar.
  60. *
  61. * @param mainFrame Main frame instance
  62. */
  63. public MDIBar(final MainFrame mainFrame) {
  64. this.mainFrame = mainFrame;
  65. this.config = IdentityManager.getGlobalConfig();
  66. visibility = config.getOption("ui", "mdiBarVisibility");
  67. closeButton = new NoFocusButton(IconManager.getIconManager().
  68. getScaledIcon("close-12", ICON_SIZE, ICON_SIZE));
  69. minimiseButton = new NoFocusButton(IconManager.getIconManager().
  70. getScaledIcon("minimise-12", ICON_SIZE, ICON_SIZE));
  71. restoreButton = new NoFocusButton(IconManager.getIconManager().
  72. getScaledIcon("maximise-12", ICON_SIZE, ICON_SIZE));
  73. setOpaque(false);
  74. setLayout(new MigLayout("hmax 17, ins 1 0 0 0, fill"));
  75. add(minimiseButton, "w 17!, h 17!, right");
  76. add(restoreButton, "w 17!, h 17!, right");
  77. add(closeButton, "w 17!, h 17!, right");
  78. WindowManager.addFrameListener(this);
  79. WindowManager.addSelectionListener(this);
  80. closeButton.addActionListener(this);
  81. minimiseButton.addActionListener(this);
  82. restoreButton.addActionListener(this);
  83. config.addChangeListener("ui", "mdiBarVisibility", this);
  84. check();
  85. }
  86. /** {@inheritDoc} */
  87. @Override
  88. public void setEnabled(final boolean enabled) {
  89. closeButton.setEnabled(enabled);
  90. minimiseButton.setEnabled(enabled);
  91. restoreButton.setEnabled(enabled);
  92. }
  93. private void check() {
  94. UIUtilities.invokeLater(new Runnable() {
  95. @Override
  96. public void run() {
  97. boolean show = true;
  98. if (mainFrame == null) {
  99. show = false;
  100. return;
  101. } else if ("alwaysShow".equalsIgnoreCase(visibility)) {
  102. show = mainFrame.getDesktopPane().getAllFrames().length > 0;
  103. } else if ("neverShow".equalsIgnoreCase(visibility)) {
  104. show = false;
  105. } else if ("showWhenMaximised".equalsIgnoreCase(visibility)) {
  106. show = mainFrame.getMaximised();
  107. }
  108. setVisible(show);
  109. setEnabled(mainFrame.getDesktopPane().getAllFrames().length > 0);
  110. }
  111. });
  112. }
  113. /** {@inheritDoc} */
  114. @Override
  115. public void addWindow(final FrameContainer window) {
  116. if (window.getFrame() instanceof JInternalFrame) {
  117. ((JInternalFrame) window.getFrame()).addPropertyChangeListener(
  118. "maximum", this);
  119. }
  120. check();
  121. }
  122. /** {@inheritDoc} */
  123. @Override
  124. public void delWindow(final FrameContainer window) {
  125. if (window.getFrame() instanceof JInternalFrame) {
  126. ((JInternalFrame) window.getFrame()).removePropertyChangeListener(
  127. this);
  128. }
  129. check();
  130. }
  131. /** {@inheritDoc} */
  132. @Override
  133. public void addWindow(final FrameContainer parent,
  134. final FrameContainer window) {
  135. addWindow(window);
  136. }
  137. /** {@inheritDoc} */
  138. @Override
  139. public void delWindow(final FrameContainer parent,
  140. final FrameContainer window) {
  141. delWindow(window);
  142. }
  143. /** {@inheritDoc} */
  144. @Override
  145. public void propertyChange(final PropertyChangeEvent evt) {
  146. if ((Boolean) evt.getNewValue()) {
  147. restoreButton.setIcon(IconManager.getIconManager().getScaledIcon(
  148. "restore-12", ICON_SIZE, ICON_SIZE));
  149. } else {
  150. restoreButton.setIcon(IconManager.getIconManager().getScaledIcon(
  151. "maximise-12", ICON_SIZE, ICON_SIZE));
  152. }
  153. check();
  154. }
  155. /**
  156. * {@inheritDoc}
  157. *
  158. * @param e Action event
  159. */
  160. @Override
  161. public void actionPerformed(final ActionEvent e) {
  162. if (activeFrame == null) {
  163. return;
  164. }
  165. if (closeButton.equals(e.getSource())) {
  166. activeFrame.close();
  167. } else if (minimiseButton.equals(e.getSource())) {
  168. ((TextFrame) activeFrame).minimise();
  169. } else if (restoreButton.equals(e.getSource())) {
  170. activeFrame.toggleMaximise();
  171. }
  172. }
  173. /** {@inheritDoc} */
  174. @Override
  175. public void configChanged(final String domain, final String key) {
  176. visibility = config.getOption("ui", "mdiBarVisibility");
  177. check();
  178. }
  179. @Override
  180. public void selectionChanged(Window window) {
  181. activeFrame = window;
  182. }
  183. }