OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 86 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
74 ilm 1
package org.openconcerto.modules.extensionbuilder.list;
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.awt.event.KeyAdapter;
9
import java.awt.event.KeyEvent;
10
import java.awt.event.MouseAdapter;
11
import java.awt.event.MouseEvent;
181 ilm 12
import java.util.List;
74 ilm 13
 
14
import javax.swing.DefaultListCellRenderer;
15
import javax.swing.ImageIcon;
16
import javax.swing.JButton;
17
import javax.swing.JList;
18
import javax.swing.JPanel;
19
import javax.swing.JScrollPane;
20
import javax.swing.JTree;
21
import javax.swing.tree.DefaultMutableTreeNode;
22
import javax.swing.tree.DefaultTreeCellRenderer;
23
import javax.swing.tree.TreePath;
24
 
25
import org.openconcerto.modules.extensionbuilder.Extension;
26
import org.openconcerto.modules.extensionbuilder.table.FieldDescriptor;
27
import org.openconcerto.ui.DefaultGridBagConstraints;
28
import org.openconcerto.ui.DefaultListModel;
29
import org.openconcerto.ui.JLabelBold;
30
import org.openconcerto.ui.list.ReorderableJList;
31
 
32
public class FieldDescSelector extends JPanel {
33
    private FieldTreeModel treeModel;
34
    private DefaultListModel listModel;
35
    private final JTree tree;
36
    private JList list;
37
    private ListDescriptor listDescriptor;
38
 
39
    FieldDescSelector(ListDescriptor desc, Extension extension) {
40
        this.listDescriptor = desc;
41
        this.setLayout(new GridBagLayout());
42
        GridBagConstraints c = new DefaultGridBagConstraints();
43
        this.add(new JLabelBold("Champs disponibles"), c);
44
        c.gridx += 2;
45
        this.add(new JLabelBold("Colonnes affichés"), c);
46
 
47
        // Col 0
48
        c.gridx = 0;
49
        c.gridy++;
181 ilm 50
        this.treeModel = new FieldTreeModel(extension);
51
        this.tree = new JTree(this.treeModel) {
74 ilm 52
            @Override
53
            public String convertValueToText(Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
54
                final Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
55
                if (userObject == null)
56
                    return "null";
57
                FieldDescriptor d = (FieldDescriptor) userObject;
58
 
59
                if (d.getForeignTable() != null) {
60
                    return " " + d.getName() + " référence vers la table " + d.getForeignTable();
61
                }
62
                return " " + d.getName();
63
            }
64
        };
181 ilm 65
        this.tree.setRootVisible(false);
66
        this.tree.setShowsRootHandles(true);
74 ilm 67
        final DefaultTreeCellRenderer treeRenderer = new DefaultTreeCellRenderer();
68
 
69
        treeRenderer.setLeafIcon(null);
70
        treeRenderer.setOpenIcon(new ImageIcon(this.getClass().getResource("ref.png")));
71
        treeRenderer.setClosedIcon(new ImageIcon(this.getClass().getResource("ref.png")));
181 ilm 72
        this.tree.setCellRenderer(treeRenderer);
74 ilm 73
        c.gridheight = 2;
74
        c.weightx = 1;
75
        c.weighty = 1;
76
        c.fill = GridBagConstraints.BOTH;
181 ilm 77
        this.add(new JScrollPane(this.tree), c);
74 ilm 78
        // Col 1
79
        c.gridx = 1;
80
        c.gridheight = 1;
81
        c.weightx = 0;
82
        c.weighty = 0;
83
        c.fill = GridBagConstraints.NONE;
84
        c.anchor = GridBagConstraints.NORTH;
85
        final JButton buttonAdd = new JButton(">");
86
        this.add(buttonAdd, c);
87
        c.gridy++;
88
        final JButton buttonRemove = new JButton("<");
89
        this.add(buttonRemove, c);
90
        // Col 2
91
        c.gridx = 2;
92
        c.gridy--;
93
        c.gridheight = 2;
94
        c.weightx = 1;
95
        c.weighty = 1;
96
        c.fill = GridBagConstraints.BOTH;
181 ilm 97
        this.listModel = new DefaultListModel() {
74 ilm 98
            @Override
99
            public void addElement(Object obj) {
100
                if (!(obj instanceof ColumnDescriptor)) {
101
                    throw new IllegalArgumentException(obj + " is not a ColumnDescriptor");
102
                }
103
                super.addElement(obj);
104
            }
105
 
106
        };
181 ilm 107
        this.list = new ReorderableJList();
108
        this.list.setModel(this.listModel);
109
        this.list.setCellRenderer(new DefaultListCellRenderer() {
74 ilm 110
            @Override
111
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
112
                ColumnDescriptor f = (ColumnDescriptor) value;
113
                String label = f.getFieldsPaths();
114
                return super.getListCellRendererComponent(list, label, index, isSelected, cellHasFocus);
115
            }
116
        });
181 ilm 117
        this.add(new JScrollPane(this.list), c);
74 ilm 118
        // Listeners
181 ilm 119
        this.tree.addMouseListener(new MouseAdapter() {
74 ilm 120
            @Override
121
            public void mouseClicked(MouseEvent e) {
122
                if (e.getClickCount() > 1) {
123
                    addTreeSelectionToList();
124
                }
125
            }
126
 
127
        });
128
        buttonAdd.addActionListener(new ActionListener() {
129
 
130
            @Override
131
            public void actionPerformed(ActionEvent e) {
132
                addTreeSelectionToList();
133
 
134
            }
135
        });
136
        buttonRemove.addActionListener(new ActionListener() {
137
 
138
            @Override
139
            public void actionPerformed(ActionEvent e) {
140
                deleteSelectedInList();
141
 
142
            }
143
        });
181 ilm 144
        this.list.addKeyListener(new KeyAdapter() {
74 ilm 145
            @Override
146
            public void keyPressed(KeyEvent e) {
147
                if (e.getKeyCode() == KeyEvent.VK_DELETE) {
148
                    deleteSelectedInList();
149
                }
150
                e.consume();
151
            }
152
        });
153
 
154
    }
155
 
156
    public void setMainTable(String table) {
86 ilm 157
        this.listDescriptor.setMainTable(table);
181 ilm 158
        this.treeModel.fillFromTable(table);
159
        this.listModel.removeAllElements();
74 ilm 160
        for (ColumnDescriptor d : this.listDescriptor.getColumns()) {
161
            this.listModel.addElement(d);
162
        }
163
    }
164
 
165
    private void deleteSelectedInList() {
181 ilm 166
        List<Object> vals = this.list.getSelectedValuesList();
167
        if (vals.isEmpty())
74 ilm 168
            return;
181 ilm 169
        for (Object object : vals) {
170
            boolean b = this.listModel.removeElement(object);
171
            if (!b) {
172
                throw new IllegalStateException("cannot remove " + object + " from list model " + this.listModel);
173
            }
174
            if (object instanceof ColumnDescriptor) {
175
                ColumnDescriptor c = (ColumnDescriptor) object;
176
                this.listDescriptor.remove(c);
177
            }
74 ilm 178
        }
181 ilm 179
        this.list.clearSelection();
74 ilm 180
    }
181
 
182
    private void addTreeSelectionToList() {
181 ilm 183
        final TreePath[] paths = this.tree.getSelectionPaths();
74 ilm 184
        if (paths == null)
185
            return;
186
        for (int i = 0; i < paths.length; i++) {
187
            TreePath treePath = paths[i];
188
            Object[] obj = treePath.getPath();
189
            FieldDescriptor d = null;
190
            FieldDescriptor root = null;
191
            for (int j = 0; j < obj.length; j++) {
192
                final Object object = obj[j];
193
                if (object != null) {
194
                    final FieldDescriptor f = (FieldDescriptor) (((DefaultMutableTreeNode) object).getUserObject());
195
                    if (f != null) {
196
                        final FieldDescriptor fieldDescriptor = new FieldDescriptor(f);
197
                        if (root == null) {
198
                            root = fieldDescriptor;
199
 
200
                        }
201
                        if (d != null) {
202
                            d.setLink(fieldDescriptor);
203
                        }
204
                        d = fieldDescriptor;
205
                    }
206
                }
207
            }
208
 
209
            // On ne met pas les foreigns keys
210
            if (d != null && root != null) {
211
                boolean add = true;
212
                final String extendedLabel = root.getPath();
181 ilm 213
                final int size = this.listModel.getSize();
74 ilm 214
                // Check if already in the list
215
                for (int j = 0; j < size; j++) {
181 ilm 216
                    if (((ColumnDescriptor) this.listModel.getElementAt(j)).getFieldsPaths().contains(extendedLabel)) {
74 ilm 217
                        add = false;
218
                        break;
219
                    }
220
                }
221
                if (add) {
222
                    final ColumnDescriptor colDesc = new ColumnDescriptor(root.getPath());
223
                    colDesc.setFieldsPaths(root.getPath());
181 ilm 224
                    this.listModel.addElement(colDesc);
225
                    this.listDescriptor.add(colDesc);
74 ilm 226
                }
227
            }
228
        }
181 ilm 229
        this.tree.clearSelection();
74 ilm 230
    }
231
 
232
}