achmad.rifai.erp1.ui.pesan.Tulis.java Source code

Java tutorial

Introduction

Here is the source code for achmad.rifai.erp1.ui.pesan.Tulis.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 achmad.rifai.erp1.ui.pesan;

import achmad.rifai.erp1.entity.Penerima;
import java.util.List;
import org.joda.time.DateTime;

/**
 *
 * @author janoko
 */
public class Tulis extends javax.swing.JDialog {
    private achmad.rifai.erp1.entity.Karyawan k, balas;

    /**
     * Creates new form Tulis
     */
    public Tulis(java.awt.Frame parent, boolean modal, achmad.rifai.erp1.entity.Karyawan ka) {
        super(parent, modal);
        k = ka;
        initComponents();
    }

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

        jScrollPane1 = new javax.swing.JScrollPane();
        perima = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        isi = new javax.swing.JTextArea();
        s = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Menulis Pesan");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
        });

        perima.setLayout(new java.awt.GridBagLayout());
        jScrollPane1.setViewportView(perima);

        isi.setColumns(20);
        isi.setRows(5);
        isi.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                isiKeyReleased(evt);
            }
        });
        jScrollPane2.setViewportView(isi);

        s.setText("Kirim");
        s.setEnabled(false);
        s.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sActionPerformed(evt);
            }
        });

        jButton1.setText("jButton1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(s, javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(layout.createSequentialGroup().addComponent(jButton1).addGap(0, 0,
                                        Short.MAX_VALUE)))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 116,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 73, Short.MAX_VALUE)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 106,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(s)
                        .addContainerGap()));

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

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        new Thread(() -> {
            try {
                muatPenerima();
            } catch (Exception ex) {
                setVisible(false);
                achmad.rifai.erp1.util.Db.hindar(ex);
            }
            setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
            perima.setEnabled(true);
            isi.setEnabled(true);
        }).start();
        this.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
        perima.setEnabled(false);
        isi.setEnabled(false);
    }//GEN-LAST:event_formWindowOpened

    private void isiKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_isiKeyReleased
        s.setEnabled(!isi.getText().isEmpty());
    }//GEN-LAST:event_isiKeyReleased

    private void sActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sActionPerformed
        DateTime t = DateTime.now();
        new Thread(() -> {
            try {
                jejakPengiriman(t);
            } catch (Exception ex) {
                achmad.rifai.erp1.util.Db.hindar(ex);
            }
        }).start();
        try {
            achmad.rifai.erp1.util.Db d = achmad.rifai.erp1.util.Work.loadDB();
            achmad.rifai.erp1.entity.Pesan p = new achmad.rifai.erp1.entity.Pesan();
            p.setDeleted(false);
            p.setKode(k.getId() + t);
            p.setPengirim(k.getId());
            p.setWaktu(t);
            p.setPesan(isi.getText());
            p.setKe(genPenerima());
            new achmad.rifai.erp1.entity.dao.DAOPesan(d).insert(p);
            d.close();
        } catch (Exception ex) {
            achmad.rifai.erp1.util.Db.hindar(ex);
        }
        this.setVisible(false);
    }//GEN-LAST:event_sActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea isi;
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPanel perima;
    private javax.swing.JButton s;
    // End of variables declaration//GEN-END:variables

    private void muatPenerima() throws Exception {
        achmad.rifai.erp1.util.Db d = achmad.rifai.erp1.util.Work.loadDB();
        int x = 0, y = 0;
        for (achmad.rifai.erp1.entity.Karyawan ka : new achmad.rifai.erp1.entity.dao.DAOKaryawan(d).all()) {
            if (k.getId() == null ? ka.getId() == null : k.getId().equals(ka.getId()))
                continue;
            java.awt.GridBagConstraints gbc = new java.awt.GridBagConstraints();
            javax.swing.JCheckBox c = new javax.swing.JCheckBox(ka.getId());
            c.setVisible(true);
            if (y > 0) {
                gbc.gridx = x;
                gbc.gridy = y;
            }
            perima.add(c, gbc);
            x++;
            if (x == 5) {
                y++;
                x = 0;
            }
            if (balas != null)
                c.setSelected((ka.getId() == null ? balas.getId() == null : ka.getId().equals(balas.getId())));
        }
        d.close();
    }

    private void jejakPengiriman(DateTime t) throws Exception {
        String kode = k.getId() + t;
        achmad.rifai.erp1.util.Db d = achmad.rifai.erp1.util.Work.loadDB();
        achmad.rifai.erp1.entity.dao.DAOTracks dao = new achmad.rifai.erp1.entity.dao.DAOTracks(d);
        achmad.rifai.erp1.entity.Tracks a = dao.current(k.getId()), b = dao.current(k.getId());
        java.util.List<achmad.rifai.erp1.entity.Jejak> l = a.getL();
        l.add(new achmad.rifai.erp1.entity.Jejak("Mengirim pesan " + kode, k.getId()));
        b.setL(l);
        dao.update(a, b);
        d.close();
    }

    private List<Penerima> genPenerima() {
        List<Penerima> l = new java.util.LinkedList<>();
        for (java.awt.Component c : perima.getComponents()) {
            Penerima p = new Penerima();
            javax.swing.JCheckBox ce = (javax.swing.JCheckBox) c;
            p.setAkun(ce.getText());
            p.setSembunyi(false);
            p.setTerbaca(false);
            l.add(p);
        }
        return l;
    }
}