/*
 * AboutAction.java  $Revision: 1.5 $  $Date: 2007/10/03 04:39:39 $
 *
 * Copyright (C) 2007 Meldcraft Corporation
 *
 * This file is part of Scaffold.
 * 
 * Scaffold is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Scaffold is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with Scaffold.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.meldcraft.application.swing.action;

import java.awt.event.ActionEvent;
import java.util.Map;

import javax.swing.JOptionPane;
import javax.swing.JLabel;

import com.meldcraft.application.Application;
import com.meldcraft.application.ApplicationContext;
import com.meldcraft.application.ApplicationElementContext;
import com.meldcraft.application.swing.SwingApplicationConstants;
import com.meldcraft.application.swing.SwingApplicationContext;
import com.meldcraft.gui.swing.HTMLEditorPane;
import com.meldcraft.util.PropertiesHelper;
import com.meldcraft.util.ReflectUtil;

/**
 * @version $Revision: 1.5 $ $Date: 2007/10/03 04:39:39 $
 * @author Rob Richards
 */
public class AboutAction extends AbstractApplicationAction
{
// Static variables
// Member variables
// Constructors and initialization
    public AboutAction(ApplicationElementContext context)
    {
        super(context);
    }

// Interface implemenations
    public void actionPerformed(ActionEvent e)
    {
        ApplicationContext context = getApplicationElementContext().getApplicationContext();
        Map config = context.getConfiguration();
        Application app = context.getApplication();

        String name = PropertiesHelper.getString(config, SwingApplicationConstants.APP_NAME_KEY, null);
        if (name == null)
        {
            name = ReflectUtil.getSimpleClassName(app.getClass());
        }

        String copyright = PropertiesHelper.getString(config, SwingApplicationConstants.APP_COPYRIGHT_KEY, null);
        String version = PropertiesHelper.getString(config, SwingApplicationConstants.APP_VERSION_KEY, null);
        String build = PropertiesHelper.getString(config, SwingApplicationConstants.APP_BUILD_KEY, null);

        String ctitle = ReflectUtil.getComponentTitle(app);
        String cvendor = ReflectUtil.getComponentVendor(app);
        String cversion = ReflectUtil.getComponentVersion(app);

        if (copyright == null)
        {
            if (cvendor != null)
            {
                copyright = cvendor;
            }
            else
            {
                copyright = "";
            }
        }

        if (version == null)
        {
            if (ctitle != null)
            {
                version = ctitle;
            }
            else
            {
                version = ReflectUtil.getSimpleClassName(app.getClass());
            }

            if (cversion != null)
            {
                version = version + " Version " + cversion;
            }
        }
        else
        {
            version = "Version " + version;
        }

        if (build != null)
        {
            build = build.replaceAll("^build", "");
            build = build.replaceAll("^\\.", "");
            version += " b" + build;
        }

        context.getLogger().info("About: " + name + " : " + version +
                                 " : " + copyright);

        if (context instanceof SwingApplicationContext)
        {
            Object[] msg = null;
            String notes = PropertiesHelper.getString(getApplicationElementContext().getConfiguration(), "notes", null);
            if (notes == null)
            {
                msg = new Object[] {name,
                                    " ",
                                    version,
                                    " ",
                                    copyright};
            }
            else
            {
                final JLabel label = new JLabel();
                HTMLEditorPane pane = new HTMLEditorPane();
                pane.setBackground(label.getBackground());
                pane.setText(notes);
                pane.configureForWidth(300);

                msg = new Object[] {name,
                                    " ",
                                    version,
                                    " ",
                                    copyright,
                                    " ",
                                    pane};
            }

            SwingApplicationContext sc = (SwingApplicationContext)context;
            JOptionPane.showMessageDialog(sc.getUIRoot(), msg,
                                          "About "+name,
                                          JOptionPane.INFORMATION_MESSAGE);
        }
    }

// Public methods
// Protected methods
// Private methods
// Inner classes
}
