OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.openconcerto.modules.customersupport;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import org.openconcerto.sql.Configuration;
import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.model.SQLRow;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowListRSH;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLSelect;
import org.openconcerto.sql.model.SQLSelect.LockStrength;
import org.openconcerto.sql.model.Where;
import org.openconcerto.sql.view.EditFrame;
import org.openconcerto.sql.view.EditPanel.EditMode;
import org.openconcerto.sql.view.EditPanelListener;
import org.openconcerto.ui.DefaultGridBagConstraints;
import org.openconcerto.ui.FrameUtil;
import org.openconcerto.ui.JLabelBold;
import org.openconcerto.ui.component.ITextArea;

public class SuiviTicketPanel extends JPanel {

    private final SQLRowAccessor rowTicket;
    private JPanel panelHistory = new JPanel(new GridBagLayout());
    private DateFormat format = new SimpleDateFormat("dd/MM/yy HH:mm");

    public SuiviTicketPanel(SQLRowAccessor rowTicket) {
        super(new GridBagLayout());
        this.rowTicket = rowTicket;
        initUI();
    }

    public void initUI() {
        GridBagConstraints c = new DefaultGridBagConstraints();

        c.gridwidth = GridBagConstraints.REMAINDER;

        JLabel label1 = new JLabel(
                "Ticket N°" + this.rowTicket.getString("NUMBER") + " du " + format.format(this.rowTicket.getDate("DATE").getTime()) + " - Statut : " + this.rowTicket.getString("STATUS"));
        JLabel label2 = new JLabel("Client : " + this.rowTicket.getForeign("ID_CLIENT").getString("NOM"));
        String type = this.rowTicket.getString("TYPE");
        if (type == null || type.trim().isEmpty()) {
            type = "non renseigné";
        }
        JLabel label3 = new JLabel("Type : " + type + " - Intitulé : " + this.rowTicket.getString("LABEL"));
        c.gridwidth = GridBagConstraints.REMAINDER;
        this.add(label1, c);
        c.gridy++;
        this.add(label2, c);
        c.gridy++;
        this.add(label3, c);

        final ITextArea textInfos = new ITextArea();
        textInfos.setEditable(false);
        textInfos.setEnabled(false);
        textInfos.setBorder(null);
        textInfos.setDisabledTextColor(Color.BLACK);
        textInfos.setBackground(this.getBackground());
        textInfos.setText(this.rowTicket.getString("INFOS"));
        c.gridy++;
        c.weightx = 1;
        c.weighty = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        final JScrollPane comp = new JScrollPane(textInfos);
        comp.setBorder(null);
        DefaultGridBagConstraints.lockMinimumSize(comp);
        this.add(comp, c);

        final JLabelBold intSep = new JLabelBold("Liste des interventions");
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.gridy++;
        c.gridx = 0;
        c.weightx = 1;
        this.add(intSep, c);

        c.gridy++;
        c.weightx = 0;
        c.weighty = 0;
        c.fill = GridBagConstraints.NONE;
        final JButton addSuivi = new JButton("Ajouter une intervention");
        this.add(addSuivi, c);
        addSuivi.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                final SQLElement element = Configuration.getInstance().getDirectory().getElement(Module.TABLE_CUSTOMER_SUPPORT_TICKET_HISTORY);
                EditFrame frame = new EditFrame(element, EditMode.CREATION);
                SQLRowValues rowVals = new SQLRowValues(element.getTable());
                rowVals.put("DATE", new Date());
                rowVals.put("ID_" + Module.TABLE_CUSTOMER_SUPPORT_TICKET, rowTicket.getID());
                frame.getSQLComponent().select(rowVals);
                FrameUtil.showPacked(frame);

                frame.addEditPanelListener(new EditPanelListener() {

                    @Override
                    public void modified() {
                    }

                    @Override
                    public void inserted(int id) {
                        fillPanelHistory();
                    }

                    @Override
                    public void deleted() {
                    }

                    @Override
                    public void cancelled() {
                    }
                });
            }
        });

        // Historique

        JScrollPane scrollPane = new JScrollPane(this.panelHistory);
        scrollPane.setBorder(null);
        fillPanelHistory();
        c.gridy++;
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.weighty = 1;
        this.add(scrollPane, c);

        JButton buttonClose = new JButton("Fermer");
        c.gridy++;
        c.fill = GridBagConstraints.NONE;
        c.weightx = 0;
        c.weighty = 0;
        c.gridwidth = GridBagConstraints.REMAINDER;
        c.anchor = GridBagConstraints.EAST;
        this.add(buttonClose, c);
        buttonClose.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JFrame frame = (JFrame) SwingUtilities.getRoot(SuiviTicketPanel.this);
                frame.dispose();
            }
        });
    }

    public void fillPanelHistory() {

        this.panelHistory.removeAll();

        GridBagConstraints cHistory = new DefaultGridBagConstraints();
        cHistory.anchor = GridBagConstraints.NORTHWEST;
        cHistory.insets = new Insets(4, 1, 4, 1);
        final SQLSelect sel = new SQLSelect();
        sel.addSelectStar(this.rowTicket.getTable().getTable(Module.TABLE_CUSTOMER_SUPPORT_TICKET_HISTORY));
        sel.setWhere(new Where(this.rowTicket.getTable().getTable(Module.TABLE_CUSTOMER_SUPPORT_TICKET_HISTORY).getField("ID_" + Module.TABLE_CUSTOMER_SUPPORT_TICKET), "=", this.rowTicket.getID()));
        sel.setLockStrength(LockStrength.UPDATE);
        List<SQLRow> histoRows = SQLRowListRSH.execute(sel, false, false);
        List<SQLRowAccessor> histoRowsOrder = new ArrayList<SQLRowAccessor>();
        histoRowsOrder.addAll(histoRows);
        Collections.sort(histoRowsOrder, new Comparator<SQLRowAccessor>() {
            @Override
            public int compare(SQLRowAccessor o1, SQLRowAccessor o2) {
                return o2.getDate("DATE").compareTo(o1.getDate("DATE"));
            }
        });

        for (SQLRowAccessor sqlRowAccessor : histoRowsOrder) {
            HistoryPanel p = new HistoryPanel(sqlRowAccessor, this);
            cHistory.gridy++;
            cHistory.weightx = 1;
            cHistory.weighty = 0;
            cHistory.fill = GridBagConstraints.BOTH;
            panelHistory.add(p, cHistory);
        }
        cHistory.gridy++;
        cHistory.weightx = 1;
        cHistory.weighty = 1;
        JPanel spacer = new JPanel();
        spacer.setOpaque(false);
        panelHistory.add(spacer, cHistory);
        this.panelHistory.updateUI();
        this.panelHistory.revalidate();
        this.panelHistory.repaint();
    }

}