OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 86 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
74 ilm 1
package org.openconcerto.modules.extensionbuilder.table;
2
 
3
import java.awt.GridBagConstraints;
4
import java.awt.GridBagLayout;
5
import java.awt.event.ActionEvent;
6
import java.awt.event.ActionListener;
7
import java.util.ArrayList;
8
import java.util.Collections;
9
import java.util.Set;
10
 
11
import javax.swing.DefaultComboBoxModel;
12
import javax.swing.JComboBox;
13
import javax.swing.JLabel;
14
import javax.swing.JPanel;
15
import javax.swing.JTextField;
16
import javax.swing.event.DocumentEvent;
17
import javax.swing.event.DocumentListener;
18
 
19
import org.openconcerto.erp.config.ComptaPropsConfiguration;
20
import org.openconcerto.sql.model.SQLTable;
21
import org.openconcerto.ui.DefaultGridBagConstraints;
22
 
23
public class FieldDescriptorEditor extends JPanel implements ActionListener, DocumentListener {
24
    final JComboBox comboType;
25
    private JTextField textName;
26
    private JLabel labelOption;
27
    private JTextField textOption;
28
    private String[] types = { "Texte", "Nombre entier", "Nombre décimal", "Booléen", "Date", "Heure", "Date et heure", "Référence" };
29
    private String[] xmltypes = { "string", "integer", "decimal", "boolean", "date", "time", "dateAndTime", "ref" };
30
    private JComboBox comboOption;
31
 
32
    private static final String[] vBoolean = { "oui", "non" };
33
    private static final String[] vDate = { "vide", "jour actuel" };
34
    private static final String[] vTime = { "vide", "heure actuelle" };
35
    private static final String[] vDateTime = { "vide", "date actuelle" };
36
    FieldDescriptor fd;
37
 
38
    FieldDescriptorEditor(FieldDescriptor fd) {
39
        this.fd = fd;
40
        this.setLayout(new GridBagLayout());
41
        GridBagConstraints c = new DefaultGridBagConstraints();
42
        this.add(new JLabel("Type"), c);
43
        c.gridx++;
44
 
45
        comboType = new JComboBox(types);
46
        comboType.setOpaque(false);
47
        this.add(comboType, c);
48
        c.gridx++;
49
        this.add(new JLabel("Nom"), c);
50
        c.gridx++;
51
        c.weightx = 1;
52
        textName = new JTextField(10);
53
        this.add(textName, c);
54
        c.weightx = 0;
55
        c.gridx++;
56
        labelOption = new JLabel("Longeur max");
57
        this.add(labelOption, c);
58
        c.gridx++;
59
        textOption = new JTextField(6);
60
        c.gridx++;
61
        this.add(textOption, c);
62
        comboOption = new JComboBox();
63
 
64
        this.add(comboOption, c);
65
 
66
        updateFrom(fd);
67
        comboType.addActionListener(this);
68
        comboOption.addActionListener(this);
69
        textOption.getDocument().addDocumentListener(this);
70
    }
71
 
72
    private void updateFrom(FieldDescriptor fd) {
73
        if (fd.getType().equals("string")) {
74
            comboType.setSelectedIndex(0);
75
            labelOption.setText("Longueur max");
76
            textOption.setVisible(true);
77
            textOption.setText(fd.getLength());
78
            comboOption.setVisible(false);
79
        } else if (fd.getType().equals("integer")) {
80
            comboType.setSelectedIndex(1);
81
            labelOption.setText("Valeur par défaut");
82
            textOption.setVisible(true);
83
            textOption.setText(fd.getDefaultValue());
84
            comboOption.setVisible(false);
85
        } else if (fd.getType().equals("decimal")) {
86
            comboType.setSelectedIndex(2);
87
            labelOption.setText("Valeur par défaut");
88
            textOption.setVisible(true);
89
            textOption.setText(fd.getDefaultValue());
90
            comboOption.setVisible(false);
91
        } else if (fd.getType().equals("boolean")) {
92
            comboType.setSelectedIndex(3);
93
            labelOption.setText("Valeur par défaut");
94
            textOption.setVisible(false);
95
            comboOption.setVisible(true);
96
            comboOption.setModel(new DefaultComboBoxModel(vBoolean));
97
            if (fd.getDefaultValue().equals("true")) {
98
                comboOption.setSelectedIndex(0);
99
            } else {
100
                comboOption.setSelectedIndex(1);
101
            }
102
 
103
        } else if (fd.getType().equals("date")) {
104
            comboType.setSelectedIndex(4);
105
            labelOption.setText("Valeur par défaut");
106
            textOption.setVisible(false);
107
            comboOption.setVisible(true);
108
            comboOption.setModel(new DefaultComboBoxModel(vDate));
109
            if (fd.getDefaultValue() == null || !fd.getDefaultValue().equals("now")) {
110
                comboOption.setSelectedIndex(0);
111
            } else {
112
                comboOption.setSelectedIndex(1);
113
            }
114
        } else if (fd.getType().equals("time")) {
115
            comboType.setSelectedIndex(5);
116
            labelOption.setText("Valeur par défaut");
117
            textOption.setVisible(false);
118
            comboOption.setVisible(true);
119
            comboOption.setModel(new DefaultComboBoxModel(vTime));
120
            if (!fd.getDefaultValue().equals("now")) {
121
                comboOption.setSelectedIndex(0);
122
            } else {
123
                comboOption.setSelectedIndex(1);
124
            }
125
        } else if (fd.getType().equals("dateAndTime")) {
126
            comboType.setSelectedIndex(6);
127
            labelOption.setText("Valeur par défaut");
128
            textOption.setVisible(false);
129
            comboOption.setVisible(true);
130
            comboOption.setModel(new DefaultComboBoxModel(vDateTime));
131
            if (!fd.getDefaultValue().equals("now")) {
132
                comboOption.setSelectedIndex(0);
133
            } else {
134
                comboOption.setSelectedIndex(1);
135
            }
136
        } else if (fd.getType().equals("ref")) {
137
            comboType.setSelectedIndex(7);
138
            labelOption.setText("Table");
139
            textOption.setVisible(false);
140
            comboOption.setVisible(true);
141
            comboOption.setModel(new DefaultComboBoxModel(getTables()));
142
            String tableName = fd.getForeignTable();
143
            comboOption.setSelectedItem(tableName);
144
        } else {
145
            throw new IllegalArgumentException("Unknow type " + fd.getType());
146
        }
147
        textName.setText(fd.getName().trim());
148
 
149
    }
150
 
151
    private String[] getTables() {
152
        final ComptaPropsConfiguration instanceCompta = ComptaPropsConfiguration.getInstanceCompta();
153
        Set<SQLTable> t = instanceCompta.getRootSociete().getTables();
154
        ArrayList<String> names = new ArrayList<String>(t.size());
155
        for (SQLTable table : t) {
156
            // TODO: Creer un renderer
157
            // final SQLElement element = instanceCompta.getDirectory().getElement(table);
158
            // String e = table.getName();
159
            // if (element != null) {
160
            // e += " (" + element.getPluralName().toLowerCase() + ")";
161
            // }
162
            names.add(table.getName());
163
        }
164
        Collections.sort(names);
165
 
166
        return names.toArray(new String[names.size()]);
167
    }
168
 
169
    @Override
170
    public void actionPerformed(ActionEvent e) {
171
        if (e.getSource() == this.comboType) {
172
            final String type = xmltypes[comboType.getSelectedIndex()];
173
            if (!type.equals(fd.getType())) {
174
                this.fd.setType(type);
175
                updateFrom(fd);
176
            }
177
        } else if (e.getSource() == this.comboOption) {
178
            // TODO combo -> FieldDescriptor
179
        }
180
 
181
    }
182
 
183
    private void updateText(DocumentEvent e) {
184
        // TODO text -> FieldDescriptor
185
 
186
    }
187
 
188
    @Override
189
    public void insertUpdate(DocumentEvent e) {
190
        updateText(e);
191
 
192
    }
193
 
194
    @Override
195
    public void removeUpdate(DocumentEvent e) {
196
        updateText(e);
197
 
198
    }
199
 
200
    @Override
201
    public void changedUpdate(DocumentEvent e) {
202
        updateText(e);
203
 
204
    }
205
}