/*
 * -------------------------------------------------------------------------
 *      $Id: SurfacePlot.java,v 1.3 2006/03/16 22:34:50 estewart Exp $
 * -------------------------------------------------------------------------
 *      Copyright (c) 1999 Visual Numerics Inc. All Rights Reserved.
 *
 *      This software is confidential information which is proprietary to
 *      and a trade secret of Visual Numerics, Inc.  Use, duplication or
 *      disclosure is subject to the terms of an appropriate license
 *      agreement.
 *
 *      VISUAL NUMERICS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
 *      SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING
 *      BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY,
 *      FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. VISUAL
 *      NUMERICS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 *      AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
 *      ITS DERIVATIVES.
 *--------------------------------------------------------------------------
 */

/*
 * SurfacePlot.java
 *
 * Created on September 15, 2004, 4:56 PM
 */

package com.imsl.demo.fitting;
import com.imsl.chart3d.*;


public class SurfacePlot extends javax.swing.JDialog {

    private com.imsl.chart.JPanelChart jPanelChart;
    private Canvas3DChart canvas;
    private AxisXYZ axis;
    private Data data;
    private Surface surface;
    private javax.swing.JTextField displayField;
    private javax.swing.JCheckBox selectBox;

    public SurfacePlot(java.awt.Frame parent, javax.swing.JCheckBox selectBox) {
        super(parent, false);
        initComponents();
        this.selectBox = selectBox;
        java.awt.Dimension parentSize = parent.getSize();
        java.awt.Point parentLoc = parent.getLocationOnScreen();
        parentLoc.x += parentSize.width/1.5;
        parentLoc.y += parentSize.height/2.5;
        setLocation(parentLoc.x, parentLoc.y);

        jPanelChart = new com.imsl.chart.JPanelChart();
        int h = (int)(parentSize.height/2);
        int w = (int)(h/0.8);
        setChart(w,h);
        pack();
    }

    private void setChart(int w, int h) {
        java.awt.Container cp = getContentPane();

        Chart3D chart = new Chart3D();
        axis = new AxisXYZ(chart);
        axis.setAxisTitlePosition(axis.AXIS_TITLE_PARALLEL);
        axis.setDataType(Data.DATA_TYPE_MARKER);
        axis.setMarkerType(Data.MARKER_TYPE_SIMPLE_CUBE);
        axis.getAxisX().getAxisTitle().setTitle("X Location");
        axis.getAxisY().getAxisTitle().setTitle("Y Location");
        axis.getAxisZ().getAxisTitle().setTitle("Elevation, m");

        canvas = new Canvas3DChart(chart);
        canvas.setSize(w, h);
        cp.add(canvas);

        // Create the display panel
        javax.swing.JPanel displayPanel = new javax.swing.JPanel();
        displayField = new javax.swing.JTextField("");
        displayField.setBorder(null);
        displayField.setEditable(false);
        displayPanel.add(displayField);
        cp.add(displayPanel, java.awt.BorderLayout.SOUTH);
        canvas.render();
    }

    void draw(double[] x, double[] y, double[] z) {
        displayField.setText("Display Field");
        if (data != null) data.remove();
        if (surface != null) surface.remove();

        data = new Data(axis, x, y, z);
        surface = new Surface(axis, x, y, z);
        surface.setSurfaceType(Surface.SURFACE_TYPE_NICEST);

        canvas.render();
    }

    void clearData() {
        if (data != null) data.remove();
        if (surface != null) surface.remove();
        displayField.setText("");
        setTitle("3D View");
        repaint();
    }

    private void initComponents() {
        setTitle("3D View");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });
        pack();
    }

    private void closeDialog(java.awt.event.WindowEvent evt) {
        selectBox.setSelected(false);
        setVisible(false);
        dispose();
    }
}