/*
 * 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 java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

import javax.swing.JComponent;

import ilog.views.IlvManagerView;
import ilog.views.IlvPoint;
import ilog.views.maps.IlvAttributeInfoProperty;
import ilog.views.maps.IlvFeatureAttributeProperty;
import ilog.views.maps.IlvMapUtil;
import ilog.views.maps.attribute.IlvStringAttribute;
import ilog.views.maps.datasource.IlvGraphicLayerDataSource;
import ilog.views.maps.datasource.IlvMapDataSource;
import ilog.views.maps.graphic.IlvMapMarker;
import ilog.views.maps.graphic.style.IlvPointStyle;
import ilog.views.maps.label.IlvMapLabeler;
import ilog.views.maps.label.IlvMapLabelerProperty;
import plugins.ImportAction;

/**
 * Simulates reading specialized data.
 */
public class MyLoadAction extends ImportAction {
  /**
   * @param view
   */
  public MyLoadAction(IlvManagerView view) {
    super(view);
  }

  /**
   * @see plugins.ImportAction#getSubDirectory()
   */
  Override
  protected String getSubDirectory() {
    return "myDefaultDirectory";//$NON-NLS-1$
  }

  /**
   * @see plugins.ImportAction#getDataSources(java.lang.String[])
   */
  Override
  public Enumeration<IlvMapDataSource> getDataSources(String[] fileNames) {
    List<IlvMapDataSource> list = new ArrayList<IlvMapDataSource>();
    // create data sources for the selected files
    // in this example, create a fake data source with simple texts.
    for (int i = 0; i < fileNames.length; i++) {
      final IlvGraphicLayerDataSource ds = new IlvGraphicLayerDataSource();
      // name the datasource and its main layer.
      ds.setName(getFormatName() + " (" + new File(fileNames[i]).getName() + ")"); //$NON-NLS-1$//$NON-NLS-2$
      // JV-3333
      ds.setManager(getView().getManager());
      ds.getInsertionLayer().setName(ds.getName());
      // create a marker in the view center
      IlvPoint point = new IlvPoint(getView().getWidth() / 2.0, getView().getHeight() / 2.0);
      getView().getTransformer().inverse(point);
      IlvMapMarker t = new IlvMapMarker(point);
      // set its attributes (used for labelling)
      IlvAttributeInfoProperty info = new IlvAttributeInfoProperty(new String[] { "Name" }, //$NON-NLS-1$
          new Class[] { IlvStringAttribute.class }, new boolean[] { true });
      IlvStringAttribute values[] = { new IlvStringAttribute(fileNames[i]) };
      t.setNamedProperty(new IlvFeatureAttributeProperty(info, values));
      // set the style for the marker.
      IlvPointStyle style = new IlvPointStyle();
      // chose a random dark color.
      style.setForeground(IlvMapUtil.RandomDarkColor());
      // labelize with the text.
      style.setAttributeInfo(info);
      style.setLabelAttribute(info.getAttributeName(0));
      t.setStyle(style);
      // make sure the layer is labelled:
      IlvMapLabeler labeler = IlvMapLabelerProperty.GetMapLabeler(getView().getManager());
      labeler.addLayer(ds.getInsertionLayer());
      ds.getInsertionLayer().setStyle(style);
      ds.add(t);
      list.add(ds);
    }
    return Collections.enumeration(list);
  }

  /**
   * @see plugins.ImportAction#getDataSources(java.net.URL[])
   */
  Override
  public Enumeration<IlvMapDataSource> getDataSources(URL[] urls) {
    // create data sources for the selected URLS
    return null;
  }

  /**
   * @see plugins.ImportAction#getFormatName()
   */
  Override
  public String getFormatName() {
    return "My format";//$NON-NLS-1$
  }

  /**
   * @see plugins.ImportAction#getAccessory()
   */
  Override
  public JComponent getAccessory() {
    // if specific parameters are necessary.
    return null;
  }

  /**
   * @see plugins.ImportAction#getExtensions()
   */
  Override
  public String[] getExtensions() {
    return new String[] { ".txt" };//$NON-NLS-1$
  }

}