OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 86 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 86 Rev 181
Line 7... Line 7...
7
import java.awt.event.ActionListener;
7
import java.awt.event.ActionListener;
8
import java.awt.event.KeyAdapter;
8
import java.awt.event.KeyAdapter;
9
import java.awt.event.KeyEvent;
9
import java.awt.event.KeyEvent;
10
import java.awt.event.MouseAdapter;
10
import java.awt.event.MouseAdapter;
11
import java.awt.event.MouseEvent;
11
import java.awt.event.MouseEvent;
-
 
12
import java.util.List;
12
 
13
 
13
import javax.swing.DefaultListCellRenderer;
14
import javax.swing.DefaultListCellRenderer;
14
import javax.swing.ImageIcon;
15
import javax.swing.ImageIcon;
15
import javax.swing.JButton;
16
import javax.swing.JButton;
16
import javax.swing.JList;
17
import javax.swing.JList;
Line 44... Line 45...
44
        this.add(new JLabelBold("Colonnes affichés"), c);
45
        this.add(new JLabelBold("Colonnes affichés"), c);
45
 
46
 
46
        // Col 0
47
        // Col 0
47
        c.gridx = 0;
48
        c.gridx = 0;
48
        c.gridy++;
49
        c.gridy++;
49
        treeModel = new FieldTreeModel(extension);
50
        this.treeModel = new FieldTreeModel(extension);
50
        this.tree = new JTree(treeModel) {
51
        this.tree = new JTree(this.treeModel) {
51
            @Override
52
            @Override
52
            public String convertValueToText(Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
53
            public String convertValueToText(Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
53
                final Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
54
                final Object userObject = ((DefaultMutableTreeNode) value).getUserObject();
54
                if (userObject == null)
55
                if (userObject == null)
55
                    return "null";
56
                    return "null";
Line 59... Line 60...
59
                    return " " + d.getName() + " référence vers la table " + d.getForeignTable();
60
                    return " " + d.getName() + " référence vers la table " + d.getForeignTable();
60
                }
61
                }
61
                return " " + d.getName();
62
                return " " + d.getName();
62
            }
63
            }
63
        };
64
        };
64
        tree.setRootVisible(false);
65
        this.tree.setRootVisible(false);
65
        tree.setShowsRootHandles(true);
66
        this.tree.setShowsRootHandles(true);
66
        final DefaultTreeCellRenderer treeRenderer = new DefaultTreeCellRenderer();
67
        final DefaultTreeCellRenderer treeRenderer = new DefaultTreeCellRenderer();
67
 
68
 
68
        treeRenderer.setLeafIcon(null);
69
        treeRenderer.setLeafIcon(null);
69
        treeRenderer.setOpenIcon(new ImageIcon(this.getClass().getResource("ref.png")));
70
        treeRenderer.setOpenIcon(new ImageIcon(this.getClass().getResource("ref.png")));
70
        treeRenderer.setClosedIcon(new ImageIcon(this.getClass().getResource("ref.png")));
71
        treeRenderer.setClosedIcon(new ImageIcon(this.getClass().getResource("ref.png")));
71
        tree.setCellRenderer(treeRenderer);
72
        this.tree.setCellRenderer(treeRenderer);
72
        c.gridheight = 2;
73
        c.gridheight = 2;
73
        c.weightx = 1;
74
        c.weightx = 1;
74
        c.weighty = 1;
75
        c.weighty = 1;
75
        c.fill = GridBagConstraints.BOTH;
76
        c.fill = GridBagConstraints.BOTH;
76
        this.add(new JScrollPane(tree), c);
77
        this.add(new JScrollPane(this.tree), c);
77
        // Col 1
78
        // Col 1
78
        c.gridx = 1;
79
        c.gridx = 1;
79
        c.gridheight = 1;
80
        c.gridheight = 1;
80
        c.weightx = 0;
81
        c.weightx = 0;
81
        c.weighty = 0;
82
        c.weighty = 0;
Line 91... Line 92...
91
        c.gridy--;
92
        c.gridy--;
92
        c.gridheight = 2;
93
        c.gridheight = 2;
93
        c.weightx = 1;
94
        c.weightx = 1;
94
        c.weighty = 1;
95
        c.weighty = 1;
95
        c.fill = GridBagConstraints.BOTH;
96
        c.fill = GridBagConstraints.BOTH;
96
        listModel = new DefaultListModel() {
97
        this.listModel = new DefaultListModel() {
97
            @Override
98
            @Override
98
            public void addElement(Object obj) {
99
            public void addElement(Object obj) {
99
                if (!(obj instanceof ColumnDescriptor)) {
100
                if (!(obj instanceof ColumnDescriptor)) {
100
                    throw new IllegalArgumentException(obj + " is not a ColumnDescriptor");
101
                    throw new IllegalArgumentException(obj + " is not a ColumnDescriptor");
101
                }
102
                }
102
                super.addElement(obj);
103
                super.addElement(obj);
103
            }
104
            }
104
 
105
 
105
        };
106
        };
106
        list = new ReorderableJList();
107
        this.list = new ReorderableJList();
107
        list.setModel(listModel);
108
        this.list.setModel(this.listModel);
108
        list.setCellRenderer(new DefaultListCellRenderer() {
109
        this.list.setCellRenderer(new DefaultListCellRenderer() {
109
            @Override
110
            @Override
110
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
111
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
111
                ColumnDescriptor f = (ColumnDescriptor) value;
112
                ColumnDescriptor f = (ColumnDescriptor) value;
112
                String label = f.getFieldsPaths();
113
                String label = f.getFieldsPaths();
113
                return super.getListCellRendererComponent(list, label, index, isSelected, cellHasFocus);
114
                return super.getListCellRendererComponent(list, label, index, isSelected, cellHasFocus);
114
            }
115
            }
115
        });
116
        });
116
        this.add(new JScrollPane(list), c);
117
        this.add(new JScrollPane(this.list), c);
117
        // Listeners
118
        // Listeners
118
        tree.addMouseListener(new MouseAdapter() {
119
        this.tree.addMouseListener(new MouseAdapter() {
119
            @Override
120
            @Override
120
            public void mouseClicked(MouseEvent e) {
121
            public void mouseClicked(MouseEvent e) {
121
                if (e.getClickCount() > 1) {
122
                if (e.getClickCount() > 1) {
122
                    addTreeSelectionToList();
123
                    addTreeSelectionToList();
123
                }
124
                }
Line 138... Line 139...
138
            public void actionPerformed(ActionEvent e) {
139
            public void actionPerformed(ActionEvent e) {
139
                deleteSelectedInList();
140
                deleteSelectedInList();
140
 
141
 
141
            }
142
            }
142
        });
143
        });
143
        list.addKeyListener(new KeyAdapter() {
144
        this.list.addKeyListener(new KeyAdapter() {
144
            @Override
145
            @Override
145
            public void keyPressed(KeyEvent e) {
146
            public void keyPressed(KeyEvent e) {
146
                if (e.getKeyCode() == KeyEvent.VK_DELETE) {
147
                if (e.getKeyCode() == KeyEvent.VK_DELETE) {
147
                    deleteSelectedInList();
148
                    deleteSelectedInList();
148
                }
149
                }
Line 152... Line 153...
152
 
153
 
153
    }
154
    }
154
 
155
 
155
    public void setMainTable(String table) {
156
    public void setMainTable(String table) {
156
        this.listDescriptor.setMainTable(table);
157
        this.listDescriptor.setMainTable(table);
157
        treeModel.fillFromTable(table);
158
        this.treeModel.fillFromTable(table);
158
        listModel.removeAllElements();
159
        this.listModel.removeAllElements();
159
        for (ColumnDescriptor d : this.listDescriptor.getColumns()) {
160
        for (ColumnDescriptor d : this.listDescriptor.getColumns()) {
160
            this.listModel.addElement(d);
161
            this.listModel.addElement(d);
161
        }
162
        }
162
    }
163
    }
163
 
164
 
164
    private void deleteSelectedInList() {
165
    private void deleteSelectedInList() {
165
        Object[] vals = list.getSelectedValues();
166
        List<Object> vals = this.list.getSelectedValuesList();
166
        if (vals == null)
167
        if (vals.isEmpty())
167
            return;
168
            return;
168
        for (int i = 0; i < vals.length; i++) {
-
 
169
            Object object = vals[i];
169
        for (Object object : vals) {
170
            this.listModel.removeElement(object);
170
            boolean b = this.listModel.removeElement(object);
-
 
171
            if (!b) {
-
 
172
                throw new IllegalStateException("cannot remove " + object + " from list model " + this.listModel);
171
        }
173
            }
-
 
174
            if (object instanceof ColumnDescriptor) {
-
 
175
                ColumnDescriptor c = (ColumnDescriptor) object;
-
 
176
                this.listDescriptor.remove(c);
-
 
177
            }
-
 
178
        }
172
        list.clearSelection();
179
        this.list.clearSelection();
173
    }
180
    }
174
 
181
 
175
    private void addTreeSelectionToList() {
182
    private void addTreeSelectionToList() {
176
        final TreePath[] paths = tree.getSelectionPaths();
183
        final TreePath[] paths = this.tree.getSelectionPaths();
177
        if (paths == null)
184
        if (paths == null)
178
            return;
185
            return;
179
        for (int i = 0; i < paths.length; i++) {
186
        for (int i = 0; i < paths.length; i++) {
180
            TreePath treePath = paths[i];
187
            TreePath treePath = paths[i];
181
            Object[] obj = treePath.getPath();
188
            Object[] obj = treePath.getPath();
Line 201... Line 208...
201
 
208
 
202
            // On ne met pas les foreigns keys
209
            // On ne met pas les foreigns keys
203
            if (d != null && root != null) {
210
            if (d != null && root != null) {
204
                boolean add = true;
211
                boolean add = true;
205
                final String extendedLabel = root.getPath();
212
                final String extendedLabel = root.getPath();
206
                final int size = listModel.getSize();
213
                final int size = this.listModel.getSize();
207
                // Check if already in the list
214
                // Check if already in the list
208
                for (int j = 0; j < size; j++) {
215
                for (int j = 0; j < size; j++) {
209
                    if (((ColumnDescriptor) listModel.getElementAt(j)).getFieldsPaths().contains(extendedLabel)) {
216
                    if (((ColumnDescriptor) this.listModel.getElementAt(j)).getFieldsPaths().contains(extendedLabel)) {
210
                        add = false;
217
                        add = false;
211
                        break;
218
                        break;
212
                    }
219
                    }
213
                }
220
                }
214
                if (add) {
221
                if (add) {
215
                    final ColumnDescriptor colDesc = new ColumnDescriptor(root.getPath());
222
                    final ColumnDescriptor colDesc = new ColumnDescriptor(root.getPath());
216
                    colDesc.setFieldsPaths(root.getPath());
223
                    colDesc.setFieldsPaths(root.getPath());
217
                    listModel.addElement(colDesc);
224
                    this.listModel.addElement(colDesc);
218
                    listDescriptor.add(colDesc);
225
                    this.listDescriptor.add(colDesc);
219
                }
226
                }
220
            }
227
            }
221
        }
228
        }
222
        tree.clearSelection();
229
        this.tree.clearSelection();
223
    }
230
    }
224
 
231
 
225
}
232
}