com.jtk.medicalrecord.view.panel.InputMedrecPemeriksaanpendukung.java Source code

Java tutorial

Introduction

Here is the source code for com.jtk.medicalrecord.view.panel.InputMedrecPemeriksaanpendukung.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 com.jtk.medicalrecord.view.panel;

import com.jtk.medicalrecord.entity.PemeriksaanPendukung;
import com.jtk.medicalrecord.util.CommonHelper;
import com.jtk.medicalrecord.util.MessageHelper;
import com.zlib.io.ZIo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.table.DefaultTableModel;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;

/**
 *
 * @author M Haska Ash Shiddiq
 */
public class InputMedrecPemeriksaanpendukung extends javax.swing.JPanel {

    private final List<PemeriksaanPendukung> pemeriksaanPendukungs = new ArrayList<>();
    private boolean view = false;

    /**
     * Creates new form InputMedrecPemeriksaanpendukung
     */
    public InputMedrecPemeriksaanpendukung() {
        initComponents();
    }

    public void viewState() {
        btnHapus.setVisible(false);
        btnTambah.setText("Download");
        view = true;
    }

    public void clear() {
        pemeriksaanPendukungs.removeAll(pemeriksaanPendukungs);
        createTableValue();
    }

    public void createTableValue() {
        Object[] columnsName = { "Nama File", "Tipe File" };

        DefaultTableModel dtm = new DefaultTableModel(null, columnsName) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        for (PemeriksaanPendukung p : pemeriksaanPendukungs) {
            Object[] o = new Object[2];
            o[0] = p.getPemNmFile();
            o[1] = p.getPemTipeFile();

            dtm.addRow(o);
        }
        tblFile.setModel(dtm);
        CommonHelper.resizeColumnWidth(tblFile);
    }

    public List<PemeriksaanPendukung> getPemeriksaanPendukungs() {
        return pemeriksaanPendukungs;
    }

    private void download() {
        if (tblFile.isRowSelected(tblFile.getSelectedRow())) {
            ZIo.saveAsFile(pemeriksaanPendukungs.get(tblFile.getSelectedRow()).getPemFile(),
                    pemeriksaanPendukungs.get(tblFile.getSelectedRow()).getPemTipeFile());
        } else {
            MessageHelper.addWarnMessage("Perhatian", "Harap pilih file terlebih dahulu");
        }
    }

    /**
     * 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() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        btnTambah = new javax.swing.JButton();
        btnHapus = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblFile = new javax.swing.JTable();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createEtchedBorder());
        setMinimumSize(new java.awt.Dimension(240, 500));
        setPreferredSize(new java.awt.Dimension(240, 650));

        jLabel1.setFont(new java.awt.Font("Segoe UI", 1, 18)); // NOI18N
        jLabel1.setText("Pemeriksaan Pendukung");

        jLabel2.setFont(new java.awt.Font("Segoe UI", 0, 11)); // NOI18N
        jLabel2.setText("File");

        btnTambah.setFont(new java.awt.Font("Segoe UI", 0, 11)); // NOI18N
        btnTambah.setIcon(
                new javax.swing.ImageIcon(getClass().getResource("/com/jtk/medicalrecord/image/plus.png"))); // NOI18N
        btnTambah.setText("Tambah");
        btnTambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTambahActionPerformed(evt);
            }
        });

        btnHapus.setFont(new java.awt.Font("Segoe UI", 0, 11)); // NOI18N
        btnHapus.setIcon(
                new javax.swing.ImageIcon(getClass().getResource("/com/jtk/medicalrecord/image/trash.png"))); // NOI18N
        btnHapus.setText("Hapus");
        btnHapus.setMaximumSize(new java.awt.Dimension(93, 25));
        btnHapus.setMinimumSize(new java.awt.Dimension(93, 25));
        btnHapus.setPreferredSize(new java.awt.Dimension(93, 25));
        btnHapus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHapusActionPerformed(evt);
            }
        });

        jScrollPane3.setMaximumSize(new java.awt.Dimension(164, 32767));
        jScrollPane3.setMinimumSize(new java.awt.Dimension(164, 23));
        jScrollPane3.setPreferredSize(new java.awt.Dimension(164, 100));

        tblFile.setFont(new java.awt.Font("Segoe UI", 0, 11)); // NOI18N
        tblFile.setModel(new javax.swing.table.DefaultTableModel(new Object[][] {

        }, new String[] { "Nama File", "Tipe File" }));
        jScrollPane3.setViewportView(tblFile);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createSequentialGroup()
                                        .addComponent(btnHapus, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(btnTambah))
                                .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel2).addComponent(jLabel1))
                                        .addGap(0, 6, Short.MAX_VALUE)))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addComponent(jLabel1).addGap(12, 12, 12)
                        .addComponent(jLabel2).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 205,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(btnTambah).addComponent(btnHapus,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap(352, Short.MAX_VALUE)));
    }// </editor-fold>//GEN-END:initComponents

    private void btnTambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTambahActionPerformed
        if (view) {
            download();
        } else {
            JFileChooser fileChooser = new JFileChooser();
            int result = fileChooser.showOpenDialog(null);
            if (result == JFileChooser.APPROVE_OPTION) {
                String url = fileChooser.getSelectedFile().getAbsolutePath();
                try {
                    InputStream is = new FileInputStream(new File(url));
                    String tipeFile = FilenameUtils.getExtension(url);
                    String namaFile = FilenameUtils.getBaseName(url);

                    PemeriksaanPendukung pp = new PemeriksaanPendukung();
                    pp.setPemId(UUID.randomUUID().toString().replace("-", ""));
                    pp.setPemNmFile(namaFile);
                    pp.setPemTipeFile(tipeFile);
                    pp.setPemFile(IOUtils.toByteArray(is));

                    pemeriksaanPendukungs.add(pp);

                    createTableValue();
                } catch (FileNotFoundException ex) {
                    MessageHelper.addErrorMessage("Error add file", ex.getMessage());
                    Logger.getLogger(InputMedrecPemeriksaanpendukung.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    MessageHelper.addErrorMessage("Error add file", ex.getMessage());
                    Logger.getLogger(InputMedrecPemeriksaanpendukung.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }//GEN-LAST:event_btnTambahActionPerformed

    private void btnHapusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHapusActionPerformed
        if (tblFile.isRowSelected(tblFile.getSelectedRow())) {
            pemeriksaanPendukungs.remove(tblFile.getSelectedRow());
            createTableValue();
        } else {
            MessageHelper.addWarnMessage("Perhatian", "Harap pilih file terlebih dahulu");
        }
    }//GEN-LAST:event_btnHapusActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnHapus;
    private javax.swing.JButton btnTambah;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable tblFile;
    // End of variables declaration//GEN-END:variables
}