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.

SwingWindowFactory.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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;
  23. import com.dmdirc.addons.ui_swing.components.frames.ChannelFrameFactory;
  24. import com.dmdirc.addons.ui_swing.components.frames.CustomFrameFactory;
  25. import com.dmdirc.addons.ui_swing.components.frames.CustomInputFrameFactory;
  26. import com.dmdirc.addons.ui_swing.components.frames.ServerFrameFactory;
  27. import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
  28. import com.dmdirc.addons.ui_swing.events.SwingActiveWindowChangeRequestEvent;
  29. import com.dmdirc.addons.ui_swing.events.SwingEventBus;
  30. import com.dmdirc.addons.ui_swing.events.SwingWindowAddedEvent;
  31. import com.dmdirc.addons.ui_swing.events.SwingWindowDeletedEvent;
  32. import com.dmdirc.interfaces.WindowModel;
  33. import com.dmdirc.interfaces.ui.FrameListener;
  34. import java.util.Collection;
  35. import java.util.HashMap;
  36. import java.util.Map;
  37. import java.util.Optional;
  38. import java.util.Set;
  39. import javax.annotation.Nullable;
  40. import javax.inject.Inject;
  41. import javax.inject.Singleton;
  42. import org.slf4j.Logger;
  43. import org.slf4j.LoggerFactory;
  44. import static com.dmdirc.util.LogUtils.USER_ERROR;
  45. /**
  46. * Handles creation of windows in the Swing UI.
  47. *
  48. * @since 0.6.4
  49. */
  50. @Singleton
  51. public class SwingWindowFactory implements FrameListener {
  52. private static final Logger LOG = LoggerFactory.getLogger(SwingWindowFactory.class);
  53. /** A map of known implementations of window interfaces. */
  54. private final Map<Collection<String>, WindowProvider> implementations = new HashMap<>();
  55. /** A map of frame containers to their Swing windows. */
  56. private final Map<WindowModel, TextFrame> windows = new HashMap<>();
  57. /** The swing event bus. */
  58. private final SwingEventBus swingEventBus;
  59. /**
  60. * Creates a new window factory for the specified controller.
  61. *
  62. * @param customFrameFactory The factory to use to produce custom frames.
  63. * @param customInputFrameFactory The factory to use to produce custom input frames.
  64. * @param serverFrameFactory The factory to use to produce server frames.
  65. * @param channelFrameFactory The factory to use to produce channel frames.
  66. * @param swingEventBus The swing event bus;
  67. */
  68. @Inject
  69. public SwingWindowFactory(
  70. final CustomFrameFactory customFrameFactory,
  71. final CustomInputFrameFactory customInputFrameFactory,
  72. final ServerFrameFactory serverFrameFactory,
  73. final ChannelFrameFactory channelFrameFactory,
  74. final SwingEventBus swingEventBus) {
  75. this.swingEventBus = swingEventBus;
  76. registerImplementation(customFrameFactory);
  77. registerImplementation(customInputFrameFactory);
  78. registerImplementation(serverFrameFactory);
  79. registerImplementation(channelFrameFactory);
  80. }
  81. /**
  82. * Registers a new provider that will be used to create certain window implementations.
  83. *
  84. * <p>
  85. * If a previous provider exists for the same configuration, it will be replaced.
  86. *
  87. * @param provider The provider to use to generate new windows.
  88. */
  89. public final void registerImplementation(final WindowProvider provider) {
  90. implementations.put(provider.getComponents(), provider);
  91. }
  92. @Override
  93. public void addWindow(final WindowModel window, final boolean focus) {
  94. addWindow(null, window, focus);
  95. }
  96. @Override
  97. public void addWindow(final WindowModel parent, final WindowModel window,
  98. final boolean focus) {
  99. UIUtilities.invokeLater(() -> {
  100. final TextFrame parentWindow = getSwingWindow(parent);
  101. final TextFrame childWindow = doAddWindow(window);
  102. if (childWindow == null) {
  103. return;
  104. }
  105. swingEventBus.publish(new SwingWindowAddedEvent(
  106. Optional.ofNullable(parentWindow), childWindow));
  107. if (focus) {
  108. swingEventBus.publishAsync(new SwingActiveWindowChangeRequestEvent(
  109. Optional.ofNullable(childWindow)));
  110. }
  111. });
  112. }
  113. /**
  114. * Creates a new window for the specified container.
  115. *
  116. * @param window The container that owns the window
  117. *
  118. * @return The created window or null on error
  119. */
  120. protected TextFrame doAddWindow(final WindowModel window) {
  121. if (!implementations.containsKey(window.getComponents())) {
  122. LOG.error(USER_ERROR, "Unable to create window: Unknown type.");
  123. return null;
  124. }
  125. final WindowProvider provider = implementations.get(window.getComponents());
  126. final TextFrame frame = provider.getWindow(window);
  127. if (frame != null) {
  128. frame.init();
  129. windows.put(window, frame);
  130. }
  131. return frame;
  132. }
  133. @Override
  134. public void delWindow(final WindowModel window) {
  135. delWindow(null, window);
  136. }
  137. @Override
  138. public void delWindow(final WindowModel parent, final WindowModel window) {
  139. final TextFrame parentWindow = getSwingWindow(parent);
  140. final TextFrame childWindow = getSwingWindow(window);
  141. windows.remove(window);
  142. UIUtilities.invokeLater(() -> swingEventBus.publish(new SwingWindowDeletedEvent(
  143. Optional.ofNullable(parentWindow), childWindow)));
  144. }
  145. /**
  146. * Retrieves a single Swing UI created window belonging to the specified container. Returns null
  147. * if the container is null or no such window exists.
  148. *
  149. * @param window The container whose windows should be searched
  150. *
  151. * @return A relevant window or null
  152. */
  153. public TextFrame getSwingWindow(@Nullable final WindowModel window) {
  154. return windows.get(window);
  155. }
  156. /** Disposes of this window factory, removing all listeners. */
  157. public void dispose() {
  158. windows.values().forEach(TextFrame::dispose);
  159. }
  160. /**
  161. * Provides a new window instance for a container.
  162. */
  163. public interface WindowProvider {
  164. /**
  165. * Gets a new window for the specified container.
  166. *
  167. * @param container The container to create a new window for.
  168. *
  169. * @return A new window for the given container.
  170. */
  171. TextFrame getWindow(WindowModel container);
  172. /**
  173. * Gets the set of components that this provider can provide windows for.
  174. *
  175. * @return The components this provider operates on.
  176. */
  177. Set<String> getComponents();
  178. }
  179. }