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

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