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

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