/*
 This file is part of the software framework JStatCom, 
 a library for building applications for data based analysis.

 Copyright (C) 2000-2005  Markus Kraetzig

 This library is free software; you can redistribute it and/or modify
 it under the terms of the GNU Lesser General Public License as
 published by the Free Software Foundation; either version 2.1 of the
 License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 USA
 */

package com.jstatcom.io;

import java.io.File;
import java.net.URL;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.TableCellEditor;

import org.apache.log4j.Logger;

import com.jstatcom.component.JHelpContextMgr;
import com.jstatcom.component.StdMessages;
import com.jstatcom.component.TopFrameReference;
import com.jstatcom.model.JSCDate;
import com.jstatcom.model.JSCNArray;
import com.jstatcom.model.JSCSArray;
import com.jstatcom.model.JSCString;
import com.jstatcom.ts.TS;
import com.jstatcom.ts.TSHolder;
import com.jstatcom.ts.TSList;
import com.jstatcom.ts.TSProject;
import com.jstatcom.ts.TSTableScrollPane;
import com.jstatcom.util.UMatrix;

/**
 * Panel for importing and displaying data files for time series analysis.
 * 
 * @author Markus Kraetzig 
 */
public final class TSImportPanel extends AbstractImportPanel {
    private static final Logger log = Logger.getLogger(TSImportPanel.class);

    // The Container that contains this panel.
    private boolean hideAfterLoad = true;

    private TSProject project = null;

    private JTextArea ivjJTextArea_Description = null;

    private JTextField ivjJTextField_FileName = null;

    private JLabel ivjJLabel_DescriptionLabel = null;

    private JLabel ivjJLabel_FileLabel = null;

    IvjEventHandler ivjEventHandler = new IvjEventHandler();

    private JScrollPane ivjJScrollPane1 = null;

    private JScrollPane ivjJScrollPane2 = null;

    private JButton ivjBrowseButton = null;

    private JButton ivjLoadButton = null;

    private TSList ivjTSList = null;

    private TSTableScrollPane jScrollPane = null;

    private com.jstatcom.ts.TSTable TSTable = null;

    class IvjEventHandler implements java.awt.event.ActionListener {
        public void actionPerformed(java.awt.event.ActionEvent e) {
            if (e.getSource() == TSImportPanel.this.getLoadButton())
                connEtoC4();
            if (e.getSource() == TSImportPanel.this.getBrowseButton())
                connEtoC1();
        };
    };

    /**
     * UIReadData constructor comment.
     */
    public TSImportPanel() {
        super();
        initialize();
    }

    /**
     * Initiates browsing for a file with data that should be imported.
     */
    public boolean importData() {
        File file = FileSupport.getInstance().selectDataFile(
                TopFrameReference.getTopFrameRef(), DataHandlerTypes.EXCEL,
                DataHandlerTypes.MATLAB, DataHandlerTypes.GAUSS_DAT,
                DataHandlerTypes.GAUSS_FMT, DataHandlerTypes.DAT);
        if (file == null)
            return false;

        // get the handler from the filename only
        DataHandlerTypes type = DataHandlerTypes.getTypeFromFileName(file
                .getAbsolutePath());
        return displayDataFile(file, type.getHandler());
    }

    /**
     * Initiates data import from importFile.
     * 
     * @param importFile
     *            the file to read data from
     * @throws IllegalArgumentException
     *             if (importFile == null)
     * @throws RuntimeException
     *             if (!importFile.canRead())   
     */
    public boolean importData(File importFile) {
        if (importFile == null)
            throw new IllegalArgumentException("Argument was null.");
        if (!importFile.canRead())
            throw new RuntimeException("Cannot read from file \""
                    + importFile.getAbsolutePath() + "\".");

        // get the handler from the filename only
        DataHandlerTypes type = DataHandlerTypes.getTypeFromFileName(importFile
                .getAbsolutePath());
        return displayDataFile(importFile, type.getHandler());
    }

    /**
     * connEtoC1: (BrowseButton.action. --> UIReadData.browseForDataFile()V)
     */
    /* WARNING: THIS METHOD WILL BE REGENERATED. */
    private void connEtoC1() {
        try {
            // user code begin {1}
            // user code end
            this.importData();
            // user code begin {2}
            // user code end
        } catch (Throwable ivjExc) {
            // user code begin {3}
            // user code end
            handleException(ivjExc);
        }
    }

    /**
     * connEtoC4: (LoadButton.action. --> UIReadData.loadButton_ActionEvents()V)
     */
    /* WARNING: THIS METHOD WILL BE REGENERATED. */
    private void connEtoC4() {
        try {
            // user code begin {1}
            // user code end
            this.loadButton_ActionEvents();
            // user code begin {2}
            // user code end
        } catch (Throwable ivjExc) {
            // user code begin {3}
            // user code end
            handleException(ivjExc);
        }
    }

    private boolean displayDataFile(File file, DataHandler handler) {
        String fName = file.getName();

        JSCDate startDate;
        JSCNArray data;
        JSCSArray varNames;
        JSCString description;

        try {
            if (!handler.importData(file))
                return false;

            data = (JSCNArray) handler.getData(TSImportTypes.DATA);
            startDate = (JSCDate) handler.getData(TSImportTypes.STARTDATE);
            varNames = (JSCSArray) handler.getData(TSImportTypes.VARNAMES);
            description = (JSCString) handler
                    .getData(TSImportTypes.DESCRIPTION);

            TS[] tsArray = new TS[data.cols()];
            double[][] values = data.doubleArray();

            String pName = fName;
            if (fName.indexOf('.') >= 0)
                pName = fName.substring(0, fName.indexOf('.'));
            project = new TSProject(pName, description.string());

            for (int i = 0; i < tsArray.length; i++) {
                tsArray[i] = new TS(UMatrix.getDoubleCol(values, i), varNames
                        .stringAt(i, 0), startDate.getTSDate());
            }

            getJTextArea_Description().setText(description.string());
            getJTextField_FileName().setText(file.getAbsolutePath());
            getTSTable().setTS(tsArray);
        } catch (Throwable ex) {
            String msg = "Error reading data file \"" + file.getAbsolutePath()
                    + "\".\n";
            String message = msg + ex.getMessage();
            StdMessages.error(message, "Data Import Error");
            log.error(msg, ex);
            return false;
        }

        return true;
    }

    /**
     * Return the JButton1 property value.
     * 
     * @return javax.swing.JButton
     */
    /* WARNING: THIS METHOD WILL BE REGENERATED. */
    private javax.swing.JButton getBrowseButton() {
        if (ivjBrowseButton == null) {
            try {
                ivjBrowseButton = new javax.swing.JButton();
                ivjBrowseButton.setName("BrowseButton");
                ivjBrowseButton
                        .setToolTipText("Choose a file with ascii data.");
                ivjBrowseButton.setLocation(new java.awt.Point(385, 15));
                ivjBrowseButton.setText("Browse...");
                ivjBrowseButton.setActionCommand("browse");
                ivjBrowseButton
                        .setPreferredSize(new java.awt.Dimension(110, 27));
                ivjBrowseButton.setBounds(new java.awt.Rectangle(385, 15, 85,
                        27));
                ivjBrowseButton.setMinimumSize(new java.awt.Dimension(110, 27));
                ivjBrowseButton.setMargin(new java.awt.Insets(2, 5, 2, 5));
                // user code begin {1}
                URL url = getClass().getResource("/images/open.gif");
                if (url != null)
                    ivjBrowseButton.setIcon(new javax.swing.ImageIcon(url));
                // user code end
            } catch (Throwable ivjExc) {
                // user code begin {2}
                // user code end
                handleException(ivjExc);
            }
        }
        return ivjBrowseButton;
    }

    /**
     * Return the JLabel_DescriptionLabel property value.
     * 
     * @return javax.swing.JLabel
     */
    /* WARNING: THIS METHOD WILL BE REGENERATED. */
    private javax.swing.JLabel getJLabel_DescriptionLabel() {
        if (ivjJLabel_DescriptionLabel == null) {
            try {
                ivjJLabel_DescriptionLabel = new javax.swing.JLabel();
                ivjJLabel_DescriptionLabel.setName("JLabel_DescriptionLabel");
                ivjJLabel_DescriptionLabel.setLocation(new java.awt.Point(11,
                        45));
                ivjJLabel_DescriptionLabel.setText("Description");
                ivjJLabel_DescriptionLabel
                        .setMaximumSize(new java.awt.Dimension(150, 20));
                ivjJLabel_DescriptionLabel.setHorizontalTextPosition(0);
                ivjJLabel_DescriptionLabel
                        .setPreferredSize(new java.awt.Dimension(140, 20));
                ivjJLabel_DescriptionLabel.setBounds(new java.awt.Rectangle(11,
                        45, 151, 17));
                ivjJLabel_DescriptionLabel
                        .setMinimumSize(new java.awt.Dimension(140, 20));
                ivjJLabel_DescriptionLabel.setHorizontalAlignment(4);
                // user code begin {1}
                // user code end
            } catch (Throwable ivjExc) {
                // user code begin {2}
                // user code end
                handleException(ivjExc);
            }
        }
        return ivjJLabel_DescriptionLabel;
    }

    /**
     * Return the JLabel_FileLabel property value.
     * 
     * @return javax.swing.JLabel
     */
    /* WARNING: THIS METHOD WILL BE REGENERATED. */
    private javax.swing.JLabel getJLabel_FileLabel() {
        if (ivjJLabel_FileLabel == null) {
            try {
                ivjJLabel_FileLabel = new javax.swing.JLabel();
                ivjJLabel_FileLabel.setName("JLabel_FileLabel");
                ivjJLabel_FileLabel.setLocation(new java.awt.Point(11, 15));
                ivjJLabel_FileLabel.setText("Filename");
                ivjJLabel_FileLabel.setMaximumSize(new java.awt.Dimension(150,
                        20));
                ivjJLabel_FileLabel.setHorizontalTextPosition(0);
                ivjJLabel_FileLabel.setPreferredSize(new java.awt.Dimension(
                        140, 20));
                ivjJLabel_FileLabel.setBounds(new java.awt.Rectangle(11, 15,
                        151, 17));
                ivjJLabel_FileLabel.setMinimumSize(new java.awt.Dimension(140,
                        20));
                ivjJLabel_FileLabel.setHorizontalAlignment(4);
                // user code begin {1}
                // user code end
            } catch (Throwable ivjExc) {
                // user code begin {2}
                // user code end
                handleException(ivjExc);
            }
        }
        return ivjJLabel_FileLabel;
    }

    /**
     * Return the JScrollPane1 property value.
     * 
     * @return javax.swing.JScrollPane
     */
    /* WARNING: THIS METHOD WILL BE REGENERATED. */
    private javax.swing.JScrollPane getJScrollPane1() {
        if (ivjJScrollPane1 == null) {
            try {
                ivjJScrollPane1 = new javax.swing.JScrollPane();
                ivjJScrollPane1.setName("JScrollPane1");
                ivjJScrollPane1.setPreferredSize(new java.awt.Dimension(3, 80));
                ivjJScrollPane1.setMinimumSize(new java.awt.Dimension(22, 80));
                getJScrollPane1().setViewportView(getJTextArea_Description());
                // user code begin {1}
                // user code end
            } catch (Throwable ivjExc) {
                // user code begin {2}
                // user code end
                handleException(ivjExc);
            }
        }
        return ivjJScrollPane1;
    }

    /**
     * Return the JScrollPane2 property value.
     * 
     * @return javax.swing.JScrollPane
     */
    /* WARNING: THIS METHOD WILL BE REGENERATED. */
    private javax.swing.JScrollPane getJScrollPane2() {
        if (ivjJScrollPane2 == null) {
            try {
                ivjJScrollPane2 = new javax.swing.JScrollPane();
                ivjJScrollPane2.setName("JScrollPane2");
                ivjJScrollPane2.setPreferredSize(new java.awt.Dimension(100,
                        131));
                getJScrollPane2().setViewportView(getTSList());
                // user code begin {1}
                TitledBorder title = new TitledBorder(new BevelBorder(
                        BevelBorder.LOWERED), "Loaded Data",
                        TitledBorder.RIGHT, TitledBorder.TOP);
                getJScrollPane2().setBorder(title);

                // user code end
            } catch (Throwable ivjExc) {
                // user code begin {2}
                // user code end
                handleException(ivjExc);
            }
        }
        return ivjJScrollPane2;
    }

    /**
     * Return the JTextArea1 property value.
     * 
     * @return javax.swing.JTextArea
     */
    /* WARNING: THIS METHOD WILL BE REGENERATED. */
    private javax.swing.JTextArea getJTextArea_Description() {
        if (ivjJTextArea_Description == null) {
            try {
                ivjJTextArea_Description = new javax.swing.JTextArea();
                ivjJTextArea_Description.setName("JTextArea_Description");
                ivjJTextArea_Description.setBounds(new java.awt.Rectangle(0, 0,
                        200, 39));
                ivjJTextArea_Description.setEditable(false);
                ivjJTextArea_Description.setBounds(0, 0, 297, 52);
                ivjJTextArea_Description.setMargin(new java.awt.Insets(0, 2, 0,
                        2));
                // user code begin {1}
                // user code end
            } catch (Throwable ivjExc) {
                // user code begin {2}
                // user code end
                handleException(ivjExc);
            }
        }
        return ivjJTextArea_Description;
    }

    /**
     * Return the JTextField1 property value.
     * 
     * @return javax.swing.JTextField
     */
    /* WARNING: THIS METHOD WILL BE REGENERATED. */
    private javax.swing.JTextField getJTextField_FileName() {
        if (ivjJTextField_FileName == null) {
            try {
                ivjJTextField_FileName = new javax.swing.JTextField();
                ivjJTextField_FileName.setName("JTextField_FileName");
                ivjJTextField_FileName.setLocation(new java.awt.Point(177, 15));
                ivjJTextField_FileName.setBounds(new java.awt.Rectangle(177,
                        15, 200, 21));
                ivjJTextField_FileName
                        .setMargin(new java.awt.Insets(0, 2, 0, 0));
                // user code begin {1}
                // user code end
            } catch (Throwable ivjExc) {
                // user code begin {2}
                // user code end
                handleException(ivjExc);
            }
        }
        return ivjJTextField_FileName;
    }

    /**
     * Return the JButton_Load property value.
     * 
     * @return javax.swing.JButton
     */
    /* WARNING: THIS METHOD WILL BE REGENERATED. */
    private javax.swing.JButton getLoadButton() {
        if (ivjLoadButton == null) {
            try {
                ivjLoadButton = new javax.swing.JButton();
                ivjLoadButton.setName("LoadButton");
                ivjLoadButton
                        .setToolTipText("\nLoad data shown in table to time series list.
\nYour changes will then apply. If you have edited
\nexisting series, then you have to confirm to overwrite
\nthese series"); ivjLoadButton.setLocation(new java.awt.Point(77, 195)); ivjLoadButton.setText("Load Dataset"); ivjLoadButton.setMaximumSize(new java.awt.Dimension(110, 27)); ivjLoadButton.setActionCommand("load"); ivjLoadButton.setPreferredSize(new java.awt.Dimension(110, 27)); ivjLoadButton .setBounds(new java.awt.Rectangle(77, 195, 85, 27)); ivjLoadButton.setMinimumSize(new java.awt.Dimension(110, 27)); // user code begin {1} // user code end } catch (Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } } return ivjLoadButton; } /** * Return the TimeSeriesList property value. * * @return com.jstatcom.ts.TSList */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private com.jstatcom.ts.TSList getTSList() { if (ivjTSList == null) { try { ivjTSList = new com.jstatcom.ts.TSList(); ivjTSList.setName("TSList"); ivjTSList.setTSListPopup(null); ivjTSList.setTSKeyListener(null); ivjTSList.setBounds(0, 0, 100, 175); ivjTSList.setTSMouseListener(null); // user code begin {1} // user code end } catch (Throwable ivjExc) { // user code begin {2} // user code end handleException(ivjExc); } } return ivjTSList; } /** * Called whenever the part throws an exception. * * @param exception * Exception */ private void handleException(Throwable exception) { log.error("Unhandled Exception", exception); } /** * Initializes connections * * @exception Exception * The exception description. */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private void initConnections() throws Exception { // user code begin {1} // user code end getLoadButton().addActionListener(ivjEventHandler); getBrowseButton().addActionListener(ivjEventHandler); } /** * Initialize the class. */ /* WARNING: THIS METHOD WILL BE REGENERATED. */ private void initialize() { try { // user code begin {1} // user code end setName("UIReadData"); setPreferredSize(new java.awt.Dimension(550, 350)); setLayout(new java.awt.GridBagLayout()); setSize(650, 400); setMinimumSize(new java.awt.Dimension(550, 350)); java.awt.GridBagConstraints constraintsJTextField_FileName = new java.awt.GridBagConstraints(); constraintsJTextField_FileName.gridx = 1; constraintsJTextField_FileName.gridy = 0; constraintsJTextField_FileName.fill = java.awt.GridBagConstraints.HORIZONTAL; constraintsJTextField_FileName.weightx = 1.0; constraintsJTextField_FileName.insets = new java.awt.Insets(0, 0, 5, 0); java.awt.GridBagConstraints constraintsBrowseButton = new java.awt.GridBagConstraints(); constraintsBrowseButton.gridx = 2; constraintsBrowseButton.gridy = 0; constraintsBrowseButton.gridheight = 2; constraintsBrowseButton.insets = new java.awt.Insets(0, 10, 0, 10); add(getBrowseButton(), constraintsBrowseButton); java.awt.GridBagConstraints constraintsJLabel_DescriptionLabel = new java.awt.GridBagConstraints(); constraintsJLabel_DescriptionLabel.gridx = 0; constraintsJLabel_DescriptionLabel.gridy = 2; constraintsJLabel_DescriptionLabel.fill = java.awt.GridBagConstraints.HORIZONTAL; constraintsJLabel_DescriptionLabel.anchor = java.awt.GridBagConstraints.NORTHEAST; java.awt.GridBagConstraints constraintsLoadButton = new java.awt.GridBagConstraints(); constraintsLoadButton.gridx = 2; constraintsLoadButton.gridy = 2; constraintsLoadButton.insets = new java.awt.Insets(5, 0, 5, 0); add(getLoadButton(), constraintsLoadButton); java.awt.GridBagConstraints constraintsJLabel_FileLabel = new java.awt.GridBagConstraints(); constraintsJLabel_FileLabel.gridx = 0; constraintsJLabel_FileLabel.gridy = 0; constraintsJLabel_FileLabel.anchor = java.awt.GridBagConstraints.NORTHEAST; constraintsJLabel_FileLabel.insets = new java.awt.Insets(0, 0, 5, 10); java.awt.GridBagConstraints constraintsJScrollPane1 = new java.awt.GridBagConstraints(); constraintsJScrollPane1.gridx = 1; constraintsJScrollPane1.gridy = 1; constraintsJScrollPane1.gridheight = 2; constraintsJScrollPane1.fill = java.awt.GridBagConstraints.BOTH; constraintsJScrollPane1.weightx = 1.0; constraintsJScrollPane1.insets = new java.awt.Insets(0, 0, 0, 0); java.awt.GridBagConstraints constraintsJScrollPane2 = new java.awt.GridBagConstraints(); java.awt.GridBagConstraints consGridBagConstraints1 = new java.awt.GridBagConstraints(); constraintsJLabel_DescriptionLabel.insets = new java.awt.Insets(0, 0, 0, 10); consGridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH; consGridBagConstraints1.weighty = 1.0; consGridBagConstraints1.weightx = 1.0; consGridBagConstraints1.gridy = 9; consGridBagConstraints1.gridx = 1; consGridBagConstraints1.gridwidth = 2; consGridBagConstraints1.insets = new java.awt.Insets(5, 0, 0, 0); constraintsJScrollPane2.gridx = 0; constraintsJScrollPane2.gridy = 9; constraintsJScrollPane2.fill = java.awt.GridBagConstraints.BOTH; constraintsJScrollPane2.weighty = 1.0; constraintsJScrollPane2.insets = new java.awt.Insets(5, 0, 0, 0); this.add(getJTextField_FileName(), constraintsJTextField_FileName); this.add(getJLabel_DescriptionLabel(), constraintsJLabel_DescriptionLabel); this.add(getJLabel_FileLabel(), constraintsJLabel_FileLabel); this.add(getJScrollPane1(), constraintsJScrollPane1); this.add(getJScrollPane2(), constraintsJScrollPane2); this.add(getTSTableScrollPane(), consGridBagConstraints1); initConnections(); } catch (Throwable ivjExc) { handleException(ivjExc); } // user code begin {2} TitledBorder title = new TitledBorder(new BevelBorder( BevelBorder.LOWERED), "Open Data File", TitledBorder.RIGHT, TitledBorder.TOP); this.setBorder(title); JHelpContextMgr.register(this); // user code end } /** * Stores changes to TSHolder. */ private void loadButton_ActionEvents() { TableCellEditor ed = getTSTable().getCellEditor(); if (ed != null) ed.stopCellEditing(); TS[] sel = getTSTable().getTS(); for (int i = 0; i < sel.length; i++) { sel[i].setTSProject(project); int res = TSHolder.getInstance().addTS(sel[i]); if (res == JOptionPane.CANCEL_OPTION) return; } if (hideAfterLoad && getParentFrame() != null) getParentFrame().setVisible(false); } /** * This method initializes jScrollPane * * @return javax.swing.JScrollPane */ private TSTableScrollPane getTSTableScrollPane() { if (jScrollPane == null) { jScrollPane = new TSTableScrollPane(); jScrollPane.setViewportView(getTSTable()); } return jScrollPane; } /** * This method initializes TSTable * * @return com.jstatcom.ts.TSTable */ private com.jstatcom.ts.TSTable getTSTable() { if (TSTable == null) { TSTable = new com.jstatcom.ts.TSTable(); } return TSTable; } /** * Gets whether parent frame is hidden after clicking load. * * @return true if parent frame is hidden after load */ public boolean isHideAfterLoad() { return hideAfterLoad; } /** * Sets whether parent frame is hidden after clicking load. * * @param b * true if parent frame is hidden after load */ public void setHideAfterLoad(boolean b) { hideAfterLoad = b; } }

Ads help maintain this website.