/*
 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.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

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

import org.apache.log4j.Logger;

import com.jstatcom.component.StdMessages;
import com.jstatcom.io.DataHandlerTypes;
import com.jstatcom.io.FileSupport;
import com.jstatcom.io.TSASCIIHandler;
import com.jstatcom.model.JSCSArray;
import com.jstatcom.model.JSCTypes;
import com.jstatcom.model.Symbol;
import com.jstatcom.table.JSCSArrayTableModel;
import com.jstatcom.util.UMatrix;
import com.jstatcom.util.UStringArray;

/**
 * Panel to display and save symbols that represent JSCSArray
 * data objects.
 * 
 * @author Markus Kraetzig 
 */
public final class JSCSArrayPanel extends SymbolPanel {
    private static final Logger log = Logger.getLogger(JSCSArrayPanel.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 com.jstatcom.table.JSCDataTableScrollPane JSCDataTableScrollPane = null;

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

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

    /**
     * Runs init tasks for this.
     * 
     */
    private void initialize() {
        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 consGridBagConstraints2 = new java.awt.GridBagConstraints();
        java.awt.GridBagConstraints consGridBagConstraints15 = new java.awt.GridBagConstraints();
        consGridBagConstraints2.fill = java.awt.GridBagConstraints.BOTH;
        consGridBagConstraints2.weighty = 1.0;
        consGridBagConstraints2.weightx = 1.0;
        consGridBagConstraints2.gridy = 4;
        consGridBagConstraints2.gridx = 0;
        consGridBagConstraints2.gridwidth = 3;
        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;
        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, 20, 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(getJSCDataTableScrollPane(), consGridBagConstraints2);
        this.setSize(500, 302);
        setBorder(new TitledBorder(new BevelBorder(BevelBorder.LOWERED),
                "Value of " + JSCTypes.SARRAY + "", TitledBorder.RIGHT,
                TitledBorder.TOP));

        consGridBagConstraints15.anchor = java.awt.GridBagConstraints.WEST;
        this.add(getSaveButton(), consGridBagConstraints15);
    }

    /*
     * @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.SARRAY)
            throw new IllegalArgumentException("Invalid type " + symbol.type
                    + " for " + JSCTypes.SARRAY + " panel.");

        JSCSArray sArray = symbol.getJSCSArray();
        getRowsLabel().setText(sArray.rows() + "");
        getColsLabel().setText(sArray.cols() + "");

        getJSCSArrayTable().setModel(new JSCSArrayTableModel(symbol));

        String[] colHeader = UStringArray.createNamedIndex("", UMatrix.seqa(1,
                1, sArray.cols()));

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

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

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

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

        currentSymbol = symbol;
    }

    /**
     * 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));
        }
        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.DAT);
            if (saveFile == null)
                return;
            JSCSArray data = symbol.getJSCSArray();

            String header = "/* name = " + symbol.NAME + ", type = SARRAY"
                    + ", rows = " + data.rows() + ", cols = " + data.cols()
                    + " */\r\n";
            String des = symbol.getDescription();
            if (des != null && des.length() > 0)
                header = header + "/* " + des + " */\r\n";

            try {
                // handler that can handle SARRAY data elements
                TSASCIIHandler.getInstance().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 JSCDataTableScrollPane
     * 
     * @return com.jstatcom.table.JSCDataTableScrollPane
     */
    private com.jstatcom.table.JSCDataTableScrollPane getJSCDataTableScrollPane() {
        if (JSCDataTableScrollPane == null) {
            JSCDataTableScrollPane = new com.jstatcom.table.JSCDataTableScrollPane();
            JSCDataTableScrollPane.setViewportView(getJSCSArrayTable());
            JSCDataTableScrollPane.setColumnHeaderShowing(true);
            JSCDataTableScrollPane.setRowHeaderShowing(true);

        }
        return JSCDataTableScrollPane;
    }

    /**
     * This method initializes JSCSArrayTable
     * 
     * @return com.jstatcom.table.JSCSArrayTable
     */
    private com.jstatcom.table.JSCSArrayTable getJSCSArrayTable() {
        if (JSCSArrayTable == null) {
            JSCSArrayTable = new com.jstatcom.table.JSCSArrayTable();
            JSCSArrayTable.setRowSelectionAllowed(true);
        }
        return JSCSArrayTable;
    }

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

    }
} // @jve:visual-info decl-index=0 visual-constraint="10,10"

 

Ads help maintain this website.