OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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.meu.actions;
2
 
3
import java.awt.Component;
4
import java.awt.GridBagConstraints;
5
import java.awt.GridBagLayout;
6
import java.awt.event.ActionEvent;
7
import java.awt.event.ActionListener;
8
import java.util.Collections;
9
import java.util.Comparator;
10
import java.util.List;
11
import java.util.Vector;
12
 
13
import javax.swing.DefaultListCellRenderer;
14
import javax.swing.JComboBox;
15
import javax.swing.JLabel;
16
import javax.swing.JList;
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.modules.extensionbuilder.Extension;
24
import org.openconcerto.modules.extensionbuilder.component.ComponentDescritor;
25
import org.openconcerto.ui.DefaultGridBagConstraints;
26
 
27
public class ActionItemEditor extends JPanel {
28
 
29
    final Extension extension;
30
    private JTextField textId;
31
    private JComboBox comboComponent;
32
    private JTextField textTable;
33
    private JComboBox comboLocation;
34
 
35
    public ActionItemEditor(final ActionDescriptor actionDescriptor, final Extension extension) {
36
        this.extension = extension;
37
        this.setLayout(new GridBagLayout());
38
        GridBagConstraints c = new DefaultGridBagConstraints();
39
 
40
        c.weightx = 0;
41
        c.fill = GridBagConstraints.HORIZONTAL;
42
        this.add(new JLabel("Identifiant", SwingConstants.RIGHT), c);
43
        c.gridx++;
44
 
45
        c.weightx = 1;
46
 
47
        textId = new JTextField();
48
        this.add(textId, c);
49
 
50
        //
51
        final List<ComponentDescritor> l = extension.getCreateComponentList();
52
        final Vector<ComponentDescritor> v = new Vector<ComponentDescritor>(l);
53
        Collections.sort(v, new Comparator<ComponentDescritor>() {
54
 
55
            @Override
56
            public int compare(ComponentDescritor o1, ComponentDescritor o2) {
57
                return o1.getId().compareTo(o2.getId());
58
            }
59
        });
60
        c.gridx = 0;
61
        c.gridy++;
62
        c.weightx = 0;
63
        c.fill = GridBagConstraints.HORIZONTAL;
64
        this.add(new JLabel("Composant", SwingConstants.RIGHT), c);
65
        c.gridx++;
66
        comboComponent = new JComboBox(v);
67
        comboComponent.setRenderer(new DefaultListCellRenderer() {
68
            @Override
69
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
70
                value = ((ComponentDescritor) value).getId();
71
                return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
72
            }
73
        });
74
        c.fill = GridBagConstraints.NONE;
75
        this.add(comboComponent, c);
76
 
77
        c.gridx = 0;
78
        c.gridy++;
79
        c.weightx = 0;
80
        c.fill = GridBagConstraints.HORIZONTAL;
81
        this.add(new JLabel("Table", SwingConstants.RIGHT), c);
82
        c.gridx++;
83
        c.fill = GridBagConstraints.NONE;
84
        textTable = new JTextField(30);
85
        textTable.setEnabled(false);
86
        this.add(textTable, c);
87
        // Location
88
        c.gridx = 0;
89
        c.gridy++;
90
        c.weightx = 0;
91
        c.fill = GridBagConstraints.HORIZONTAL;
92
        this.add(new JLabel("Emplacement", SwingConstants.RIGHT), c);
93
        c.gridx++;
94
        c.fill = GridBagConstraints.NONE;
95
        comboLocation = new JComboBox(new String[] { "Bouton et clic droit", "clic droit uniquement", "bouton uniquement" });
96
        this.add(comboLocation, c);
97
        c.gridy++;
98
        c.weighty = 1;
99
        this.add(new JPanel(), c);
100
 
101
        initUIFrom(actionDescriptor);
102
 
103
        comboComponent.addActionListener(new ActionListener() {
104
 
105
            @Override
106
            public void actionPerformed(ActionEvent e) {
107
                final ComponentDescritor componentDescritor = getComponentDescritor(comboComponent.getSelectedItem().toString());
108
                if (componentDescritor != null) {
109
                    textTable.setText(componentDescritor.getTable());
110
                    actionDescriptor.setComponentId(componentDescritor.getId());
111
                    actionDescriptor.setTable(componentDescritor.getTable());
112
                } else {
113
                    textTable.setText("");
114
                }
115
            }
116
        });
117
        comboLocation.addActionListener(new ActionListener() {
118
 
119
            @Override
120
            public void actionPerformed(ActionEvent e) {
121
                int index = comboLocation.getSelectedIndex();
122
                if (index == 0) {
123
                    actionDescriptor.setLocation(ActionDescriptor.LOCATION_HEADER_POPUP);
124
                } else if (index == 1) {
125
                    actionDescriptor.setLocation(ActionDescriptor.LOCATION_POPUP);
126
                } else {
127
                    actionDescriptor.setLocation(ActionDescriptor.LOCATION_HEADER);
128
                }
129
 
130
            }
131
        });
132
        textId.getDocument().addDocumentListener(new DocumentListener() {
133
 
134
            @Override
135
            public void removeUpdate(DocumentEvent e) {
136
                changedUpdate(e);
137
 
138
            }
139
 
140
            @Override
141
            public void insertUpdate(DocumentEvent e) {
142
                changedUpdate(e);
143
 
144
            }
145
 
146
            @Override
147
            public void changedUpdate(DocumentEvent e) {
148
                actionDescriptor.setId(textId.getText());
149
            }
150
        });
151
 
152
    }
153
 
154
    private void initUIFrom(ActionDescriptor item) {
155
 
156
        textId.setText(item.getId());
157
        final ComponentDescritor componentDescritor = getComponentDescritor(item.getComponentId());
158
        if (componentDescritor != null) {
159
            comboComponent.setSelectedItem(componentDescritor);
160
        }
161
        textTable.setText(item.getTable());
162
        String loc = item.getLocation();
163
        if (loc.equals(ActionDescriptor.LOCATION_HEADER_POPUP)) {
164
            comboLocation.setSelectedIndex(0);
165
        } else if (loc.equals(ActionDescriptor.LOCATION_HEADER)) {
166
            comboLocation.setSelectedIndex(2);
167
        } else {
168
            comboLocation.setSelectedIndex(1);
169
        }
170
    }
171
 
172
    private ComponentDescritor getComponentDescritor(String componentId) {
173
        List<ComponentDescritor> l = extension.getCreateComponentList();
174
        for (ComponentDescritor componentDescritor : l) {
175
            if (componentDescritor.getId().equals(componentId)) {
176
                return componentDescritor;
177
            }
178
        }
179
        return null;
180
    }
181
}