您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SwingWindowFactory.java 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing;
  18. import com.dmdirc.addons.ui_swing.components.frames.ChannelFrameFactory;
  19. import com.dmdirc.addons.ui_swing.components.frames.CustomFrameFactory;
  20. import com.dmdirc.addons.ui_swing.components.frames.CustomInputFrameFactory;
  21. import com.dmdirc.addons.ui_swing.components.frames.ServerFrameFactory;
  22. import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
  23. import com.dmdirc.addons.ui_swing.events.SwingActiveWindowChangeRequestEvent;
  24. import com.dmdirc.addons.ui_swing.events.SwingEventBus;
  25. import com.dmdirc.addons.ui_swing.events.SwingWindowAddedEvent;
  26. import com.dmdirc.addons.ui_swing.events.SwingWindowDeletedEvent;
  27. import com.dmdirc.interfaces.WindowModel;
  28. import com.dmdirc.interfaces.ui.FrameListener;
  29. import java.util.Collection;
  30. import java.util.HashMap;
  31. import java.util.Map;
  32. import java.util.Optional;
  33. import java.util.Set;
  34. import javax.annotation.Nullable;
  35. import javax.inject.Inject;
  36. import javax.inject.Singleton;
  37. import org.slf4j.Logger;
  38. import org.slf4j.LoggerFactory;
  39. import static com.dmdirc.util.LogUtils.USER_ERROR;
  40. /**
  41. * Handles creation of windows in the Swing UI.
  42. *
  43. * @since 0.6.4
  44. */
  45. @Singleton
  46. public class SwingWindowFactory implements FrameListener {
  47. private static final Logger LOG = LoggerFactory.getLogger(SwingWindowFactory.class);
  48. /** A map of known implementations of window interfaces. */
  49. private final Map<Collection<String>, WindowProvider> implementations = new HashMap<>();
  50. /** A map of frame containers to their Swing windows. */
  51. private final Map<WindowModel, TextFrame> windows = new HashMap<>();
  52. /** The swing event bus. */
  53. private final SwingEventBus swingEventBus;
  54. /**
  55. * Creates a new window factory for the specified controller.
  56. *
  57. * @param customFrameFactory The factory to use to produce custom frames.
  58. * @param customInputFrameFactory The factory to use to produce custom input frames.
  59. * @param serverFrameFactory The factory to use to produce server frames.
  60. * @param channelFrameFactory The factory to use to produce channel frames.
  61. * @param swingEventBus The swing event bus;
  62. */
  63. @Inject
  64. public SwingWindowFactory(
  65. final CustomFrameFactory customFrameFactory,
  66. final CustomInputFrameFactory customInputFrameFactory,
  67. final ServerFrameFactory serverFrameFactory,
  68. final ChannelFrameFactory channelFrameFactory,
  69. final SwingEventBus swingEventBus) {
  70. this.swingEventBus = swingEventBus;
  71. registerImplementation(customFrameFactory);
  72. registerImplementation(customInputFrameFactory);
  73. registerImplementation(serverFrameFactory);
  74. registerImplementation(channelFrameFactory);
  75. }
  76. /**
  77. * Registers a new provider that will be used to create certain window implementations.
  78. *
  79. * <p>
  80. * If a previous provider exists for the same configuration, it will be replaced.
  81. *
  82. * @param provider The provider to use to generate new windows.
  83. */
  84. public final void registerImplementation(final WindowProvider provider) {
  85. implementations.put(provider.getComponents(), provider);
  86. }
  87. @Override
  88. public void addWindow(final WindowModel window, final boolean focus) {
  89. addWindow(null, window, focus);
  90. }
  91. @Override
  92. public void addWindow(final WindowModel parent, final WindowModel window,
  93. final boolean focus) {
  94. UIUtilities.invokeLater(() -> {
  95. final TextFrame parentWindow = getSwingWindow(parent);
  96. final TextFrame childWindow = doAddWindow(window);
  97. if (childWindow == null) {
  98. return;
  99. }
  100. swingEventBus.publish(new SwingWindowAddedEvent(
  101. Optional.ofNullable(parentWindow), childWindow));
  102. if (focus) {
  103. swingEventBus.publishAsync(new SwingActiveWindowChangeRequestEvent(
  104. Optional.ofNullable(childWindow)));
  105. }
  106. });
  107. }
  108. /**
  109. * Creates a new window for the specified container.
  110. *
  111. * @param window The container that owns the window
  112. *
  113. * @return The created window or null on error
  114. */
  115. protected TextFrame doAddWindow(final WindowModel window) {
  116. if (!implementations.containsKey(window.getComponents())) {
  117. LOG.error(USER_ERROR, "Unable to create window: Unknown type.");
  118. return null;
  119. }
  120. final WindowProvider provider = implementations.get(window.getComponents());
  121. final TextFrame frame = provider.getWindow(window);
  122. if (frame != null) {
  123. frame.init();
  124. windows.put(window, frame);
  125. }
  126. return frame;
  127. }
  128. @Override
  129. public void delWindow(final WindowModel window) {
  130. delWindow(null, window);
  131. }
  132. @Override
  133. public void delWindow(final WindowModel parent, final WindowModel window) {
  134. final TextFrame parentWindow = getSwingWindow(parent);
  135. final TextFrame childWindow = getSwingWindow(window);
  136. windows.remove(window);
  137. UIUtilities.invokeLater(() -> swingEventBus.publish(new SwingWindowDeletedEvent(
  138. Optional.ofNullable(parentWindow), childWindow)));
  139. }
  140. /**
  141. * Retrieves a single Swing UI created window belonging to the specified container. Returns null
  142. * if the container is null or no such window exists.
  143. *
  144. * @param window The container whose windows should be searched
  145. *
  146. * @return A relevant window or null
  147. */
  148. public TextFrame getSwingWindow(@Nullable final WindowModel window) {
  149. return windows.get(window);
  150. }
  151. /** Disposes of this window factory, removing all listeners. */
  152. public void dispose() {
  153. windows.values().forEach(TextFrame::dispose);
  154. }
  155. /**
  156. * Provides a new window instance for a container.
  157. */
  158. public interface WindowProvider {
  159. /**
  160. * Gets a new window for the specified container.
  161. *
  162. * @param container The container to create a new window for.
  163. *
  164. * @return A new window for the given container.
  165. */
  166. TextFrame getWindow(WindowModel container);
  167. /**
  168. * Gets the set of components that this provider can provide windows for.
  169. *
  170. * @return The components this provider operates on.
  171. */
  172. Set<String> getComponents();
  173. }
  174. }