OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 153 | 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;
181 ilm 62
        this.textId = new JTextField();
74 ilm 63
 
181 ilm 64
        this.add(this.textId, c);
74 ilm 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;
181 ilm 75
            this.comboActionType = new JComboBox();
74 ilm 76
 
181 ilm 77
            this.add(this.comboActionType, c);
74 ilm 78
            c.gridx++;
79
            c.weightx = 1;
181 ilm 80
            this.comboActionChoice = new JComboBox();
81
            this.add(this.comboActionChoice, c);
74 ilm 82
 
83
            c.gridy++;
84
        }
85
 
86
        c.fill = GridBagConstraints.HORIZONTAL;
87
        c.gridx = 1;
88
        c.weightx = 0;
89
        c.fill = GridBagConstraints.NONE;
181 ilm 90
        this.shownInMenu = new JCheckBox("Afficher dans le menu");
74 ilm 91
 
181 ilm 92
        this.add(this.shownInMenu, c);
74 ilm 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
181 ilm 103
        if (!this.isEditingGroup) {
74 ilm 104
            // comboActionType
181 ilm 105
            this.comboActionType.addActionListener(new ActionListener() {
74 ilm 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
181 ilm 113
                        MenuItemEditor.this.comboActionChoice.setModel(new DefaultComboBoxModel(MenuItemEditor.this.componentIds));
86 ilm 114
 
115
                        MenuDescriptor desc = extension.getCreateMenuItemFromId(item.getId());
116
                        desc.setType(MenuDescriptor.CREATE);
117
                        desc.setListId(null);
181 ilm 118
                        if (MenuItemEditor.this.componentIds.size() > 0) {
119
                            MenuItemEditor.this.comboActionChoice.setSelectedIndex(0);
120
                            desc.setComponentId(MenuItemEditor.this.comboActionChoice.getSelectedItem().toString());
86 ilm 121
                        } else {
122
                            desc.setComponentId(null);
123
                        }
124
                        extension.setChanged();
74 ilm 125
                    } else {
153 ilm 126
                        // Liste
181 ilm 127
                        MenuItemEditor.this.comboActionChoice.setModel(new DefaultComboBoxModel(MenuItemEditor.this.listIds));
86 ilm 128
                        MenuDescriptor desc = extension.getCreateMenuItemFromId(item.getId());
129
                        desc.setType(MenuDescriptor.LIST);
181 ilm 130
                        if (MenuItemEditor.this.listIds.size() > 0) {
131
                            MenuItemEditor.this.comboActionChoice.setSelectedIndex(0);
132
                            desc.setListId(MenuItemEditor.this.comboActionChoice.getSelectedItem().toString());
86 ilm 133
                        } else {
134
                            desc.setListId(null);
135
                        }
136
                        desc.setComponentId(null);
137
                        extension.setChanged();
74 ilm 138
                    }
139
 
140
                }
141
            });
181 ilm 142
            this.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)) {
181 ilm 148
                        desc.setComponentId(MenuItemEditor.this.comboActionChoice.getSelectedItem().toString());
153 ilm 149
                    } else if (desc.getType().equals(MenuDescriptor.LIST)) {
181 ilm 150
                        desc.setListId(MenuItemEditor.this.comboActionChoice.getSelectedItem().toString());
86 ilm 151
                    } else {
152
                        desc.setComponentId(null);
153
                        desc.setListId(null);
154
                    }
155
                }
156
            });
157
 
74 ilm 158
        }
181 ilm 159
        this.shownInMenu.addActionListener(new ActionListener() {
74 ilm 160
 
161
            @Override
162
            public void actionPerformed(ActionEvent e) {
181 ilm 163
                final boolean selected = MenuItemEditor.this.shownInMenu.isSelected();
74 ilm 164
 
181 ilm 165
                MenuItemEditor.this.treeModel.setActive(selected, item.getId());
74 ilm 166
            }
167
        });
168
 
169
    }
170
 
171
    private void initUIFrom(String itemId) {
181 ilm 172
        boolean hasCreated = this.extension.getCreateMenuItemFromId(itemId) != null;
173
        this.textId.setEnabled(hasCreated);
174
        this.previousId = itemId;
74 ilm 175
        if (hasCreated) {
181 ilm 176
            this.textId.getDocument().addDocumentListener(new DocumentListener() {
74 ilm 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) {
181 ilm 190
                    String t = MenuItemEditor.this.textId.getText();
74 ilm 191
                    System.err.println("MenuItemEditor.initUIFrom(...).new DocumentListener() {...}.changedUpdate()" + t);
192
 
181 ilm 193
                    if (!MenuItemEditor.this.previousId.equals(t)) {
194
                        MenuItemEditor.this.treeModel.renameMenuItem(MenuItemEditor.this.previousId, t);
195
                        MenuItemEditor.this.previousId = t;
74 ilm 196
 
197
                    }
198
                }
199
            });
200
        }
201
 
181 ilm 202
        this.shownInMenu.setSelected(this.extension.getRemoveMenuItemFromId(itemId) == null);
203
        if (this.textId != null) {
204
            this.textId.setText(itemId);
74 ilm 205
        }
181 ilm 206
        if (!this.isEditingGroup) {
207
            this.comboActionType.setEnabled(true);
208
            this.comboActionChoice.setEnabled(true);
74 ilm 209
            final Action actionForId = MenuManager.getInstance().getActionForId(itemId);
210
            if (hasCreated) {
181 ilm 211
                MenuDescriptor desc = this.extension.getCreateMenuItemFromId(itemId);
212
                this.comboActionType.setModel(new DefaultComboBoxModel(new String[] { "Saisie", "Liste" }));
74 ilm 213
                //
181 ilm 214
                final List<ComponentDescritor> compDescList = this.extension.getCreateComponentList();
215
                this.componentIds = new Vector<String>(compDescList.size());
74 ilm 216
                for (ComponentDescritor componentDescritor : compDescList) {
139 ilm 217
                    final String id = componentDescritor.getId();
218
                    if (id != null) {
181 ilm 219
                        this.componentIds.add(id);
139 ilm 220
                    }
74 ilm 221
                }
222
 
181 ilm 223
                Collections.sort(this.componentIds);
74 ilm 224
 
181 ilm 225
                final List<ListDescriptor> listDescList = this.extension.getCreateListList();
226
                this.listIds = new Vector<>(listDescList.size());
74 ilm 227
                for (ListDescriptor listDescritor : listDescList) {
139 ilm 228
                    final String id = listDescritor.getId();
229
                    if (id != null) {
181 ilm 230
                        this.listIds.add(id);
139 ilm 231
                    }
74 ilm 232
                }
233
 
181 ilm 234
                Collections.sort(this.listIds);
74 ilm 235
                //
236
 
237
                String type = desc.getType();
238
                if (type.equals(MenuDescriptor.CREATE)) {
239
                    final String componentId = desc.getComponentId();
181 ilm 240
                    if (!this.componentIds.contains(componentId) && componentId != null) {
241
                        this.componentIds.add(componentId);
74 ilm 242
                    }
181 ilm 243
                    this.comboActionType.setSelectedIndex(0);
244
                    this.comboActionChoice.setModel(new DefaultComboBoxModel(this.componentIds));
245
                    this.comboActionChoice.setSelectedItem(componentId);
74 ilm 246
 
247
                } else if (type.equals(MenuDescriptor.LIST)) {
248
                    final String listId = desc.getListId();
181 ilm 249
                    if (!this.listIds.contains(listId) && listId != null) {
250
                        this.listIds.add(listId);
74 ilm 251
                    }
181 ilm 252
                    this.comboActionType.setSelectedIndex(1);
253
                    this.comboActionChoice.setModel(new DefaultComboBoxModel(this.listIds));
254
                    this.comboActionChoice.setSelectedItem(listId);
74 ilm 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) {
181 ilm 266
                                this.comboActionType.setModel(new DefaultComboBoxModel(new String[] { "Saisie" }));
74 ilm 267
                            } else if (frame instanceof IListFrame) {
181 ilm 268
                                this.comboActionType.setModel(new DefaultComboBoxModel(new String[] { "Liste" }));
74 ilm 269
                            } else {
181 ilm 270
                                this.comboActionType.setModel(new DefaultComboBoxModel(new String[] { "Autre" }));
74 ilm 271
                            }
272
 
181 ilm 273
                            this.comboActionChoice.setModel(new DefaultComboBoxModel(new String[] { frame.getTitle() }));
74 ilm 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
}