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

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