/*
 * 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.
 */
package demo;

import java.util.ArrayList;
import java.util.List;

import ilog.views.diagrammer.IlvDiagrammer;
import ilog.views.diagrammer.faces.dhtml.component.IlvFacesDiagrammerPropertyAccessor;
import ilog.views.sdm.IlvSDMModel;

/**
 * Server side bean used to provide the list of properties and values for the
 * selected item.
 */
public class SelectionProviderBean {

  /**
   * Computes and returns the list of properties for the node (select interactor
   * use).
   * 
   * @param diagrammer
   *          Diagrammer instance
   * @param node
   *          node to retrieve properties for.
   * @return the list of properties for the node.
   */
  public List<List<Object>> getAdditionnalProperties(IlvDiagrammer diagrammer, Object node) {
    if (diagrammer.getView().getManager().getSelectedObjectsCount() == 1) {
      List<List<Object>> props = new ArrayList<List<Object>>();
      IlvSDMModel model = diagrammer.getEngine().getModel();
      String names[] = model.getObjectPropertyNames(node);
      for (int i = 0; i < names.length; i++) {
        List<Object> l = new ArrayList<Object>();
        l.add(names[i]);
        l.add(model.getObjectProperty(node, names[i]));
        props.add(l);
      }
      return props;
    }
    // no selected object or more that one
    return null;
  }

  private IlvFacesDiagrammerPropertyAccessor accessor = new IlvFacesDiagrammerPropertyAccessor();

  /**
   * Returns a {@link IlvFacesDiagrammerPropertyAccessor} to access the
   * properties objects.
   * 
   * @return the default accessor to retrieve properties.
   */
  public IlvFacesDiagrammerPropertyAccessor getPropertyAccessor() {
    return accessor;
  }

}