/*
 * ProgressReflectApplicationAction.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.logging.Level;

import javax.swing.JDialog;

import com.meldcraft.application.ApplicationElementContext;
import com.meldcraft.application.swing.SwingApplicationContext;
import com.meldcraft.gui.swing.SwingUtil;
import com.meldcraft.gui.swing.Progress;
import com.meldcraft.util.BasicThreadPool;
import com.meldcraft.util.PropertiesHelper;
import com.meldcraft.util.StringUtil;

/**
 * @version $Revision: 1.5 $ $Date: 2007/10/03 04:39:39 $
 * @author Rob Richards
 */
public class ProgressReflectApplicationAction extends ReflectApplicationAction
    implements Runnable
{
// Static variables
    public static final String  KEY_PROGRESS_DIALOG_TITLE = "Progress.title";

// Member variables
    private BasicThreadPool mPool = new BasicThreadPool(1, "ProgressReflectApplicationAction-Thread");

// Constructors and initialization
    public ProgressReflectApplicationAction(ApplicationElementContext context)
    {
        super(context);
    }

// Interface implemenations
    // Runnable interface
    /**
     * Displays a busy progress dialog, runs the method using reflection,
     * closes the dialog and unsets the busy cursor.
     */
    public void run()
    {
        SwingApplicationContext ctx =
            (SwingApplicationContext)getApplicationElementContext().getApplicationContext();

        final JDialog dialog = Progress.createBusyDialog(ctx.getUIRoot());
        String title = PropertiesHelper.getString(getApplicationElementContext().getConfiguration(), KEY_PROGRESS_DIALOG_TITLE);
        if (!StringUtil.isEmpty(title))
        {
            dialog.setTitle(title);
        }

        try
        {
            SwingUtil.waitForInvokeLater(new Runnable() {
                public void run()
                {
                    dialog.show();
                }
            });

            super.actionPerformed(null);
        }
        catch (Throwable e)
        {
            ctx.getLogger().log(Level.WARNING, "Error running comand.", e);
        }
        finally
        {
            SwingUtil.setBusyCursor(ctx.getUIRoot(), false);
            SwingUtil.waitForInvokeLater(new Runnable() {
                public void run()
                {
                    dialog.hide();
                    dialog.dispose();
                }
            });
        }
    }

    // Action interface
    /**
     * Sets the busy cursor, and then asynchronously executes the run()
     * method in a separate thread.
     */
    public void actionPerformed(ActionEvent event)
    {
        SwingApplicationContext ctx =
            (SwingApplicationContext)getApplicationElementContext().getApplicationContext();
        try
        {
            SwingUtil.setBusyCursor(ctx.getUIRoot());
            mPool.addTask(this);
        }
        catch (Throwable e)
        {
            SwingUtil.setBusyCursor(ctx.getUIRoot(), false);
            ctx.getLogger().log(Level.WARNING, "Error scheduling progess task.", e);
        }
    }

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