Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

SwingWindowFactory.java 8.0KB

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