OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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