/*
 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.model.control;

import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;

import org.apache.log4j.Logger;

import com.jstatcom.component.AppSettings;
import com.jstatcom.component.StdMessages;
import com.jstatcom.component.TopFrame;
import com.jstatcom.io.DataHandlerTypes;
import com.jstatcom.io.FileSupport;
import com.jstatcom.model.JSCInt;
import com.jstatcom.model.JSCNArray;
import com.jstatcom.model.JSCSArray;
import com.jstatcom.model.JSCTypeDef;
import com.jstatcom.model.JSCTypes;
import com.jstatcom.model.Symbol;
import com.jstatcom.table.JSCNArrayTableModel;
import com.jstatcom.util.UMatrix;
import com.jstatcom.util.UStringArray;

/**
 * Panel to display and save symbols that represent JSCNArray
 * data objects.
 * 
 * @author Markus Kraetzig 
 */
public final class JSCNArrayPanel extends SymbolPanel {
    private static final Logger log = Logger.getLogger(JSCNArrayPanel.class);

    private JFrame top = null;

    private Symbol currentSymbol = null;

    private javax.swing.JLabel jLabel = null;

    private javax.swing.JLabel jLabel1 = null;

    private javax.swing.JLabel jLabel2 = null;

    private javax.swing.JLabel jLabel3 = null;

    private javax.swing.JButton jButton = null;

    private javax.swing.JButton jButton1 = null;

    private NArraySaveDlg optionsDlg = null;

    private com.jstatcom.table.JSCDataTableScrollPane JSCDataTableScrollPane = null;

    private com.jstatcom.table.JSCNArrayTable JSCNArrayTable = null;

    private JComboBox selModeCombo = null;

    /**
     * Creates a JSCNArrayPanel.
     */
    public JSCNArrayPanel() {
        super();
        initialize();
    }

    /**
     * Runs init tasks for this.
     * 
     */
    private void initialize() {
        GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
        java.awt.GridBagConstraints consGridBagConstraints11 = new java.awt.GridBagConstraints();
        java.awt.GridBagConstraints consGridBagConstraints12 = new java.awt.GridBagConstraints();
        java.awt.GridBagConstraints consGridBagConstraints10 = new java.awt.GridBagConstraints();
        java.awt.GridBagConstraints consGridBagConstraints13 = new java.awt.GridBagConstraints();
        java.awt.GridBagConstraints consGridBagConstraints15 = new java.awt.GridBagConstraints();
        java.awt.GridBagConstraints consGridBagConstraints1 = new java.awt.GridBagConstraints();
        java.awt.GridBagConstraints consGridBagConstraints16 = new java.awt.GridBagConstraints();
        consGridBagConstraints1.fill = java.awt.GridBagConstraints.BOTH;
        consGridBagConstraints1.weighty = 1.0;
        consGridBagConstraints1.weightx = 1.0;
        consGridBagConstraints1.gridy = 4;
        consGridBagConstraints1.gridx = 0;
        consGridBagConstraints1.gridwidth = 6;
        consGridBagConstraints13.gridy = 3;
        consGridBagConstraints13.gridx = 1;
        consGridBagConstraints13.fill = java.awt.GridBagConstraints.HORIZONTAL;
        consGridBagConstraints13.insets = new java.awt.Insets(2, 0, 5, 0);
        consGridBagConstraints12.gridy = 1;
        consGridBagConstraints12.gridx = 1;
        consGridBagConstraints12.fill = java.awt.GridBagConstraints.HORIZONTAL;
        consGridBagConstraints15.gridy = 1;
        consGridBagConstraints15.gridx = 2;
        consGridBagConstraints15.gridheight = 3;
        consGridBagConstraints15.weightx = 0.0D;
        consGridBagConstraints16.gridy = 1;
        consGridBagConstraints16.gridx = 3;
        consGridBagConstraints16.gridheight = 3;
        consGridBagConstraints16.weightx = 0.0D;
        consGridBagConstraints11.gridy = 3;
        consGridBagConstraints11.gridx = 0;
        consGridBagConstraints11.fill = java.awt.GridBagConstraints.HORIZONTAL;
        consGridBagConstraints10.gridy = 1;
        consGridBagConstraints10.gridx = 0;
        consGridBagConstraints10.fill = java.awt.GridBagConstraints.HORIZONTAL;
        consGridBagConstraints10.insets = new java.awt.Insets(0, 5, 0, 0);
        consGridBagConstraints11.insets = new java.awt.Insets(2, 5, 5, 0);
        consGridBagConstraints12.insets = new java.awt.Insets(0, 0, 0, 0);
        consGridBagConstraints15.insets = new java.awt.Insets(0, 10, 0, 0);
        consGridBagConstraints16.anchor = java.awt.GridBagConstraints.WEST;
        consGridBagConstraints16.insets = new java.awt.Insets(0, 10, 0, 0);
        this.setLayout(new java.awt.GridBagLayout());
        this.add(getJLabel(), consGridBagConstraints10);
        this.add(getJLabel1(), consGridBagConstraints11);
        this.add(getRowsLabel(), consGridBagConstraints12);
        this.add(getColsLabel(), consGridBagConstraints13);
        this.add(getSaveButton(), consGridBagConstraints15);
        this.setSize(500, 302);
        setBorder(new TitledBorder(new BevelBorder(BevelBorder.LOWERED),
                "Value of " + JSCTypes.NARRAY + "", TitledBorder.RIGHT,
                TitledBorder.TOP));
        gridBagConstraints1.gridx = 4;
        gridBagConstraints1.gridy = 1;
        gridBagConstraints1.weightx = 1.0;
        gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints1.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints1.insets = new java.awt.Insets(0, 10, 0, 0);
        gridBagConstraints1.gridheight = 3;
        this.add(getOptionsButton(), consGridBagConstraints16);
        this.add(getJSCDataTableScrollPane(), consGridBagConstraints1);
        this.add(getSelModeCombo(), gridBagConstraints1);

    }

    private NArraySaveDlg getArraySaveDlg() {
        if (optionsDlg == null) {
            SwingUtilities.getWindowAncestor(this);
            Window window = SwingUtilities.getWindowAncestor(this);
            if (window instanceof JFrame)
                top = (JFrame) window;
            optionsDlg = new NArraySaveDlg(top, true);
            if (top != null)
                optionsDlg.setLocationRelativeTo(top);
        }
        return optionsDlg;
    }

    /*
     * @see com.jstatcom.model.symbolpanels.SymbolPanel#setSymbol(com.jstatcom.model.Symbol)
     */
    public void setSymbol(Symbol symbol) {
        if (symbol == null)
            throw new IllegalArgumentException("Argument was null.");
        if (symbol.type != JSCTypes.NARRAY)
            throw new IllegalArgumentException("Invalid type " + symbol.type
                    + " for " + JSCTypes.NARRAY + " panel.");
        JSCNArray nArray = symbol.getJSCNArray();
        getRowsLabel().setText(nArray.rows() + "");
        getColsLabel().setText(nArray.cols() + "");
        currentSymbol = symbol;
        AppSettings app = TopFrame.getApplicationSettings();
        JSCInt maxNum = app.getMaxArrayDisplaySize();
        int max = (maxNum == null) ? 10000 : maxNum.intVal();
        if (nArray.rows() * nArray.cols() > max) {
            getJSCDataTableScrollPane().setColumnHeaderData(
                    new JSCSArray("colHeader",
                            new String[] { "Array too large for display" }));

            getJSCDataTableScrollPane().setRowHeaderData(
                    new JSCSArray("rowHeader", new String[] {}));

            getJSCNArrayTable().setModel(
                    new JSCNArrayTableModel(new Symbol(new JSCTypeDef("EMPTY",
                            JSCTypes.NARRAY))));
            getSelModeCombo().setEnabled(false);
            return;
        }
        getSelModeCombo().setEnabled(true);
        String[] colHeader = UStringArray.createNamedIndex("", UMatrix.seqa(1,
                1, nArray.cols()));

        String[] rowHeader = UStringArray.createNamedIndex("", UMatrix.seqa(1,
                1, nArray.rows()));

        getJSCDataTableScrollPane().setColumnHeaderData(
                new JSCSArray("colHeader", colHeader));

        getJSCDataTableScrollPane().setRowHeaderData(
                new JSCSArray("rowHeader", rowHeader));

        getJSCNArrayTable().setModel(new JSCNArrayTableModel(symbol));

        /*
         * getResultField().setText(""); if (symbol.isEmpty())
         * getResultField().getTextArea().setBackground(Color.LIGHT_GRAY); else {
         * getResultField().getTextArea().setBackground(Color.WHITE);
         * getResultField().appendWithoutScroll(symbol.display()); }
         */

    }

    /**
     * This method initializes jLabel
     * 
     * @return javax.swing.JLabel
     */
    private javax.swing.JLabel getJLabel() {
        if (jLabel == null) {
            jLabel = new javax.swing.JLabel();
            jLabel.setText("Rows");
            jLabel.setPreferredSize(new java.awt.Dimension(38, 20));
            jLabel.setMinimumSize(new java.awt.Dimension(40, 16));
        }
        return jLabel;
    }

    /**
     * This method initializes jLabel1
     * 
     * @return javax.swing.JLabel
     */
    private javax.swing.JLabel getJLabel1() {
        if (jLabel1 == null) {
            jLabel1 = new javax.swing.JLabel();
            jLabel1.setText("Cols");
            jLabel1.setPreferredSize(new java.awt.Dimension(40, 20));
            jLabel1.setMinimumSize(new java.awt.Dimension(40, 16));
        }
        return jLabel1;
    }

    /**
     * This method initializes jLabel2
     * 
     * @return javax.swing.JLabel
     */
    private javax.swing.JLabel getRowsLabel() {
        if (jLabel2 == null) {
            jLabel2 = new javax.swing.JLabel();
            jLabel2.setText("");
            jLabel2.setPreferredSize(new java.awt.Dimension(38, 20));
        }
        return jLabel2;
    }

    /**
     * This method initializes jLabel3
     * 
     * @return javax.swing.JLabel
     */
    private javax.swing.JLabel getColsLabel() {
        if (jLabel3 == null) {
            jLabel3 = new javax.swing.JLabel();
            jLabel3.setText("");
            jLabel3.setPreferredSize(new java.awt.Dimension(50, 20));
            jLabel3.setMinimumSize(new java.awt.Dimension(50, 20));
        }
        return jLabel3;
    }

    /**
     * Gets the symbol currently in use.
     * 
     * @return Symbol
     */
    private Symbol getCurrentSymbol() {
        return currentSymbol;
    }

    /**
     * Saves the content of symbol to a file.
     * 
     * @param symbol
     */
    private void saveSymbolToFile(Symbol symbol) {
        if (symbol == null || symbol.isEmpty())
            StdMessages.infoGeneral(
                    "Symbol is empty. There is nothing to save.", top);
        else {
            File saveFile = FileSupport.getInstance().saveDataFile(top,
                    symbol.name, DataHandlerTypes.EXCEL,
                    DataHandlerTypes.MATLAB, DataHandlerTypes.GAUSS_DAT,
                    DataHandlerTypes.GAUSS_FMT, DataHandlerTypes.DAT);
            if (saveFile == null)
                return;
            JSCNArray data = symbol.getJSCNArray();

            // used by all handlers
            if (getArraySaveDlg().isTransposed())
                data = new JSCNArray(data.name(), data.getTransposed());

            String header = null;
            if (getArraySaveDlg().isInfo()) {
                String trans = getArraySaveDlg().isTransposed() ? ", transposed"
                        : "";
                header = "name = " + symbol.NAME + ", type = NARRAY"
                        + ", rows = " + data.rows() + ", cols = " + data.cols()
                        + trans;
                String des = symbol.getDescription();
                if (des != null && des.length() > 0)
                    header = header + "\r\n" + des;
            }
            // only taken into account by ASCII handler
            String format = "%- " + getArraySaveDlg().getFWidth() + "."
                    + getArraySaveDlg().getPrec() + "f ";
            if (getArraySaveDlg().isRaw())
                format = "%-s ";
            data.setFormatString(format);

            // data handler depends on selection
            DataHandlerTypes handlerType = FileSupport.getInstance()
                    .getSelectedSaveHandlerType();

            try {
                handlerType.getHandler().exportData(saveFile, data, header);
            } catch (Throwable t) {
                String msg = "Error writing symbol \"" + symbol.NAME
                        + "\" to file\n\"" + saveFile.getAbsolutePath() + "\".";
                String message = msg + "\n\n" + t.getMessage();
                StdMessages.error(message, "Data Export Error");
                log.error(msg, t);

            }

        }
    }

    /**
     * This method initializes jButton
     * 
     * @return javax.swing.JButton
     */
    private javax.swing.JButton getSaveButton() {
        if (jButton == null) {
            jButton = new javax.swing.JButton();
            jButton.setText("Save");
            jButton.setPreferredSize(new java.awt.Dimension(100, 25));
            jButton.setMinimumSize(new java.awt.Dimension(100, 25));
            jButton.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent evt) {
                    saveSymbolToFile(getCurrentSymbol());
                }
            });
        }
        return jButton;
    }

    /**
     * This method initializes jButton1
     * 
     * @return javax.swing.JButton
     */
    private javax.swing.JButton getOptionsButton() {
        if (jButton1 == null) {
            jButton1 = new javax.swing.JButton();
            jButton1.setPreferredSize(new java.awt.Dimension(100, 25));
            jButton1.setMinimumSize(new java.awt.Dimension(100, 25));
            jButton1.setMargin(new Insets(1, 1, 1, 1));
            jButton1.setText("Save Options");
            jButton1.addActionListener(new ActionListener() {

                public void actionPerformed(ActionEvent evt) {
                    getArraySaveDlg().setVisible(true);
                }
            });
        }
        return jButton1;
    }

    /**
     * This method initializes JSCDataTableScrollPane
     * 
     * @return com.jstatcom.table.JSCDataTableScrollPane
     */
    private com.jstatcom.table.JSCDataTableScrollPane getJSCDataTableScrollPane() {
        if (JSCDataTableScrollPane == null) {
            JSCDataTableScrollPane = new com.jstatcom.table.JSCDataTableScrollPane();
            JSCDataTableScrollPane.setViewportView(getJSCNArrayTable());
            JSCDataTableScrollPane.setColumnHeaderShowing(true);
            JSCDataTableScrollPane.setRowHeaderShowing(true);

        }
        return JSCDataTableScrollPane;
    }

    /**
     * This method initializes JSCNArrayTable
     * 
     * @return com.jstatcom.table.JSCNArrayTable
     */
    private com.jstatcom.table.JSCNArrayTable getJSCNArrayTable() {
        if (JSCNArrayTable == null) {
            JSCNArrayTable = new com.jstatcom.table.JSCNArrayTable();
            JSCNArrayTable.setPrecision(4);
            JSCNArrayTable.setRowSelectionAllowed(true);
        }
        return JSCNArrayTable;
    }

    /**
     * Sets whether the data table is editable.
     * 
     * @param editable
     */
    public void setSymbolEditable(boolean editable) {
        getJSCNArrayTable().setEditable(editable);

    }

    /**
     * This method initializes jComboBox
     * 
     * @return javax.swing.JComboBox
     */
    private JComboBox getSelModeCombo() {
        if (selModeCombo == null) {
            selModeCombo = new JComboBox();
            selModeCombo.setPreferredSize(new java.awt.Dimension(100, 25));
            selModeCombo.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e) {
                    if (selModeCombo.getSelectedIndex() == 0) {
                        getJSCNArrayTable().setRowSelectionAllowed(false);
                        getJSCNArrayTable().setColumnSelectionAllowed(true);
                        return;
                    }
                    if (selModeCombo.getSelectedIndex() == 1) {
                        getJSCNArrayTable().setRowSelectionAllowed(true);
                        getJSCNArrayTable().setColumnSelectionAllowed(false);
                        return;
                    }
                    if (selModeCombo.getSelectedIndex() == 2) {
                        getJSCNArrayTable().setRowSelectionAllowed(true);
                        getJSCNArrayTable().setColumnSelectionAllowed(true);
                        return;
                    }
                }
            });
            selModeCombo.addItem("Column Selection");
            selModeCombo.addItem("Row Selection");
            selModeCombo.addItem("Rectangular Selection");
        }
        return selModeCombo;
    }
} // @jve:visual-info decl-index=0 visual-constraint="10,10"

 

Ads help maintain this website.