op.care.med.structure.DlgProduct.java Source code

Java tutorial

Introduction

Here is the source code for op.care.med.structure.DlgProduct.java

Source

/*
 * OffenePflege
 * Copyright (C) 2006-2012 Torsten Lhr
 * This program is free software; you can redistribute it and/or modify it under the terms of the 
 * GNU General Public License V2 as published by the Free Software Foundation
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even 
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if not, write to 
 * the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
 * www.offene-pflege.de
 * ------------------------ 
 * Auf deutsch (freie bersetzung. Rechtlich gilt die englische Version)
 * Dieses Programm ist freie Software. Sie knnen es unter den Bedingungen der GNU General Public License, 
 * wie von der Free Software Foundation verffentlicht, weitergeben und/oder modifizieren, gem Version 2 der Lizenz.
 *
 * Die Verffentlichung dieses Programms erfolgt in der Hoffnung, da es Ihnen von Nutzen sein wird, aber 
 * OHNE IRGENDEINE GARANTIE, sogar ohne die implizite Garantie der MARKTREIFE oder der VERWENDBARKEIT FR EINEN 
 * BESTIMMTEN ZWECK. Details finden Sie in der GNU General Public License.
 *
 * Sie sollten ein Exemplar der GNU General Public License zusammen mit diesem Programm erhalten haben. Falls nicht, 
 * schreiben Sie an die Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA.
 * 
 */

package op.care.med.structure;

import com.jgoodies.forms.factories.CC;
import com.jgoodies.forms.layout.FormLayout;
import com.jidesoft.popup.JidePopup;
import entity.prescription.*;
import op.OPDE;
import op.care.med.prodassistant.DlgACME;
import op.tools.MyJDialog;
import op.tools.SYSTools;
import org.apache.commons.collections.Closure;

import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
import javax.persistence.Query;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

/**
 * @author tloehr
 */
public class DlgProduct extends MyJDialog {

    private MedProducts product;

    /**
     * Creates new form DlgProdukt
     */
    public DlgProduct(String title, MedProducts product) {
        super(false);
        this.product = product;
        setTitle(title);
        initComponents();
        initDialog();
        pack();
        setVisible(true);
    }

    private void btnAddActionPerformed(ActionEvent e) {
        final JidePopup popup = new JidePopup();

        DlgACME dlg = new DlgACME(new Closure() {
            @Override
            public void execute(Object o) {
                if (o != null) {
                    cmbAcme.setModel(new DefaultComboBoxModel(new ACME[] { (ACME) o }));
                    cmbAcme.setSelectedIndex(0);
                    popup.hidePopup();
                }
            }
        });

        popup.setMovable(false);
        popup.setResizable(false);
        popup.getContentPane().setLayout(new BoxLayout(popup.getContentPane(), BoxLayout.LINE_AXIS));
        popup.getContentPane().add(dlg);
        popup.setOwner(btnAdd);
        popup.removeExcludedComponent(btnAdd);
        popup.setTransient(true);
        popup.setDefaultFocusComponent(dlg);

        popup.showPopup(new Insets(-5, 0, -5, 0), btnAdd);
    }

    private void cmbAcmeItemStateChanged(ItemEvent e) {

    }

    private void initDialog() {

        lblProductName.setText(SYSTools.xx("newstocks.lblProd"));
        lblAcme.setText(SYSTools.xx("misc.msg.acme"));
        lblSideEffects.setText(SYSTools.xx("misc.msg.sideeffects"));

        EntityManager em = OPDE.createEM();
        Query query2 = em.createQuery("SELECT m FROM ACME m ORDER BY m.name, m.city");
        cmbAcme.setModel(new DefaultComboBoxModel(query2.getResultList().toArray(new ACME[] {})));
        cmbAcme.setRenderer(ACMETools.getRenderer(0));
        em.close();

        if (product != null) {
            cmbAcme.setSelectedItem(product.getACME());
        }

        txtName.setText(product.getText());
        txtSideEffects.setText(product.getSideEffects());

    }

    /**
     * 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 PrinterForm Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Erzeugter Quelltext ">//GEN-BEGIN:initComponents
    private void initComponents() {
        lblProductName = new JLabel();
        txtName = new JTextField();
        cmbAcme = new JComboBox<>();
        lblAcme = new JLabel();
        btnAdd = new JButton();
        lblSideEffects = new JLabel();
        scrollPane1 = new JScrollPane();
        txtSideEffects = new JTextArea();
        panel1 = new JPanel();
        btnCancel = new JButton();
        btnOK = new JButton();

        //======== this ========
        setModal(true);
        Container contentPane = getContentPane();
        contentPane.setLayout(new FormLayout("2*(default, $lcgap), default:grow, 2*($lcgap, default)",
                "default, 2*($lgap, fill:default), $lgap, fill:default:grow, $lgap, fill:default, $lgap, default"));

        //---- lblProductName ----
        lblProductName.setText("Produktname");
        lblProductName.setFont(new Font("Arial", Font.PLAIN, 14));
        contentPane.add(lblProductName, CC.xy(3, 3));

        //---- txtName ----
        txtName.setFont(new Font("Arial", Font.PLAIN, 14));
        contentPane.add(txtName, CC.xywh(5, 3, 3, 1));

        //---- cmbAcme ----
        cmbAcme.setModel(
                new DefaultComboBoxModel<>(new String[] { "Eintrag 1", "Eintrag 2", "Eintrag 3", "Eintrag 4" }));
        cmbAcme.setFont(new Font("Arial", Font.PLAIN, 14));
        cmbAcme.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                cmbAcmeItemStateChanged(e);
            }
        });
        contentPane.add(cmbAcme, CC.xy(5, 5));

        //---- lblAcme ----
        lblAcme.setText("Hersteller");
        lblAcme.setFont(new Font("Arial", Font.PLAIN, 14));
        contentPane.add(lblAcme, CC.xy(3, 5));

        //---- btnAdd ----
        btnAdd.setText(null);
        btnAdd.setIcon(new ImageIcon(getClass().getResource("/artwork/22x22/bw/add.png")));
        btnAdd.setContentAreaFilled(false);
        btnAdd.setBorder(null);
        btnAdd.setSelectedIcon(new ImageIcon(getClass().getResource("/artwork/22x22/bw/add-pressed.png")));
        btnAdd.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        btnAdd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                btnAddActionPerformed(e);
            }
        });
        contentPane.add(btnAdd, CC.xy(7, 5, CC.LEFT, CC.DEFAULT));

        //---- lblSideEffects ----
        lblSideEffects.setText("Hersteller");
        lblSideEffects.setFont(new Font("Arial", Font.PLAIN, 14));
        contentPane.add(lblSideEffects, CC.xy(3, 7, CC.DEFAULT, CC.TOP));

        //======== scrollPane1 ========
        {

            //---- txtSideEffects ----
            txtSideEffects.setFont(new Font("Arial", Font.PLAIN, 14));
            scrollPane1.setViewportView(txtSideEffects);
        }
        contentPane.add(scrollPane1, CC.xywh(5, 7, 3, 1));

        //======== panel1 ========
        {
            panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS));

            //---- btnCancel ----
            btnCancel.setIcon(new ImageIcon(getClass().getResource("/artwork/16x16/cancel.png")));
            btnCancel.setText(null);
            btnCancel.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    btnCancelActionPerformed(e);
                }
            });
            panel1.add(btnCancel);

            //---- btnOK ----
            btnOK.setIcon(new ImageIcon(getClass().getResource("/artwork/16x16/apply.png")));
            btnOK.setText(null);
            btnOK.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    btnOKActionPerformed(e);
                }
            });
            panel1.add(btnOK);
        }
        contentPane.add(panel1, CC.xywh(5, 9, 3, 1, CC.RIGHT, CC.DEFAULT));
        setSize(585, 285);
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed

        if (!saveOK())
            return;

        EntityManager em = OPDE.createEM();
        try {
            em.getTransaction().begin();
            MedProducts myProduct = em.merge(product);

            myProduct.setText(txtName.getText().trim());
            myProduct.setSideEffects(txtSideEffects.getText().trim());
            myProduct.setACME(em.merge((ACME) cmbAcme.getSelectedItem()));

            for (TradeForm tf : myProduct.getTradeforms()) {
                em.lock(em.merge(tf), LockModeType.OPTIMISTIC_FORCE_INCREMENT);
                for (MedPackage mp : tf.getPackages()) {
                    em.lock(em.merge(mp), LockModeType.OPTIMISTIC_FORCE_INCREMENT);
                }
            }

            em.lock(myProduct, LockModeType.OPTIMISTIC);

            em.getTransaction().commit();

            product = myProduct;
        } catch (Exception e) {
            if (em.getTransaction().isActive()) {
                em.getTransaction().rollback();
            }
            OPDE.fatal(e);
        } finally {
            em.close();
        }
        dispose();

        //hier gehts weiter. prf auch die anderen locks bei den anderen editoren

    }//GEN-LAST:event_btnOKActionPerformed

    public MedProducts getProduct() {
        return product;
    }

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        //        product = null;
        dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    private boolean saveOK() {
        return !txtName.getText().trim().isEmpty() && cmbAcme.getSelectedItem() != null;
    }

    // Variablendeklaration - nicht modifizieren//GEN-BEGIN:variables
    private JLabel lblProductName;
    private JTextField txtName;
    private JComboBox<String> cmbAcme;
    private JLabel lblAcme;
    private JButton btnAdd;
    private JLabel lblSideEffects;
    private JScrollPane scrollPane1;
    private JTextArea txtSideEffects;
    private JPanel panel1;
    private JButton btnCancel;
    private JButton btnOK;
    // Ende der Variablendeklaration//GEN-END:variables

}