OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
74 ilm 1
package org.openconcerto.modules.extensionbuilder.menu.mainmenu;
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.Collections;
8
import java.util.List;
9
import java.util.Vector;
10
 
11
import javax.swing.Action;
12
import javax.swing.DefaultComboBoxModel;
13
import javax.swing.JCheckBox;
14
import javax.swing.JComboBox;
15
import javax.swing.JFrame;
16
import javax.swing.JLabel;
17
import javax.swing.JPanel;
18
import javax.swing.JTextField;
19
import javax.swing.SwingConstants;
20
import javax.swing.event.DocumentEvent;
21
import javax.swing.event.DocumentListener;
22
 
23
import org.openconcerto.erp.action.CreateFrameAbstractAction;
24
import org.openconcerto.erp.config.MenuManager;
25
import org.openconcerto.modules.extensionbuilder.Extension;
26
import org.openconcerto.modules.extensionbuilder.component.ComponentDescritor;
27
import org.openconcerto.modules.extensionbuilder.list.ListDescriptor;
28
import org.openconcerto.sql.view.EditFrame;
29
import org.openconcerto.sql.view.IListFrame;
30
import org.openconcerto.ui.DefaultGridBagConstraints;
31
import org.openconcerto.ui.group.Group;
32
import org.openconcerto.ui.group.Item;
33
 
34
public class MenuItemEditor extends JPanel {
35
    private JTextField textId;
36
    private JComboBox comboActionType;
37
    private JComboBox comboActionChoice;
38
    private JCheckBox shownInMenu;
39
    private boolean isEditingGroup;
40
    final Extension extension;
41
    private Vector<String> componentIds;
42
    private Vector<String> listIds;
43
    protected String previousId;
44
    private MenuItemTreeModel treeModel;
45
 
46
    public MenuItemEditor(MenuItemTreeModel model, final Item item, final Extension extension) {
47
        this.extension = extension;
48
        this.treeModel = model;
49
        this.setLayout(new GridBagLayout());
50
        GridBagConstraints c = new DefaultGridBagConstraints();
51
        if (item instanceof Group) {
52
            this.isEditingGroup = true;
53
        }
54
 
55
        c.weightx = 0;
56
        c.fill = GridBagConstraints.HORIZONTAL;
57
        this.add(new JLabel("Identifiant", SwingConstants.RIGHT), c);
58
        c.gridx++;
59
        c.fill = GridBagConstraints.HORIZONTAL;
60
        c.weightx = 1;
61
        c.gridwidth = 2;
62
        textId = new JTextField();
63
 
64
        this.add(textId, c);
65
        c.gridy++;
66
 
67
        c.gridwidth = 1;
68
        if (!this.isEditingGroup) {
69
            c.weightx = 0;
70
            c.gridx = 0;
71
            this.add(new JLabel("Action", SwingConstants.RIGHT), c);
72
            c.gridx++;
73
 
74
            c.fill = GridBagConstraints.HORIZONTAL;
75
            comboActionType = new JComboBox();
76
 
77
            this.add(comboActionType, c);
78
            c.gridx++;
79
            c.weightx = 1;
80
            comboActionChoice = new JComboBox();
81
            this.add(comboActionChoice, c);
82
 
83
            c.gridy++;
84
        }
85
 
86
        c.fill = GridBagConstraints.HORIZONTAL;
87
        c.gridx = 1;
88
        c.weightx = 0;
89
        c.fill = GridBagConstraints.NONE;
90
        shownInMenu = new JCheckBox("Afficher dans le menu");
91
 
92
        this.add(shownInMenu, c);
93
 
94
        JPanel spacer = new JPanel();
95
        c.gridx = 1;
96
        c.gridy++;
97
        c.weighty = 1;
98
 
99
        this.add(spacer, c);
100
        initUIFrom(item.getId());
101
 
102
        // Listeners
103
        if (!isEditingGroup) {
104
            // comboActionType
105
            comboActionType.addActionListener(new ActionListener() {
106
 
107
                @Override
108
                public void actionPerformed(ActionEvent e) {
109
                    JComboBox cb = (JComboBox) e.getSource();
110
                    int type = cb.getSelectedIndex();
111
                    if (type == 0) {
153 ilm 112
                        // Saisie
74 ilm 113
                        comboActionChoice.setModel(new DefaultComboBoxModel(componentIds));
86 ilm 114
 
115
                        MenuDescriptor desc = extension.getCreateMenuItemFromId(item.getId());
116
                        desc.setType(MenuDescriptor.CREATE);
117
                        desc.setListId(null);
118
                        if (componentIds.size() > 0) {
119
                            comboActionChoice.setSelectedIndex(0);
120
                            desc.setComponentId(comboActionChoice.getSelectedItem().toString());
121
                        } else {
122
                            desc.setComponentId(null);
123
                        }
124
                        extension.setChanged();
74 ilm 125
                    } else {
153 ilm 126
                        // Liste
74 ilm 127
                        comboActionChoice.setModel(new DefaultComboBoxModel(listIds));
86 ilm 128
                        MenuDescriptor desc = extension.getCreateMenuItemFromId(item.getId());
129
                        desc.setType(MenuDescriptor.LIST);
130
                        if (listIds.size() > 0) {
131
                            comboActionChoice.setSelectedIndex(0);
132
                            desc.setListId(comboActionChoice.getSelectedItem().toString());
133
                        } else {
134
                            desc.setListId(null);
135
                        }
136
                        desc.setComponentId(null);
137
                        extension.setChanged();
74 ilm 138
                    }
139
 
140
                }
141
            });
86 ilm 142
            comboActionChoice.addActionListener(new ActionListener() {
74 ilm 143
 
86 ilm 144
                @Override
145
                public void actionPerformed(ActionEvent e) {
146
                    MenuDescriptor desc = extension.getCreateMenuItemFromId(item.getId());
153 ilm 147
                    if (desc.getType().equals(MenuDescriptor.CREATE)) {
86 ilm 148
                        desc.setComponentId(comboActionChoice.getSelectedItem().toString());
153 ilm 149
                    } else if (desc.getType().equals(MenuDescriptor.LIST)) {
86 ilm 150
                        desc.setListId(comboActionChoice.getSelectedItem().toString());
151
                    } else {
152
                        desc.setComponentId(null);
153
                        desc.setListId(null);
154
                    }
155
                }
156
            });
157
 
74 ilm 158
        }
159
        shownInMenu.addActionListener(new ActionListener() {
160
 
161
            @Override
162
            public void actionPerformed(ActionEvent e) {
163
                final boolean selected = shownInMenu.isSelected();
164
 
165
                treeModel.setActive(selected, item.getId());
166
            }
167
        });
168
 
169
    }
170
 
171
    private void initUIFrom(String itemId) {
172
        boolean hasCreated = extension.getCreateMenuItemFromId(itemId) != null;
173
        textId.setEnabled(hasCreated);
174
        previousId = itemId;
175
        if (hasCreated) {
176
            textId.getDocument().addDocumentListener(new DocumentListener() {
177
 
178
                @Override
179
                public void removeUpdate(DocumentEvent e) {
180
                    changedUpdate(e);
181
                }
182
 
183
                @Override
184
                public void insertUpdate(DocumentEvent e) {
185
                    changedUpdate(e);
186
                }
187
 
188
                @Override
189
                public void changedUpdate(DocumentEvent e) {
190
                    String t = textId.getText();
191
                    System.err.println("MenuItemEditor.initUIFrom(...).new DocumentListener() {...}.changedUpdate()" + t);
192
 
193
                    if (!previousId.equals(t)) {
194
                        treeModel.renameMenuItem(previousId, t);
195
                        previousId = t;
196
 
197
                    }
198
                }
199
            });
200
        }
201
 
202
        shownInMenu.setSelected(extension.getRemoveMenuItemFromId(itemId) == null);
203
        if (textId != null) {
204
            textId.setText(itemId);
205
        }
206
        if (!isEditingGroup) {
207
            comboActionType.setEnabled(true);
208
            comboActionChoice.setEnabled(true);
209
            final Action actionForId = MenuManager.getInstance().getActionForId(itemId);
210
            if (hasCreated) {
211
                MenuDescriptor desc = extension.getCreateMenuItemFromId(itemId);
212
                comboActionType.setModel(new DefaultComboBoxModel(new String[] { "Saisie", "Liste" }));
213
                //
214
                final List<ComponentDescritor> compDescList = extension.getCreateComponentList();
215
                componentIds = new Vector<String>(compDescList.size());
216
                for (ComponentDescritor componentDescritor : compDescList) {
139 ilm 217
                    final String id = componentDescritor.getId();
218
                    if (id != null) {
219
                        componentIds.add(id);
220
                    }
74 ilm 221
                }
222
 
223
                Collections.sort(componentIds);
224
 
225
                final List<ListDescriptor> listDescList = extension.getCreateListList();
153 ilm 226
                listIds = new Vector<>(listDescList.size());
74 ilm 227
                for (ListDescriptor listDescritor : listDescList) {
139 ilm 228
                    final String id = listDescritor.getId();
229
                    if (id != null) {
230
                        listIds.add(id);
231
                    }
74 ilm 232
                }
233
 
234
                Collections.sort(listIds);
235
                //
236
 
237
                String type = desc.getType();
238
                if (type.equals(MenuDescriptor.CREATE)) {
239
                    final String componentId = desc.getComponentId();
139 ilm 240
                    if (!componentIds.contains(componentId) && componentId != null) {
74 ilm 241
                        componentIds.add(componentId);
242
                    }
243
                    comboActionType.setSelectedIndex(0);
244
                    comboActionChoice.setModel(new DefaultComboBoxModel(componentIds));
245
                    comboActionChoice.setSelectedItem(componentId);
246
 
247
                } else if (type.equals(MenuDescriptor.LIST)) {
248
                    final String listId = desc.getListId();
139 ilm 249
                    if (!listIds.contains(listId) && listId != null) {
74 ilm 250
                        listIds.add(listId);
251
                    }
252
                    comboActionType.setSelectedIndex(1);
253
                    comboActionChoice.setModel(new DefaultComboBoxModel(listIds));
254
                    comboActionChoice.setSelectedItem(listId);
255
 
256
                } else {
257
                    throw new IllegalArgumentException("Unknown type " + type);
258
                }
259
            } else {
260
                if (actionForId != null) {
261
                    if (actionForId instanceof CreateFrameAbstractAction) {
262
                        CreateFrameAbstractAction a = (CreateFrameAbstractAction) actionForId;
263
                        JFrame frame = a.createFrame();
264
                        if (frame != null) {
265
                            if (frame instanceof EditFrame) {
266
                                comboActionType.setModel(new DefaultComboBoxModel(new String[] { "Saisie" }));
267
                            } else if (frame instanceof IListFrame) {
268
                                comboActionType.setModel(new DefaultComboBoxModel(new String[] { "Liste" }));
269
                            } else {
270
                                comboActionType.setModel(new DefaultComboBoxModel(new String[] { "Autre" }));
271
                            }
272
 
273
                            comboActionChoice.setModel(new DefaultComboBoxModel(new String[] { frame.getTitle() }));
274
                        } else {
275
                            comboActionType.setModel(new DefaultComboBoxModel(new String[] { "Autre" }));
276
                            comboActionChoice.setModel(new DefaultComboBoxModel(new String[] { actionForId.getClass().getName() }));
277
                        }
278
 
279
                    } else {
280
                        comboActionType.setModel(new DefaultComboBoxModel(new String[] { "Autre" }));
281
                        comboActionChoice.setModel(new DefaultComboBoxModel(new String[] { actionForId.getClass().getName() }));
282
                    }
283
 
284
                }
285
 
286
            }
287
        }
288
 
289
    }
290
}