OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
74 ilm 1
package org.openconcerto.modules.extensionbuilder.component;
2
 
3
import java.awt.GridBagConstraints;
4
import java.awt.GridBagLayout;
5
import java.awt.event.ActionEvent;
6
import java.awt.event.ActionListener;
7
 
8
import javax.swing.JCheckBox;
9
import javax.swing.JComboBox;
10
import javax.swing.JLabel;
11
import javax.swing.JPanel;
12
import javax.swing.JTextField;
13
import javax.swing.SwingConstants;
14
import javax.swing.event.DocumentEvent;
15
import javax.swing.event.DocumentListener;
16
 
17
import org.openconcerto.erp.config.ComptaPropsConfiguration;
18
import org.openconcerto.sql.model.SQLField;
19
import org.openconcerto.sql.model.SQLTable;
20
import org.openconcerto.ui.DefaultGridBagConstraints;
21
import org.openconcerto.ui.JLabelBold;
22
import org.openconcerto.ui.group.Group;
23
import org.openconcerto.ui.group.Item;
24
import org.openconcerto.ui.group.LayoutHints;
25
import org.openconcerto.ui.group.LayoutHintsBuilder;
26
 
27
public class ItemEditor extends JPanel {
28
 
29
    private JTextField textId;
30
    private JComboBox comboType;
31
    private JCheckBox checkSeparated;
32
    private JCheckBox checkLabel;
33
    private boolean isEditingGroup;
34
    private JCheckBox checkFillH;
35
 
36
    public ItemEditor(final Item item, final ComponentDescritor component) {
37
        this.setLayout(new GridBagLayout());
38
        GridBagConstraints c = new DefaultGridBagConstraints();
39
        if (item instanceof Group) {
40
            this.isEditingGroup = true;
41
        }
42
 
43
        if (this.isEditingGroup) {
44
            // Identifiant du groupe
45
            c.weightx = 0;
46
            this.add(new JLabel("Identifiant", SwingConstants.RIGHT), c);
47
            c.gridx++;
48
            c.weightx = 1;
49
            c.fill = GridBagConstraints.HORIZONTAL;
181 ilm 50
            this.textId = new JTextField(30);
51
            this.add(this.textId, c);
74 ilm 52
            c.gridy++;
53
        } else {
54
            // Label du champs
55
            c.gridwidth = 2;
56
            String label = item.getId();
57
            SQLTable t = ComptaPropsConfiguration.getInstanceCompta().getRootSociete().getTable(component.getTable());
58
            if (t != null) {
59
                SQLField field = t.getField(item.getId());
60
                if (field != null) {
61
                    final String labelFor = ComptaPropsConfiguration.getTranslator(t).getLabelFor(field);
62
                    if (labelFor != null) {
63
                        label += " (" + labelFor + ")";
64
                    }
65
                }
66
            }
67
            this.add(new JLabelBold(label), c);
68
            c.gridy++;
69
        }
70
        c.gridwidth = 1;
71
 
72
        if (!this.isEditingGroup) {
73
            c.gridx = 0;
74
            c.weightx = 0;
75
            c.fill = GridBagConstraints.HORIZONTAL;
76
            this.add(new JLabel("Type", SwingConstants.RIGHT), c);
77
            c.gridx++;
78
            c.fill = GridBagConstraints.NONE;
79
            c.weightx = 1;
181 ilm 80
            this.comboType = new JComboBox(new String[] { "normal", "large", "très large" });
81
            this.add(this.comboType, c);
74 ilm 82
            c.gridy++;
83
        }
84
        c.gridx = 0;
85
 
86
        c.fill = GridBagConstraints.HORIZONTAL;
87
        c.weightx = 0;
88
        final JLabel labelSep = new JLabel("Sépararer le label", SwingConstants.RIGHT);
89
        if (this.isEditingGroup) {
90
            labelSep.setText("Dissocier");
91
        }
92
        this.add(labelSep, c);
93
        c.gridx++;
94
        c.fill = GridBagConstraints.NONE;
181 ilm 95
        this.checkSeparated = new JCheckBox();
74 ilm 96
        c.weightx = 1;
181 ilm 97
        this.add(this.checkSeparated, c);
74 ilm 98
        c.gridx = 0;
99
        c.gridy++;
100
        if (!this.isEditingGroup) {
101
            c.fill = GridBagConstraints.HORIZONTAL;
102
            c.weightx = 0;
103
            this.add(new JLabel("Afficher le label", SwingConstants.RIGHT), c);
104
            c.gridx++;
105
            c.weightx = 1;
106
            c.fill = GridBagConstraints.NONE;
181 ilm 107
            this.checkLabel = new JCheckBox();
74 ilm 108
 
181 ilm 109
            this.add(this.checkLabel, c);
74 ilm 110
 
111
            c.gridy++;
112
 
113
            c.gridx = 0;
114
            c.weightx = 0;
115
            c.fill = GridBagConstraints.HORIZONTAL;
116
            this.add(new JLabel("Maximiser la taille", SwingConstants.RIGHT), c);
117
            c.gridx++;
118
            c.fill = GridBagConstraints.NONE;
119
            c.weightx = 1;
181 ilm 120
            this.checkFillH = new JCheckBox();
121
            this.add(this.checkFillH, c);
74 ilm 122
            c.gridy++;
123
        }
124
 
125
        JPanel spacer = new JPanel();
126
        c.weighty = 1;
127
 
128
        this.add(spacer, c);
129
        initUIFrom(item);
130
 
131
        // Listeners
181 ilm 132
        if (this.isEditingGroup) {
133
            this.textId.getDocument().addDocumentListener(new DocumentListener() {
74 ilm 134
 
135
                @Override
136
                public void removeUpdate(DocumentEvent e) {
137
                    changedUpdate(e);
138
 
139
                }
140
 
141
                @Override
142
                public void insertUpdate(DocumentEvent e) {
143
                    changedUpdate(e);
144
 
145
                }
146
 
147
                @Override
148
                public void changedUpdate(DocumentEvent e) {
181 ilm 149
                    item.setId(ItemEditor.this.textId.getText());
74 ilm 150
                    component.fireGroupChanged();
151
 
152
                }
153
            });
154
        }
181 ilm 155
        this.checkSeparated.addActionListener(new ActionListener() {
74 ilm 156
 
157
            @Override
158
            public void actionPerformed(ActionEvent e) {
181 ilm 159
                item.setLocalHint(item.getLocalHint().getBuilder().setSeparated(ItemEditor.this.checkSeparated.isSelected()).build());
74 ilm 160
                component.fireGroupChanged();
161
            }
162
        });
163
 
181 ilm 164
        if (!this.isEditingGroup) {
165
            this.checkLabel.addActionListener(new ActionListener() {
74 ilm 166
 
167
                @Override
168
                public void actionPerformed(ActionEvent e) {
181 ilm 169
                    item.setLocalHint(item.getLocalHint().getBuilder().setShowLabel(ItemEditor.this.checkLabel.isSelected()).build());
74 ilm 170
                    component.fireGroupChanged();
171
                }
172
            });
181 ilm 173
            this.checkFillH.addActionListener(new ActionListener() {
74 ilm 174
 
175
                @Override
176
                public void actionPerformed(ActionEvent e) {
181 ilm 177
                    item.setLocalHint(item.getLocalHint().getBuilder().setFillHeight(ItemEditor.this.checkFillH.isSelected()).build());
74 ilm 178
                    component.fireGroupChanged();
179
                }
180
            });
181
 
181 ilm 182
            this.comboType.addActionListener(new ActionListener() {
74 ilm 183
 
184
                @Override
185
                public void actionPerformed(ActionEvent e) {
181 ilm 186
                    int i = ItemEditor.this.comboType.getSelectedIndex();
74 ilm 187
                    final LayoutHintsBuilder h = item.getLocalHint().getBuilder();
188
                    if (i == 0) {
189
                        h.setFillWidth(false);
190
                        h.setLargeWidth(false);
191
                    } else if (i == 1) {
192
                        h.setFillWidth(true);
193
                        h.setLargeWidth(false);
194
                    } else if (i == 2) {
195
                        h.setFillWidth(true);
196
                        h.setLargeWidth(true);
197
                    } else {
198
                        throw new IllegalStateException("No hints for index " + i);
199
                    }
200
                    item.setLocalHint(h.build());
201
                    component.fireGroupChanged();
202
                }
203
            });
204
        }
205
    }
206
 
207
    private void initUIFrom(Item item) {
208
 
209
        final LayoutHints localHint = item.getLocalHint();
210
 
181 ilm 211
        this.checkSeparated.setSelected(localHint.isSeparated());
74 ilm 212
 
181 ilm 213
        if (!this.isEditingGroup) {
74 ilm 214
            if (localHint.fillWidth() && localHint.largeWidth()) {
181 ilm 215
                this.comboType.setSelectedIndex(2);
74 ilm 216
            } else if (localHint.fillWidth()) {
181 ilm 217
                this.comboType.setSelectedIndex(1);
74 ilm 218
            } else {
181 ilm 219
                this.comboType.setSelectedIndex(0);
74 ilm 220
            }
181 ilm 221
            this.checkFillH.setSelected(localHint.fillHeight());
222
            this.checkLabel.setSelected(localHint.showLabel());
74 ilm 223
        } else {
181 ilm 224
            this.textId.setText(item.getId());
74 ilm 225
 
226
        }
227
    }
228
}