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.

SwingWindowManager.java 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2006-2015 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.addons.ui_swing.events.SwingWindowEvent;
  24. import com.dmdirc.interfaces.EventBus;
  25. import java.awt.Window;
  26. import java.awt.event.WindowEvent;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. import javax.inject.Inject;
  30. import javax.inject.Singleton;
  31. import net.engio.mbassy.listener.Handler;
  32. /**
  33. * Manages all swing windows.
  34. */
  35. @Singleton
  36. public class SwingWindowManager {
  37. /** Top level window list. */
  38. private final List<Window> windows;
  39. @Inject
  40. public SwingWindowManager(final EventBus eventBus) {
  41. windows = new ArrayList<>();
  42. eventBus.subscribe(this);
  43. }
  44. @Handler
  45. public void handleWindowEvent(final SwingWindowEvent event) {
  46. if (event.getEvent().getSource() instanceof Window) {
  47. if (event.getEvent().getID() == WindowEvent.WINDOW_OPENED) {
  48. addTopLevelWindow((Window) event.getEvent().getSource());
  49. } else if (event.getEvent().getID() == WindowEvent.WINDOW_CLOSED) {
  50. delTopLevelWindow((Window) event.getEvent().getSource());
  51. }
  52. }
  53. }
  54. /**
  55. * Adds a top level window to the window list.
  56. *
  57. * @param source New window
  58. */
  59. private void addTopLevelWindow(final Window source) {
  60. synchronized (windows) {
  61. windows.add(source);
  62. }
  63. }
  64. /**
  65. * Deletes a top level window to the window list.
  66. *
  67. * @param source Old window
  68. */
  69. private void delTopLevelWindow(final Window source) {
  70. synchronized (windows) {
  71. windows.remove(source);
  72. }
  73. }
  74. /**
  75. * Returns a list of top level windows.
  76. *
  77. * @return Top level window list
  78. */
  79. public List<Window> getTopLevelWindows() {
  80. synchronized (windows) {
  81. return new ArrayList<>(windows);
  82. }
  83. }
  84. }