Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

DialogProvider.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing.injection;
  18. import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
  19. import java.awt.Window;
  20. import java.awt.event.WindowAdapter;
  21. import java.awt.event.WindowEvent;
  22. import javax.inject.Provider;
  23. import static com.dmdirc.addons.ui_swing.SwingPreconditions.checkOnEDT;
  24. /**
  25. * Simple provider for {@link StandardDialog} based windows.
  26. *
  27. * <p>
  28. * This provider will cache instances that are created until the windows are closed. Once a window
  29. * has been closed, the next call to {@link #get()} or {@link #displayOrRequestFocus()} will result
  30. * in a new instance being created.
  31. *
  32. * @param <T> The type of dialog that will be managed.
  33. */
  34. public class DialogProvider<T extends StandardDialog> {
  35. /** Provider used to generate new instances. */
  36. private final Provider<T> provider;
  37. /** The existing instance being displayed, if any. */
  38. private T instance;
  39. /**
  40. * Creates a new instance of {@link DialogProvider}.
  41. *
  42. * @param provider The provider to use to generate new instances of the dialog, when required.
  43. */
  44. public DialogProvider(final Provider<T> provider) {
  45. this.provider = provider;
  46. }
  47. /**
  48. * Gets an instance of the dialog provided by this class.
  49. *
  50. * <p>
  51. * If there is an existing instance that hasn't been closed, it will be returned. Otherwise a
  52. * new instance will be created and returned. New instances will not be automatically be
  53. * displayed to the user - use {@link #displayOrRequestFocus()} to do so.
  54. *
  55. * <p>
  56. * This method <em>must</em> be called on the Event Dispatch Thread.
  57. *
  58. * @return An instance of the dialog.
  59. */
  60. public T get() {
  61. checkOnEDT();
  62. if (instance == null) {
  63. instance = provider.get();
  64. instance.addWindowListener(new Listener());
  65. }
  66. return instance;
  67. }
  68. /**
  69. * Ensures the dialog is visible to the user.
  70. *
  71. * <p>
  72. * If no dialog currently exists, a new one will be created and displayed to the user. If a
  73. * dialog existed prior to this method being invoked, it will request focus to bring it to the
  74. * user's attention.
  75. *
  76. * <p>
  77. * This method <em>must</em> be called on the Event Dispatch Thread.
  78. */
  79. public void displayOrRequestFocus() {
  80. checkOnEDT();
  81. get().displayOrRequestFocus();
  82. }
  83. /**
  84. * Ensures the dialog is visible to the user.
  85. *
  86. * <p>
  87. * The parent will only change if the dialog has not been made visible to the user.
  88. *
  89. * <p>
  90. * If no dialog currently exists, a new one will be created and displayed to the user. If a
  91. * dialog existed prior to this method being invoked, it will request focus to bring it to the
  92. * user's attention.
  93. *
  94. * <p>
  95. * This method <em>must</em> be called on the Event Dispatch Thread.
  96. *
  97. * @param parent Parent window for the dialog
  98. */
  99. public void displayOrRequestFocus(final Window parent) {
  100. checkOnEDT();
  101. get().displayOrRequestFocus(parent);
  102. }
  103. /**
  104. * Listens to window closing events to remove the cached instance of the dialog.
  105. */
  106. private class Listener extends WindowAdapter {
  107. @Override
  108. public void windowClosed(final WindowEvent e) {
  109. super.windowClosed(e);
  110. instance = null;
  111. }
  112. }
  113. }