OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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.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;
50
            textId = new JTextField(30);
51
            this.add(textId, c);
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;
80
            comboType = new JComboBox(new String[] { "normal", "large", "très large" });
81
            this.add(comboType, c);
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;
95
        checkSeparated = new JCheckBox();
96
        c.weightx = 1;
97
        this.add(checkSeparated, c);
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;
107
            checkLabel = new JCheckBox();
108
 
109
            this.add(checkLabel, c);
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;
120
            checkFillH = new JCheckBox();
121
            this.add(checkFillH, c);
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
132
        if (isEditingGroup) {
133
            textId.getDocument().addDocumentListener(new DocumentListener() {
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) {
149
                    item.setId(textId.getText());
150
                    component.fireGroupChanged();
151
 
152
                }
153
            });
154
        }
155
        checkSeparated.addActionListener(new ActionListener() {
156
 
157
            @Override
158
            public void actionPerformed(ActionEvent e) {
159
                item.setLocalHint(item.getLocalHint().getBuilder().setSeparated(checkSeparated.isSelected()).build());
160
                component.fireGroupChanged();
161
            }
162
        });
163
 
164
        if (!isEditingGroup) {
165
            checkLabel.addActionListener(new ActionListener() {
166
 
167
                @Override
168
                public void actionPerformed(ActionEvent e) {
169
                    item.setLocalHint(item.getLocalHint().getBuilder().setShowLabel(checkLabel.isSelected()).build());
170
                    component.fireGroupChanged();
171
                }
172
            });
173
            checkFillH.addActionListener(new ActionListener() {
174
 
175
                @Override
176
                public void actionPerformed(ActionEvent e) {
177
                    item.setLocalHint(item.getLocalHint().getBuilder().setFillHeight(checkFillH.isSelected()).build());
178
                    component.fireGroupChanged();
179
                }
180
            });
181
 
182
            comboType.addActionListener(new ActionListener() {
183
 
184
                @Override
185
                public void actionPerformed(ActionEvent e) {
186
                    int i = comboType.getSelectedIndex();
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
 
211
        checkSeparated.setSelected(localHint.isSeparated());
212
 
213
        if (!isEditingGroup) {
214
            if (localHint.fillWidth() && localHint.largeWidth()) {
215
                comboType.setSelectedIndex(2);
216
            } else if (localHint.fillWidth()) {
217
                comboType.setSelectedIndex(1);
218
            } else {
219
                comboType.setSelectedIndex(0);
220
            }
221
            checkFillH.setSelected(localHint.fillHeight());
222
            checkLabel.setSelected(localHint.showLabel());
223
        } else {
224
            textId.setText(item.getId());
225
 
226
        }
227
    }
228
}