/*
 * @(#)InfoAction.java  1.0  2009-12-24
 * 
 * Copyright (c) 2009 Werner Randelshofer, Immensee, Switzerland.
 * All rights reserved.
 *
 * You may not use, copy or modify this file, except in compliance with the
 * license agreement you entered into with Werner Randelshofer.
 * For details see accompanying license terms.
 */
package ch.randelshofer.multishow;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.prefs.Preferences;
import javax.swing.JDialog;
import javax.swing.SwingUtilities;
import org.jhotdraw.app.Application;
import org.jhotdraw.app.Disposable;
import org.jhotdraw.app.View;
import org.jhotdraw.app.action.AbstractViewAction;
import org.jhotdraw.util.ResourceBundleUtil;
import org.jhotdraw.util.prefs.PreferencesUtil;

/**
 * InfoAction.
 *
 * @author Werner Randelshofer
 * @version 1.0 2009-12-24 Created.
 */
public class InfoAction extends AbstractViewAction {

    public static String ID = "view.info";
    /**
     * We store the dialog as a client property in the view.
     */
    private final static String DIALOG_CLIENT_PROPERTY = "view.viewInfo.dialog";

    public InfoAction(Application a, View v) {
        super(a, v);
        ResourceBundleUtil labels = ResourceBundleUtil.getBundle("ch.randelshofer.multishow.Labels");
        labels.configureAction(this, ID);
    }

    public void actionPerformed(ActionEvent e) {
        ResourceBundleUtil labels = ResourceBundleUtil.getBundle("ch.randelshofer.multishow.Labels");
        final MultiShowView v = (MultiShowView) getActiveView();
        Object commandObject = v.getCommandObject();
        final InfoDialog dialog;
        if (v.getClientProperty(DIALOG_CLIENT_PROPERTY) == null) {
            dialog = new InfoDialog((Frame) SwingUtilities.getWindowAncestor(v.getComponent()),
                    new File(v.getURI()), commandObject.toString());

            v.putClientProperty(DIALOG_CLIENT_PROPERTY, dialog);
            dialog.setTitle(labels.getFormatted("view.info.titleText", v.getTitle()));
            dialog.setResizable(false);
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setSize(400, 400);
            dialog.setLocationByPlatform(true);
            updateSource(commandObject, dialog);

            final PropertyChangeListener propertyChangeHandler = new PropertyChangeListener() {

                @Override
                public void propertyChange(PropertyChangeEvent evt) {
                    if (evt.getPropertyName() == MultiShowView.COMMAND_OBJECT_PROPERTY) {
                        updateSource(evt.getNewValue(), dialog);
                    } else if (evt.getPropertyName() == MultiShowView.SELECTED_COMMAND_INFO_PROPERTY) {
                        updateSource(v.getCommandObject(), dialog);
                    } else if (evt.getPropertyName() == ch.randelshofer.view.View.REQUEST_INFO_UPDATE_PROPERTY) {
                        updateSource(v.getCommandObject(), dialog);
                    }
                }
            };
            v.addPropertyChangeListener(propertyChangeHandler);

            final Disposable disposable = new Disposable() {

                @Override
                public void dispose() {
                    v.removePropertyChangeListener(propertyChangeHandler);
                    getApplication().removeWindow(dialog);
                    v.putClientProperty(DIALOG_CLIENT_PROPERTY, null);
                    v.removeDisposable(this);
                }
            };

            dialog.addWindowListener(new WindowAdapter() {

                @Override
                public void windowClosed(WindowEvent evt) {
                    disposable.dispose();
                }
            });

            v.addDisposable(disposable);
        } else {
            dialog = (InfoDialog) (JDialog) v.getClientProperty(DIALOG_CLIENT_PROPERTY);
            updateSource(commandObject, dialog);
        }

        Preferences prefs = PreferencesUtil.userNodeForPackage(getClass());
        PreferencesUtil.installFramePrefsHandler(prefs, "viewInfo", dialog);

        getApplication().addWindow(dialog, v);
        dialog.setVisible(true);
    }

    private void updateSource(Object commandObject, InfoDialog dialog) {
        MultiShowView v = (MultiShowView) getActiveView();
        if (v != null) {
            dialog.setInfo(v.getCommandObject().toString());
            dialog.setFile(new File(v.getURI()));
            dialog.pack();
        }
    }
}
