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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Copyright (c) 2006-2013 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.FrameContainer;
  24. import com.dmdirc.addons.ui_swing.components.frames.ChannelFrame;
  25. import com.dmdirc.addons.ui_swing.components.frames.CustomFrame;
  26. import com.dmdirc.addons.ui_swing.components.frames.CustomInputFrame;
  27. import com.dmdirc.addons.ui_swing.components.frames.ServerFrame;
  28. import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
  29. import com.dmdirc.logger.ErrorLevel;
  30. import com.dmdirc.logger.Logger;
  31. import com.dmdirc.ui.core.components.WindowComponent;
  32. import com.dmdirc.interfaces.ui.FrameListener;
  33. import com.dmdirc.interfaces.ui.Window;
  34. import com.dmdirc.util.collections.ListenerList;
  35. import java.util.Arrays;
  36. import java.util.Collection;
  37. import java.util.HashMap;
  38. import java.util.HashSet;
  39. import java.util.Map;
  40. import java.util.Set;
  41. /**
  42. * Handles creation of windows in the Swing UI.
  43. *
  44. * @since 0.6.4
  45. */
  46. public class SwingWindowFactory implements FrameListener {
  47. /** A map of known implementations of window interfaces. */
  48. private final Map<Collection<String>, Class<? extends Window>>
  49. implementations = new HashMap<Collection<String>,
  50. Class<? extends Window>>();
  51. /** A map of frame containers to their Swing windows. */
  52. private final Map<FrameContainer, TextFrame> windows
  53. = new HashMap<FrameContainer, TextFrame>();
  54. /** The controller that owns this window factory. */
  55. private final SwingController controller;
  56. /** Our list of listeners. */
  57. private final ListenerList listeners = new ListenerList();
  58. /**
  59. * Creates a new window factory for the specified controller.
  60. *
  61. * @param controller The controller this factory is for
  62. */
  63. public SwingWindowFactory(final SwingController controller) {
  64. this.controller = controller;
  65. registerImplementation(new HashSet<String>(
  66. Arrays.asList(WindowComponent.TEXTAREA.getIdentifier())),
  67. CustomFrame.class);
  68. registerImplementation(new HashSet<String>(
  69. Arrays.asList(WindowComponent.TEXTAREA.getIdentifier(),
  70. WindowComponent.INPUTFIELD.getIdentifier())),
  71. CustomInputFrame.class);
  72. registerImplementation(new HashSet<String>(
  73. Arrays.asList(WindowComponent.TEXTAREA.getIdentifier(),
  74. WindowComponent.INPUTFIELD.getIdentifier(),
  75. WindowComponent.CERTIFICATE_VIEWER.getIdentifier())),
  76. ServerFrame.class);
  77. registerImplementation(new HashSet<String>(
  78. Arrays.asList(WindowComponent.TEXTAREA.getIdentifier(),
  79. WindowComponent.INPUTFIELD.getIdentifier(),
  80. WindowComponent.TOPICBAR.getIdentifier(),
  81. WindowComponent.USERLIST.getIdentifier())),
  82. ChannelFrame.class);
  83. }
  84. public final void registerImplementation(final Set<String> components,
  85. final Class<? extends Window> clazz) {
  86. implementations.put(components, clazz);
  87. }
  88. /**
  89. * Registers a new listener which will be notified about the addition
  90. * and deletion of all Swing UI windows.
  91. *
  92. * @param listener The listener to be added
  93. */
  94. public void addWindowListener(final SwingWindowListener listener) {
  95. listeners.add(SwingWindowListener.class, listener);
  96. }
  97. /**
  98. * Un-registers the specified listener from being notified about the
  99. * addiction and deletion of all Swing UI windows.
  100. *
  101. * @param listener The listener to be removed
  102. */
  103. public void removeWindowListener(final SwingWindowListener listener) {
  104. listeners.remove(SwingWindowListener.class, listener);
  105. }
  106. /** {@inheritDoc} */
  107. @Override
  108. public void addWindow(final FrameContainer window, final boolean focus) {
  109. addWindow(null, window, focus);
  110. }
  111. /**
  112. * Creates a new window for the specified container.
  113. *
  114. * @param window The container that owns the window
  115. * @param focus Whether the window should be focused initially
  116. * @return The created window or null on error
  117. */
  118. protected TextFrame doAddWindow(final FrameContainer window,
  119. final boolean focus) {
  120. final Class<? extends Window> clazz;
  121. if (implementations.containsKey(window.getComponents())) {
  122. clazz = implementations.get(window.getComponents());
  123. } else {
  124. Logger.userError(ErrorLevel.HIGH,
  125. "Unable to create window: Unknown type");
  126. return null;
  127. }
  128. try {
  129. final TextFrame frame = (TextFrame) clazz.getConstructors()[0]
  130. .newInstance(controller, window);
  131. windows.put(window, frame);
  132. return frame;
  133. } catch (Exception ex) {
  134. Logger.appError(ErrorLevel.HIGH, "Unable to create window", ex);
  135. return null;
  136. }
  137. }
  138. /**
  139. * Retrieves a single Swing UI created window belonging to the specified
  140. * container. Returns null if the container is null or no such window
  141. * exists.
  142. *
  143. * @param window The container whose windows should be searched
  144. * @return A relevant window or null
  145. */
  146. public TextFrame getSwingWindow(final FrameContainer window) {
  147. return windows.get(window);
  148. }
  149. /** {@inheritDoc} */
  150. @Override
  151. public void delWindow(final FrameContainer window) {
  152. delWindow(null, window);
  153. }
  154. /** {@inheritDoc} */
  155. @Override
  156. public void addWindow(final FrameContainer parent,
  157. final FrameContainer window, final boolean focus) {
  158. UIUtilities.invokeLater(new Runnable() {
  159. @Override
  160. public void run() {
  161. final TextFrame parentWindow = getSwingWindow(parent);
  162. final TextFrame childWindow = doAddWindow(window, focus);
  163. if (childWindow == null) {
  164. return;
  165. }
  166. for (SwingWindowListener listener : listeners.get(
  167. SwingWindowListener.class)) {
  168. listener.windowAdded(parentWindow, childWindow);
  169. }
  170. if (focus) {
  171. controller.requestWindowFocus(childWindow);
  172. }
  173. }
  174. });
  175. }
  176. /** {@inheritDoc} */
  177. @Override
  178. public void delWindow(final FrameContainer parent,
  179. final FrameContainer window) {
  180. UIUtilities.invokeLater(new Runnable() {
  181. @Override
  182. public void run() {
  183. final TextFrame parentWindow = getSwingWindow(parent);
  184. final TextFrame childWindow = getSwingWindow(window);
  185. for (SwingWindowListener listener : listeners.get(
  186. SwingWindowListener.class)) {
  187. listener.windowDeleted(parentWindow, childWindow);
  188. }
  189. windows.remove(window);
  190. }
  191. });
  192. }
  193. /** Disposes of this window factory, removing all listeners. */
  194. public void dispose() {
  195. for (SwingWindowListener listener : listeners.get(
  196. SwingWindowListener.class)) {
  197. listeners.remove(SwingWindowListener.class, listener);
  198. }
  199. for (TextFrame frame : windows.values()) {
  200. frame.dispose();
  201. }
  202. }
  203. }