OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 156 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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