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.

WindowMenuFrameManager.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 2006-2007 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.ui.swing.framemanager.windowmenu;
  23. import com.dmdirc.Channel;
  24. import com.dmdirc.FrameContainer;
  25. import com.dmdirc.Main;
  26. import com.dmdirc.Query;
  27. import com.dmdirc.Server;
  28. import com.dmdirc.ui.interfaces.FrameManager;
  29. import com.dmdirc.ui.swing.components.FrameContainerComparator;
  30. import java.awt.Color;
  31. import java.awt.Font;
  32. import java.awt.event.ActionEvent;
  33. import java.awt.event.ActionListener;
  34. import java.io.Serializable;
  35. import java.util.Map;
  36. import java.util.Map.Entry;
  37. import java.util.TreeMap;
  38. import javax.swing.JComponent;
  39. import javax.swing.JMenuItem;
  40. /**
  41. * Manages the window menu window list.
  42. */
  43. public final class WindowMenuFrameManager implements FrameManager,
  44. ActionListener, Serializable {
  45. /**
  46. * A version number for this class. It should be changed whenever the class
  47. * structure is changed (or anything else that would prevent serialized
  48. * objects being unserialized with the new class).
  49. */
  50. private static final long serialVersionUID = 1;
  51. /** Menu item list. */
  52. private final Map<FrameContainer, JMenuItem> menuItemMap;
  53. /** Creates a new instance of WindowMenuFrameManager. */
  54. public WindowMenuFrameManager() {
  55. menuItemMap = new TreeMap<FrameContainer, JMenuItem>(new FrameContainerComparator());
  56. new TreeMap();
  57. }
  58. /** {@inheritDoc} */
  59. public void setParent(final JComponent parent) {
  60. //Ignore
  61. }
  62. /** {@inheritDoc} */
  63. public boolean canPositionVertically() {
  64. return true;
  65. }
  66. /** {@inheritDoc} */
  67. public boolean canPositionHorizontally() {
  68. return true;
  69. }
  70. /** {@inheritDoc} */
  71. public void setSelected(final FrameContainer source) {
  72. synchronized (menuItemMap) {
  73. for (Entry<FrameContainer, JMenuItem> entry : menuItemMap.entrySet()) {
  74. final JMenuItem mi = entry.getValue();
  75. if (entry.getKey() == source) {
  76. mi.setFont(mi.getFont().deriveFont(Font.BOLD));
  77. } else {
  78. mi.setFont(mi.getFont().deriveFont(Font.PLAIN));
  79. }
  80. }
  81. }
  82. }
  83. /** {@inheritDoc} */
  84. public void showNotification(final FrameContainer source, final Color colour) {
  85. //Ignore
  86. }
  87. /** {@inheritDoc} */
  88. public void clearNotification(final FrameContainer source) {
  89. //Ignore
  90. }
  91. /** {@inheritDoc} */
  92. public void addServer(final Server server) {
  93. addFrameContainer(server);
  94. }
  95. /** {@inheritDoc} */
  96. public void delServer(final Server server) {
  97. removeFramecontainer(server);
  98. }
  99. /** {@inheritDoc} */
  100. public void addChannel(final Server server, final Channel channel) {
  101. addFrameContainer(channel);
  102. }
  103. /** {@inheritDoc} */
  104. public void delChannel(final Server server, final Channel channel) {
  105. removeFramecontainer(channel);
  106. }
  107. /** {@inheritDoc} */
  108. public void addQuery(final Server server, final Query query) {
  109. addFrameContainer(query);
  110. }
  111. /** {@inheritDoc} */
  112. public void delQuery(final Server server, final Query query) {
  113. removeFramecontainer(query);
  114. }
  115. /** {@inheritDoc} */
  116. public void addCustom(final Server server, final FrameContainer window) {
  117. addFrameContainer(window);
  118. }
  119. /** {@inheritDoc} */
  120. public void delCustom(final Server server, final FrameContainer window) {
  121. removeFramecontainer(window);
  122. }
  123. /** {@inheritDoc} */
  124. public void iconUpdated(final FrameContainer window) {
  125. menuItemMap.get(window).setIcon(window.getIcon());
  126. }
  127. /**
  128. * Adds a frame container to the list.
  129. *
  130. * @param window Window to add to the list
  131. */
  132. private void addFrameContainer(final FrameContainer window) {
  133. final JMenuItem mi = new JMenuItem(window.toString(), window.getIcon());
  134. final TreeMap<FrameContainer, JMenuItem> newMap =
  135. new TreeMap<FrameContainer, JMenuItem>(new FrameContainerComparator());
  136. mi.addActionListener(this);
  137. menuItemMap.put(window, mi);
  138. newMap.putAll(menuItemMap);
  139. Main.getUI().getMainWindow().populateWindowMenu(newMap);
  140. }
  141. /**
  142. * Removes a frame container from the list.
  143. *
  144. * @param window Window to remove from list
  145. */
  146. private void removeFramecontainer(final FrameContainer window) {
  147. final TreeMap<FrameContainer, JMenuItem> newMap =
  148. new TreeMap<FrameContainer, JMenuItem>(new FrameContainerComparator());
  149. menuItemMap.remove(window);
  150. newMap.putAll(menuItemMap);
  151. Main.getUI().getMainWindow().populateWindowMenu(newMap);
  152. }
  153. /** {@inheritDoc} */
  154. public void actionPerformed(final ActionEvent e) {
  155. synchronized (menuItemMap) {
  156. for (Entry<FrameContainer, JMenuItem> entry : menuItemMap.entrySet()) {
  157. if (entry.getValue() == e.getSource()) {
  158. entry.getKey().activateFrame();
  159. }
  160. }
  161. }
  162. }
  163. }