Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

WebWindowManager.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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_web;
  23. import com.dmdirc.FrameContainer;
  24. import com.dmdirc.addons.ui_web.uicomponents.WebChannelWindow;
  25. import com.dmdirc.addons.ui_web.uicomponents.WebInputWindow;
  26. import com.dmdirc.addons.ui_web.uicomponents.WebQueryWindow;
  27. import com.dmdirc.addons.ui_web.uicomponents.WebServerWindow;
  28. import com.dmdirc.addons.ui_web.uicomponents.WebWindow;
  29. import com.dmdirc.logger.ErrorLevel;
  30. import com.dmdirc.logger.Logger;
  31. import com.dmdirc.ui.WindowManager;
  32. import com.dmdirc.ui.interfaces.ChannelWindow;
  33. import com.dmdirc.ui.interfaces.FrameListener;
  34. import com.dmdirc.ui.interfaces.InputWindow;
  35. import com.dmdirc.ui.interfaces.QueryWindow;
  36. import com.dmdirc.ui.interfaces.ServerWindow;
  37. import com.dmdirc.ui.interfaces.Window;
  38. import java.util.HashMap;
  39. import java.util.Map;
  40. /**
  41. * Manages WebUI windows.
  42. */
  43. public class WebWindowManager implements FrameListener {
  44. /** A map of known implementations of window interfaces. */
  45. private static final Map<Class<? extends Window>, Class<? extends Window>> IMPLEMENTATIONS
  46. = new HashMap<Class<? extends Window>, Class<? extends Window>>();
  47. static {
  48. IMPLEMENTATIONS.put(Window.class, WebWindow.class);
  49. IMPLEMENTATIONS.put(InputWindow.class, WebInputWindow.class);
  50. IMPLEMENTATIONS.put(ServerWindow.class, WebServerWindow.class);
  51. IMPLEMENTATIONS.put(QueryWindow.class, WebQueryWindow.class);
  52. IMPLEMENTATIONS.put(ChannelWindow.class, WebChannelWindow.class);
  53. }
  54. /** The controller that owns this manager. */
  55. private final WebInterfaceUI controller;
  56. /** Map of known windows. */
  57. private final Map<FrameContainer<?>, Window> windows
  58. = new HashMap<FrameContainer<?>, Window>();
  59. /**
  60. * Creates a new window manager for the specified controller.
  61. *
  62. * @param controller The Web UI controller that owns this manager
  63. */
  64. public WebWindowManager(final WebInterfaceUI controller) {
  65. this.controller = controller;
  66. WindowManager.addFrameListener(this);
  67. for (FrameContainer<?> container : WindowManager.getRootWindows()) {
  68. recursiveAdd(container);
  69. }
  70. }
  71. /**
  72. * Retrieves the web window corresponding to the specified container,
  73. * if any.
  74. *
  75. * @param container The container whose window should be retrieved
  76. * @return The corresponding web window, or null if there is none
  77. */
  78. public Window getWindow(final FrameContainer<?> container) {
  79. return windows.get(container);
  80. }
  81. /**
  82. * Recursively adds a window for the specified container and all of its
  83. * children.
  84. *
  85. * @param container The container to create windows for
  86. */
  87. private void recursiveAdd(final FrameContainer<?> container) {
  88. addWindow(container, false);
  89. for (FrameContainer<?> child : container.getChildren()) {
  90. recursiveAdd(child);
  91. }
  92. }
  93. /** {@inheritDoc} */
  94. @Override
  95. public void addWindow(final FrameContainer<?> window, final boolean focus) {
  96. doAddWindow(window, focus);
  97. }
  98. /** {@inheritDoc} */
  99. @Override
  100. public void delWindow(final FrameContainer<?> window) {
  101. if (windows.containsKey(window)) {
  102. windows.get(window).close();
  103. windows.remove(window);
  104. }
  105. }
  106. /** {@inheritDoc} */
  107. @Override
  108. public void addWindow(final FrameContainer<?> parent, final FrameContainer<?> window,
  109. final boolean focus) {
  110. if (windows.containsKey(parent)) {
  111. addWindow(window, focus);
  112. }
  113. }
  114. /** {@inheritDoc} */
  115. @Override
  116. public void delWindow(final FrameContainer<?> parent, final FrameContainer<?> window) {
  117. if (windows.containsKey(parent)) {
  118. delWindow(window);
  119. }
  120. }
  121. /**
  122. * Creates a new window for the specified container.
  123. *
  124. * @param <T> The type of window that should be created
  125. * @param window The container that owns the window
  126. * @param focus Whether the window should be focused initially
  127. */
  128. @SuppressWarnings("unchecked")
  129. protected <T extends Window> void doAddWindow(final FrameContainer<T> window,
  130. final boolean focus) {
  131. final Class<T> clazz;
  132. if (IMPLEMENTATIONS.containsKey(window.getWindowClass())) {
  133. clazz = (Class<T>) IMPLEMENTATIONS.get(window.getWindowClass());
  134. } else {
  135. clazz = window.getWindowClass();
  136. }
  137. try {
  138. final T frame = (T) clazz.getConstructors()[0].newInstance(controller, window);
  139. window.addWindow(frame);
  140. windows.put(window, frame);
  141. } catch (Exception ex) {
  142. Logger.appError(ErrorLevel.MEDIUM, "Unable to create window of type "
  143. + clazz.getCanonicalName() + " for web ui", ex);
  144. }
  145. }
  146. }