/*
 * ArbitraryDataPatternDialog.java
 *
 * Created on April 15, 2008, 8:49 AM
 */

package org.statcato.dialogs.data;

import org.statcato.*;
import org.statcato.utils.HelperFunctions;
import java.util.Vector;

/**
 * A dialog for creating a data sequence from a given arbitrary data pattern.
 * Provides options to repeat each data value in the pattern and the 
 * whole sequence for a specified number of times.
 * 
 * @author  Margaret Yau
 * @version %I%, %G%
 * @since 1.0
 */
public class ArbitraryDataPatternDialog extends StatcatoDialog {

    /** Creates new form ArbitraryDataPatternDialog */
    public ArbitraryDataPatternDialog(java.awt.Frame parent, boolean modal, Statcato app) {
        super(parent, modal);
        ParentSpreadsheet = app.getSpreadsheet();
        this.app = app;         
        initComponents();
        
        getRootPane().setDefaultButton(OKButton);
        setHelpFile("data-data-sequence");
        name = "Arbitrary Data Pattern";
        description = "For generating repeated values of an arbitrary data sequence.";
        helpStrings.add("Provide the column(s) in which the results should be stored.");
        helpStrings.add("Provide the arbitrary data sequence and the number" +
                "of times that each item and the sequence should be repeated.");
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // //GEN-BEGIN:initComponents
    private void initComponents() {

        OKButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        StoreTextField = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        DataTextArea = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        RepeatSeqTextField = new javax.swing.JTextField();
        RepeatItemTextField = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Arbitrary Data Pattern");

        OKButton.setText("OK");
        OKButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OKButtonActionPerformed(evt);
            }
        });

        CancelButton.setText("Cancel");
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Results"));

        jLabel7.setText("\n- Enter valid column names separated by space.
\nFor a continuous range of columns, separate using dash (e.g. C1-C30).
\n- The random sampling process is repeated for each column.\n\n"); jLabel1.setText("Store data pattern in:"); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(jLabel1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(StoreTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent(StoreTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap()) ); jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Options")); DataTextArea.setColumns(20); DataTextArea.setRows(5); jScrollPane1.setViewportView(DataTextArea); jLabel5.setText("Number of times to list each value:"); jLabel6.setText("Number of times to list each sequence:"); jLabel2.setText("\nArbitrary Data Sequence:
\n (separate values by space; enclose string values by double quotes \"\")\n\n"); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 306, javax.swing.GroupLayout.PREFERRED_SIZE) .addGroup(jPanel2Layout.createSequentialGroup() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel5) .addComponent(jLabel6)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(RepeatItemTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(RepeatSeqTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {RepeatItemTextField, RepeatSeqTextField}); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(29, 29, 29) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel5) .addComponent(RepeatItemTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel6) .addComponent(RepeatSeqTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap()) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addGroup(layout.createSequentialGroup() .addGap(130, 130, 130) .addComponent(OKButton, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(CancelButton))) .addContainerGap(19, Short.MAX_VALUE)) ); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {CancelButton, OKButton}); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(CancelButton) .addComponent(OKButton)) .addGap(16, 16, 16)) ); pack(); }//
//GEN-END:initComponents private void OKButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OKButtonActionPerformed app.compoundEdit = new DialogEdit("data pattern"); // get and check column for storing data pattern int storeColumnNum; String StoreColumn = StoreTextField.getText(); // get data sequnce from data input text field String str = DataTextArea.getText(); if (countDoubleQuotes(str) % 2 != 0) { // odd number of double quotes app.showErrorDialog("unmatched double quotes"); return; } Vector data = parseDataSequence(str); // get and check repetition values int repeatNum = 0; try { repeatNum = Integer.parseInt(RepeatItemTextField.getText()); } catch (NumberFormatException e) { app.showErrorDialog("Enter an integer for the number of repetitions for each value."); return; } int repeatSeq = 0; try { repeatSeq = Integer.parseInt(RepeatSeqTextField.getText()); } catch (NumberFormatException e) { app.showErrorDialog("Enter an integer for the number of repetitions for the number pattern."); return; } // generate the number sequence as a vector Vector Pattern = new Vector(); for (int seq = 0; seq < repeatSeq; ++seq) { for (int i = 0; i < data.size(); ++i) { String value = ((String)data.elementAt(i)).trim(); for (int repeat = 0; repeat < repeatNum; ++repeat) { Pattern.addElement(value); } } } Vector nums = ParentSpreadsheet.getColumnNumbersFromString(StoreColumn); if (nums == null) { app.showErrorDialog( "Invalid column(s) for storing results."); return; } for (int i = 0; i < nums.size(); ++i) ParentSpreadsheet.setColumn(nums.elementAt(i).intValue(), Pattern); String text = "Data sequence: " + HelperFunctions.convertVectorToString(data) + "
"; text += "Number of times to list each number: " + repeatNum + "
"; text += "Number of times to list each sequence: " + repeatSeq; app.addLogParagraph("Generate Arbitrary Data Sequence in " + StoreColumn, text); app.compoundEdit.end(); app.addCompoundEdit(app.compoundEdit); setVisible(false); }//GEN-LAST:event_OKButtonActionPerformed /** * Returns a vector of substrings in the given input string. * Substrings are delimited by "" or space. * * @param str string to be parsed * @return a vector of substrings */ Vector parseDataSequence(String str) { Vector result = new Vector(); int i = 0; String value = ""; while (i < str.length()) { if (Character.isWhitespace(str.charAt(i))) { // white space if (!value.equals("")) { result.addElement(value); value = ""; } i++; } else if (str.charAt(i) == '"') { // read string delimited by " " if (!value.equals("")) { result.addElement(value); value = ""; } i++; while (str.charAt(i) != '"') { value += str.charAt(i); i++; } i++; if (!value.equals("")) { result.addElement(value); value = ""; } } else { value += str.charAt(i); i++; } } if (!value.equals("")) { result.addElement(value); } return result; } /** * Returns the number of double quotes in the given string. * * @param str string for which double quotes are counted * @return number of double quotes */ int countDoubleQuotes(String str) { int count = 0; for (int i = 0; i < str.length(); ++i) { if (str.charAt(i) == '"') count++; } return count; } private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed setVisible(false); }//GEN-LAST:event_CancelButtonActionPerformed // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton CancelButton; private javax.swing.JTextArea DataTextArea; private javax.swing.JButton OKButton; private javax.swing.JTextField RepeatItemTextField; private javax.swing.JTextField RepeatSeqTextField; private javax.swing.JTextField StoreTextField; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JLabel jLabel7; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; private javax.swing.JScrollPane jScrollPane1; // End of variables declaration//GEN-END:variables }

Ads help maintain this website.