OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 74 | Details | Compare with Previous | 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
 
181 ilm 47
        this.textId = new JTextField();
48
        this.add(this.textId, c);
74 ilm 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++;
181 ilm 66
        this.comboComponent = new JComboBox(v);
67
        this.comboComponent.setRenderer(new DefaultListCellRenderer() {
74 ilm 68
            @Override
69
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
181 ilm 70
                if (value != null) {
71
                    value = ((ComponentDescritor) value).getId();
72
                }
74 ilm 73
                return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
74
            }
75
        });
76
        c.fill = GridBagConstraints.NONE;
181 ilm 77
        this.add(this.comboComponent, c);
74 ilm 78
 
79
        c.gridx = 0;
80
        c.gridy++;
81
        c.weightx = 0;
82
        c.fill = GridBagConstraints.HORIZONTAL;
83
        this.add(new JLabel("Table", SwingConstants.RIGHT), c);
84
        c.gridx++;
85
        c.fill = GridBagConstraints.NONE;
181 ilm 86
        this.textTable = new JTextField(30);
87
        this.textTable.setEnabled(false);
88
        this.add(this.textTable, c);
74 ilm 89
        // Location
90
        c.gridx = 0;
91
        c.gridy++;
92
        c.weightx = 0;
93
        c.fill = GridBagConstraints.HORIZONTAL;
94
        this.add(new JLabel("Emplacement", SwingConstants.RIGHT), c);
95
        c.gridx++;
96
        c.fill = GridBagConstraints.NONE;
181 ilm 97
        this.comboLocation = new JComboBox(new String[] { "Bouton et clic droit", "clic droit uniquement", "bouton uniquement" });
98
        this.add(this.comboLocation, c);
74 ilm 99
        c.gridy++;
100
        c.weighty = 1;
101
        this.add(new JPanel(), c);
102
 
103
        initUIFrom(actionDescriptor);
104
 
181 ilm 105
        this.comboComponent.addActionListener(new ActionListener() {
74 ilm 106
 
107
            @Override
108
            public void actionPerformed(ActionEvent e) {
181 ilm 109
                final ComponentDescritor componentDescritor = getComponentDescritor(ActionItemEditor.this.comboComponent.getSelectedItem().toString());
74 ilm 110
                if (componentDescritor != null) {
181 ilm 111
                    ActionItemEditor.this.textTable.setText(componentDescritor.getTable());
74 ilm 112
                    actionDescriptor.setComponentId(componentDescritor.getId());
113
                    actionDescriptor.setTable(componentDescritor.getTable());
114
                } else {
181 ilm 115
                    ActionItemEditor.this.textTable.setText("");
74 ilm 116
                }
117
            }
118
        });
181 ilm 119
        this.comboLocation.addActionListener(new ActionListener() {
74 ilm 120
 
121
            @Override
122
            public void actionPerformed(ActionEvent e) {
181 ilm 123
                int index = ActionItemEditor.this.comboLocation.getSelectedIndex();
74 ilm 124
                if (index == 0) {
125
                    actionDescriptor.setLocation(ActionDescriptor.LOCATION_HEADER_POPUP);
126
                } else if (index == 1) {
127
                    actionDescriptor.setLocation(ActionDescriptor.LOCATION_POPUP);
128
                } else {
129
                    actionDescriptor.setLocation(ActionDescriptor.LOCATION_HEADER);
130
                }
131
 
132
            }
133
        });
181 ilm 134
        this.textId.getDocument().addDocumentListener(new DocumentListener() {
74 ilm 135
 
136
            @Override
137
            public void removeUpdate(DocumentEvent e) {
138
                changedUpdate(e);
139
 
140
            }
141
 
142
            @Override
143
            public void insertUpdate(DocumentEvent e) {
144
                changedUpdate(e);
145
 
146
            }
147
 
148
            @Override
149
            public void changedUpdate(DocumentEvent e) {
181 ilm 150
                actionDescriptor.setId(ActionItemEditor.this.textId.getText());
74 ilm 151
            }
152
        });
153
 
154
    }
155
 
156
    private void initUIFrom(ActionDescriptor item) {
157
 
181 ilm 158
        this.textId.setText(item.getId());
74 ilm 159
        final ComponentDescritor componentDescritor = getComponentDescritor(item.getComponentId());
160
        if (componentDescritor != null) {
181 ilm 161
            this.comboComponent.setSelectedItem(componentDescritor);
74 ilm 162
        }
181 ilm 163
        this.textTable.setText(item.getTable());
74 ilm 164
        String loc = item.getLocation();
165
        if (loc.equals(ActionDescriptor.LOCATION_HEADER_POPUP)) {
181 ilm 166
            this.comboLocation.setSelectedIndex(0);
74 ilm 167
        } else if (loc.equals(ActionDescriptor.LOCATION_HEADER)) {
181 ilm 168
            this.comboLocation.setSelectedIndex(2);
74 ilm 169
        } else {
181 ilm 170
            this.comboLocation.setSelectedIndex(1);
74 ilm 171
        }
172
    }
173
 
174
    private ComponentDescritor getComponentDescritor(String componentId) {
181 ilm 175
        List<ComponentDescritor> l = this.extension.getCreateComponentList();
74 ilm 176
        for (ComponentDescritor componentDescritor : l) {
177
            if (componentDescritor.getId().equals(componentId)) {
178
                return componentDescritor;
179
            }
180
        }
181
        return null;
182
    }
183
}