/*
 * SSFieldPane.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.awt.Component;
import java.util.Map;

import com.meldcraft.application.ApplicationContext;
import com.meldcraft.application.ApplicationElementContext;
import com.meldcraft.application.swing.SwingApplicationContext;
import com.meldcraft.application.swing.action.ActionGUIFactory;
import com.meldcraft.gui.swing.JFieldPane;
import com.meldcraft.util.PropertiesHelper;

/**
 * @version $Revision: 1.5 $ $Date: 2007/10/03 04:39:40 $
 * @author Rob Richards
 */
public class SSFieldPane extends JFieldPane
{
    protected   ApplicationContext  mApplicationContext;
    protected   String              mElementId;

    protected   boolean             mExpandHorizontally = true;
    protected   boolean             mExpandVertically = false;
    protected   boolean             mComponentBeneath = false;
    protected   boolean             mRequired = false;

    public void applicationInitialize(ApplicationElementContext context)
    {
        mApplicationContext = context.getApplicationContext();
        mElementId = context.getElementId();
        
        doApplicationInitialize();
    }

    protected void doApplicationInitialize()
    {
        Map config = PropertiesHelper.getSubset(mApplicationContext.getConfiguration(), mElementId + ".");

        mExpandHorizontally = PropertiesHelper.getBoolean(config, "expandHorizontally", mExpandHorizontally); 
        mExpandVertically = PropertiesHelper.getBoolean(config, "expandVertically", mExpandVertically); 
        mComponentBeneath = PropertiesHelper.getBoolean(config, "componentBeneath", mComponentBeneath); 
        mRequired = PropertiesHelper.getBoolean(config, "required", mRequired); 

        String[] fields = PropertiesHelper.getStringArray(config, "fields");
        if (fields != null)
        {
            for (int i=0 ; i < fields.length ; i++)
            {
                String field = fields[i];
                Map fconfig = PropertiesHelper.getSubset(mApplicationContext.getConfiguration(), field + ".");

                Component comp = null;
                String label = PropertiesHelper.getString(fconfig, "fieldLabel", field);
                Object obj = mApplicationContext.getElementManager().getApplicationElement(field);
                if (obj == null)
                {
                    obj = ActionGUIFactory.getInstance((SwingApplicationContext)mApplicationContext).createButton(field);
                }

                if (obj instanceof Component)
                {
                    comp = (Component)obj;
                }

                boolean expandHorizontally = PropertiesHelper.getBoolean(fconfig, "expandHorizontally", mExpandHorizontally); 
                boolean expandVertically = PropertiesHelper.getBoolean(fconfig, "expandVertically", mExpandVertically); 
                boolean componentBeneath = PropertiesHelper.getBoolean(fconfig, "componentBeneath", mComponentBeneath); 
                boolean required = PropertiesHelper.getBoolean(fconfig, "required", mRequired);

                setExpandHorizontally(expandHorizontally);
                setExpandVertically(expandVertically);
                setComponentBeneath(componentBeneath);
                addRow(label, comp, required);
            }
        }
    }
}



