OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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