OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 139 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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