/*
 * Licensed Materials - Property of Perforce Software, Inc. 
 * © Copyright Perforce Software, Inc. 2014, 2021 
 * © Copyright IBM Corp. 2009, 2014
 * © Copyright ILOG 1996, 2009
 * All Rights Reserved.
 *
 * Note to U.S. Government Users Restricted Rights:
 * The Software and Documentation were developed at private expense and
 * are "Commercial Items" as that term is defined at 48 CFR 2.101,
 * consisting of "Commercial Computer Software" and
 * "Commercial Computer Software Documentation", as such terms are
 * used in 48 CFR 12.212 or 48 CFR 227.7202-1 through 227.7202-4,
 * as applicable.
 */


import ilog.views.dashboard.IlvDashboardContext;
import ilog.views.dashboard.IlvDashboardDiagram;
import ilog.views.diagrammer.IlvDiagrammerException;
import ilog.views.graphlayout.IlvGraphLayout;
import ilog.views.sdm.IlvSDMModel;
import ilog.views.sdm.event.SDMPropertyChangeEvent;
import ilog.views.sdm.event.SDMPropertyChangeListener;
import ilog.views.sdm.model.IlvDefaultSDMNode;
import ilog.views.sdm.renderer.IlvRendererUtil;
import ilog.views.sdm.renderer.graphlayout.IlvGraphLayoutRenderer;

import java.awt.Dimension;
import java.net.URL;
import java.util.Enumeration;

/**
 * A control panel displays a dashboard containing symbols used to modify the parameters of a layout.
 * It is associated to a diagrammer containing the graph and the layout to modify and also to a controller which will
 * perform the real job.
 *
 */
public class GraphLayoutControlPanel extends IlvDashboardDiagram {

    GraphLayoutDiagrammer layoutDiagrammer = null;
    GraphLayoutController controller = null;


    /**
     * 
     * @param dashboardURL
     * @param ld
     * @param c
     */
    public GraphLayoutControlPanel(URL dashboardURL,GraphLayoutDiagrammer ld,GraphLayoutController c){    

        super(new IlvDashboardContext());  
        layoutDiagrammer = ld;
        this.controller = c;

        // ilog.views.graphlayout.internalutil.LogResUtil.logStackTrace = true;
        
        // define a property listener in order to/ detect changes on symbols and to set layout properties 
        // on the associated layout diagrammer
        SDMPropertyChangeListener lstnr=  new SDMPropertyChangeListener(){
            Override
            public void propertyChanged(SDMPropertyChangeEvent event) {
                // get data about the event
                IlvDefaultSDMNode object = (IlvDefaultSDMNode)event.getObject();
                String property = event.getPropertyName();
                Object newValue = event.getNewValue();


                if(property.startsWith("layout.") ){
                    String layoutProperty = property.substring(7);

                    //try to get a layout renderer
                    IlvGraphLayoutRenderer glr = null;
                    glr = (IlvGraphLayoutRenderer)IlvRendererUtil.getRenderer( layoutDiagrammer.getEngine(),IlvRendererUtil.GraphLayout);
                    //if it failed, then try to get a link layout
                    if(glr==null){
                        glr = (IlvGraphLayoutRenderer)IlvRendererUtil.getRenderer( layoutDiagrammer.getEngine(),IlvRendererUtil.LinkLayout);
                    }
                    
                    IlvGraphLayout l = glr.getGraphLayout();
                                                                                
                    //delegate control of the layout to the associated controller
                    if(GraphLayoutControlPanel.this.controller!=null ){                        
                        GraphLayoutControlPanel.this.controller.apply(GraphLayoutControlPanel.this,object,layoutDiagrammer, l,layoutProperty,newValue);
                    }

                    glr.performLayout(layoutDiagrammer.getEngine());
                    layoutDiagrammer.fitToContents();
                }
            }
        };        


        
        // load the dashboard representing the control panel.
        if(dashboardURL!=null){
            setPreferredSize(new Dimension(350,99));
            setBackground(null);
            setOpaque(false);
            getView().setBackground(null);
            getView().setTransparent(true);
            setScrollable(false);
            try {
                readDashboard(dashboardURL);
            } catch (IlvDiagrammerException e) {
                e.printStackTrace();
            }
            
            //add the listener
            getEngine().getModel().addSDMPropertyChangeListener(lstnr);
        }          

        c.init(GraphLayoutControlPanel.this,layoutDiagrammer);
        
    }

    /**
     * Returns the widget for a given id.
     */
    Object getWidget(String id)
    {
        IlvSDMModel model = getEngine().getModel();
        Enumeration e = model.getObjects();
        while (e.hasMoreElements()) {
            Object obj = e.nextElement();
            String[] props = model.getObjectPropertyNames(obj);
            for (int i = 0; i < props.length; i++) {
                if (props[i].contains(id))
                    return obj;
            }
        }
        return null;
    }
}