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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.QueryFrame;
  28. import com.dmdirc.addons.ui_swing.components.frames.ServerFrame;
  29. import com.dmdirc.logger.ErrorLevel;
  30. import com.dmdirc.logger.Logger;
  31. import com.dmdirc.ui.interfaces.ChannelWindow;
  32. import com.dmdirc.ui.interfaces.FrameListener;
  33. import com.dmdirc.ui.interfaces.InputWindow;
  34. import com.dmdirc.ui.interfaces.QueryWindow;
  35. import com.dmdirc.ui.interfaces.ServerWindow;
  36. import com.dmdirc.ui.interfaces.Window;
  37. import com.dmdirc.util.ListenerList;
  38. import java.util.HashMap;
  39. import java.util.Map;
  40. /**
  41. * Handles creation of windows in the Swing UI.
  42. *
  43. * @since 0.6.4
  44. * @author chris
  45. */
  46. public class SwingWindowFactory implements FrameListener {
  47. /** A map of known implementations of window interfaces. */
  48. private static final Map<Class<? extends Window>, Class<? extends Window>> IMPLEMENTATIONS
  49. = new HashMap<Class<? extends Window>, Class<? extends Window>>();
  50. static {
  51. IMPLEMENTATIONS.put(Window.class, CustomFrame.class);
  52. IMPLEMENTATIONS.put(InputWindow.class, CustomInputFrame.class);
  53. IMPLEMENTATIONS.put(ServerWindow.class, ServerFrame.class);
  54. IMPLEMENTATIONS.put(QueryWindow.class, QueryFrame.class);
  55. IMPLEMENTATIONS.put(ChannelWindow.class, ChannelFrame.class);
  56. }
  57. /** The controller that owns this window factory. */
  58. private final SwingController controller;
  59. /** Our list of listeners. */
  60. private final ListenerList listeners = new ListenerList();
  61. /**
  62. * Creates a new window factory for the specified controller.
  63. *
  64. * @param controller The controller this factory is for
  65. */
  66. public SwingWindowFactory(final SwingController controller) {
  67. this.controller = controller;
  68. }
  69. /**
  70. * Registers a new listener which will be notified about the addition
  71. * and deletion of all Swing UI windows.
  72. *
  73. * @param listener The listener to be added
  74. */
  75. public void addWindowListener(final SwingWindowListener listener) {
  76. listeners.add(SwingWindowListener.class, listener);
  77. }
  78. /** {@inheritDoc} */
  79. @Override
  80. public void addWindow(final FrameContainer<?> window, final boolean focus) {
  81. addWindow(null, window, focus);
  82. }
  83. /**
  84. * Creates a new window for the specified container.
  85. *
  86. * @param <T> The type of window that should be created
  87. * @param window The container that owns the window
  88. * @param focus Whether the window should be focused initially
  89. * @return The created window or null on error
  90. */
  91. @SuppressWarnings("unchecked")
  92. protected <T extends Window> T doAddWindow(final FrameContainer<T> window,
  93. final boolean focus) {
  94. final Class<T> clazz;
  95. if (IMPLEMENTATIONS.containsKey(window.getWindowClass())) {
  96. clazz = (Class<T>) IMPLEMENTATIONS.get(window.getWindowClass());
  97. } else {
  98. clazz = window.getWindowClass();
  99. }
  100. try {
  101. final T frame = (T) clazz.getConstructors()[0].newInstance(controller, window);
  102. window.addWindow(frame);
  103. return frame;
  104. } catch (Exception ex) {
  105. Logger.appError(ErrorLevel.HIGH, "Unable to create window", ex);
  106. return null;
  107. }
  108. }
  109. /**
  110. * Retrieves a single Swing UI created window belonging to the specified
  111. * container. Returns null if the container is null or no such window exists.
  112. *
  113. * @param window The container whose windows should be searched
  114. * @return A relevant window or null
  115. */
  116. public Window getSwingWindow(final FrameContainer<?> window) {
  117. if (window == null) {
  118. return null;
  119. }
  120. for (Window child : window.getWindows()) {
  121. if (child.getController() == controller) {
  122. return child;
  123. }
  124. }
  125. return null;
  126. }
  127. /** {@inheritDoc} */
  128. @Override
  129. public void delWindow(final FrameContainer<?> window) {
  130. delWindow(null, window);
  131. }
  132. /** {@inheritDoc} */
  133. @Override
  134. public void addWindow(final FrameContainer<?> parent,
  135. final FrameContainer<?> window, final boolean focus) {
  136. UIUtilities.invokeLater(new Runnable() {
  137. @Override
  138. public void run() {
  139. final Window parentWindow = getSwingWindow(parent);
  140. final Window childWindow = doAddWindow(window, focus);
  141. if (childWindow == null) {
  142. return;
  143. }
  144. for (SwingWindowListener listener : listeners.get(SwingWindowListener.class)) {
  145. listener.windowAdded(parentWindow, childWindow);
  146. }
  147. if (focus) {
  148. childWindow.activateFrame();
  149. }
  150. }
  151. });
  152. }
  153. /** {@inheritDoc} */
  154. @Override
  155. public void delWindow(final FrameContainer<?> parent, final FrameContainer<?> window) {
  156. UIUtilities.invokeLater(new Runnable() {
  157. @Override
  158. public void run() {
  159. final Window parentWindow = getSwingWindow(parent);
  160. final Window childWindow = getSwingWindow(window);
  161. for (SwingWindowListener listener : listeners.get(SwingWindowListener.class)) {
  162. listener.windowDeleted(parentWindow, childWindow);
  163. }
  164. }
  165. });
  166. }
  167. }