Package clips.doctor.checkup

Source Code of clips.doctor.checkup.FieldListner

/*
* PanelCheckUp.java
*
* Created on 12 Март 2008 г., 14:11
*/
package clips.doctor.checkup;

import cli_fmw.delegate.directory.DirectoryItem;
import cli_fmw.utils.Selector;
import clips.delegate.directory.complex.DirectoryCollaboratorFunctionsItem;
import clips.delegate.directory.filtered.DirectoryCheckupType;
import clips.delegate.directory.filtered.DirectoryCheckupTypeItem;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import clips.delegate.doctor.checkup.CheckupLocal;
import clips.delegate.doctor.checkup.CheckupXMLData;
import clips.delegate.doctor.checkup.FieldForXML;
import cli_fmw.delegate.DelegateLine2;
import cli_fmw.main.MainWindow;
import cli_fmw.utils.SelectorEditableExceptional;
import cli_fmw.utils.chooser.ChoosePanelComboException;
import cli_fmw.main.ClipsException;
import cli_fmw.main.DirectoryItemNotFoundException;
import cli_fmw.main.PrintCreators;
import cli_fmw.main.PageContainer;
import cli_fmw.main.PageGeneric;
import cli_fmw.main.Persistentable;
import cli_fmw.main.Printable;
import cli_fmw.report.CombinedReportCreator;
import cli_fmw.utils.MessageBox;
import cli_fmw.utils.chooser.ChoosePanelComboListener;
import cli_fmw.utils.chooser.ChoosePanelData;
import cli_fmw.utils.intelliwriter.IntelliWriter;
import cli_fmw.utils.table_config_states.StateSaver;
import clips.delegate.directory.complex.DirectoryCollaboratorFunctions;
import clips.login.UserInfo;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.LineBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.TableColumnModel;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

/**
*Панель отображения осмотра клиента
* @author  lacoste
*/
public class PanelCheckUp extends PageGeneric implements Persistentable, ChoosePanelComboListener, Printable {

    final private CheckupLocal checkupLocal;
    private CheckupXMLData xml;
    private boolean canBeEdit;
    private DirectoryCheckupType dir;
    ArrayList<FieldForXML> preFields;
    ArrayList<FieldForXML> postFields;

    /** Creates new form PanelCheckUp */
    public PanelCheckUp(PageContainer container, CheckupLocal checkUp) throws ClipsException {
        super(container);
        initComponents();
        this.checkupLocal = checkUp;
        jScrollPane1.getVerticalScrollBar().setUnitIncrement(10);

        //формирование данных для чузпанели (только осмотры)
        DirectoryItem currentType = checkupLocal.getType();
        ChoosePanelData currentData = null;
        dir = DirectoryLocator.getDirectory(DirectoryCheckupType.class, false);
        Set<DirectoryCheckupTypeItem> items = new HashSet<DirectoryCheckupTypeItem>();
        if (!UserInfo.get().isSuperUser()) {
            DirectoryCollaboratorFunctions dcf = DirectoryLocator.getDirectory(DirectoryCollaboratorFunctions.class);
            Selector<DirectoryCollaboratorFunctionsItem> functions = dcf.getFilteredItems(UserInfo.get().getCollaborator());
            for (int i = 0; i < functions.size(); i++) {
                SelectorEditableExceptional<DirectoryCheckupTypeItem> types = dir.getFilteredItems(functions.get(i).getSpeciality());
                for (int j = 0; j < types.size(); j++) {
                    items.add(types.get(j));
                }
            }
        } else {
            SelectorEditableExceptional<DirectoryCheckupTypeItem> selector = dir.getItems();
            for (int i = 0; i < selector.size(); i++) {
                items.add(selector.get(i));
            }
        }
        choosePanel1.setEnableChoose(!items.isEmpty());

        LinkedList<ChoosePanelData> data = new LinkedList<ChoosePanelData>();

        for (DirectoryCheckupTypeItem item : items) {
            if (!item.isVisible() || item.isAnalyse()) {
                continue;
            }
            ChoosePanelData d = new ChoosePanelData();
            d.value = item.getID();
            d.title = item.toString();
            d.canBeSelected = true;
            data.add(d);
            if (currentType == item) {
                currentData = d;
            }
        }
        ChoosePanelData array[] = new ChoosePanelData[data.size()];
        data.toArray(array);
        choosePanel1.setData(array, currentData, "Тип осмотра");
        choosePanel1.addChoosePanelListener(this);
        canBeEdit = checkUp.getSerrenLocal().canModify();
        xml = checkupLocal.getCheckupXMLData();
        setScreenFields();

        StateSaver.excludeFromAttach(this, jTable1);
        StateSaver.attachTo(this);
    }

    @Override
    public void chooseDone(int type, int id) throws ChoosePanelComboException {
        try {
            checkupLocal.setType(dir.getItemFromID(id));
            xml = checkupLocal.getCheckupXMLData();
            setScreenFields();
        } catch (ClipsException ex) {
            MessageBox.showException(ex);
            throw new ChoosePanelComboException();
        }
    }

    private JPanel createListPanel(ArrayList<JComponent> components) {
        JPanel panel = new JPanel();
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(panel);
        panel.setLayout(jPanel1Layout);
        ParallelGroup horisontalGroup = jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING);
        for (JComponent jLabel : components) {
            horisontalGroup.addComponent(jLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE);
        }
        jPanel1Layout.setHorizontalGroup(horisontalGroup);
        ParallelGroup verticalGroup = jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING);
        SequentialGroup sequentialGroup = jPanel1Layout.createSequentialGroup();
        for (JComponent jLabel : components) {
            sequentialGroup.addComponent(jLabel);
            sequentialGroup.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED);
        }
        verticalGroup.addGroup(sequentialGroup);
        jPanel1Layout.setVerticalGroup(verticalGroup);
        return panel;
    }

    public void setScreenFields() throws ClipsException {
        /**
         * Листенер, перехватывающий прокрутку колеса и передающий его основной скролпанели
         */
        MouseWheelListener mwl = new MouseWheelListener() {

            @Override
            public void mouseWheelMoved(MouseWheelEvent evt) {
                jScrollPane1.dispatchEvent(evt);
                evt.consume();
            }
        };

        xml.addContentStateListener(getContainer());

//        northPanel.removeAll();
//        southPanel.removeAll();
        preFields = xml.getPreFieldList();
        postFields = xml.getPostFieldList();

        ArrayList<JComponent> components = new ArrayList<JComponent>();
        final ArrayList<ImagePanel> panels = new ArrayList<ImagePanel>();

        //Добавляем поля перед таблицей
        for (int i = 0; i < preFields.size(); i++) {
            final FieldForXML field = preFields.get(i);
            if (field.getType() == FieldForXML.Type.image) {
                ImagePanel imagePanel = new ImagePanel((Image) field.getObject());
                panels.add(imagePanel);
                components.add(imagePanel);
            } else {
                JScrollPane jScrollPane = new javax.swing.JScrollPane();
                jScrollPane.setWheelScrollingEnabled(false);
                jScrollPane.addMouseWheelListener(mwl);
                JTextArea jTextArea = new javax.swing.JTextArea();
                jTextArea.addMouseWheelListener(mwl);
                jTextArea.setText(xml.getField(field.id));
                jTextArea.setEditable(canBeEdit);
                jTextArea.setRows(5);
                jTextArea.setLineWrap(true);
                //jTextArea.setWrapStyleWord(true);

                jScrollPane.setViewportView(jTextArea);
                jScrollPane.setBorder(javax.swing.BorderFactory.createTitledBorder(field.title));
                jScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
//                northPanel.add(jScrollPane);
                components.add(jScrollPane);
                jTextArea.getDocument().addDocumentListener(new FieldListner(xml, field.id));
                try {
                    //Attach template manager
//                type = field.getType();
//                if(type != null) {
//                    IntelliTemplate itempl = new IntelliTemplate(type, MainWindow.mainWindow, jTextPane);
//                }
                    // attach intelliwriter
                    Set<String> strings = field.getIntelliItems();
                    if (strings != null
                            && strings.size() > 0) {
                        IntelliWriter ir = new IntelliWriter(strings, MainWindow.mainWindow, jTextArea);
//                        jTextArea.addFocusListener(new IntelliWriterFocusListener(ir));
                    }
                } catch (DirectoryItemNotFoundException ex) {
                    //do nothing
                    ex.printStackTrace();
                }
            }
        }

//        northPanel.setLayout(new BorderLayout());
//        northPanel.add(listPanel, BorderLayout.CENTER);
        //Добавляем таблицу
        if (xml.getProbeValueList().size() > 0) {
            JTable jTable = new JTable();
//        jScrollPane2.setVisible(xml.getProbeValueList().size() > 0);
//        jScrollPane2.addMouseWheelListener(mwl);
            TableModelCheckUp tmodel = new TableModelCheckUp(xml, canBeEdit);
//        jTable1.setModel(tmodel);
//        jTable1.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
//        jTable1.setDefaultRenderer(Object.class, new CheckUpTableCellRenderer());
            jTable.setModel(tmodel);
            jTable.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
            jTable.setDefaultRenderer(Object.class, new CheckUpTableCellRenderer());
            TableColumnModel model = jTable.getColumnModel();
            //всем столбцам кроме первого и последнего назначаем свой редактор
            for (int i = 1; i < model.getColumnCount() - 1; i++) {
                model.getColumn(i).setCellEditor(
                        new CheckUpCellEditor(xml, TableModelCheckUp.COL_COUNT));
            }
            components.add(jTable);
        }

        //Добавляем поля после таблицы
        for (int i = 0; i < postFields.size(); i++) {
            FieldForXML field = postFields.get(i);

            JScrollPane jScrollPane = new javax.swing.JScrollPane();
            jScrollPane.setWheelScrollingEnabled(false);
            jScrollPane.addMouseWheelListener(mwl);
            JTextArea jTextArea = new javax.swing.JTextArea();
            jTextArea.addMouseWheelListener(mwl);
            jTextArea.setText(xml.getField(field.id));
            jTextArea.setEditable(canBeEdit);
            jTextArea.setRows(5);

            jScrollPane.setViewportView(jTextArea);
            jScrollPane.setBorder(javax.swing.BorderFactory.createTitledBorder(field.title));
//            southPanel.add(jScrollPane);

            components.add(jScrollPane);

            jTextArea.getDocument().addDocumentListener(new FieldListner(xml, field.id));
            try {
                //Attach template manager
//                type = field.getType();
//                if(type != null) {
//                    IntelliTemplate itempl = new IntelliTemplate(type, MainWindow.mainWindow, jTextPane);
//                }
                // attach intelliwriter
                Set<String> strings = field.getIntelliItems();
                if (strings != null
                        && strings.size() > 0) {
                    IntelliWriter ir = new IntelliWriter(strings, MainWindow.mainWindow, jTextArea);
                    //jTextArea.addFocusListener(new IntelliWriterFocusListener(ir));
                }
            } catch (DirectoryItemNotFoundException ex) {
                //do nothing
                ex.printStackTrace();
            }
        }
        JPanel listPanel = createListPanel(components);
        listPanel.addComponentListener(new ComponentAdapter() {

            @Override
            public void componentResized(ComponentEvent e) {
                for (ImagePanel imagePanel : panels) {
                    imagePanel.setPreferredSize(imagePanel.getPanelSize());
                }
            }

        });

        //Таблицу добавили
//        bigPanel.revalidate();
        jScrollPane1.setViewportView(listPanel);

        if (canBeEdit && checkupLocal.getType().getID() == 0) {
            choosePanel1.setEnableChoose(true);
        } else {
            choosePanel1.setEnableChoose(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 Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bigPanel = new javax.swing.JPanel();
        northPanel = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable1 = new cli_fmw.utils.sorted_table.SortedTable();
        southPanel = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        choosePanel1 = new cli_fmw.utils.chooser.ChoosePanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();

        bigPanel.setLayout(new java.awt.BorderLayout());

        northPanel.setLayout(new java.awt.GridLayout(0, 1));
        bigPanel.add(northPanel, java.awt.BorderLayout.NORTH);

        jScrollPane2.setBorder(javax.swing.BorderFactory.createTitledBorder("Табличные данные"));
        jScrollPane2.setWheelScrollingEnabled(false);
        jScrollPane2.setViewportView(jTable1);

        bigPanel.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        southPanel.setLayout(new java.awt.GridLayout(0, 1));
        bigPanel.add(southPanel, java.awt.BorderLayout.SOUTH);

        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });

        jPanel1.setLayout(new java.awt.BorderLayout());
        jPanel1.add(choosePanel1, java.awt.BorderLayout.CENTER);

        jLabel1.setText("Данные осмотра");

        jScrollPane1.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 470, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
    Component[] components = northPanel.getComponents();
    for (int i = 0; i < components.length; i++) {
        Component component = components[i];
        if (component instanceof JScrollPane) {
            JScrollPane pane = (JScrollPane) component;
            Component[] components1 = pane.getViewport().getComponents();
            for (int j = 0; j < components1.length; j++) {
                Component component1 = components1[j];
                if (component1 instanceof JTextArea) {
                    JTextArea area = (JTextArea) component1;
                    Dimension size = area.getSize();
                    size.width = 1;
                    area.setSize(size);
                    area.revalidate();
                    area.updateUI();
                }
            }
        }
    }
}//GEN-LAST:event_formComponentResized
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel bigPanel;
    private cli_fmw.utils.chooser.ChoosePanel choosePanel1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private cli_fmw.utils.sorted_table.SortedTable jTable1;
    private javax.swing.JPanel northPanel;
    private javax.swing.JPanel southPanel;
    // End of variables declaration//GEN-END:variables

    @Override
    public String getPageTitle() {
        return "Осмотр";
    }

    @Override
    public boolean isDirty() {
        return xml.isDirty();
    }

    @Override
    public void save() throws ClipsException {
        //Завершаем редактирование текущей ячейки
        if (jTable1.isEditing()) {
            jTable1.getCellEditor().stopCellEditing();
        }
        checkupLocal.setCheckupXMLData(xml);
        checkupLocal.save1();
        xml.commit();
        setScreenFields();
        getContainer().contentStateChanged();
    }

    @Override
    public void restore() {
        try {
            if (checkupLocal.isNewlyCreated()) {
                getContainer().removePage(this, true);
            }
            checkupLocal.restore();
            xml = checkupLocal.getCheckupXMLData();
            setScreenFields();
            getContainer().contentStateChanged();
        } catch (Exception ex) {
            MessageBox.showException(ex);
        }
    }

    @Override
    public boolean readyForPrint() {
        return (xml != null) && !checkupLocal.isNewlyCreated() && !isDirty();
    }

    @Override
    public void print() {
        try {
            if (readyForPrint()) {
                CombinedReportCreator parCreator = PrintCreators.createCombinedReportHeader(getClass());

                checkupLocal.print(parCreator);
                parCreator.finish();
            }
        } catch (ClipsException ex) {
            MessageBox.showException(ex);
        }
    }

    @Override
    public DelegateLine2 getDelegate() {
        return checkupLocal;
    }
}

class FieldListner implements DocumentListener {

    private CheckupXMLData xml;
    private int fieldID;

    public FieldListner(CheckupXMLData xml, int fieldID) {
        this.xml = xml;
        this.fieldID = fieldID;
    }

    @Override
    public void insertUpdate(DocumentEvent e) {
        setField(e.getDocument());
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        setField(e.getDocument());
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    private void setField(Document doc) {
        try {
            //Log.printlnAnsPos("XML changed, fieldID = " + fieldID);
            xml.setField(fieldID, doc.getText(0, doc.getLength()));
            //System.out.println(">>>>>>" + xml.getField(fieldID));
        } catch (BadLocationException ex) {
            MessageBox.showException(ex);
        }
    }
}

/*class IntelliWriterFocusListener implements FocusListener {
IntelliWriter intelliWriter;

public IntelliWriterFocusListener(IntelliWriter intelliWriter) {
this.intelliWriter = intelliWriter;
}

@Override
public void focusGained(FocusEvent e) {
//do nothing
}

@Override
public void focusLost(FocusEvent e) {
intelliWriter.hideMenu();
}

}*/ 
TOP

Related Classes of clips.doctor.checkup.FieldListner

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.