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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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.Channel;
  24. import com.dmdirc.FrameContainer;
  25. import com.dmdirc.Server;
  26. import com.dmdirc.WritableFrameContainer;
  27. import com.dmdirc.addons.ui_swing.components.frames.ChannelFrameFactory;
  28. import com.dmdirc.addons.ui_swing.components.frames.CustomFrameFactory;
  29. import com.dmdirc.addons.ui_swing.components.frames.CustomInputFrameFactory;
  30. import com.dmdirc.addons.ui_swing.components.frames.ServerFrameFactory;
  31. import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
  32. import com.dmdirc.interfaces.ui.FrameListener;
  33. import com.dmdirc.logger.ErrorLevel;
  34. import com.dmdirc.logger.Logger;
  35. import com.dmdirc.ui.core.components.WindowComponent;
  36. import com.dmdirc.util.collections.ListenerList;
  37. import java.util.Arrays;
  38. import java.util.Collection;
  39. import java.util.HashMap;
  40. import java.util.HashSet;
  41. import java.util.Map;
  42. import java.util.Set;
  43. import javax.inject.Inject;
  44. import javax.inject.Singleton;
  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. /** A map of known implementations of window interfaces. */
  53. private final Map<Collection<String>, WindowProvider> implementations = new HashMap<>();
  54. /** A map of frame containers to their Swing windows. */
  55. private final Map<FrameContainer, TextFrame> windows = new HashMap<>();
  56. /** The controller that owns this window factory. */
  57. private final SwingController controller;
  58. /** Our list of listeners. */
  59. private final ListenerList listeners = new ListenerList();
  60. /**
  61. * Creates a new window factory for the specified controller.
  62. *
  63. * @param controller The controller this factory is for
  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. */
  69. @Inject
  70. public SwingWindowFactory(
  71. final SwingController controller,
  72. final CustomFrameFactory customFrameFactory,
  73. final CustomInputFrameFactory customInputFrameFactory,
  74. final ServerFrameFactory serverFrameFactory,
  75. final ChannelFrameFactory channelFrameFactory) {
  76. this.controller = controller;
  77. // TODO: Allow auto-factories to implement an interface and simplify this a bit.
  78. registerImplementation(
  79. new HashSet<>(Arrays.asList(
  80. WindowComponent.TEXTAREA.getIdentifier())),
  81. new WindowProvider() {
  82. @Override
  83. public TextFrame getWindow(final FrameContainer container) {
  84. return customFrameFactory.getCustomFrame(container);
  85. }
  86. });
  87. registerImplementation(
  88. new HashSet<>(Arrays.asList(
  89. WindowComponent.TEXTAREA.getIdentifier(),
  90. WindowComponent.INPUTFIELD.getIdentifier())),
  91. new WindowProvider() {
  92. @Override
  93. public TextFrame getWindow(final FrameContainer container) {
  94. return customInputFrameFactory.getCustomInputFrame(
  95. (WritableFrameContainer) container);
  96. }
  97. });
  98. registerImplementation(
  99. new HashSet<>(Arrays.asList(
  100. WindowComponent.TEXTAREA.getIdentifier(),
  101. WindowComponent.INPUTFIELD.getIdentifier(),
  102. WindowComponent.CERTIFICATE_VIEWER.getIdentifier())),
  103. new WindowProvider() {
  104. @Override
  105. public TextFrame getWindow(final FrameContainer container) {
  106. return serverFrameFactory.getServerFrame((Server) container);
  107. }
  108. });
  109. registerImplementation(
  110. new HashSet<>(Arrays.asList(
  111. WindowComponent.TEXTAREA.getIdentifier(),
  112. WindowComponent.INPUTFIELD.getIdentifier(),
  113. WindowComponent.TOPICBAR.getIdentifier(),
  114. WindowComponent.USERLIST.getIdentifier())),
  115. new WindowProvider() {
  116. @Override
  117. public TextFrame getWindow(final FrameContainer container) {
  118. return channelFrameFactory.getChannelFrame((Channel) container);
  119. }
  120. });
  121. }
  122. /**
  123. * Registers a new provider that will be used to create certain window implementations.
  124. *
  125. * <p>If a previous provider exists for the same configuration, it will be replaced.
  126. *
  127. * @param components The component configuration that is provided by the implementation.
  128. * @param provider The provider to use to generate new windows.
  129. */
  130. public final void registerImplementation(
  131. final Set<String> components,
  132. final WindowProvider provider) {
  133. implementations.put(components, provider);
  134. }
  135. /**
  136. * Registers a new listener which will be notified about the addition and deletion of all Swing
  137. * UI windows.
  138. *
  139. * @param listener The listener to be added
  140. */
  141. public void addWindowListener(final SwingWindowListener listener) {
  142. listeners.add(SwingWindowListener.class, listener);
  143. }
  144. /**
  145. * Un-registers the specified listener from being notified about the addiction and deletion of
  146. * all Swing UI windows.
  147. *
  148. * @param listener The listener to be removed
  149. */
  150. public void removeWindowListener(final SwingWindowListener listener) {
  151. listeners.remove(SwingWindowListener.class, listener);
  152. }
  153. /** {@inheritDoc} */
  154. @Override
  155. public void addWindow(final FrameContainer window, final boolean focus) {
  156. addWindow(null, window, focus);
  157. }
  158. /**
  159. * Creates a new window for the specified container.
  160. *
  161. * @param window The container that owns the window
  162. * @param focus Whether the window should be focused initially
  163. *
  164. * @return The created window or null on error
  165. */
  166. protected TextFrame doAddWindow(final FrameContainer window, final boolean focus) {
  167. if (!implementations.containsKey(window.getComponents())) {
  168. Logger.userError(ErrorLevel.HIGH, "Unable to create window: Unknown type");
  169. return null;
  170. }
  171. final WindowProvider provider = implementations.get(window.getComponents());
  172. final TextFrame frame = provider.getWindow(window);
  173. if (frame != null) {
  174. windows.put(window, frame);
  175. }
  176. return frame;
  177. }
  178. /**
  179. * Retrieves a single Swing UI created window belonging to the specified container. Returns null
  180. * if the container is null or no such window exists.
  181. *
  182. * @param window The container whose windows should be searched
  183. *
  184. * @return A relevant window or null
  185. */
  186. public TextFrame getSwingWindow(final FrameContainer window) {
  187. return windows.get(window);
  188. }
  189. /** {@inheritDoc} */
  190. @Override
  191. public void delWindow(final FrameContainer window) {
  192. delWindow(null, window);
  193. }
  194. /** {@inheritDoc} */
  195. @Override
  196. public void addWindow(final FrameContainer parent,
  197. final FrameContainer window, final boolean focus) {
  198. UIUtilities.invokeLater(new Runnable() {
  199. @Override
  200. public void run() {
  201. final TextFrame parentWindow = getSwingWindow(parent);
  202. final TextFrame childWindow = doAddWindow(window, focus);
  203. if (childWindow == null) {
  204. return;
  205. }
  206. for (SwingWindowListener listener : listeners.get(SwingWindowListener.class)) {
  207. listener.windowAdded(parentWindow, childWindow);
  208. }
  209. if (focus) {
  210. controller.requestWindowFocus(childWindow);
  211. }
  212. }
  213. });
  214. }
  215. /** {@inheritDoc} */
  216. @Override
  217. public void delWindow(final FrameContainer parent,
  218. final FrameContainer window) {
  219. UIUtilities.invokeLater(new Runnable() {
  220. @Override
  221. public void run() {
  222. final TextFrame parentWindow = getSwingWindow(parent);
  223. final TextFrame childWindow = getSwingWindow(window);
  224. for (SwingWindowListener listener : listeners.get(SwingWindowListener.class)) {
  225. listener.windowDeleted(parentWindow, childWindow);
  226. }
  227. windows.remove(window);
  228. }
  229. });
  230. }
  231. /** Disposes of this window factory, removing all listeners. */
  232. public void dispose() {
  233. for (SwingWindowListener listener : listeners.get(SwingWindowListener.class)) {
  234. listeners.remove(SwingWindowListener.class, listener);
  235. }
  236. for (TextFrame frame : windows.values()) {
  237. frame.dispose();
  238. }
  239. }
  240. /**
  241. * Provides a new window instance for a container.
  242. */
  243. public interface WindowProvider {
  244. /**
  245. * Gets a new window for the specified container.
  246. *
  247. * @param container The container to create a new window for.
  248. *
  249. * @return A new window for the given container.
  250. */
  251. TextFrame getWindow(FrameContainer container);
  252. }
  253. }