/*
 * 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 utils;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.plaf.basic.BasicOptionPaneUI.ButtonAreaLayout;
import javax.swing.tree.TreePath;

import ilog.views.maps.IlvMapUtil;
import ilog.views.maps.beans.IlvJCoordinateSystemEditorPanel;
import ilog.views.maps.srs.coordsys.IlvCoordinateSystem;
import ilog.views.maps.srs.wkt.IlvWKTCoordinateSystemDictionary;

/**
 * This dialog displays all the known coordinate systems (using
 * {@link ilog.views.maps.srs.wkt.IlvWKTCoordinateSystemDictionary}) and gives
 * users the possibility to chose and preview them.
 */
public class CoordinateSystemDictionaryManager extends JDialog {
  JButton okButton = new JButton(UIManager.getString("OptionPane.okButtonText")); //$NON-NLS-1$
  JButton cancelButton = new JButton(UIManager.getString("OptionPane.cancelButtonText")); //$NON-NLS-1$

  /**
   * Default constructor.
   */
  public CoordinateSystemDictionaryManager() {
    super();
    setTitle(IlvMapUtil.getString(CoordinateSystemDictionaryManager.class, "CoordinateSystemDictionaryManager.Title")); //$NON-NLS-1$
    setModal(true);
    makeGUI();
    pack();
  }

  private IlvCoordinateSystem selectedCoordinateSystem;

  /**
   * @return Returns the selected CoordinateSystem.
   */
  public IlvCoordinateSystem getSelectedCoordinateSystem() {
    return selectedCoordinateSystem;
  }

  /**
   * @param selectedCoordinateSystem
   *          The selected CoordinateSystem to set.
   */
  public void setSelectedCoordinateSystem(IlvCoordinateSystem selectedCoordinateSystem) {
    this.selectedCoordinateSystem = selectedCoordinateSystem;
  }

  private void makeGUI() {
    IlvWKTCoordinateSystemDictionary dic = new IlvWKTCoordinateSystemDictionary();
    final IlvJCoordinateSystemEditorPanel panel = new IlvJCoordinateSystemEditorPanel();
    if (selectedCoordinateSystem != null) {
      panel.setCoordinateSystem(selectedCoordinateSystem);
    }
    panel.setEnabled(false);
    JTree tree = new JTree(dic.makeTreeModel(IlvWKTCoordinateSystemDictionary.CATEGORY));
    tree.putClientProperty("JTree.lineStyle", "Angled"); //$NON-NLS-1$ //$NON-NLS-2$
    tree.setRootVisible(false);
    tree.setShowsRootHandles(true);
    tree.setExpandsSelectedPaths(true);
    tree.getSelectionModel().addTreeSelectionListener(new TreeSelectionListener() {
      Override
      public void valueChanged(TreeSelectionEvent e) {
        TreePath path = e.getNewLeadSelectionPath();
        if (path == null) {
          okButton.setEnabled(false);
          return;
        }
        okButton.setEnabled(true);
        Object node = path.getLastPathComponent();
        if (node instanceof IlvWKTCoordinateSystemDictionary.CoordinateSystemNode) {
          setSelectedCoordinateSystem(
              ((IlvWKTCoordinateSystemDictionary.CoordinateSystemNode) node).getCoordinateSystem());
          panel.setCoordinateSystem(getSelectedCoordinateSystem());
          panel.repaint();
        } else {
          okButton.setEnabled(false);
        }
      }
    });
    tree.addMouseListener(new MouseAdapter() {

      Override
      public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {
          okButton.doClick();
        }
      }
    });
    okButton.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent e) {
        dispose();
      }
    });
    cancelButton.addActionListener(new ActionListener() {
      Override
      public void actionPerformed(ActionEvent e) {
        setSelectedCoordinateSystem(null);
        dispose();
      }
    });
    JPanel buttons = new JPanel();
    buttons.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
    buttons.setLayout(new ButtonAreaLayout(true, 6));
    buttons.add(okButton);
    buttons.add(cancelButton);
    configureButton(okButton);
    configureButton(cancelButton);
    okButton.setEnabled(false);
    getContentPane().add(new JScrollPane(tree));
    getContentPane().add(panel, BorderLayout.LINE_END);
    getContentPane().add(buttons, BorderLayout.SOUTH);
    SwingUtilities.getRootPane(this).setDefaultButton(okButton);
  }

  private void configureButton(JButton aButton) {
    aButton.setMultiClickThreshhold(UIManager.getInt("OptionPane.buttonClickThreshhold")); //$NON-NLS-1$
    Font buttonFont = UIManager.getFont("OptionPane.buttonFont"); //$NON-NLS-1$
    if (buttonFont != null) {
      aButton.setFont(buttonFont);
    }
    aButton.setMargin(new Insets(2, 4, 2, 4));
  }
}