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

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;

import org.apache.log4j.Logger;

import com.jstatcom.component.CompSettings;
import com.jstatcom.component.StdMessages;
import com.jstatcom.component.TopFrameReference;
import com.jstatcom.util.UString;

/**
 * This component provides editing capabilities for selected time series in a
 * TSList. If data for a series was changed and the
 * OK button was pressed, the data of the respective series is
 * overwritten with the changes.
 * 
 * @author Markus Kraetzig 
 */
public final class TSEditDialog extends JDialog {
    private static final Logger log = Logger.getLogger(TSEditDialog.class);

    private int searchIndex = 0;

    private TSList tSList;

    private JPanel ivjJDialogContentPane = null;

    private JButton ivjCancel = null;

    IvjEventHandler ivjEventHandler = new IvjEventHandler();

    private JPanel ivjJPanel1 = null;

    private JButton ivjOK = null;

    private TSTable ivjTSTable = null;

    private TSTableScrollPane ivjJScrollPane1 = null;

    private JPanel ivjJPanel2 = null;

    private JButton ivjSearchButton = null;

    private JTextField ivjSearchField = null;

    class IvjEventHandler implements java.awt.event.ActionListener {
        public void actionPerformed(java.awt.event.ActionEvent e) {
            if (e.getSource() == TSEditDialog.this.getOK())
                connEtoC1();
            if (e.getSource() == TSEditDialog.this.getCancel())
                connEtoC2();
            if (e.getSource() == TSEditDialog.this.getSearchButton())
                connEtoC3();
            if (e.getSource() == TSEditDialog.this.getSearchField())
                connEtoC4();
        };
    };

    /**
     * TSEditDialog default constructor.
     */
    public TSEditDialog() {
        super();
        initialize();
    }

    /**
     * TSEditDialog constructor.
     * 
     * @param owner
     *            java.awt.Frame
     * @param modal
     *            boolean
     */
    public TSEditDialog(java.awt.Frame owner, boolean modal) {
        super(owner, modal);
        initialize();
    }

    /**
     * Discard changes made and quit dialog.
     */
    private void cancel_ActionEvents() {
        this.setVisible(false);
        return;
    }

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

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

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

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

    /**
     * Return the Cancel property value.
     * 
     * @return javax.swing.JButton
     */
    /* WARNING: THIS METHOD WILL BE REGENERATED. */
    private javax.swing.JButton getCancel() {
        if (ivjCancel == null) {
            try {
                ivjCancel = new javax.swing.JButton();
                ivjCancel.setName("Cancel");
                ivjCancel.setPreferredSize(new java.awt.Dimension(85, 27));
                ivjCancel.setText("Cancel");
                ivjCancel.setMinimumSize(new java.awt.Dimension(80, 27));
                // user code begin {1}
                // user code end
            } catch (Throwable ivjExc) {
                // user code begin {2}
                // user code end
                handleException(ivjExc);
            }
        }
        return ivjCancel;
    }

    /**
     * Return the JDialogContentPane property value.
     * 
     * @return javax.swing.JPanel
     */
    /* WARNING: THIS METHOD WILL BE REGENERATED. */
    private JPanel getJDialogContentPane() {
        if (ivjJDialogContentPane == null) {
            try {
                ivjJDialogContentPane = new JPanel();
                ivjJDialogContentPane.setName("JDialogContentPane");
                ivjJDialogContentPane.setLayout(new java.awt.BorderLayout());
                getJDialogContentPane().add(getJPanel1(), "South");
                getJDialogContentPane().add(getJPanel2(), "North");
                getJDialogContentPane().add(getTSTableScrollPane(), "Center");
                // user code begin {1}
                // user code end
            } catch (Throwable ivjExc) {
                // user code begin {2}
                // user code end
                handleException(ivjExc);
            }
        }
        return ivjJDialogContentPane;
    }

    /**
     * Return the JPanel1 property value.
     * 
     * @return javax.swing.JPanel
     */
    /* WARNING: THIS METHOD WILL BE REGENERATED. */
    private javax.swing.JPanel getJPanel1() {
        if (ivjJPanel1 == null) {
            try {
                ivjJPanel1 = new javax.swing.JPanel();
                ivjJPanel1.setName("JPanel1");
                ivjJPanel1.setPreferredSize(new java.awt.Dimension(520, 50));
                ivjJPanel1.setLayout(getJPanel1FlowLayout());
                ivjJPanel1.setMinimumSize(new java.awt.Dimension(520, 50));
                ivjJPanel1.setMaximumSize(new java.awt.Dimension(520, 50));
                getJPanel1().add(getOK(), getOK().getName());
                getJPanel1().add(getCancel(), getCancel().getName());
                // user code begin {1}
                // user code end
            } catch (Throwable ivjExc) {
                // user code begin {2}
                // user code end
                handleException(ivjExc);
            }
        }
        return ivjJPanel1;
    }

    /**
     * Return the JPanel1FlowLayout property value.
     * 
     * @return java.awt.FlowLayout
     */
    /* WARNING: THIS METHOD WILL BE REGENERATED. */
    private java.awt.FlowLayout getJPanel1FlowLayout() {
        java.awt.FlowLayout ivjJPanel1FlowLayout = null;
        try {
            /* Create part */
            ivjJPanel1FlowLayout = new java.awt.FlowLayout();
            ivjJPanel1FlowLayout.setVgap(10);
            ivjJPanel1FlowLayout.setHgap(50);
        } catch (Throwable ivjExc) {
            handleException(ivjExc);
        }
        ;
        return ivjJPanel1FlowLayout;
    }

    /**
     * Return the JPanel2 property value.
     * 
     * @return javax.swing.JPanel
     */
    /* WARNING: THIS METHOD WILL BE REGENERATED. */
    private javax.swing.JPanel getJPanel2() {
        if (ivjJPanel2 == null) {
            try {
                ivjJPanel2 = new javax.swing.JPanel();
                ivjJPanel2.setName("JPanel2");
                ivjJPanel2.setPreferredSize(new java.awt.Dimension(200, 30));
                ivjJPanel2.setLayout(new java.awt.GridBagLayout());

                java.awt.GridBagConstraints constraintsSearchField = new java.awt.GridBagConstraints();
                constraintsSearchField.gridx = 1;
                constraintsSearchField.gridy = 1;
                constraintsSearchField.fill = java.awt.GridBagConstraints.HORIZONTAL;
                constraintsSearchField.insets = new java.awt.Insets(0, 5, 0, 0);
                getJPanel2().add(getSearchField(), constraintsSearchField);

                java.awt.GridBagConstraints constraintsSearchButton = new java.awt.GridBagConstraints();
                constraintsSearchButton.gridx = 2;
                constraintsSearchButton.gridy = 1;
                constraintsSearchButton.anchor = java.awt.GridBagConstraints.WEST;
                constraintsSearchButton.weightx = 1.0;
                constraintsSearchButton.insets = new java.awt.Insets(0, 10, 0,
                        0);
                getJPanel2().add(getSearchButton(), constraintsSearchButton);
                // user code begin {1}
                // user code end
            } catch (Throwable ivjExc) {
                // user code begin {2}
                // user code end
                handleException(ivjExc);
            }
        }
        return ivjJPanel2;
    }

    /**
     * Return the JScrollPane1 property value.
     * 
     * @return javax.swing.JScrollPane
     */
    /* WARNING: THIS METHOD WILL BE REGENERATED. */
    private TSTableScrollPane getTSTableScrollPane() {
        if (ivjJScrollPane1 == null) {
            try {
                ivjJScrollPane1 = new TSTableScrollPane();
                ivjJScrollPane1.setName("JScrollPane1");
                getTSTableScrollPane().setViewportView(getTSTable());
                // user code begin {1}
                // user code end
            } catch (Throwable ivjExc) {
                // user code begin {2}
                // user code end
                handleException(ivjExc);
            }
        }
        return ivjJScrollPane1;
    }

    /**
     * Return the OK property value.
     * 
     * @return javax.swing.JButton
     */
    /* WARNING: THIS METHOD WILL BE REGENERATED. */
    private javax.swing.JButton getOK() {
        if (ivjOK == null) {
            try {
                ivjOK = new javax.swing.JButton();
                ivjOK.setName("OK");
                ivjOK.setPreferredSize(new java.awt.Dimension(85, 27));
                ivjOK.setText("OK");
                ivjOK.setMaximumSize(new java.awt.Dimension(80, 27));
                ivjOK.setMinimumSize(new java.awt.Dimension(80, 27));
                // user code begin {1}
                // user code end
            } catch (Throwable ivjExc) {
                // user code begin {2}
                // user code end
                handleException(ivjExc);
            }
        }
        return ivjOK;
    }

    /**
     * Return the SearchButton property value.
     * 
     * @return javax.swing.JButton
     */
    /* WARNING: THIS METHOD WILL BE REGENERATED. */
    private javax.swing.JButton getSearchButton() {
        if (ivjSearchButton == null) {
            try {
                ivjSearchButton = new javax.swing.JButton();
                ivjSearchButton.setName("SearchButton");
                ivjSearchButton.setToolTipText("Search for given number.");
                ivjSearchButton
                        .setPreferredSize(new java.awt.Dimension(100, 25));
                ivjSearchButton.setText("Find");
                // user code begin {1}
                // user code end
            } catch (Throwable ivjExc) {
                // user code begin {2}
                // user code end
                handleException(ivjExc);
            }
        }
        return ivjSearchButton;
    }

    /**
     * Return the JTextField1 property value.
     * 
     * @return javax.swing.JTextField
     */
    /* WARNING: THIS METHOD WILL BE REGENERATED. */
    private javax.swing.JTextField getSearchField() {
        if (ivjSearchField == null) {
            try {
                ivjSearchField = new javax.swing.JTextField();
                ivjSearchField.setName("SearchField");
                ivjSearchField
                        .setPreferredSize(new java.awt.Dimension(150, 20));
                ivjSearchField.setMinimumSize(new java.awt.Dimension(100, 20));
                // user code begin {1}
                // user code end
            } catch (Throwable ivjExc) {
                // user code begin {2}
                // user code end
                handleException(ivjExc);
            }
        }
        return ivjSearchField;
    }

    /**
     * Return the TSTable property value.
     * 
     * @return com.jstatcom.ts.TSTable
     */
    /* WARNING: THIS METHOD WILL BE REGENERATED. */
    private TSTable getTSTable() {
        if (ivjTSTable == null) {
            try {
                ivjTSTable = new com.jstatcom.ts.TSTable();
                ivjTSTable.setName("TSTable");
                getTSTableScrollPane().setColumnHeaderView(
                        ivjTSTable.getTableHeader());
                ivjTSTable.setBounds(0, 0, 472, 325);
                // user code begin {1}
                // user code end
            } catch (Throwable ivjExc) {
                // user code begin {2}
                // user code end
                handleException(ivjExc);
            }
        }
        return ivjTSTable;
    }

    /**
     * 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
        getOK().addActionListener(ivjEventHandler);
        getCancel().addActionListener(ivjEventHandler);
        getSearchButton().addActionListener(ivjEventHandler);
        getSearchField().addActionListener(ivjEventHandler);
    }

    /**
     * Initialize the class.
     */
    /* WARNING: THIS METHOD WILL BE REGENERATED. */
    private void initialize() {
        try {
            // user code begin {1}
            // user code end
            setName("TSEditor");
            setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
            setSize(472, 425);
            setTitle("Edit Time Series");
            setContentPane(getJDialogContentPane());
            initConnections();
        } catch (Throwable ivjExc) {
            handleException(ivjExc);
        }
        // user code begin {2}
        // user code end
    }

    /**
     * Accepts changes made, store them and quit dialog.
     */
    private void oK_ActionEvents() {
        TableCellEditor ed = getTSTable().getCellEditor();
        if (ed != null)
            ed.stopCellEditing();

        if (!getTSTable().isModelChanged()) {
            setVisible(false);
            return;
        }
        TS[] sel = getTSTable().getTS();

        int accept = JOptionPane.showConfirmDialog(TopFrameReference
                .getTopFrameRef(), "Do you want to save your changes?",
                "Question", JOptionPane.YES_NO_OPTION);

        if (accept == JOptionPane.YES_OPTION)
            for (int i = 0; i < sel.length; i++) {
                TSHolder.getInstance().removeTS(sel[i].name());
                TSHolder.getInstance().addTS(sel[i]);
            }
        setVisible(false);

    }

    /**
     * Comment
     */
    private void searchButton_ActionEvents() {

        String text = getSearchField().getText().trim();
        if (text.length() == 0)
            return;

        Double searchVal = UString.parseToNumber(text);

        // Remember where I stopped last time, to find other occurences.
        int start = searchIndex;
        TSTableModel tableModel = ((TSTableModel) getTSTable().getModel());
        int[] matches = tableModel.findRowIndexOf(searchVal, searchIndex);

        // Nothing was found in first run, but maybe we did not start at the
        // beginning.
        if (start > 0 && matches[0] == -1)
            matches = ((TSTableModel) getTSTable().getModel()).findRowIndexOf(
                    searchVal, 0);

        // Nothing was found in second run.
        if (matches[0] == -1) {
            String msg = "Could not find \"" + text + "\".";
            msg = (matches[1] > -1) ? msg + "\nThe closest match appeared at "
                    + tableModel.getStartTSDate().addPeriods(matches[1]) + "."
                    : msg;
            StdMessages.info(msg, "Not Found");
            searchIndex = 0;
        }
        // Do nothing if searchVal was null.
        if (matches[1] < 0)
            return;

        // Set the row selection.
        final int set = matches[1];

        getTSTableScrollPane().getVerticalScrollBar().setValue(
                new Double(getTSTable().getCellRect(set, 0, true).getY())
                        .intValue());

        Runnable runner = new Runnable() {
            public void run() {
                getTSTable().setRowSelectionInterval(set, set);
            }
        };
        CompSettings.syncWithSwing(runner);
        getTSTable().repaint();
        searchIndex = matches[0] + 1;

        return;
    }

    /**
     * Sets TSList for this dialog, where the selected
     * TS are retrieved from.
     * 
     * @param newTimeSeriesList
     *            the TSList to be referenced
     */
    public void setTSList(TSList newTimeSeriesList) {

        tSList = newTimeSeriesList;
        TS sel[] = tSList.getSelectedTS();
        setLocationRelativeTo(TopFrameReference.getTopFrameRef());
        getTSTable().setTS(sel);

        searchIndex = 0;
    }
}
 

Ads help maintain this website.