OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
74 ilm 1
package org.openconcerto.modules.extensionbuilder.table;
2
 
181 ilm 3
import java.awt.Color;
4
import java.awt.Component;
74 ilm 5
import java.awt.GridBagConstraints;
6
import java.awt.GridBagLayout;
7
import java.awt.event.ActionEvent;
8
import java.awt.event.ActionListener;
9
import java.util.ArrayList;
181 ilm 10
import java.util.Arrays;
74 ilm 11
import java.util.Collections;
12
import java.util.Set;
181 ilm 13
import java.util.Vector;
74 ilm 14
 
15
import javax.swing.DefaultComboBoxModel;
181 ilm 16
import javax.swing.DefaultListCellRenderer;
74 ilm 17
import javax.swing.JComboBox;
18
import javax.swing.JLabel;
181 ilm 19
import javax.swing.JList;
74 ilm 20
import javax.swing.JPanel;
21
import javax.swing.JTextField;
22
import javax.swing.event.DocumentEvent;
23
import javax.swing.event.DocumentListener;
24
 
25
import org.openconcerto.erp.config.ComptaPropsConfiguration;
86 ilm 26
import org.openconcerto.modules.extensionbuilder.Extension;
74 ilm 27
import org.openconcerto.sql.model.SQLTable;
28
import org.openconcerto.ui.DefaultGridBagConstraints;
29
 
86 ilm 30
public class FieldDescriptorEditor extends JPanel implements ActionListener {
74 ilm 31
    final JComboBox comboType;
86 ilm 32
    private JTextField fieldName;
74 ilm 33
    private JLabel labelOption;
34
    private JTextField textOption;
35
    private String[] types = { "Texte", "Nombre entier", "Nombre décimal", "Booléen", "Date", "Heure", "Date et heure", "Référence" };
36
    private String[] xmltypes = { "string", "integer", "decimal", "boolean", "date", "time", "dateAndTime", "ref" };
37
    private JComboBox comboOption;
38
 
39
    private static final String[] vBoolean = { "oui", "non" };
40
    private static final String[] vDate = { "vide", "jour actuel" };
41
    private static final String[] vTime = { "vide", "heure actuelle" };
42
    private static final String[] vDateTime = { "vide", "date actuelle" };
86 ilm 43
    private static final int TYPE_STRING = 0;
44
    private static final int TYPE_INTEGER = 1;
45
    private static final int TYPE_DECIMAL = 2;
46
    private static final int TYPE_BOOLEAN = 3;
47
    private static final int TYPE_DATE = 4;
48
    private static final int TYPE_TIME = 5;
49
    private static final int TYPE_DATE_TIME = 6;
50
    private static final int TYPE_REF = 7;
51
 
74 ilm 52
    FieldDescriptor fd;
86 ilm 53
    private Extension extension;
74 ilm 54
 
86 ilm 55
    FieldDescriptorEditor(Extension extension, FieldDescriptor fd) {
56
        this.extension = extension;
74 ilm 57
        this.fd = fd;
58
        this.setLayout(new GridBagLayout());
59
        GridBagConstraints c = new DefaultGridBagConstraints();
60
        this.add(new JLabel("Type"), c);
61
        c.gridx++;
62
 
181 ilm 63
        this.comboType = new JComboBox(this.types);
64
        this.comboType.setOpaque(false);
65
        this.add(this.comboType, c);
74 ilm 66
        c.gridx++;
67
        this.add(new JLabel("Nom"), c);
68
        c.gridx++;
69
        c.weightx = 1;
181 ilm 70
        this.fieldName = new JTextField(10);
71
        this.add(this.fieldName, c);
74 ilm 72
        c.weightx = 0;
73
        c.gridx++;
181 ilm 74
        this.labelOption = new JLabel("Longeur max");
75
        this.add(this.labelOption, c);
74 ilm 76
        c.gridx++;
181 ilm 77
        this.textOption = new JTextField(6);
74 ilm 78
        c.gridx++;
181 ilm 79
        this.add(this.textOption, c);
80
        this.comboOption = new JComboBox();
74 ilm 81
 
181 ilm 82
        this.add(this.comboOption, c);
74 ilm 83
 
84
        updateFrom(fd);
181 ilm 85
        this.comboType.addActionListener(this);
86
        this.fieldName.getDocument().addDocumentListener(new DocumentListener() {
86 ilm 87
 
88
            @Override
89
            public void removeUpdate(DocumentEvent e) {
90
                fieldNameModified();
91
 
92
            }
93
 
94
            @Override
95
            public void insertUpdate(DocumentEvent e) {
96
                fieldNameModified();
97
 
98
            }
99
 
100
            @Override
101
            public void changedUpdate(DocumentEvent e) {
102
                fieldNameModified();
103
 
104
            }
105
        });
181 ilm 106
        this.comboOption.addActionListener(this);
107
        this.textOption.getDocument().addDocumentListener(new DocumentListener() {
86 ilm 108
 
109
            @Override
110
            public void removeUpdate(DocumentEvent e) {
111
                fieldOptionModified();
112
 
113
            }
114
 
115
            @Override
116
            public void insertUpdate(DocumentEvent e) {
117
                fieldOptionModified();
118
 
119
            }
120
 
121
            @Override
122
            public void changedUpdate(DocumentEvent e) {
123
                fieldOptionModified();
124
 
125
            }
126
        });
74 ilm 127
    }
128
 
86 ilm 129
    protected void fieldNameModified() {
130
        final String text = this.fieldName.getText();
131
        if (text.trim().length() > 0) {
132
            this.fd.setName(text);
133
        }
181 ilm 134
        this.extension.setChanged();
86 ilm 135
 
136
    }
137
 
138
    protected void fieldOptionModified() {
139
        String text = this.textOption.getText();
140
        if (text.trim().length() > 0) {
141
 
142
            switch (this.comboType.getSelectedIndex()) {
143
 
144
            case TYPE_STRING:
181 ilm 145
                this.fd.setLength(text);
146
                this.fd.setDefaultValue(null);
147
                this.fd.setForeignTable(null);
148
                this.fd.setLink(null);
86 ilm 149
                break;
150
            case TYPE_INTEGER:
181 ilm 151
                this.fd.setLength(null);
86 ilm 152
                try {
153
                    int i = Integer.parseInt(text);
181 ilm 154
                    this.fd.setDefaultValue(text);
86 ilm 155
                } catch (Exception e) {
181 ilm 156
                    this.fd.setDefaultValue("0");
86 ilm 157
                }
181 ilm 158
                this.fd.setForeignTable(null);
159
                this.fd.setLink(null);
86 ilm 160
            case TYPE_DECIMAL:
181 ilm 161
                this.fd.setLength(null);
86 ilm 162
                try {
163
                    text = text.replace(',', '.');
164
                    float i = Float.parseFloat(text);
181 ilm 165
                    this.fd.setDefaultValue(text);
86 ilm 166
                } catch (Exception e) {
181 ilm 167
                    this.fd.setDefaultValue("0");
86 ilm 168
                }
181 ilm 169
                this.fd.setForeignTable(null);
170
                this.fd.setLink(null);
86 ilm 171
                break;
172
 
173
            }
181 ilm 174
            this.extension.setChanged();
86 ilm 175
 
176
        }
177
    }
178
 
179
    protected void comboOptionModified() {
181 ilm 180
        final int index = this.comboOption.getSelectedIndex();
86 ilm 181
 
182
        switch (this.comboType.getSelectedIndex()) {
183
 
184
        case TYPE_BOOLEAN:
181 ilm 185
            this.fd.setLength(null);
86 ilm 186
            if (index == 0) {
181 ilm 187
                this.fd.setDefaultValue("true");
86 ilm 188
            } else {
181 ilm 189
                this.fd.setDefaultValue("false");
86 ilm 190
            }
181 ilm 191
            this.fd.setForeignTable(null);
192
            this.fd.setLink(null);
86 ilm 193
            break;
194
        case TYPE_DATE:
195
        case TYPE_TIME:
196
        case TYPE_DATE_TIME:
181 ilm 197
            this.fd.setLength(null);
86 ilm 198
            if (index == 0) {
181 ilm 199
                this.fd.setDefaultValue(null);
86 ilm 200
            } else {
181 ilm 201
                this.fd.setDefaultValue("now");
86 ilm 202
            }
181 ilm 203
            this.fd.setForeignTable(null);
204
            this.fd.setLink(null);
86 ilm 205
            break;
206
        case TYPE_REF:
181 ilm 207
            this.fd.setLength(null);
208
            this.fd.setDefaultValue(null);
209
            this.fd.setForeignTable(this.comboOption.getSelectedItem().toString());
210
            this.fd.setLink(null);
86 ilm 211
            break;
212
 
213
        }
181 ilm 214
        this.extension.setChanged();
86 ilm 215
 
216
    }
217
 
74 ilm 218
    private void updateFrom(FieldDescriptor fd) {
219
        if (fd.getType().equals("string")) {
181 ilm 220
            this.comboType.setSelectedIndex(TYPE_STRING);
221
            this.labelOption.setText("Longueur max");
222
            this.textOption.setVisible(true);
223
            this.textOption.setText(fd.getLength());
224
            this.comboOption.setVisible(false);
74 ilm 225
        } else if (fd.getType().equals("integer")) {
181 ilm 226
            this.comboType.setSelectedIndex(TYPE_INTEGER);
227
            this.labelOption.setText("Valeur par défaut");
228
            this.textOption.setVisible(true);
229
            this.textOption.setText(fd.getDefaultValue());
230
            this.comboOption.setVisible(false);
74 ilm 231
        } else if (fd.getType().equals("decimal")) {
181 ilm 232
            this.comboType.setSelectedIndex(TYPE_DECIMAL);
233
            this.labelOption.setText("Valeur par défaut");
234
            this.textOption.setVisible(true);
235
            this.textOption.setText(fd.getDefaultValue());
236
            this.comboOption.setVisible(false);
74 ilm 237
        } else if (fd.getType().equals("boolean")) {
181 ilm 238
            this.comboType.setSelectedIndex(TYPE_BOOLEAN);
239
            this.labelOption.setText("Valeur par défaut");
240
            this.textOption.setVisible(false);
241
            this.comboOption.setVisible(true);
242
            this.comboOption.setModel(new DefaultComboBoxModel(vBoolean));
74 ilm 243
            if (fd.getDefaultValue().equals("true")) {
181 ilm 244
                this.comboOption.setSelectedIndex(0);
74 ilm 245
            } else {
181 ilm 246
                this.comboOption.setSelectedIndex(1);
74 ilm 247
            }
248
 
249
        } else if (fd.getType().equals("date")) {
181 ilm 250
            this.comboType.setSelectedIndex(TYPE_DATE);
251
            this.labelOption.setText("Valeur par défaut");
252
            this.textOption.setVisible(false);
253
            this.comboOption.setVisible(true);
254
            this.comboOption.setModel(new DefaultComboBoxModel(vDate));
74 ilm 255
            if (fd.getDefaultValue() == null || !fd.getDefaultValue().equals("now")) {
181 ilm 256
                this.comboOption.setSelectedIndex(0);
74 ilm 257
            } else {
181 ilm 258
                this.comboOption.setSelectedIndex(1);
74 ilm 259
            }
260
        } else if (fd.getType().equals("time")) {
181 ilm 261
            this.comboType.setSelectedIndex(TYPE_TIME);
262
            this.labelOption.setText("Valeur par défaut");
263
            this.textOption.setVisible(false);
264
            this.comboOption.setVisible(true);
265
            this.comboOption.setModel(new DefaultComboBoxModel(vTime));
86 ilm 266
            if (fd.getDefaultValue() == null || !fd.getDefaultValue().equals("now")) {
74 ilm 267
                comboOption.setSelectedIndex(0);
268
            } else {
269
                comboOption.setSelectedIndex(1);
270
            }
271
        } else if (fd.getType().equals("dateAndTime")) {
86 ilm 272
            comboType.setSelectedIndex(TYPE_DATE_TIME);
74 ilm 273
            labelOption.setText("Valeur par défaut");
274
            textOption.setVisible(false);
275
            comboOption.setVisible(true);
276
            comboOption.setModel(new DefaultComboBoxModel(vDateTime));
86 ilm 277
            if (fd.getDefaultValue() == null || !fd.getDefaultValue().equals("now")) {
74 ilm 278
                comboOption.setSelectedIndex(0);
279
            } else {
280
                comboOption.setSelectedIndex(1);
281
            }
282
        } else if (fd.getType().equals("ref")) {
86 ilm 283
            comboType.setSelectedIndex(TYPE_REF);
74 ilm 284
            labelOption.setText("Table");
285
            textOption.setVisible(false);
286
            comboOption.setVisible(true);
181 ilm 287
            final Vector<String> tables = new Vector<String>();
288
            tables.addAll(Arrays.asList(getTables()));
74 ilm 289
            String tableName = fd.getForeignTable();
181 ilm 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
            });
74 ilm 308
            comboOption.setSelectedItem(tableName);
309
        } else {
310
            throw new IllegalArgumentException("Unknow type " + fd.getType());
311
        }
86 ilm 312
        fieldName.setText(fd.getName().trim());
74 ilm 313
 
314
    }
315
 
316
    private String[] getTables() {
317
        final ComptaPropsConfiguration instanceCompta = ComptaPropsConfiguration.getInstanceCompta();
318
        Set<SQLTable> t = instanceCompta.getRootSociete().getTables();
319
        ArrayList<String> names = new ArrayList<String>(t.size());
320
        for (SQLTable table : t) {
321
            // TODO: Creer un renderer
322
            // final SQLElement element = instanceCompta.getDirectory().getElement(table);
323
            // String e = table.getName();
324
            // if (element != null) {
325
            // e += " (" + element.getPluralName().toLowerCase() + ")";
326
            // }
327
            names.add(table.getName());
328
        }
329
        Collections.sort(names);
330
 
331
        return names.toArray(new String[names.size()]);
332
    }
333
 
334
    @Override
335
    public void actionPerformed(ActionEvent e) {
336
        if (e.getSource() == this.comboType) {
337
            final String type = xmltypes[comboType.getSelectedIndex()];
338
            if (!type.equals(fd.getType())) {
339
                this.fd.setType(type);
340
                updateFrom(fd);
341
            }
342
        } else if (e.getSource() == this.comboOption) {
86 ilm 343
            comboOptionModified();
74 ilm 344
        }
345
 
346
    }
347
 
348
}