OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 86 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 86 Rev 181
Line 1... Line 1...
1
package org.openconcerto.modules.extensionbuilder.table;
1
package org.openconcerto.modules.extensionbuilder.table;
2
 
2
 
-
 
3
import java.awt.Color;
-
 
4
import java.awt.Component;
3
import java.awt.GridBagConstraints;
5
import java.awt.GridBagConstraints;
4
import java.awt.GridBagLayout;
6
import java.awt.GridBagLayout;
5
import java.awt.event.ActionEvent;
7
import java.awt.event.ActionEvent;
6
import java.awt.event.ActionListener;
8
import java.awt.event.ActionListener;
7
import java.util.ArrayList;
9
import java.util.ArrayList;
-
 
10
import java.util.Arrays;
8
import java.util.Collections;
11
import java.util.Collections;
9
import java.util.Set;
12
import java.util.Set;
-
 
13
import java.util.Vector;
10
 
14
 
11
import javax.swing.DefaultComboBoxModel;
15
import javax.swing.DefaultComboBoxModel;
-
 
16
import javax.swing.DefaultListCellRenderer;
12
import javax.swing.JComboBox;
17
import javax.swing.JComboBox;
13
import javax.swing.JLabel;
18
import javax.swing.JLabel;
-
 
19
import javax.swing.JList;
14
import javax.swing.JPanel;
20
import javax.swing.JPanel;
15
import javax.swing.JTextField;
21
import javax.swing.JTextField;
16
import javax.swing.event.DocumentEvent;
22
import javax.swing.event.DocumentEvent;
17
import javax.swing.event.DocumentListener;
23
import javax.swing.event.DocumentListener;
18
 
24
 
Line 52... Line 58...
52
        this.setLayout(new GridBagLayout());
58
        this.setLayout(new GridBagLayout());
53
        GridBagConstraints c = new DefaultGridBagConstraints();
59
        GridBagConstraints c = new DefaultGridBagConstraints();
54
        this.add(new JLabel("Type"), c);
60
        this.add(new JLabel("Type"), c);
55
        c.gridx++;
61
        c.gridx++;
56
 
62
 
57
        comboType = new JComboBox(types);
63
        this.comboType = new JComboBox(this.types);
58
        comboType.setOpaque(false);
64
        this.comboType.setOpaque(false);
59
        this.add(comboType, c);
65
        this.add(this.comboType, c);
60
        c.gridx++;
66
        c.gridx++;
61
        this.add(new JLabel("Nom"), c);
67
        this.add(new JLabel("Nom"), c);
62
        c.gridx++;
68
        c.gridx++;
63
        c.weightx = 1;
69
        c.weightx = 1;
64
        fieldName = new JTextField(10);
70
        this.fieldName = new JTextField(10);
65
        this.add(fieldName, c);
71
        this.add(this.fieldName, c);
66
        c.weightx = 0;
72
        c.weightx = 0;
67
        c.gridx++;
73
        c.gridx++;
68
        labelOption = new JLabel("Longeur max");
74
        this.labelOption = new JLabel("Longeur max");
69
        this.add(labelOption, c);
75
        this.add(this.labelOption, c);
70
        c.gridx++;
76
        c.gridx++;
71
        textOption = new JTextField(6);
77
        this.textOption = new JTextField(6);
72
        c.gridx++;
78
        c.gridx++;
73
        this.add(textOption, c);
79
        this.add(this.textOption, c);
74
        comboOption = new JComboBox();
80
        this.comboOption = new JComboBox();
75
 
81
 
76
        this.add(comboOption, c);
82
        this.add(this.comboOption, c);
77
 
83
 
78
        updateFrom(fd);
84
        updateFrom(fd);
79
        comboType.addActionListener(this);
85
        this.comboType.addActionListener(this);
80
        fieldName.getDocument().addDocumentListener(new DocumentListener() {
86
        this.fieldName.getDocument().addDocumentListener(new DocumentListener() {
81
 
87
 
82
            @Override
88
            @Override
83
            public void removeUpdate(DocumentEvent e) {
89
            public void removeUpdate(DocumentEvent e) {
84
                fieldNameModified();
90
                fieldNameModified();
85
 
91
 
Line 95... Line 101...
95
            public void changedUpdate(DocumentEvent e) {
101
            public void changedUpdate(DocumentEvent e) {
96
                fieldNameModified();
102
                fieldNameModified();
97
 
103
 
98
            }
104
            }
99
        });
105
        });
100
        comboOption.addActionListener(this);
106
        this.comboOption.addActionListener(this);
101
        textOption.getDocument().addDocumentListener(new DocumentListener() {
107
        this.textOption.getDocument().addDocumentListener(new DocumentListener() {
102
 
108
 
103
            @Override
109
            @Override
104
            public void removeUpdate(DocumentEvent e) {
110
            public void removeUpdate(DocumentEvent e) {
105
                fieldOptionModified();
111
                fieldOptionModified();
106
 
112
 
Line 123... Line 129...
123
    protected void fieldNameModified() {
129
    protected void fieldNameModified() {
124
        final String text = this.fieldName.getText();
130
        final String text = this.fieldName.getText();
125
        if (text.trim().length() > 0) {
131
        if (text.trim().length() > 0) {
126
            this.fd.setName(text);
132
            this.fd.setName(text);
127
        }
133
        }
128
        extension.setChanged();
134
        this.extension.setChanged();
129
 
135
 
130
    }
136
    }
131
 
137
 
132
    protected void fieldOptionModified() {
138
    protected void fieldOptionModified() {
133
        String text = this.textOption.getText();
139
        String text = this.textOption.getText();
134
        if (text.trim().length() > 0) {
140
        if (text.trim().length() > 0) {
135
 
141
 
136
            switch (this.comboType.getSelectedIndex()) {
142
            switch (this.comboType.getSelectedIndex()) {
137
 
143
 
138
            case TYPE_STRING:
144
            case TYPE_STRING:
139
                fd.setLength(text);
145
                this.fd.setLength(text);
140
                fd.setDefaultValue(null);
146
                this.fd.setDefaultValue(null);
141
                fd.setForeignTable(null);
147
                this.fd.setForeignTable(null);
142
                fd.setLink(null);
148
                this.fd.setLink(null);
143
                break;
149
                break;
144
            case TYPE_INTEGER:
150
            case TYPE_INTEGER:
145
                fd.setLength(null);
151
                this.fd.setLength(null);
146
                try {
152
                try {
147
                    int i = Integer.parseInt(text);
153
                    int i = Integer.parseInt(text);
148
                    fd.setDefaultValue(text);
154
                    this.fd.setDefaultValue(text);
149
                } catch (Exception e) {
155
                } catch (Exception e) {
150
                    fd.setDefaultValue("0");
156
                    this.fd.setDefaultValue("0");
151
                }
157
                }
152
                fd.setForeignTable(null);
158
                this.fd.setForeignTable(null);
153
                fd.setLink(null);
159
                this.fd.setLink(null);
154
            case TYPE_DECIMAL:
160
            case TYPE_DECIMAL:
155
                fd.setLength(null);
161
                this.fd.setLength(null);
156
                try {
162
                try {
157
                    text = text.replace(',', '.');
163
                    text = text.replace(',', '.');
158
                    float i = Float.parseFloat(text);
164
                    float i = Float.parseFloat(text);
159
                    fd.setDefaultValue(text);
165
                    this.fd.setDefaultValue(text);
160
                } catch (Exception e) {
166
                } catch (Exception e) {
161
                    fd.setDefaultValue("0");
167
                    this.fd.setDefaultValue("0");
162
                }
168
                }
163
                fd.setForeignTable(null);
169
                this.fd.setForeignTable(null);
164
                fd.setLink(null);
170
                this.fd.setLink(null);
165
                break;
171
                break;
166
 
172
 
167
            }
173
            }
168
            extension.setChanged();
174
            this.extension.setChanged();
169
 
175
 
170
        }
176
        }
171
    }
177
    }
172
 
178
 
173
    protected void comboOptionModified() {
179
    protected void comboOptionModified() {
174
        final int index = comboOption.getSelectedIndex();
180
        final int index = this.comboOption.getSelectedIndex();
175
 
181
 
176
        switch (this.comboType.getSelectedIndex()) {
182
        switch (this.comboType.getSelectedIndex()) {
177
 
183
 
178
        case TYPE_BOOLEAN:
184
        case TYPE_BOOLEAN:
179
            fd.setLength(null);
185
            this.fd.setLength(null);
180
            if (index == 0) {
186
            if (index == 0) {
181
                fd.setDefaultValue("true");
187
                this.fd.setDefaultValue("true");
182
            } else {
188
            } else {
183
                fd.setDefaultValue("false");
189
                this.fd.setDefaultValue("false");
184
            }
190
            }
185
            fd.setForeignTable(null);
191
            this.fd.setForeignTable(null);
186
            fd.setLink(null);
192
            this.fd.setLink(null);
187
            break;
193
            break;
188
        case TYPE_DATE:
194
        case TYPE_DATE:
189
        case TYPE_TIME:
195
        case TYPE_TIME:
190
        case TYPE_DATE_TIME:
196
        case TYPE_DATE_TIME:
191
            fd.setLength(null);
197
            this.fd.setLength(null);
192
            if (index == 0) {
198
            if (index == 0) {
193
                fd.setDefaultValue(null);
199
                this.fd.setDefaultValue(null);
194
            } else {
200
            } else {
195
                fd.setDefaultValue("now");
201
                this.fd.setDefaultValue("now");
196
            }
202
            }
197
            fd.setForeignTable(null);
203
            this.fd.setForeignTable(null);
198
            fd.setLink(null);
204
            this.fd.setLink(null);
199
            break;
205
            break;
200
        case TYPE_REF:
206
        case TYPE_REF:
201
            fd.setLength(null);
207
            this.fd.setLength(null);
202
            fd.setDefaultValue(null);
208
            this.fd.setDefaultValue(null);
203
            fd.setDefaultValue("now");
-
 
204
            fd.setForeignTable(comboOption.getSelectedItem().toString());
209
            this.fd.setForeignTable(this.comboOption.getSelectedItem().toString());
205
            fd.setLink(null);
210
            this.fd.setLink(null);
206
            break;
211
            break;
207
 
212
 
208
        }
213
        }
209
        extension.setChanged();
214
        this.extension.setChanged();
210
 
215
 
211
    }
216
    }
212
 
217
 
213
    private void updateFrom(FieldDescriptor fd) {
218
    private void updateFrom(FieldDescriptor fd) {
214
        if (fd.getType().equals("string")) {
219
        if (fd.getType().equals("string")) {
215
            comboType.setSelectedIndex(TYPE_STRING);
220
            this.comboType.setSelectedIndex(TYPE_STRING);
216
            labelOption.setText("Longueur max");
221
            this.labelOption.setText("Longueur max");
217
            textOption.setVisible(true);
222
            this.textOption.setVisible(true);
218
            textOption.setText(fd.getLength());
223
            this.textOption.setText(fd.getLength());
219
            comboOption.setVisible(false);
224
            this.comboOption.setVisible(false);
220
        } else if (fd.getType().equals("integer")) {
225
        } else if (fd.getType().equals("integer")) {
221
            comboType.setSelectedIndex(TYPE_INTEGER);
226
            this.comboType.setSelectedIndex(TYPE_INTEGER);
222
            labelOption.setText("Valeur par défaut");
227
            this.labelOption.setText("Valeur par défaut");
223
            textOption.setVisible(true);
228
            this.textOption.setVisible(true);
224
            textOption.setText(fd.getDefaultValue());
229
            this.textOption.setText(fd.getDefaultValue());
225
            comboOption.setVisible(false);
230
            this.comboOption.setVisible(false);
226
        } else if (fd.getType().equals("decimal")) {
231
        } else if (fd.getType().equals("decimal")) {
227
            comboType.setSelectedIndex(TYPE_DECIMAL);
232
            this.comboType.setSelectedIndex(TYPE_DECIMAL);
228
            labelOption.setText("Valeur par défaut");
233
            this.labelOption.setText("Valeur par défaut");
229
            textOption.setVisible(true);
234
            this.textOption.setVisible(true);
230
            textOption.setText(fd.getDefaultValue());
235
            this.textOption.setText(fd.getDefaultValue());
231
            comboOption.setVisible(false);
236
            this.comboOption.setVisible(false);
232
        } else if (fd.getType().equals("boolean")) {
237
        } else if (fd.getType().equals("boolean")) {
233
            comboType.setSelectedIndex(TYPE_BOOLEAN);
238
            this.comboType.setSelectedIndex(TYPE_BOOLEAN);
234
            labelOption.setText("Valeur par défaut");
239
            this.labelOption.setText("Valeur par défaut");
235
            textOption.setVisible(false);
240
            this.textOption.setVisible(false);
236
            comboOption.setVisible(true);
241
            this.comboOption.setVisible(true);
237
            comboOption.setModel(new DefaultComboBoxModel(vBoolean));
242
            this.comboOption.setModel(new DefaultComboBoxModel(vBoolean));
238
            if (fd.getDefaultValue().equals("true")) {
243
            if (fd.getDefaultValue().equals("true")) {
239
                comboOption.setSelectedIndex(0);
244
                this.comboOption.setSelectedIndex(0);
240
            } else {
245
            } else {
241
                comboOption.setSelectedIndex(1);
246
                this.comboOption.setSelectedIndex(1);
242
            }
247
            }
243
 
248
 
244
        } else if (fd.getType().equals("date")) {
249
        } else if (fd.getType().equals("date")) {
245
            comboType.setSelectedIndex(TYPE_DATE);
250
            this.comboType.setSelectedIndex(TYPE_DATE);
246
            labelOption.setText("Valeur par défaut");
251
            this.labelOption.setText("Valeur par défaut");
247
            textOption.setVisible(false);
252
            this.textOption.setVisible(false);
248
            comboOption.setVisible(true);
253
            this.comboOption.setVisible(true);
249
            comboOption.setModel(new DefaultComboBoxModel(vDate));
254
            this.comboOption.setModel(new DefaultComboBoxModel(vDate));
250
            if (fd.getDefaultValue() == null || !fd.getDefaultValue().equals("now")) {
255
            if (fd.getDefaultValue() == null || !fd.getDefaultValue().equals("now")) {
251
                comboOption.setSelectedIndex(0);
256
                this.comboOption.setSelectedIndex(0);
252
            } else {
257
            } else {
253
                comboOption.setSelectedIndex(1);
258
                this.comboOption.setSelectedIndex(1);
254
            }
259
            }
255
        } else if (fd.getType().equals("time")) {
260
        } else if (fd.getType().equals("time")) {
256
            comboType.setSelectedIndex(TYPE_TIME);
261
            this.comboType.setSelectedIndex(TYPE_TIME);
257
            labelOption.setText("Valeur par défaut");
262
            this.labelOption.setText("Valeur par défaut");
258
            textOption.setVisible(false);
263
            this.textOption.setVisible(false);
259
            comboOption.setVisible(true);
264
            this.comboOption.setVisible(true);
260
            comboOption.setModel(new DefaultComboBoxModel(vTime));
265
            this.comboOption.setModel(new DefaultComboBoxModel(vTime));
261
            if (fd.getDefaultValue() == null || !fd.getDefaultValue().equals("now")) {
266
            if (fd.getDefaultValue() == null || !fd.getDefaultValue().equals("now")) {
262
                comboOption.setSelectedIndex(0);
267
                comboOption.setSelectedIndex(0);
263
            } else {
268
            } else {
264
                comboOption.setSelectedIndex(1);
269
                comboOption.setSelectedIndex(1);
265
            }
270
            }
Line 277... Line 282...
277
        } else if (fd.getType().equals("ref")) {
282
        } else if (fd.getType().equals("ref")) {
278
            comboType.setSelectedIndex(TYPE_REF);
283
            comboType.setSelectedIndex(TYPE_REF);
279
            labelOption.setText("Table");
284
            labelOption.setText("Table");
280
            textOption.setVisible(false);
285
            textOption.setVisible(false);
281
            comboOption.setVisible(true);
286
            comboOption.setVisible(true);
-
 
287
            final Vector<String> tables = new Vector<String>();
282
            comboOption.setModel(new DefaultComboBoxModel(getTables()));
288
            tables.addAll(Arrays.asList(getTables()));
283
            String tableName = fd.getForeignTable();
289
            String tableName = fd.getForeignTable();
-
 
290
            if (!tables.contains(tableName)) {
-
 
291
                tables.add(tableName);
-
 
292
            }
-
 
293
            comboOption.setModel(new DefaultComboBoxModel<String>(tables));
-
 
294
            comboOption.setRenderer(new DefaultListCellRenderer() {
-
 
295
                @Override
-
 
296
                public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
-
 
297
                    if (value.toString().trim().isEmpty()) {
-
 
298
                        value = "!! non renseignée !!";
-
 
299
                    }
-
 
300
                    DefaultListCellRenderer r = (DefaultListCellRenderer) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
-
 
301
                    if (!tables.contains(value)) {
-
 
302
                        r.setForeground(Color.RED);
-
 
303
                    }
-
 
304
                    return r;
-
 
305
                }
-
 
306
 
-
 
307
            });
284
            comboOption.setSelectedItem(tableName);
308
            comboOption.setSelectedItem(tableName);
285
        } else {
309
        } else {
286
            throw new IllegalArgumentException("Unknow type " + fd.getType());
310
            throw new IllegalArgumentException("Unknow type " + fd.getType());
287
        }
311
        }
288
        fieldName.setText(fd.getName().trim());
312
        fieldName.setText(fd.getName().trim());