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

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