package org.mbari.seepsearch.ui;

import java.awt.*;
import java.io.*;
import org.mbari.awt.layout.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import java.util.Arrays;
import java.util.TreeSet;
import org.mbari.swing.FileOpenPanel1;
import org.mbari.swing.FileSavePanel1;

/**
 * <p><!--Insert summary here--></p>
 *
 * <p>The Seep Search project is a sub-project of the Continental
 * Margin Fluids and Gases project (200010). It involves the analysis of
 * archived ROV data to obtain understanding of seep distritution in Monterey
 * Bay.</p>
 *
 * <p>The Purpose of this Java package is to intercompare ROV Navigation files,
 * VIMS data on seep community observations and the EM-300 bathymetry. We are
 * looking for locations where the ROV navigation files and the EM-300 data
 * have similar depths between the 2 data sets, which implies that the ROV is
 * observing the sea floor.</p><hr><br>
 *
 * <p>MBARI provides this documentation and code &quot;as is&quot;, with no
 * warranty, express or implied, of its quality or consistency. It is provided
 * without support and without obligation on the part of the Monterey Bay
 * Aquarium Research Institute to assist in its use, correction, modification,
 * or enhancement. This information should not be published or distributed to
 * third parties without specific written permission from MBARI.</p><br>
 *
 * Copyright 2002 MBARI.<br>
 * MBARI Proprietary Information. All rights reserved.<br><hr><br>
 *
 * $Log: FileManagerPanel1.java,v $
 * Revision 1.1  2002/02/06 00:39:54  brian
 * Started developing a panel to connect to VARS
 *
 * Revision 1.1  2002/01/30 00:23:56  brian
 * Added comments to all classes and methods. Tested the accuracy of AsciiRasterGridReader and AsciiRasterGridWriter. Also checked the the GridUtil was populating the spatial grids correctly and accurately. The data files generated matched up perfectly with the orginal text files. Also implemented an RovAltitudeFilter class to encapsulate different filtering methods.
 *<br><hr><br>
 *
 * @author  : $Author: brian $
 * @version : $Revision: 1.1 $
 */
public class FileManagerPanel1 extends JPanel {
    final VerticalFlowLayout verticalFlowLayout1 = new VerticalFlowLayout();
    final JPanel panelNav = new JPanel();
    final JList selectedList = new JList();
    final JButton btnAdd = new JButton();
    Border border1;
    TitledBorder titledBorder1;
    Border border2;
    TitledBorder titledBorder2;
    Border border3;
    TitledBorder titledBorder3;
    final JFileChooser jFileChooser = new JFileChooser();
    final JPanel editPanel = new JPanel();
    final VerticalFlowLayout verticalFlowLayout2 = new VerticalFlowLayout();
    final JButton btnRemove = new JButton();
    protected final FileOpenPanel1 fileOpenPanel = new FileOpenPanel1();
    protected final FileSavePanel1 fileSavePanel = new FileSavePanel1();
    Border border4;
    TitledBorder titledBorder4;
    DefaultListModel model = new DefaultListModel();
    final JScrollPane jScrollPane1 = new JScrollPane();

    public FileManagerPanel1() {
        try {
            jbInit();
            initComponents();
        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
    }
    void jbInit() throws Exception {
        border1 = BorderFactory.createEtchedBorder(Color.white,new Color(142, 142, 142));
        titledBorder1 = new TitledBorder(border1,"Select Bathymetry File");
        border2 = new EtchedBorder(EtchedBorder.RAISED,Color.white,new Color(142, 142, 142));
        titledBorder2 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(142, 142, 142)),"Select ROV Navigation Files");
        border3 = BorderFactory.createEtchedBorder(Color.white,new Color(142, 142, 142));
        titledBorder3 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(142, 142, 142)),"Specify Output Grid File");
        border4 = BorderFactory.createEtchedBorder(Color.white,new Color(142, 142, 142));
        titledBorder4 = new TitledBorder(BorderFactory.createEtchedBorder(Color.white,new Color(142, 142, 142)),"Select Bathymetry Grid File");
        this.setLayout(verticalFlowLayout1);
        selectedList.setPreferredSize(new Dimension(220, 150));
        btnAdd.setPreferredSize(new Dimension(69, 25));
        btnAdd.setText("<< Add");
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnAdd_actionPerformed(e);
            }
        });
        panelNav.setBorder(titledBorder2);
        fileSavePanel.setBorder(titledBorder3);
        fileSavePanel.setChooser(jFileChooser);
        editPanel.setLayout(verticalFlowLayout2);
        editPanel.setMinimumSize(new Dimension(60, 60));
        btnRemove.setToolTipText("");
        btnRemove.setText("Remove >>");
        btnRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(ActionEvent e) {
                btnRemove_actionPerformed(e);
            }
        });
        fileOpenPanel.setBorder(titledBorder4);
        fileOpenPanel.setChooser(jFileChooser);
        jScrollPane1.setPreferredSize(new Dimension(220, 150));
        jScrollPane1.setToolTipText("");
        this.add(fileOpenPanel, null);
        this.add(panelNav, null);
        panelNav.add(jScrollPane1, null);
        jScrollPane1.getViewport().add(selectedList, null);
        panelNav.add(editPanel, null);
        editPanel.add(btnAdd, null);
        editPanel.add(btnRemove, null);
        this.add(fileSavePanel, null);

    }

    void initComponents() {
        selectedList.setModel(model);
    }

    void btnBrowse1_mouseClicked(MouseEvent e) {
        int state = jFileChooser.showOpenDialog(this);

    }

    void btnAdd_actionPerformed(ActionEvent e) {
        JFrame f = new FrameSelectNav1(model);
        f.pack();
        f.setVisible(true);
    }

    void btnRemove_actionPerformed(ActionEvent e) {
        int[] removeThese = selectedList.getSelectedIndices();

        // Must remove the items with hihest indices first. Otherwise the Vector
        // storing the data will resize on the fly and remove the wrong items.
        Arrays.sort(removeThese);
        for (int i = removeThese.length; i > 0; i--) {
            model.removeElementAt(removeThese[i - 1]);
        }
    }

    public File getInputFile() {
        return fileOpenPanel.getFile();
    }

    public File getOutputFile() {
        return fileSavePanel.getFile();
    }

    public File[] getSelectedFiles() {

        TreeSet set = new TreeSet();
        for (int i = 0; i < model.size(); i++) {
            set.add((File) model.getElementAt(i));
        }

        File[] files = new File[set.size()];
        set.toArray(files);
        return files;

    }

}