/*
 * Copyright (c) 2006-2014 DMDirc Developers
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.dmdirc.addons.ui_swing.injection;

import com.dmdirc.addons.ui_swing.dialogs.StandardDialog;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.Map;

import static com.dmdirc.addons.ui_swing.SwingPreconditions.checkOnEDT;

/**
 * Provider for {@link StandardDialog} based windows that correspond to some key.
 *
 * <p>This provider will cache instances that are created until the windows are closed. Once a
 * window has been closed, the next call to {@link #get()} or {@link #displayOrRequestFocus()} will
 * result in a new instance being created.
 *
 * <p>Dialogs with different keys may be open simultaneously, and are treated independently.
 *
 * @param <K> The type of key that dialogs are associated with.
 * @param <T> The type of dialog that will be managed.
 */
public abstract class KeyedDialogProvider<K, T extends StandardDialog> {

    /** The existing instances being displayed, if any. */
    private final Map<K, T> instances = new HashMap<>();

    /**
     * Gets an instance of the dialog provided by this class.
     *
     * <p>If there is an existing instance with the same key that hasn't been closed, it will be
     * returned. Otherwise a new instance will be created and returned. New instances will not be
     * automatically be * displayed to the user - use
     * {@link #displayOrRequestFocus(java.lang.Object)} to do so.
     *
     * <p>This method <em>must</em> be called on the Event Despatch Thread.
     *
     * @param key The key associated with the dialog to get.
     *
     * @return An instance of the dialog.
     */
    public T get(final K key) {
        checkOnEDT();
        if (!instances.containsKey(key)) {
            final T instance = getInstance(key);
            instance.addWindowListener(new Listener(key));
            instances.put(key, instance);
        }
        return instances.get(key);
    }

    /**
     * Disposes of the dialog associated with the given key, if it exists.
     *
     * <p>This method <em>must</em> be called on the Event Despatch Thread.
     *
     * @param key The key associated with the dialog to close.
     */
    public void dispose(final K key) {
        checkOnEDT();
        if (instances.containsKey(key)) {
            instances.get(key).dispose();
        }
    }

    /**
     * Ensures the dialog is visible to the user.
     *
     * <p>If no dialog currently exists with the same key, a new one will be created and displayed
     * to the user. If a dialog existed prior to this method being invoked, it will request focus to
     * bring it to the user's attention.
     *
     * <p>This method <em>must</em> be called on the Event Despatch Thread.
     *
     * @param key The key associated with the dialog to display.
     */
    public void displayOrRequestFocus(final K key) {
        checkOnEDT();
        get(key).displayOrRequestFocus();
    }

    /**
     * Returns a new instance of the dialog with the specified key.
     *
     * @param key The key to create a new dialog for.
     *
     * @return A new instance of this provider's dialog.
     */
    protected abstract T getInstance(final K key);

    /**
     * Listens to window closing events to remove the cached instance of the dialog.
     */
    private class Listener extends WindowAdapter {

        private final K key;

        public Listener(final K key) {
            this.key = key;
        }

        @Override
        public void windowClosing(final WindowEvent e) {
            super.windowClosing(e);
            instances.remove(key);
        }

    }

}

@@ -1509,23 +1509,24 @@
  key.%0A *
+ %3Cp%3E
 %0A * 
-%3Cp%3E
 This pro
@@ -1609,18 +1609,18 @@
 ce a
-%0A *
  window
+%0A *
  has
@@ -1709,18 +1709,18 @@
 will
-%0A *
  result
+%0A *
  in 
@@ -1751,23 +1751,24 @@
 ated.%0A *
+ %3Cp%3E
 %0A * 
-%3Cp%3E
 Dialogs 
@@ -2235,32 +2235,36 @@
 lass.%0A     *
+ %3Cp%3E
 %0A     * 
 %3Cp%3EIf there 
@@ -2251,19 +2251,16 @@
 %0A     * 
-%3Cp%3E
 If there
@@ -2459,18 +2459,16 @@
 cally be
- *
  display
@@ -2487,23 +2487,16 @@
 er - use
-%0A     *
  %7B@link 
@@ -2536,16 +2536,23 @@
 Object)%7D
+%0A     *
  to do s
@@ -2556,32 +2556,36 @@
 o so.%0A     *
+ %3Cp%3E
 %0A     * 
 %3Cp%3EThis meth
@@ -2564,35 +2564,32 @@
    * %3Cp%3E%0A     * 
-%3Cp%3E
 This method %3Cem%3E
@@ -3147,32 +3147,36 @@
 ists.%0A     *
+ %3Cp%3E
 %0A     * 
 %3Cp%3EThis meth
@@ -3155,35 +3155,32 @@
    * %3Cp%3E%0A     * 
-%3Cp%3E
 This method %3Cem%3E
@@ -3533,24 +3533,28 @@
 .%0A     *
+ %3Cp%3E
 %0A     * 
 %3Cp%3EIf no
@@ -3545,19 +3545,16 @@
 %0A     * 
-%3Cp%3E
 If no di
@@ -3633,26 +3633,26 @@
 isplayed
+ to
 %0A     *
- to
  the use
@@ -3782,24 +3782,28 @@
 .%0A     *
+ %3Cp%3E
 %0A     * 
 %3Cp%3EThis 
@@ -3794,19 +3794,16 @@
 %0A     * 
-%3Cp%3E
 This met
@@ -4362,19 +4362,18 @@
 dow clos
-ing
+ed
  events 
@@ -4515,23 +4515,16 @@
         
-public 
 Listener
@@ -4626,19 +4626,18 @@
 ndowClos
-ing
+ed
 (final W
@@ -4680,19 +4680,18 @@
 ndowClos
-ing
+ed
 (e);%0A   

Use the right windowClosed method...