sofmeth.mco3.gui.DRLFrame.java Source code

Java tutorial

Introduction

Here is the source code for sofmeth.mco3.gui.DRLFrame.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sofmeth.mco3.gui;

/**
 *
 * @author owner
 */
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import javax.swing.DefaultCellEditor;
import javax.swing.JComboBox;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import java.math.BigInteger;
import panels.DRLPanel;

public class DRLFrame extends javax.swing.JFrame {

    /**
     * Creates new form DRLFrame
     */
    private ArrayList<DRLPanel> drlList = new ArrayList<DRLPanel>();
    private JPanel mainPanel = new JPanel();
    private JComboBox typeCombo = new JComboBox<>();
    private TableColumn tableColumn;
    private String nameField, profField, progField, progNumField, dateField, langField;

    public DRLFrame() {
        drlList.add(new DRLPanel());
        initComponents();
        defectPanel.setLayout(new GridBagLayout());
        defectPanel.add(new DRLPanel());
    }

    public DRLFrame(String comboValue, String nameField, String profField, String progField, String progNumField,
            String dateField, String langField) {
        initComponents();
        tableColumn = drlTable.getColumn(drlTable.getColumnName(2));
        typeCombo.setModel(new javax.swing.DefaultComboBoxModel<>(
                new String[] { "10", "20", "30", "40", "50", "60", "70", "80", "90", "100" }));
        tableColumn.setCellEditor(new DefaultCellEditor(typeCombo));
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        tableColumn.setCellRenderer(renderer);
        this.nameField = nameField;
        this.profField = profField;
        this.progField = progField;
        this.progNumField = progNumField;
        this.dateField = dateField;
        this.langField = langField;
        this.setVisible(true);

    }

    /**
     * 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.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        defectPanel = new javax.swing.JPanel();
        doneButton = new javax.swing.JButton();
        closeButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        drlTable = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        doneButton.setText("Done");
        doneButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doneButtonActionPerformed(evt);
            }
        });

        closeButton.setText("Close");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        drlTable.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][] { { null, null, "10", null, null, null, null, null },
                        { null, null, "10", null, null, null, null, null },
                        { null, null, "10", null, null, null, null, null },
                        { null, null, "10", null, null, null, null, null },
                        { null, null, "10", null, null, null, null, null },
                        { null, null, "10", null, null, null, null, null },
                        { null, null, "10", null, null, null, null, null },
                        { null, null, "10", null, null, null, null, null },
                        { null, null, "10", null, null, null, null, null },
                        { null, null, "10", null, null, null, null, null },
                        { null, null, "10", null, null, null, null, null },
                        { null, null, "10", null, null, null, null, null },
                        { null, null, "10", null, null, null, null, null },
                        { null, null, "10", null, null, null, null, null },
                        { null, null, "10", null, null, null, null, null },
                        { null, null, "10", null, null, null, null, null },
                        { null, null, "10", null, null, null, null, null },
                        { null, null, "10", null, null, null, null, null },
                        { null, null, "10", null, null, null, null, null },
                        { null, null, "10", null, null, null, null, null } },
                new String[] { "Date", "Number", "Type", "Inject", "Remove", "Fix Time", "Fix Defect",
                        "Description" }));
        jScrollPane2.setViewportView(drlTable);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setText("Type is a combo box, click cell to show choices");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel2.setText("Double click on a cell to edit its value");

        javax.swing.GroupLayout defectPanelLayout = new javax.swing.GroupLayout(defectPanel);
        defectPanel.setLayout(defectPanelLayout);
        defectPanelLayout.setHorizontalGroup(
                defectPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(
                                javax.swing.GroupLayout.Alignment.TRAILING,
                                defectPanelLayout
                                        .createSequentialGroup().addContainerGap()
                                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 947,
                                                Short.MAX_VALUE)
                                        .addContainerGap())
                        .addGroup(defectPanelLayout.createSequentialGroup().addGap(27, 27, 27)
                                .addGroup(defectPanelLayout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel2).addComponent(jLabel1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(doneButton).addGap(34, 34, 34).addComponent(closeButton)
                                .addGap(26, 26, 26)));
        defectPanelLayout.setVerticalGroup(defectPanelLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, defectPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18).addComponent(jLabel2)
                        .addGroup(defectPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(defectPanelLayout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addGroup(defectPanelLayout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                                .addComponent(doneButton).addComponent(closeButton))
                                        .addContainerGap())
                                .addGroup(defectPanelLayout.createSequentialGroup()
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(jLabel1).addContainerGap(22, Short.MAX_VALUE)))));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(defectPanel, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addComponent(defectPanel, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_closeButtonActionPerformed

    private void doneButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_doneButtonActionPerformed
        // TODO add your handling code here:

        //TODO: need error handling to check if inputs in Fix Time etc are numbers and not letters
        XWPFDocument document = new XWPFDocument();
        try {
            FileOutputStream out = new FileOutputStream(new File("defect recording log.docx"));
            //making of title
            XWPFParagraph para = document.createParagraph();
            para.setAlignment(ParagraphAlignment.CENTER);
            para.setSpacingAfter(500);
            XWPFRun run = para.createRun();
            run.setText("Defect Recording Log");
            run.setBold(true);
            run.setFontSize(16);
            //end making of title
            //adding name etc to document
            XWPFTable details = document.createTable(3, 2);
            //setting cell width
            CTTblWidth width = details.getCTTbl().addNewTblPr().addNewTblW();
            width.setType(STTblWidth.DXA);
            width.setW(BigInteger.valueOf(9500));
            //end setting cell width
            //details.getCTTbl().getTblPr().unsetTblBorders();
            XWPFTableRow dtlRow = details.getRow(0);
            dtlRow.getCell(0).setText("Name: " + nameField);
            dtlRow.getCell(1).setText("Date: " + dateField);
            dtlRow = details.getRow(1);
            dtlRow.getCell(0).setText("Program: " + progField);
            dtlRow.getCell(1).setText("Program#: " + progNumField);
            dtlRow = details.getRow(2);
            dtlRow.getCell(0).setText("Professor: " + profField);
            dtlRow.getCell(1).setText("Language: " + langField);
            //end adding name to document
            para = document.createParagraph();
            run = para.createRun();
            run.addBreak();
            run.addBreak();
            XWPFTable table = document.createTable(6, 8);
            width = table.getCTTbl().addNewTblPr().addNewTblW();
            width.setType(STTblWidth.DXA);
            width.setW(BigInteger.valueOf(9500));
            for (int i = -1; i < 6; i++) {

                if (i == 5)
                    break;
                if (i == -1) { //top row of the table
                    XWPFTableRow row1 = table.getRow(i + 1);
                    for (int j = 0; j < 8; j++) {

                        switch (j) { //no formatting yet like bold etc
                        case 0:
                            row1.getCell(j).setText("Date");
                            break;
                        case 1:
                            row1.getCell(j).setText("Number");
                            break;
                        case 2:
                            row1.getCell(j).setText("Type");
                            break;
                        case 3:
                            row1.getCell(j).setText("Inject");
                            break;
                        case 4:
                            row1.getCell(j).setText("Remove");
                            break;
                        case 5:
                            row1.getCell(j).setText("Fix Time");
                            break;
                        case 6:
                            row1.getCell(j).setText("Fix Defect");
                            break;
                        case 7:
                            row1.getCell(j).setText("Description");
                            break;
                        }

                    }
                }
                //although this code assumes that there are values inside
                //UPDATE1: ok i fixed it i think
                else {
                    XWPFTableRow row = table.getRow(i + 1);
                    for (int j = 0; j < 8; j++) {
                        if (drlTable.getModel().getValueAt(i, 1) != null
                                && !drlTable.getModel().getValueAt(i, j).toString().isEmpty()) {

                            if (drlTable.getModel().getValueAt(i, j) != null
                                    && !drlTable.getModel().getValueAt(i, j).toString().isEmpty())

                                row.getCell(j).setText(drlTable.getModel().getValueAt(i, j).toString());
                        } else
                            break;
                    }
                }

            }
            document.write(out);

            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }//GEN-LAST:event_doneButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(DRLFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(DRLFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(DRLFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(DRLFrame.class.getName()).log(java.util.logging.Level.SEVERE, null,
                    ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new DRLFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeButton;
    private javax.swing.JPanel defectPanel;
    private javax.swing.JButton doneButton;
    private javax.swing.JTable drlTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
}