OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 177 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
17 ilm 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.sql.view.list;
15
 
149 ilm 16
import org.openconcerto.sql.TM;
182 ilm 17
import org.openconcerto.sql.model.SQLField;
17 ilm 18
import org.openconcerto.sql.model.SQLRowValues;
19
import org.openconcerto.sql.view.IListFrame;
20
import org.openconcerto.ui.DefaultGridBagConstraints;
21
import org.openconcerto.ui.JComponentUtils;
22
 
23
import java.awt.GridBagConstraints;
24
import java.awt.GridBagLayout;
25
import java.awt.event.ActionEvent;
26
import java.awt.event.ActionListener;
27
import java.util.List;
28
 
29
import javax.swing.ImageIcon;
30
import javax.swing.JButton;
31
import javax.swing.JPanel;
32
import javax.swing.event.ListSelectionEvent;
33
import javax.swing.event.ListSelectionListener;
34
import javax.swing.table.TableCellEditor;
35
 
67 ilm 36
public class RowValuesTableControlPanel extends JPanel {
17 ilm 37
 
38
    private final RowValuesTable table;
39
    private final RowValuesTableModel model;
67 ilm 40
    private JButton buttonBas, buttonHaut, buttonAjouter, buttonInserer, buttonClone, buttonSuppr;
17 ilm 41
 
42
    public RowValuesTableControlPanel(final RowValuesTable table) {
43
        this(table, null);
44
    }
45
 
46
    public void setEditable(boolean b) {
47
        this.buttonAjouter.setEnabled(b);
48
        this.buttonHaut.setEnabled(b);
49
        this.buttonInserer.setEnabled(b);
50
        this.buttonClone.setEnabled(b);
51
        this.buttonBas.setEnabled(b);
52
        this.buttonSuppr.setEnabled(b);
53
    }
54
 
55
    public RowValuesTableControlPanel(final RowValuesTable table, final List<JButton> l) {
56
        super(new GridBagLayout());
67 ilm 57
        if (table == null) {
58
            throw new IllegalArgumentException("RowValuesTable null");
59
        }
17 ilm 60
        this.model = table.getRowValuesTableModel();
67 ilm 61
        if (this.model == null) {
17 ilm 62
            throw new IllegalArgumentException("RowValuesTableModel null");
67 ilm 63
        }
17 ilm 64
        this.table = table;
65
 
66
        this.setOpaque(false);
67
 
68
        GridBagConstraints c = new DefaultGridBagConstraints();
69
 
70
        this.buttonHaut = new JButton(new ImageIcon(IListFrame.class.getResource("fleche_haut.png")));
71
        this.buttonHaut.addActionListener(new ActionListener() {
72
            public void actionPerformed(ActionEvent event) {
73
                deplacerDe(-1);
74
            }
75
        });
76
 
77
        this.add(this.buttonHaut, c);
78
        this.buttonHaut.setEnabled(false);
79
 
80
        this.buttonBas = new JButton(new ImageIcon(IListFrame.class.getResource("fleche_bas.png")));
81
        this.buttonBas.addActionListener(new ActionListener() {
82
            public void actionPerformed(ActionEvent event) {
83
                deplacerDe(1);
84
            }
85
        });
86
        c.gridx++;
87
        this.add(this.buttonBas, c);
88
        this.buttonBas.setEnabled(false);
89
 
73 ilm 90
        this.buttonAjouter = new JButton(TM.tr("addNewLine"));
17 ilm 91
        this.buttonAjouter.addActionListener(new ActionListener() {
92
            public void actionPerformed(ActionEvent event) {
93
                RowValuesTableControlPanel.this.model.addNewRowAt(table.getRowCount());
94
            }
95
        });
96
        c.gridx++;
97
        JComponentUtils.setMinimumWidth(this.buttonAjouter, 88);
98
        this.add(this.buttonAjouter, c);
99
 
73 ilm 100
        this.buttonInserer = new JButton(TM.tr("insertNewLine"));
17 ilm 101
        this.buttonInserer.addActionListener(new ActionListener() {
102
            public void actionPerformed(ActionEvent event) {
156 ilm 103
                int index = table.getSelectedRow();
104
                if (index < 0 || index > table.getRowCount()) {
105
                    index = table.getRowCount();
106
                }
107
                RowValuesTableControlPanel.this.model.addNewRowAt(index);
17 ilm 108
            }
109
        });
110
        this.buttonInserer.setEnabled(false);
111
        c.gridx++;
112
        JComponentUtils.setMinimumWidth(this.buttonInserer, 85);
113
        this.add(this.buttonInserer, c);
114
 
73 ilm 115
        this.buttonClone = new JButton(TM.tr("duplicateLine"));
17 ilm 116
        this.buttonClone.addActionListener(new ActionListener() {
117
            public void actionPerformed(ActionEvent event) {
118
                cloneLine(table.getSelectedRow());
119
            }
120
        });
121
        this.buttonClone.setEnabled(false);
122
        c.gridx++;
123
        JComponentUtils.setMinimumWidth(this.buttonClone, 95);
124
        this.add(this.buttonClone, c);
125
 
73 ilm 126
        this.buttonSuppr = new JButton(TM.tr("deleteLine"));
17 ilm 127
        this.buttonSuppr.addActionListener(new ActionListener() {
128
 
129
            public void actionPerformed(ActionEvent event) {
130
                final TableCellEditor cellEditor = RowValuesTableControlPanel.this.table.getCellEditor();
131
                if (cellEditor != null) {
132
                    cellEditor.cancelCellEditing();
133
                }
182 ilm 134
                SQLField validationField = RowValuesTableControlPanel.this.model.getValidationField();
135
                if (validationField != null) {
136
                    boolean canDelete = true;
137
                    for (int i : table.getSelectedRows()) {
138
                        SQLRowValues rowVals = RowValuesTableControlPanel.this.model.getRowValuesAt(i);
139
                        canDelete &= (!rowVals.contains(validationField.getName()) || rowVals.getObject(validationField.getName()) == null || !rowVals.getBoolean(validationField.getName()));
140
                    }
141
                    if (canDelete) {
142
                        RowValuesTableControlPanel.this.model.removeRowsAt(table.getSelectedRows());
143
                    }
144
                    // MAYBE show popup if can't delete
145
                } else {
146
                    RowValuesTableControlPanel.this.model.removeRowsAt(table.getSelectedRows());
147
                }
67 ilm 148
                table.clearSelection();
17 ilm 149
            }
150
        });
151
        this.buttonSuppr.setEnabled(false);
152
        JComponentUtils.setMinimumWidth(this.buttonSuppr, 95);
153
        c.gridx++;
154
        this.add(this.buttonSuppr, c);
155
 
156
        if (l != null) {
157
            for (JButton button : l) {
158
                c.gridx++;
159
                button.setEnabled(false);
160
                this.add(button, c);
161
            }
162
        }
163
 
164
        table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
165
 
166
            public void valueChanged(ListSelectionEvent event) {
167
                boolean b = table.getSelectedRow() >= 0;
168
 
169
                RowValuesTableControlPanel.this.buttonClone.setEnabled(b);
170
                RowValuesTableControlPanel.this.buttonSuppr.setEnabled(b);
171
                RowValuesTableControlPanel.this.buttonInserer.setEnabled(b);
172
                RowValuesTableControlPanel.this.buttonHaut.setEnabled(b);
173
                RowValuesTableControlPanel.this.buttonBas.setEnabled(b);
174
 
175
                if (l != null) {
176
                    for (JButton button : l) {
177
                        button.setEnabled(b);
178
                    }
179
                }
180
            }
181
        });
182
 
183
        c.gridx++;
184
        c.weightx = 1;
185
        final JPanel panelStuff = new JPanel();
186
        panelStuff.setOpaque(false);
187
        this.add(panelStuff, c);
188
    }
189
 
190
    public void deplacerDe(int inc) {
191
        final int rowIndex = this.table.getSelectedRow();
192
        int dest = this.model.moveBy(rowIndex, inc);
193
        this.table.getSelectionModel().setSelectionInterval(dest, dest);
194
    }
195
 
196
    private void cloneLine(int row) {
197
        if (row < 0) {
198
            System.err.println("RowValuesTableControlPanel.cloneLine() wrong selected line, index = " + row);
199
            Thread.dumpStack();
200
            return;
201
        }
202
        SQLRowValues rowVals = this.model.getRowValuesAt(row);
203
 
93 ilm 204
        SQLRowValues rowValsBis = rowVals.deepCopy();
17 ilm 205
        rowValsBis.clearPrimaryKeys();
206
        rowValsBis.put(rowValsBis.getTable().getOrderField().getName(), null);
207
 
208
        this.model.getSQLElement().clearPrivateFields(rowValsBis);
209
 
210
        for (String elt : this.table.getClearCloneTableElement()) {
211
            if (rowValsBis.getTable().getFieldsName().contains(elt)) {
177 ilm 212
                if (rowValsBis.getTable().getField(elt).isKey()) {
213
                    rowValsBis.putEmptyLink(elt);
214
                } else {
215
                    rowValsBis.put(elt, this.model.getDefaultRowValues().getObject(elt));
216
                }
17 ilm 217
            }
218
        }
219
 
220
        this.model.addRow(rowValsBis);
221
    }
222
 
149 ilm 223
    public void setButtonAjouterEnabled(boolean b) {
224
        this.buttonAjouter.setEnabled(b);
225
    }
226
 
17 ilm 227
    public void setVisibleButtonClone(boolean b) {
228
        this.buttonClone.setVisible(b);
229
    }
230
 
231
    public void setVisibleButtonAjouter(boolean b) {
232
        this.buttonAjouter.setVisible(b);
233
    }
234
 
235
    public void setVisibleButtonInserer(boolean b) {
236
        this.buttonInserer.setVisible(b);
237
    }
238
 
239
    public void setVisibleButtonHaut(boolean b) {
240
        this.buttonHaut.setVisible(b);
241
    }
242
 
243
    public void setVisibleButtonBas(boolean b) {
244
        this.buttonBas.setVisible(b);
245
    }
246
 
247
    public void setVisibleButtonSuppr(boolean b) {
248
        this.buttonSuppr.setVisible(b);
249
    }
250
}