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.

KeyedDialogProvider.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2006-2014 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.injection;
  23. import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;
  24. import java.awt.event.WindowAdapter;
  25. import java.awt.event.WindowEvent;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. import static com.dmdirc.addons.ui_swing.SwingPreconditions.checkOnEDT;
  29. /**
  30. * Provider for {@link StandardDialog} based windows that correspond to some key.
  31. * <p>
  32. * This provider will cache instances that are created until the windows are closed. Once a window
  33. * has been closed, the next call to {@link #get()} or {@link #displayOrRequestFocus()} will result
  34. * in a new instance being created.
  35. * <p>
  36. * Dialogs with different keys may be open simultaneously, and are treated independently.
  37. *
  38. * @param <K> The type of key that dialogs are associated with.
  39. * @param <T> The type of dialog that will be managed.
  40. */
  41. public abstract class KeyedDialogProvider<K, T extends StandardDialog> {
  42. /** The existing instances being displayed, if any. */
  43. private final Map<K, T> instances = new HashMap<>();
  44. /**
  45. * Gets an instance of the dialog provided by this class.
  46. * <p>
  47. * If there is an existing instance with the same key that hasn't been closed, it will be
  48. * returned. Otherwise a new instance will be created and returned. New instances will not be
  49. * automatically be displayed to the user - use {@link #displayOrRequestFocus(java.lang.Object)}
  50. * to do so.
  51. * <p>
  52. * This method <em>must</em> be called on the Event Despatch Thread.
  53. *
  54. * @param key The key associated with the dialog to get.
  55. *
  56. * @return An instance of the dialog.
  57. */
  58. public T get(final K key) {
  59. checkOnEDT();
  60. if (!instances.containsKey(key)) {
  61. final T instance = getInstance(key);
  62. instance.addWindowListener(new Listener(key));
  63. instances.put(key, instance);
  64. }
  65. return instances.get(key);
  66. }
  67. /**
  68. * Disposes of the dialog associated with the given key, if it exists.
  69. * <p>
  70. * This method <em>must</em> be called on the Event Despatch Thread.
  71. *
  72. * @param key The key associated with the dialog to close.
  73. */
  74. public void dispose(final K key) {
  75. checkOnEDT();
  76. if (instances.containsKey(key)) {
  77. instances.get(key).dispose();
  78. }
  79. }
  80. /**
  81. * Ensures the dialog is visible to the user.
  82. * <p>
  83. * If no dialog currently exists with the same key, a new one will be created and displayed to
  84. * the user. If a dialog existed prior to this method being invoked, it will request focus to
  85. * bring it to the user's attention.
  86. * <p>
  87. * This method <em>must</em> be called on the Event Despatch Thread.
  88. *
  89. * @param key The key associated with the dialog to display.
  90. */
  91. public void displayOrRequestFocus(final K key) {
  92. checkOnEDT();
  93. get(key).displayOrRequestFocus();
  94. }
  95. /**
  96. * Returns a new instance of the dialog with the specified key.
  97. *
  98. * @param key The key to create a new dialog for.
  99. *
  100. * @return A new instance of this provider's dialog.
  101. */
  102. protected abstract T getInstance(final K key);
  103. /**
  104. * Listens to window closed events to remove the cached instance of the dialog.
  105. */
  106. private class Listener extends WindowAdapter {
  107. private final K key;
  108. Listener(final K key) {
  109. this.key = key;
  110. }
  111. @Override
  112. public void windowClosed(final WindowEvent e) {
  113. super.windowClosed(e);
  114. instances.remove(key);
  115. }
  116. }
  117. }