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

Java tutorial

Introduction

Here is the source code for op.care.med.structure.PnlMed.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.jidesoft.pane.CollapsiblePane;
import com.jidesoft.pane.CollapsiblePanes;
import com.jidesoft.popup.JidePopup;
import com.jidesoft.swing.JideBoxLayout;
import com.jidesoft.swing.JideButton;
import com.jidesoft.wizard.WizardDialog;
import entity.prescription.*;
import op.OPDE;
import op.care.med.inventory.DlgNewStocks;
import op.care.med.prodassistant.MedProductWizard;
import op.system.InternalClassACL;
import gui.interfaces.CleanablePanel;
import gui.GUITools;
import op.tools.SYSConst;
import op.tools.SYSTools;
import org.apache.commons.collections.Closure;
import org.jdesktop.swingx.JXSearchField;
import org.jdesktop.swingx.VerticalLayout;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import java.awt.*;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyVetoException;
import java.util.ArrayList;

/**
 * @author tloehr
 */
public class PnlMed extends CleanablePanel {

    private DefaultTreeModel tree;
    private MedProducts product;
    private JPopupMenu menu;
    private CollapsiblePanes searchPanes;
    private JScrollPane jspSearch;
    private JXSearchField txtSuche;
    private JList lstPraep;
    private JToggleButton tbIDs;

    /**
     * Creates new form FrmMed
     */
    public PnlMed(JScrollPane jspSearch) {
        super("opde.medication");
        this.jspSearch = jspSearch;
        initComponents();
        initDialog();
    }

    @Override
    public void cleanup() {
        SYSTools.unregisterListeners(menu);
        menu = null;
        SYSTools.unregisterListeners(this);
    }

    @Override
    public void reload() {
        createTree();
    }

    private void initDialog() {
        prepareSearchArea();
        product = null;
        treeMed.setModel(new DefaultTreeModel(new DefaultMutableTreeNode()));
        treeMed.setVisible(false);
    }

    /**
     * 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="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        jScrollPane1 = new JScrollPane();
        treeMed = new JTree();
        panel1 = new JPanel();
        label1 = new JLabel();

        //======== this ========
        setLayout(new CardLayout());

        //======== jScrollPane1 ========
        {

            //---- treeMed ----
            treeMed.setFont(new Font("Arial", Font.PLAIN, 14));
            treeMed.addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    treeMedMousePressed(e);
                }
            });
            jScrollPane1.setViewportView(treeMed);
        }
        add(jScrollPane1, "card1");

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

            //---- label1 ----
            label1.setText("text");
            label1.setIcon(null);
            panel1.add(label1);
        }
        add(panel1, "card2");
    }// </editor-fold>//GEN-END:initComponents

    private void lstPraepValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_lstPraepValueChanged
        if (!evt.getValueIsAdjusting() && lstPraep.getSelectedValue() != null) {
            product = (MedProducts) lstPraep.getSelectedValue();
            createTree();
        }
    }//GEN-LAST:event_lstPraepValueChanged

    @Override
    public String getInternalClassID() {
        return internalClassID;
    }

    private void txtSucheActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtSucheActionPerformed
        //        treeMed.setCellRenderer(new DefaultTreeCellRenderer());
        treeMed.setModel(new DefaultTreeModel(new DefaultMutableTreeNode()));
        //        treeMed.setVisible(false);
        if (txtSuche.getText().isEmpty()) {
            lstPraep.setModel(new DefaultListModel());
        } else {
            EntityManager em = OPDE.createEM();
            Query query = em
                    .createQuery("SELECT m FROM MedProducts m WHERE m.text LIKE :bezeichnung ORDER BY m.text");
            query.setParameter("bezeichnung", "%" + txtSuche.getText() + "%");
            lstPraep.setModel(SYSTools.list2dlm(query.getResultList()));
            em.close();
        }
    }//GEN-LAST:event_txtSucheActionPerformed

    private void treeMedMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_treeMedMousePressed
        if (product == null)
            return;
        if (!OPDE.getAppInfo().isAllowedTo(InternalClassACL.MANAGER, internalClassID))
            return;

        if (SwingUtilities.isRightMouseButton(evt)) {
            // Dieses Popupmenu fr den Table
            SYSTools.unregisterListeners(menu);
            menu = new JPopupMenu();
            //            JMenuItem itemdaf = new JMenuItem("Neue Darreichungsform");
            //            itemdaf.addActionListener(new java.awt.event.ActionListener() {
            //                public void actionPerformed(java.awt.event.ActionEvent evt) {
            //                    btnNeuDAF(evt);
            //                }
            //            });
            //            menu.add(itemdaf);

            if (treeMed.getRowForLocation(evt.getX(), evt.getY()) != -1
                    && OPDE.getAppInfo().isAllowedTo(InternalClassACL.UPDATE, internalClassID)) {
                JMenuItem itemedit = null;
                JMenuItem itemUPRedit = null;
                //                JMenuItem itemnew = null;
                //                JMenuItem itempack = null;
                TreePath curPath = treeMed.getPathForLocation(evt.getX(), evt.getY());
                final DefaultMutableTreeNode dmtn = (DefaultMutableTreeNode) curPath.getLastPathComponent();
                treeMed.setSelectionPath(curPath);
                //                final ListElement le = (ListElement) dmtn.getUserObject();
                //                int nodetype = ((Integer) le.getObject()).intValue();

                if (dmtn.getUserObject() instanceof TradeForm) {
                    final TradeForm tradeForm = (TradeForm) dmtn.getUserObject();
                    itemedit = new JMenuItem(SYSTools.xx("misc.msg.edit"));
                    itemedit.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                            new DlgTradeForm(tradeForm);
                            createTree();
                        }
                    });
                    itemUPRedit = new JMenuItem(SYSTools.xx("upreditor.tooltip"));
                    itemUPRedit.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                            new DlgUPREditor(tradeForm, new Closure() {
                                @Override
                                public void execute(Object o) {
                                    reload();
                                }
                            });
                        }
                    });
                    itemUPRedit.setEnabled(tradeForm.getDosageForm().isUPRn());
                    //                    itempack = new JMenuItem("Neue Verpackung");
                    //                    itempack.addActionListener(new java.awt.event.ActionListener() {
                    //                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                    //                            MedPackage mypack = new MedPackage(tradeForm);
                    //                            new DlgPack(OPDE.getMainframe(), "Neu", mypack);
                    ////                            OPDE.getEMF().getCache().evict(Darreichung.class, darreichung.getID());
                    //                            createTree();
                    //                        }
                    //                    });
                } else if (dmtn.getUserObject() instanceof MedPackage) {
                    final MedPackage packung = (MedPackage) dmtn.getUserObject();
                    itemedit = new JMenuItem("Bearbeiten");
                    itemedit.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                            new DlgPack(SYSTools.xx("misc.msg.edit"), packung);
                            createTree();
                        }
                    });
                } else if (dmtn.getUserObject() instanceof MedProducts) {

                    itemedit = new JMenuItem("Bearbeiten");
                    itemedit.addActionListener(new java.awt.event.ActionListener() {
                        public void actionPerformed(java.awt.event.ActionEvent evt) {
                            DlgProduct dlg = new DlgProduct(SYSTools.xx("misc.msg.edit"),
                                    (MedProducts) dmtn.getUserObject());
                            product = dlg.getProduct();
                            createTree();
                        }
                    });
                }

                if (itemedit != null)
                    menu.add(itemedit);
                if (itemUPRedit != null)
                    menu.add(itemUPRedit);
            }
            menu.show(evt.getComponent(), evt.getX(), evt.getY());
        }
    }//GEN-LAST:event_treeMedMousePressed

    private void createTree() {
        if (product == null)
            return;
        treeMed.setVisible(true);
        tree = new DefaultTreeModel(getRoot());
        treeMed.setModel(tree);
        treeMed.setCellRenderer(new TreeRenderer());
        SYSTools.expandAll(treeMed);
    }

    private DefaultMutableTreeNode getRoot() {
        DefaultMutableTreeNode root;
        root = new DefaultMutableTreeNode(product);
        SYSTools.addAllNodes(root, getTradeForms());
        return root;
    }

    private java.util.List<TradeForm> getTradeForms() {
        java.util.List result = new ArrayList();

        EntityManager em = OPDE.createEM();
        Query query = em.createQuery(
                "SELECT m FROM TradeForm m WHERE m.medProduct = :medProdukt ORDER BY m.dosageForm.preparation");
        query.setParameter("medProdukt", product);

        java.util.List<TradeForm> listDAF = query.getResultList();
        em.close();

        for (TradeForm daf : listDAF) {
            DefaultMutableTreeNode nodeDAF = new DefaultMutableTreeNode(daf);
            SYSTools.addAllNodes(nodeDAF, getPackung(daf));
            result.add(nodeDAF);
        }

        return result;
    }

    private java.util.List getPackung(TradeForm darreichung) {
        java.util.List result = new ArrayList();
        for (MedPackage aPackage : darreichung.getPackages()) {
            result.add(new DefaultMutableTreeNode(aPackage));
        }

        return result;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private JScrollPane jScrollPane1;
    private JTree treeMed;
    private JPanel panel1;
    private JLabel label1;
    // End of variables declaration//GEN-END:variables

    private class TreeRenderer extends DefaultTreeCellRenderer {
        TreeRenderer() {
            super();
        }

        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
                boolean leaf, int row, boolean hasFocus) {
            JLabel component = (JLabel) super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf,
                    row, hasFocus);
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
            if (node.getUserObject() instanceof MedProducts) {
                component.setIcon(new ImageIcon(getClass().getResource("/artwork/16x16/info.png")));
                MedProducts myprod = (MedProducts) node.getUserObject();
                component.setText(
                        (tbIDs.isSelected() ? "[" + ((MedProducts) node.getUserObject()).getMedPID() + "] " : "")
                                + myprod.getText() + ", " + myprod.getACME().getName() + ", "
                                + myprod.getACME().getCity());
            } else if (node.getUserObject() instanceof TradeForm) {
                component.setIcon(new ImageIcon(getClass().getResource("/artwork/16x16/medical.png")));
                component
                        .setText((tbIDs.isSelected() ? "[" + ((TradeForm) node.getUserObject()).getID() + "] " : "")
                                + TradeFormTools.toPrettyStringMediumWithExpiry((TradeForm) node.getUserObject()));
            } else if (node.getUserObject() instanceof MedPackage) {
                component.setIcon(new ImageIcon(getClass().getResource("/artwork/16x16/package.png")));
                component.setText(
                        (tbIDs.isSelected() ? "[" + ((MedPackage) node.getUserObject()).getID() + "] " : "")
                                + MedPackageTools.toPrettyString((MedPackage) node.getUserObject()));
            } else {
                component.setIcon(new ImageIcon(getClass().getResource("/artwork/16x16/filenew.png")));
                component.setText(null);
            }
            component.setFont(SYSConst.ARIAL14);
            //            setBackground(selected ? SYSConst.lightblue : Color.WHITE);

            return component;
        }

    }

    private void prepareSearchArea() {
        searchPanes = new CollapsiblePanes();
        searchPanes.setLayout(new JideBoxLayout(searchPanes, JideBoxLayout.Y_AXIS));
        jspSearch.setViewportView(searchPanes);

        JPanel mypanel = new JPanel();
        mypanel.setLayout(new VerticalLayout(3));
        mypanel.setBackground(Color.WHITE);

        CollapsiblePane searchPane = new CollapsiblePane(SYSTools.xx(internalClassID));
        searchPane.setStyle(CollapsiblePane.PLAIN_STYLE);
        searchPane.setCollapsible(false);

        try {
            searchPane.setCollapsed(false);
        } catch (PropertyVetoException e) {
            OPDE.error(e);
        }

        GUITools.addAllComponents(mypanel, addCommands());
        GUITools.addAllComponents(mypanel, addFilters());

        searchPane.setContentPane(mypanel);

        searchPanes.add(searchPane);
        searchPanes.addExpansion();

    }

    private java.util.List<Component> addFilters() {
        java.util.List<Component> list = new ArrayList<Component>();

        tbIDs = GUITools.getNiceToggleButton("misc.msg.showIDs");
        tbIDs.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                reload();
            }
        });
        tbIDs.setHorizontalAlignment(SwingConstants.LEFT);
        list.add(tbIDs);

        txtSuche = new JXSearchField("Suchen");
        txtSuche.setFont(SYSConst.ARIAL14);
        txtSuche.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                txtSucheActionPerformed(actionEvent);
            }
        });
        txtSuche.addFocusListener(new FocusAdapter() {
            @Override
            public void focusGained(FocusEvent focusEvent) {
                SYSTools.markAllTxt(txtSuche);
            }
        });
        list.add(txtSuche);

        lstPraep = new JList(new DefaultListModel());
        lstPraep.setCellRenderer(MedProductsTools.getMedProdukteRenderer());
        lstPraep.setFont(SYSConst.ARIAL14);
        lstPraep.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent listSelectionEvent) {
                lstPraepValueChanged(listSelectionEvent);
            }
        });
        lstPraep.setFixedCellWidth(200);

        list.add(new JScrollPane(lstPraep));

        return list;
    }

    private java.util.List<Component> addCommands() {
        java.util.List<Component> list = new ArrayList<Component>();

        if (OPDE.getAppInfo().isAllowedTo(InternalClassACL.INSERT, internalClassID)) {
            final JideButton addButton = GUITools.createHyperlinkButton(MedProductWizard.internalClassID,
                    SYSConst.icon22wizard, null);

            addButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent actionEvent) {

                    final JidePopup popup = new JidePopup();

                    WizardDialog wizard = new MedProductWizard(new Closure() {
                        @Override
                        public void execute(Object o) {
                            popup.hidePopup();
                            // keine Manahme ntig
                        }
                    }).getWizard();

                    popup.setMovable(false);
                    popup.setPreferredSize((new Dimension(800, 450)));
                    popup.setResizable(false);
                    popup.getContentPane().setLayout(new BoxLayout(popup.getContentPane(), BoxLayout.LINE_AXIS));
                    popup.getContentPane().add(wizard.getContentPane());
                    popup.setOwner(addButton);
                    popup.removeExcludedComponent(addButton);
                    popup.setTransient(false);
                    popup.setDefaultFocusComponent(wizard.getContentPane());
                    popup.addPropertyChangeListener("visible", new PropertyChangeListener() {
                        @Override
                        public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
                            popup.getContentPane().getComponentCount();
                        }
                    });

                    GUITools.showPopup(popup, SwingConstants.NORTH_EAST);
                }
            });

            list.add(addButton);
        }

        //       OPDE.debug("isCalcMediUPR1: " + OPDE.isCalcMediUPR1());
        //
        //        if (OPDE.isDebug()) {
        //            Iterator it = OPDE.getProps().entrySet().iterator();
        //            while (it.hasNext()) {
        //                OPDE.debug(it.next().toString());
        //            }
        //        }

        if (OPDE.isCalcMediUPR1() && OPDE.getAppInfo().isAllowedTo(InternalClassACL.INSERT, internalClassID)) {
            JideButton buchenButton = GUITools.createHyperlinkButton("nursingrecords.inventory.newstocks",
                    SYSConst.icon22addrow, new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent actionEvent) {
                            new DlgNewStocks(null);
                        }
                    });
            list.add(buchenButton);
        }

        return list;
    }
}