OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
144 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.supplychain.order.table;
15
 
16
import org.openconcerto.erp.core.sales.order.ui.TypeFactureCommandeCellRenderer;
17
import org.openconcerto.erp.core.sales.order.ui.TypeFactureCommandeTableCellEditor;
18
import org.openconcerto.sql.Configuration;
19
import org.openconcerto.sql.element.SQLComponent;
20
import org.openconcerto.sql.element.SQLElement;
21
import org.openconcerto.sql.model.SQLRow;
22
import org.openconcerto.sql.model.SQLRowValues;
180 ilm 23
import org.openconcerto.sql.model.SQLTable;
144 ilm 24
import org.openconcerto.sql.model.UndefinedRowValuesCache;
25
import org.openconcerto.sql.model.Where;
26
import org.openconcerto.sql.request.ComboSQLRequest;
27
import org.openconcerto.sql.sqlobject.ElementComboBox;
28
import org.openconcerto.sql.view.list.RowValuesTable;
29
import org.openconcerto.sql.view.list.RowValuesTableControlPanel;
30
import org.openconcerto.sql.view.list.RowValuesTableModel;
31
import org.openconcerto.sql.view.list.RowValuesTableRenderer;
32
import org.openconcerto.sql.view.list.SQLTableElement;
33
import org.openconcerto.ui.DefaultGridBagConstraints;
34
import org.openconcerto.ui.table.PercentTableCellRenderer;
35
import org.openconcerto.ui.table.TimestampTableCellEditor;
180 ilm 36
import org.openconcerto.ui.table.XTableColumnModel;
144 ilm 37
 
38
import java.awt.GridBagConstraints;
39
import java.awt.GridBagLayout;
40
import java.awt.event.ActionEvent;
41
import java.awt.event.ActionListener;
42
import java.math.BigDecimal;
43
import java.sql.Date;
44
import java.sql.Timestamp;
180 ilm 45
import java.util.HashMap;
144 ilm 46
import java.util.List;
180 ilm 47
import java.util.Map;
144 ilm 48
import java.util.Vector;
49
 
50
import javax.swing.JButton;
51
import javax.swing.JLabel;
52
import javax.swing.JPanel;
53
import javax.swing.JScrollPane;
54
import javax.swing.ScrollPaneConstants;
55
import javax.swing.ToolTipManager;
56
 
57
public class FacturationCommandeTable extends JPanel {
58
    private RowValuesTable table;
59
    private RowValuesTableModel model;
60
    private SQLComponent comp;
61
 
180 ilm 62
    public static Map<String, Boolean> map = new HashMap<String, Boolean>();
63
 
64
    protected Map<String, Boolean> getCustomVisibilityMap() {
65
        return map;
66
    }
67
 
144 ilm 68
    public FacturationCommandeTable(SQLComponent comp) {
69
        this.comp = comp;
70
        init();
71
        uiInit();
72
    }
73
 
74
    protected void init() {
75
 
76
        final SQLElement e = getSQLElement();
77
 
78
        final List<SQLTableElement> list = new Vector<SQLTableElement>();
79
 
80
        final SQLTableElement tableElementNom = new SQLTableElement(e.getTable().getField("NOM"));
81
        list.add(tableElementNom);
82
 
83
        final SQLTableElement tableElementTF = new SQLTableElement(e.getTable().getField("TYPE_FACTURE"), Integer.class, new TypeFactureCommandeTableCellEditor());
84
        tableElementTF.setRenderer(new TypeFactureCommandeCellRenderer());
85
        list.add(tableElementTF);
86
 
87
        final SQLTableElement pourcent = new SQLTableElement(e.getTable().getField("POURCENT"), BigDecimal.class);
88
        pourcent.setRenderer(new PercentTableCellRenderer());
89
        list.add(pourcent);
90
 
91
        final SQLTableElement type = new SQLTableElement(e.getTable().getField("ID_TYPE_REGLEMENT"));
92
        list.add(type);
93
 
94
        final SQLTableElement ajours = new SQLTableElement(e.getTable().getField("AJOURS"));
95
        list.add(ajours);
96
        final SQLTableElement finM = new SQLTableElement(e.getTable().getField("FIN_MOIS"));
97
        list.add(finM);
98
 
99
        final SQLTableElement comptant = new SQLTableElement(e.getTable().getField("COMPTANT"));
100
        list.add(comptant);
101
 
180 ilm 102
        // TODO fix return value of timestamp if not show hour return date object
144 ilm 103
        final SQLTableElement date = new SQLTableElement(e.getTable().getField("DATE_PREVISIONNELLE"), Date.class, new TimestampTableCellEditor(false) {
104
            @Override
105
            public Object getCellEditorValue() {
106
 
107
                Object o = super.getCellEditorValue();
108
                Date d = null;
109
                if (o != null) {
110
                    d = new Date(((Timestamp) o).getTime());
111
                }
112
                return d;
113
            }
114
        });
115
        list.add(date);
116
 
117
        SQLRowValues defautRow = new SQLRowValues(UndefinedRowValuesCache.getInstance().getDefaultRowValues(e.getTable()));
118
        this.model = new RowValuesTableModel(e, list, e.getTable().getField("NOM"), false, defautRow);
119
 
120
        this.table = new RowValuesTable(this.model, null, true);
121
        ToolTipManager.sharedInstance().unregisterComponent(this.table);
122
        ToolTipManager.sharedInstance().unregisterComponent(this.table.getTableHeader());
123
 
180 ilm 124
        Map<String, Boolean> mapCustom = getCustomVisibilityMap();
125
        if (mapCustom != null) {
126
            for (String string : mapCustom.keySet()) {
127
                setColumnVisible(model.getColumnForField(string), mapCustom.get(string));
128
            }
129
        }
144 ilm 130
 
180 ilm 131
    }
144 ilm 132
 
180 ilm 133
    protected void setColumnVisible(int col, boolean visible) {
134
        if (col >= 0) {
135
            XTableColumnModel columnModel = this.table.getColumnModel();
136
            columnModel.setColumnVisible(columnModel.getColumnByModelIndex(col), visible);
137
        }
144 ilm 138
    }
139
 
140
    /**
141
     *
142
     */
143
    protected void uiInit() {
144
        // Ui init
145
        this.setLayout(new GridBagLayout());
146
        this.setOpaque(false);
147
        final GridBagConstraints c = new DefaultGridBagConstraints();
148
        c.weightx = 1;
149
 
150
        // Ajout catégorie
151
        c.gridwidth = 1;
152
        c.weightx = 0;
153
        c.gridx = 0;
154
        this.add(new JLabel("Ajouter un terme"), c);
155
 
156
        final ElementComboBox boxCat = new ElementComboBox();
180 ilm 157
        final SQLElement element = getSQLElement();
144 ilm 158
        ComboSQLRequest req = element.getComboRequest(true);
159
        req.setWhere(new Where(element.getTable().getField("CHOICE"), "=", Boolean.TRUE));
160
        boxCat.init(element, req);
161
 
162
        c.gridx++;
163
        this.add(boxCat, c);
164
 
165
        c.fill = GridBagConstraints.NONE;
166
        c.gridx++;
167
        JButton buttonAjouter = new JButton("Ajouter");
168
        buttonAjouter.setOpaque(false);
169
        this.add(buttonAjouter, c);
170
 
171
        c.gridwidth = GridBagConstraints.REMAINDER;
172
        // Listeners
173
        buttonAjouter.addActionListener(new ActionListener() {
174
 
175
            @Override
176
            public void actionPerformed(ActionEvent e) {
177
 
178
                SQLRow rowCat = boxCat.getSelectedRow();
179
                if (rowCat == null || rowCat.isUndefined()) {
180
                    return;
181
                }
182
 
180 ilm 183
                final SQLTable table2 = getSQLElement().getTable();
184
                SQLRowValues rowVals = new SQLRowValues(table2);
185
                if (table2.getName().equals("FACTURATION_COMMANDE_CLIENT")) {
186
                    if (comp != null && comp.getSelectedID() > 1) {
187
                        rowVals.put("ID_COMMANDE_CLIENT", comp.getSelectedID());
188
                    }
144 ilm 189
                }
190
                rowVals.put("NOM", rowCat.getObject("NOM"));
191
                rowVals.put("ID_TYPE_REGLEMENT", rowCat.getObject("ID_TYPE_REGLEMENT"));
192
                rowVals.put("AJOURS", rowCat.getObject("AJOURS"));
193
                rowVals.put("COMPTANT", rowCat.getObject("COMPTANT"));
194
                rowVals.put("FIN_MOIS", rowCat.getObject("FIN_MOIS"));
195
                rowVals.put("MONTANT", rowCat.getObject("MONTANT"));
196
                rowVals.put("POURCENT", rowCat.getObject("POURCENT"));
197
                rowVals.put("TYPE_FACTURE", rowCat.getObject("TYPE_FACTURE"));
198
                getModel().addRow(rowVals);
199
            }
200
        });
201
 
202
        c.gridy++;
203
        c.gridx = 0;
204
        final JPanel control = new RowValuesTableControlPanel(this.table);
205
        control.setOpaque(false);
206
        this.add(control, c);
207
 
208
        c.gridy++;
209
        c.fill = GridBagConstraints.BOTH;
210
        c.weighty = 1;
211
        c.weightx = 1;
212
        JScrollPane comp = new JScrollPane(this.table);
213
        comp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
214
        this.add(comp, c);
215
        this.table.setDefaultRenderer(Long.class, new RowValuesTableRenderer());
216
    }
217
 
218
    public SQLElement getSQLElement() {
219
        return Configuration.getInstance().getDirectory().getElement("FACTURATION_COMMANDE_CLIENT");
220
    }
221
 
222
    public void updateField(String field, int id) {
223
        this.table.updateField(field, id);
224
    }
225
 
226
    public RowValuesTable getRowValuesTable() {
227
        return this.table;
228
    }
229
 
230
    public void insertFrom(String field, int id) {
231
        this.table.insertFrom(field, id);
232
    }
233
 
234
    public RowValuesTableModel getModel() {
235
        return this.table.getRowValuesTableModel();
236
    }
237
 
238
    public void refreshTable() {
239
        this.table.repaint();
240
    }
241
}