GUI_Apoteker.Panel_Detil_PO.java Source code

Java tutorial

Introduction

Here is the source code for GUI_Apoteker.Panel_Detil_PO.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 GUI_Apoteker;

import Client_Application_Model.TableModel_Detail_List_PO;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import database.entity.Pemesanan_Obat;
import database.entity.detil_pesan_obat;
import java.awt.Desktop;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;

/**
 *
 * @author aliffahri
 */
public class Panel_Detil_PO extends javax.swing.JPanel {
    Pemesanan_Obat po;
    TableModel_Detail_List_PO tabel = new TableModel_Detail_List_PO();
    List<detil_pesan_obat> list = new ArrayList<>();
    private Font font1 = new Font(Font.FontFamily.HELVETICA, 14, Font.BOLD);
    private Font font2 = new Font(Font.FontFamily.HELVETICA, 18, Font.BOLD);
    private Font font3 = new Font(Font.FontFamily.HELVETICA, 11, Font.BOLD);
    private Font font5 = new Font(Font.FontFamily.HELVETICA, 11);
    private Font font4 = new Font(Font.FontFamily.HELVETICA, 9);
    private Font font6 = new Font(Font.FontFamily.HELVETICA, 9, Font.BOLD);

    public Panel_Detil_PO(GUI_Apoteker gui, Pemesanan_Obat po) {
        initComponents();
        this.po = po;
        label_nama_sup.setText(po.getNama_supplier());
        label_id_po.setText(po.getId_Pemesanan_obat());
        label_tgl.setText(po.getTgl_Pemesanan());
        list = po.getList();
    }

    public void updateTabel(List<detil_pesan_obat> list) {
        tabel.setData(list);
        jTable1.setModel(tabel);
    }

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

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        label_tgl = new javax.swing.JLabel();
        label_nama_sup = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        label_id_po = new javax.swing.JLabel();

        setMinimumSize(new java.awt.Dimension(1170, 570));
        setPreferredSize(new java.awt.Dimension(1170, 570));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel1.setText("Detil PO");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
                new Object[][] { { null, null, null, null }, { null, null, null, null }, { null, null, null, null },
                        { null, null, null, null } },
                new String[] { "id_obat", "nama_obat", "jumlah_dipesan", "tanggal_pemesanan" }));
        jScrollPane1.setViewportView(jTable1);

        jButton1.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jButton1.setText("Cetak PDF");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jLabel3.setText("tanggal pesan :");

        label_tgl.setText("jLabel4");

        label_nama_sup.setText("Supp");

        jLabel4.setText("supplier          :");

        jLabel5.setText("Id_PO            :");

        label_id_po.setText("jLabel6");

        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)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 150,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(461, 461, 461))
                        .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout
                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane1)
                                .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                        .addGroup(jPanel1Layout.createSequentialGroup()
                                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 85,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(18, 18, 18).addComponent(label_id_po,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, 245, Short.MAX_VALUE))
                                        .addGroup(jPanel1Layout.createSequentialGroup().addGroup(jPanel1Layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING,
                                                        false)
                                                .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 85,
                                                        Short.MAX_VALUE))
                                                .addGap(18, 18, 18)
                                                .addGroup(jPanel1Layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING, false)
                                                        .addComponent(label_nama_sup,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                Short.MAX_VALUE)
                                                        .addComponent(label_tgl,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE, 245,
                                                                Short.MAX_VALUE))))
                                        .addGap(0, 0, Short.MAX_VALUE)))
                                .addContainerGap())))
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap(525, Short.MAX_VALUE).addComponent(jButton1,
                                javax.swing.GroupLayout.PREFERRED_SIZE, 123, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(502, 502, 502)));
        jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap().addComponent(jLabel1)
                        .addGap(20, 20, 20)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel5).addComponent(label_id_po))
                        .addGap(20, 20, 20)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(label_nama_sup).addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel3).addComponent(label_tgl))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 351,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 33,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(126, 126, 126)));

        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().addComponent(jPanel1,
                        javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout
                        .createSequentialGroup().addContainerGap().addComponent(jPanel1,
                                javax.swing.GroupLayout.PREFERRED_SIZE, 546, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(13, Short.MAX_VALUE)));
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        createPdf(list);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void createPdf(List<detil_pesan_obat> a) {
        try {
            JFileChooser saveFile = new JFileChooser();
            String namaFile = "D:/kampus/si-klinik-PO" + po.getId_Pemesanan_obat() + ".pdf";
            saveFile.setSelectedFile(new File(namaFile));
            String result = null;
            if (saveFile.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
                result = saveFile.getSelectedFile().toString();
            } else {
                System.out.println("No Selection ");
            }

            // TODO add your handling code here:
            Document document = new Document();

            PdfWriter writer = null;
            try {
                writer = PdfWriter.getInstance(document, new FileOutputStream(result));
            } catch (FileNotFoundException ex) {
                Logger.getLogger(Panel_Detil_PO.class.getName()).log(Level.SEVERE, null, ex);
            }
            document.open();
            PdfContentByte canvas = writer.getDirectContent();
            Rectangle rect = new Rectangle(50, 800, 550, 700);
            rect.setBorder(Rectangle.BOX);
            rect.setBorderWidth(5);
            rect.setBorderColor(BaseColor.BLACK);
            canvas.rectangle(rect);
            Paragraph preface;
            preface = getPreface("Bulanan");
            document.add(preface);
            document.add(Chunk.NEWLINE);
            document.add(Chunk.NEWLINE);
            document.add(createTableBulanan(a));
            document.close();
            open(result);
        } catch (DocumentException ex) {
            Logger.getLogger(Panel_Detil_PO.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private PdfPTable createTableBulanan(List<detil_pesan_obat> a) {
        PdfPTable tabel = new PdfPTable(2);
        PdfPCell cell;
        cell = new PdfPCell(new Phrase(("Nama Obat"), font6));
        cell.setRowspan(2);
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        cell.setBorder(Rectangle.BOX);
        cell.setBorderWidth(1);
        tabel.addCell(cell);
        cell = new PdfPCell(new Phrase(("Jumlah Obat yang dipesan"), font6));
        cell.setRowspan(2);
        cell.setBorder(Rectangle.BOX);
        cell.setBorderWidth(1);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        tabel.addCell(cell);
        for (int i = 0; i < a.size(); i++) {
            cell = new PdfPCell(new Phrase(("" + a.get(i).getNama_Obat()), font5));
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            tabel.addCell(cell);

            cell = new PdfPCell(new Phrase(("" + a.get(i).getJumlah_Pesan()), font5));
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            tabel.addCell(cell);
        }
        return tabel;
    }

    private Paragraph getPreface(String status) {
        Paragraph preface = new Paragraph();
        Calendar cal = new GregorianCalendar();
        String tanggal = "0";
        if (cal.get(Calendar.DATE) < 0) {
            tanggal = "0" + cal.get(Calendar.DATE);
        } else {
            tanggal = "" + cal.get(Calendar.DATE);
        }
        int bulan = (cal.get(Calendar.MONTH)) + 1;
        ;
        int tahun = cal.get(Calendar.YEAR);
        preface.setAlignment(Element.ALIGN_CENTER);

        Chunk chunk = new Chunk("Laporan Pemesanan Obat dengan ID - PO" + po.getId_Pemesanan_obat(), font1);
        preface.add(Chunk.NEWLINE);
        preface.add(chunk);
        chunk = new Chunk("Apotek 'SI-KLINIK'", font2);
        preface.add(Chunk.NEWLINE);
        preface.add(chunk);
        chunk = new Chunk("Tanggal : " + po.getTgl_Pemesanan(), font3);
        preface.add(Chunk.NEWLINE);
        preface.add(chunk);
        chunk = new Chunk("Nama Supplier : " + po.getNama_supplier(), font5);
        preface.add(Chunk.NEWLINE);
        preface.add(chunk);
        return preface;
    }

    public void open(String url) {
        try {
            Desktop desktop = Desktop.getDesktop();
            if (desktop.isSupported(Desktop.Action.OPEN)) {
                desktop.open(new File(url));
            } else {
                System.out.println("Open is not supported");
            }
        } catch (IOException exp) {
            exp.printStackTrace();
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel label_id_po;
    private javax.swing.JLabel label_nama_sup;
    private javax.swing.JLabel label_tgl;
    // End of variables declaration//GEN-END:variables
}