OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 74 | Rev 139 | Go to most recent revision | 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) {
112
                        comboActionChoice.setModel(new DefaultComboBoxModel(componentIds));
86 ilm 113
 
114
                        MenuDescriptor desc = extension.getCreateMenuItemFromId(item.getId());
115
                        desc.setType(MenuDescriptor.CREATE);
116
                        desc.setListId(null);
117
                        if (componentIds.size() > 0) {
118
                            comboActionChoice.setSelectedIndex(0);
119
                            desc.setComponentId(comboActionChoice.getSelectedItem().toString());
120
                        } else {
121
                            desc.setComponentId(null);
122
                        }
123
                        extension.setChanged();
74 ilm 124
                    } else {
125
                        comboActionChoice.setModel(new DefaultComboBoxModel(listIds));
86 ilm 126
                        MenuDescriptor desc = extension.getCreateMenuItemFromId(item.getId());
127
                        desc.setType(MenuDescriptor.LIST);
128
                        if (listIds.size() > 0) {
129
                            comboActionChoice.setSelectedIndex(0);
130
                            desc.setListId(comboActionChoice.getSelectedItem().toString());
131
                        } else {
132
                            desc.setListId(null);
133
                        }
134
                        desc.setComponentId(null);
135
                        extension.setChanged();
74 ilm 136
                    }
137
 
138
                }
139
            });
86 ilm 140
            comboActionChoice.addActionListener(new ActionListener() {
74 ilm 141
 
86 ilm 142
                @Override
143
                public void actionPerformed(ActionEvent e) {
144
                    MenuDescriptor desc = extension.getCreateMenuItemFromId(item.getId());
145
                    if (desc.getType() == MenuDescriptor.CREATE) {
146
                        desc.setComponentId(comboActionChoice.getSelectedItem().toString());
147
                    } else if (desc.getType() == MenuDescriptor.LIST) {
148
                        desc.setListId(comboActionChoice.getSelectedItem().toString());
149
                    } else {
150
                        desc.setComponentId(null);
151
                        desc.setListId(null);
152
                    }
153
                }
154
            });
155
 
74 ilm 156
        }
157
        shownInMenu.addActionListener(new ActionListener() {
158
 
159
            @Override
160
            public void actionPerformed(ActionEvent e) {
161
                final boolean selected = shownInMenu.isSelected();
162
 
163
                treeModel.setActive(selected, item.getId());
164
            }
165
        });
166
 
167
    }
168
 
169
    private void initUIFrom(String itemId) {
170
        boolean hasCreated = extension.getCreateMenuItemFromId(itemId) != null;
171
        textId.setEnabled(hasCreated);
172
        previousId = itemId;
173
        if (hasCreated) {
174
            textId.getDocument().addDocumentListener(new DocumentListener() {
175
 
176
                @Override
177
                public void removeUpdate(DocumentEvent e) {
178
                    changedUpdate(e);
179
                }
180
 
181
                @Override
182
                public void insertUpdate(DocumentEvent e) {
183
                    changedUpdate(e);
184
                }
185
 
186
                @Override
187
                public void changedUpdate(DocumentEvent e) {
188
                    String t = textId.getText();
189
                    System.err.println("MenuItemEditor.initUIFrom(...).new DocumentListener() {...}.changedUpdate()" + t);
190
 
191
                    if (!previousId.equals(t)) {
192
                        treeModel.renameMenuItem(previousId, t);
193
                        previousId = t;
194
 
195
                    }
196
                }
197
            });
198
        }
199
 
200
        shownInMenu.setSelected(extension.getRemoveMenuItemFromId(itemId) == null);
201
        if (textId != null) {
202
            textId.setText(itemId);
203
        }
204
        if (!isEditingGroup) {
205
            comboActionType.setEnabled(true);
206
            comboActionChoice.setEnabled(true);
207
            final Action actionForId = MenuManager.getInstance().getActionForId(itemId);
208
            if (hasCreated) {
209
                MenuDescriptor desc = extension.getCreateMenuItemFromId(itemId);
210
                comboActionType.setModel(new DefaultComboBoxModel(new String[] { "Saisie", "Liste" }));
211
                //
212
                final List<ComponentDescritor> compDescList = extension.getCreateComponentList();
213
                componentIds = new Vector<String>(compDescList.size());
214
                for (ComponentDescritor componentDescritor : compDescList) {
215
                    componentIds.add(componentDescritor.getId());
216
                }
217
 
218
                Collections.sort(componentIds);
219
 
220
                final List<ListDescriptor> listDescList = extension.getCreateListList();
221
                listIds = new Vector<String>(listDescList.size());
222
                for (ListDescriptor listDescritor : listDescList) {
223
                    listIds.add(listDescritor.getId());
224
                }
225
 
226
                Collections.sort(listIds);
227
                //
228
 
229
                String type = desc.getType();
230
                if (type.equals(MenuDescriptor.CREATE)) {
231
                    final String componentId = desc.getComponentId();
232
                    if (!componentIds.contains(componentId)) {
233
                        componentIds.add(componentId);
234
                    }
235
                    comboActionType.setSelectedIndex(0);
236
                    comboActionChoice.setModel(new DefaultComboBoxModel(componentIds));
237
                    comboActionChoice.setSelectedItem(componentId);
238
 
239
                } else if (type.equals(MenuDescriptor.LIST)) {
240
                    final String listId = desc.getListId();
241
                    if (!listIds.contains(listId)) {
242
                        listIds.add(listId);
243
                    }
244
                    comboActionType.setSelectedIndex(1);
245
                    comboActionChoice.setModel(new DefaultComboBoxModel(listIds));
246
                    comboActionChoice.setSelectedItem(listId);
247
 
248
                } else {
249
                    throw new IllegalArgumentException("Unknown type " + type);
250
                }
251
            } else {
252
                if (actionForId != null) {
253
                    if (actionForId instanceof CreateFrameAbstractAction) {
254
                        CreateFrameAbstractAction a = (CreateFrameAbstractAction) actionForId;
255
                        JFrame frame = a.createFrame();
256
                        if (frame != null) {
257
                            if (frame instanceof EditFrame) {
258
                                comboActionType.setModel(new DefaultComboBoxModel(new String[] { "Saisie" }));
259
                            } else if (frame instanceof IListFrame) {
260
                                comboActionType.setModel(new DefaultComboBoxModel(new String[] { "Liste" }));
261
                            } else {
262
                                comboActionType.setModel(new DefaultComboBoxModel(new String[] { "Autre" }));
263
                            }
264
 
265
                            comboActionChoice.setModel(new DefaultComboBoxModel(new String[] { frame.getTitle() }));
266
                        } else {
267
                            comboActionType.setModel(new DefaultComboBoxModel(new String[] { "Autre" }));
268
                            comboActionChoice.setModel(new DefaultComboBoxModel(new String[] { actionForId.getClass().getName() }));
269
                        }
270
 
271
                    } else {
272
                        comboActionType.setModel(new DefaultComboBoxModel(new String[] { "Autre" }));
273
                        comboActionChoice.setModel(new DefaultComboBoxModel(new String[] { actionForId.getClass().getName() }));
274
                    }
275
 
276
                }
277
 
278
            }
279
        }
280
 
281
    }
282
}