OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Blame | Last modification | View Log | RSS feed

package ilm.erp.core.customerrelationship.customer.element;

import ilm.sql.Configuration;
import ilm.sql.element.SQLElement;
import ilm.sql.model.SQLRowValues;
import ilm.sql.view.list.RowValuesTable;
import ilm.sql.view.list.RowValuesTableControlPanel;
import ilm.sql.view.list.RowValuesTableModel;
import ilm.sql.view.list.RowValuesTableRenderer;
import ilm.sql.view.list.SQLTableElement;
import ilm.ui.FormatEditor;
import ilm.utils.FormatGroup;
import ilm.utils.convertor.DateToSQLConvertor;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.File;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ToolTipManager;

public class ContactItemTable extends JPanel {

    private RowValuesTable table;
    final RowValuesTableControlPanel comp;

    public ContactItemTable(SQLRowValues defaultRow) {
        this(defaultRow, Configuration.getInstance().getDirectory().getElement(defaultRow.getTable()));
    }

    private ContactItemTable(SQLRowValues defaultRow, SQLElement elt) {
        this.setOpaque(false);
        this.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.gridwidth = 1;
        c.gridheight = 1;
        c.gridx = 0;
        c.gridy = 0;
        c.fill = GridBagConstraints.HORIZONTAL;
        c.weightx = 1;
        c.weighty = 0;

        List<SQLTableElement> list = new Vector<SQLTableElement>();
        SQLTableElement tableElementTitre = new SQLTableElement(elt.getTable().getField("ID_TITRE_PERSONNEL"));
        list.add(tableElementTitre);

        SQLTableElement tableElementNom = new SQLTableElement(elt.getTable().getField("NOM"));
        list.add(tableElementNom);

        SQLTableElement tableElementPrenom = new SQLTableElement(elt.getTable().getField("PRENOM"));
        list.add(tableElementPrenom);

        SQLTableElement tableElementFonction = new SQLTableElement(elt.getTable().getField("FONCTION"));
        list.add(tableElementFonction);

        if (elt.getTable().contains("SERVICE")) {
            SQLTableElement tableElementService = new SQLTableElement(elt.getTable().getField("SERVICE"));
            list.add(tableElementService);
        }

        SQLTableElement tableElementTel = new SQLTableElement(elt.getTable().getField("TEL_DIRECT"));
        list.add(tableElementTel);

        SQLTableElement tableElementFax = new SQLTableElement(elt.getTable().getField("FAX"));
        list.add(tableElementFax);

        SQLTableElement tableElementTelP = new SQLTableElement(elt.getTable().getField("TEL_MOBILE"));
        list.add(tableElementTelP);

        SQLTableElement tableElementMail = new SQLTableElement(elt.getTable().getField("EMAIL"));
        list.add(tableElementMail);

        if (elt.getTable().contains("TYPE")) {
            SQLTableElement tableElementType = new SQLTableElement(elt.getTable().getField("TYPE"));
            list.add(tableElementType);
        }

        if (elt.getTable().contains("PAYS")) {
            SQLTableElement tableElementPays = new SQLTableElement(elt.getTable().getField("PAYS"));
            list.add(tableElementPays);
        }

        if (elt.getTable().contains("ENVOI_RAPPORT_MAIL")) {
            SQLTableElement tableElementEnvoiMail = new SQLTableElement(elt.getTable().getField("ENVOI_RAPPORT_MAIL"));
            list.add(tableElementEnvoiMail);

        }

        if (elt.getTable().contains("NO_MAILING")) {
            SQLTableElement tableElementNoMailing = new SQLTableElement(elt.getTable().getField("NO_MAILING"));
            list.add(tableElementNoMailing);
        }
        if (elt.getTable().contains("DATE_NAISSANCE")) {
            list.add(new SQLTableElement(elt.getTable().getField("DATE_NAISSANCE")) {
                private final DateToSQLConvertor conv = new DateToSQLConvertor();
                {
                    this.setEditor(new FormatEditor(
                            new FormatGroup(DateFormat.getDateInstance(DateFormat.SHORT), DateFormat.getDateInstance(DateFormat.MEDIUM), DateFormat.getDateInstance(DateFormat.LONG))));
                }

                @Override
                public Object convertEditorValueToModel(Object value, SQLRowValues row) {
                    return this.conv.convert((Date) value);
                }
            });
        }
        final RowValuesTableModel model = new RowValuesTableModel(elt, list, elt.getTable().getField("NOM"), false, defaultRow);

        this.table = new RowValuesTable(model, new File(Configuration.getInstance().getConfDir(), "Table" + File.separator + "Table_Contact.xml"));
        ToolTipManager.sharedInstance().unregisterComponent(this.table);
        ToolTipManager.sharedInstance().unregisterComponent(this.table.getTableHeader());
        this.comp = new RowValuesTableControlPanel(this.table);
        this.add(this.comp, c);

        c.gridy++;
        c.fill = GridBagConstraints.BOTH;
        c.weightx = 1;
        c.weighty = 1;
        this.add(new JScrollPane(this.table), c);
        this.table.setDefaultRenderer(Long.class, new RowValuesTableRenderer());
    }

    public void updateField(String field, int id) {
        this.table.updateField(field, id);
    }

    public void insertFrom(String field, SQLRowValues row) {
        this.table.insertFrom(field, row);
    }

    public void insertFrom(String field, int id) {
        this.table.insertFrom(field, id);
    }

    public RowValuesTableModel getModel() {
        return this.table.getRowValuesTableModel();
    }

    public void setEditable(boolean b) {
        this.comp.setEditable(b);
        this.table.setEditable(b);
    }

}