/*
 * SSOptionPane.java  $Revision: 1.5 $  $Date: 2007/10/03 04:39:40 $
 *
 * 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.guis;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import javax.swing.Icon;
import javax.swing.JOptionPane;

import com.meldcraft.application.ApplicationElementContext;
import com.meldcraft.application.swing.SwingApplicationContext;
import com.meldcraft.application.util.BeanContext;
import com.meldcraft.application.util.BeanUtil;
import com.meldcraft.util.PropertiesHelper;
import com.meldcraft.util.ReflectUtil;

/**
 * @version $Revision: 1.5 $ $Date: 2007/10/03 04:39:40 $
 * @author Rob Richards
 */
public class SSOptionPane
{
    private SwingApplicationContext mApplicationContext;
    private Map                     mConfig;

    public void applicationInitialize(ApplicationElementContext context)
    {
        mApplicationContext = (SwingApplicationContext)context.getApplicationContext();
        mConfig = context.getConfiguration();
    }

    public Object show(Object value)
    {
        String style = PropertiesHelper.getString(mConfig, "style", "message");
        if (style.length() > 1)
        {
            style = style.substring(0,1).toUpperCase() + style.substring(1);
        }

        String method = "show" + style + "Dialog";

        Object message = PropertiesHelper.getString(mConfig, "message");
        if (message != null)
        {
            message = BeanUtil.translateValue(new BeanContext(mApplicationContext, value, message));
        }

        String title = PropertiesHelper.getString(mConfig, "title", null);
        String type = PropertiesHelper.getString(mConfig, "type", null);
        String iconstr = PropertiesHelper.getString(mConfig, "icon", null);
        String options = PropertiesHelper.getString(mConfig, "options", null);
        String initialValue = PropertiesHelper.getString(mConfig, "initialValue", null);
        
        Icon icon = null;//TODO
        Object ui = mApplicationContext.getUIRoot();
        Object[] args = new Object[] {ui};
        if (initialValue != null)
        {
            args = new Object[] {ui, message, title, type, icon, options, initialValue}; 
        }
        else if (iconstr != null)
        {
            args = new Object[] {ui, message, title, type, icon}; 
        }
        else if (type != null)
        {
            args = new Object[] {ui, message, title, type}; 
        }
        else if (title != null)
        {
            args = new Object[] {ui, message, title}; 
        }
        else
        {
            args = new Object[] {ui, message}; 
        }

        try
        {
            Object val = ReflectUtil.invokeMethod(JOptionPane.class, method, args);
            if (val != null)
            {
                if (val.equals(JOptionPane.OK_OPTION))
                {
                    val = "OK";
                }
                else if (val.equals(JOptionPane.CANCEL_OPTION))
                {
                    val = "CANCEL";
                }
                else if (val.equals(JOptionPane.YES_OPTION))
                {
                    val = "YES";
                }
                else if (val.equals(JOptionPane.NO_OPTION))
                {
                    val = "NO";
                }
            }
            return val;
        }
        catch (NoSuchMethodException e)
        {
            throw new RuntimeException("Error showing dialog", e);
        }
        catch (IllegalAccessException e)
        {
            throw new RuntimeException("Error showing dialog", e);
        }
        catch (InvocationTargetException e)
        {
            throw new RuntimeException("Error showing dialog", e);
        }
    }
}



