OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 180 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 180 Rev 182
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
9
 * language governing permissions and limitations under the License.
10
 * 
10
 * 
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.erp.core.edm;
14
 package org.openconcerto.erp.core.edm;
15
 
15
 
16
import org.openconcerto.erp.core.common.ui.ScrollablePanel;
16
import org.openconcerto.erp.core.common.ui.ScrollablePanel;
17
import org.openconcerto.sql.model.SQLRowAccessor;
17
import org.openconcerto.sql.model.SQLRowAccessor;
18
import org.openconcerto.sql.model.SQLRowValues;
18
import org.openconcerto.sql.model.SQLRowValues;
19
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
19
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
20
import org.openconcerto.sql.model.SQLTable;
20
import org.openconcerto.sql.model.SQLTable;
21
import org.openconcerto.sql.model.Where;
21
import org.openconcerto.sql.model.Where;
22
import org.openconcerto.ui.DefaultGridBagConstraints;
22
import org.openconcerto.ui.DefaultGridBagConstraints;
23
import org.openconcerto.ui.SwingThreadUtils;
23
import org.openconcerto.ui.SwingThreadUtils;
24
import org.openconcerto.utils.ExceptionHandler;
24
import org.openconcerto.utils.ExceptionHandler;
25
import org.openconcerto.utils.FileUtils;
25
import org.openconcerto.utils.FileUtils;
26
 
26
 
27
import java.awt.Color;
27
import java.awt.Color;
28
import java.awt.Component;
28
import java.awt.Component;
-
 
29
import java.awt.Desktop;
29
import java.awt.Dimension;
30
import java.awt.Dimension;
30
import java.awt.FileDialog;
31
import java.awt.FileDialog;
31
import java.awt.FlowLayout;
32
import java.awt.FlowLayout;
32
import java.awt.Frame;
33
import java.awt.Frame;
33
import java.awt.GridBagConstraints;
34
import java.awt.GridBagConstraints;
34
import java.awt.GridBagLayout;
35
import java.awt.GridBagLayout;
35
import java.awt.datatransfer.DataFlavor;
36
import java.awt.datatransfer.DataFlavor;
36
import java.awt.datatransfer.Transferable;
37
import java.awt.datatransfer.Transferable;
37
import java.awt.dnd.DnDConstants;
38
import java.awt.dnd.DnDConstants;
38
import java.awt.dnd.DropTarget;
39
import java.awt.dnd.DropTarget;
39
import java.awt.dnd.DropTargetDropEvent;
40
import java.awt.dnd.DropTargetDropEvent;
40
import java.awt.event.ActionEvent;
41
import java.awt.event.ActionEvent;
41
import java.awt.event.ActionListener;
42
import java.awt.event.ActionListener;
42
import java.awt.event.MouseAdapter;
43
import java.awt.event.MouseAdapter;
43
import java.awt.event.MouseEvent;
44
import java.awt.event.MouseEvent;
44
import java.io.File;
45
import java.io.File;
45
import java.io.IOException;
46
import java.io.IOException;
-
 
47
import java.net.URI;
-
 
48
import java.net.URISyntaxException;
46
import java.sql.SQLException;
49
import java.sql.SQLException;
47
import java.util.ArrayList;
50
import java.util.ArrayList;
48
import java.util.Collection;
51
import java.util.Collection;
49
import java.util.Collections;
52
import java.util.Collections;
50
import java.util.Comparator;
53
import java.util.Comparator;
51
import java.util.HashSet;
54
import java.util.HashSet;
52
import java.util.List;
55
import java.util.List;
53
import java.util.Set;
56
import java.util.Set;
54
 
57
 
55
import javax.swing.BorderFactory;
58
import javax.swing.BorderFactory;
56
import javax.swing.JButton;
59
import javax.swing.JButton;
57
import javax.swing.JLabel;
60
import javax.swing.JLabel;
58
import javax.swing.JOptionPane;
61
import javax.swing.JOptionPane;
59
import javax.swing.JPanel;
62
import javax.swing.JPanel;
60
import javax.swing.JProgressBar;
63
import javax.swing.JProgressBar;
61
import javax.swing.JScrollPane;
64
import javax.swing.JScrollPane;
62
import javax.swing.SwingUtilities;
65
import javax.swing.SwingUtilities;
63
import javax.swing.event.ListDataEvent;
66
import javax.swing.event.ListDataEvent;
64
import javax.swing.event.ListDataListener;
67
import javax.swing.event.ListDataListener;
65
 
68
 
66
public class AttachmentPanel extends JPanel {
69
public class AttachmentPanel extends JPanel {
67
 
70
 
68
    private final SQLRowAccessor rowSource;
71
    private final SQLRowAccessor rowSource;
69
    private final Collection<SQLRowAccessor> rowSecondaires;
72
    private final Collection<SQLRowAccessor> rowSecondaires;
70
    private List<ListDataListener> listeners = new ArrayList<>();
73
    private List<ListDataListener> listeners = new ArrayList<>();
71
 
74
 
72
    private int idParent = 1;
75
    private int idParent = 1;
73
    private List<Integer> parents = new ArrayList<>();
76
    private List<Integer> parents = new ArrayList<>();
74
    private List<String> parentsNames = new ArrayList<>();
77
    private List<String> parentsNames = new ArrayList<>();
75
    private Set<Attachment> selectedAttachments = new HashSet<>();
78
    private Set<Attachment> selectedAttachments = new HashSet<>();
76
    private List<FilePanel> filePanels = new ArrayList<>();
79
    private List<FilePanel> filePanels = new ArrayList<>();
77
 
80
 
78
    public AttachmentPanel(SQLRowAccessor rowSource) {
81
    public AttachmentPanel(SQLRowAccessor rowSource) {
79
        this(rowSource, Collections.emptyList());
82
        this(rowSource, Collections.emptyList());
80
    }
83
    }
81
 
84
 
82
    public AttachmentPanel(SQLRowAccessor rowSource, Collection<SQLRowAccessor> rowSecondaires) {
85
    public AttachmentPanel(SQLRowAccessor rowSource, Collection<SQLRowAccessor> rowSecondaires) {
83
        super();
86
        super();
84
        this.rowSource = rowSource;
87
        this.rowSource = rowSource;
85
        this.rowSecondaires = rowSecondaires;
88
        this.rowSecondaires = rowSecondaires;
86
 
89
 
87
        this.setLayout(new GridBagLayout());
90
        this.setLayout(new GridBagLayout());
88
        this.parents.add(1);
91
        this.parents.add(1);
89
        this.parentsNames.add("Racine");
92
        this.parentsNames.add("Racine");
90
        initUI();
93
        initUI();
91
        setFocusable(true);
94
        setFocusable(true);
92
    }
95
    }
93
 
96
 
94
    public void addListener(ListDataListener l) {
97
    public void addListener(ListDataListener l) {
95
        this.listeners.add(l);
98
        this.listeners.add(l);
96
    }
99
    }
97
 
100
 
98
    public void removeListener(ListDataListener l) {
101
    public void removeListener(ListDataListener l) {
99
        this.listeners.remove(l);
102
        this.listeners.remove(l);
100
    }
103
    }
101
 
104
 
102
    public void fireDataChanged() {
105
    public void fireDataChanged() {
103
        for (ListDataListener listDataListener : this.listeners) {
106
        for (ListDataListener listDataListener : this.listeners) {
104
            listDataListener.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, 0));
107
            listDataListener.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, 0));
105
        }
108
        }
106
    }
109
    }
107
 
110
 
108
    public int getAttachementsSize() {
111
    public int getAttachementsSize() {
109
        return this.filePanels.size();
112
        return this.filePanels.size();
110
    }
113
    }
111
 
114
 
112
    public void initUI() {
115
    public void initUI() {
113
        this.filePanels.clear();
116
        this.filePanels.clear();
114
        this.invalidate();
117
        this.invalidate();
115
        this.removeAll();
118
        this.removeAll();
116
        GridBagConstraints c = new DefaultGridBagConstraints();
119
        GridBagConstraints c = new DefaultGridBagConstraints();
117
 
120
 
118
        // Recupération de la liste des fichiers
121
        // Recupération de la liste des fichiers
119
 
122
 
120
        // TODO requete dans un SwingWorker
123
        // TODO requete dans un SwingWorker
121
        final SQLTable tableAttachment = this.rowSource.getTable().getTable("ATTACHMENT");
124
        final SQLTable tableAttachment = this.rowSource.getTable().getTable("ATTACHMENT");
122
        SQLRowValues rowVals = new SQLRowValues(tableAttachment);
125
        SQLRowValues rowVals = new SQLRowValues(tableAttachment);
123
        rowVals.putNulls(tableAttachment.getFieldsName());
126
        rowVals.putNulls(tableAttachment.getFieldsName());
124
 
127
 
125
        SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowVals);
128
        SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowVals);
126
        Where where = new Where(tableAttachment.getField("SOURCE_TABLE"), "=", this.rowSource.getTable().getName());
129
        Where where = new Where(tableAttachment.getField("SOURCE_TABLE"), "=", this.rowSource.getTable().getName());
127
        where = where.and(new Where(tableAttachment.getField("SOURCE_ID"), "=", this.rowSource.getID()));
130
        where = where.and(new Where(tableAttachment.getField("SOURCE_ID"), "=", this.rowSource.getID()));
128
 
131
 
129
        where = where.and(new Where(tableAttachment.getField("ID_PARENT"), "=", this.idParent));
132
        where = where.and(new Where(tableAttachment.getField("ID_PARENT"), "=", this.idParent));
130
        for (SQLRowAccessor rowSecondaire : this.rowSecondaires) {
133
        for (SQLRowAccessor rowSecondaire : this.rowSecondaires) {
131
 
134
 
132
            Where whereSec = new Where(tableAttachment.getField("SOURCE_TABLE"), "=", rowSecondaire.getTable().getName());
135
            Where whereSec = new Where(tableAttachment.getField("SOURCE_TABLE"), "=", rowSecondaire.getTable().getName());
133
            whereSec = whereSec.and(new Where(tableAttachment.getField("SOURCE_ID"), "=", rowSecondaire.getID()));
136
            whereSec = whereSec.and(new Where(tableAttachment.getField("SOURCE_ID"), "=", rowSecondaire.getID()));
134
 
137
 
135
            where = where.or(whereSec);
138
            where = where.or(whereSec);
136
        }
139
        }
137
        final List<SQLRowValues> rAttachments = fetcher.fetch(where);
140
        final List<SQLRowValues> rAttachments = fetcher.fetch(where);
138
        c.fill = GridBagConstraints.BOTH;
141
        c.fill = GridBagConstraints.BOTH;
139
        c.anchor = GridBagConstraints.WEST;
142
        c.anchor = GridBagConstraints.WEST;
140
        c.gridx = 0;
143
        c.gridx = 0;
141
        c.gridy = 0;
144
        c.gridy = 0;
142
        // Folder paths
145
        // Folder paths
143
        if (this.parents.size() > 1) {
146
        if (this.parents.size() > 1) {
144
            JPanel navPanel = new JPanel();
147
            JPanel navPanel = new JPanel();
145
            navPanel.setBackground(Color.WHITE);
148
            navPanel.setBackground(Color.WHITE);
146
            navPanel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
149
            navPanel.setBorder(BorderFactory.createLineBorder(Color.GRAY));
147
            navPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 8, 2));
150
            navPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 8, 2));
148
 
151
 
149
            for (int i = 0; i < this.parents.size(); i++) {
152
            for (int i = 0; i < this.parents.size(); i++) {
150
                JLabel b = new JLabel(" " + this.parentsNames.get(i) + " ");
153
                JLabel b = new JLabel(" " + this.parentsNames.get(i) + " ");
151
                b.setOpaque(true);
154
                b.setOpaque(true);
152
                b.setBackground(new Color(230, 240, 255));
155
                b.setBackground(new Color(230, 240, 255));
153
                navPanel.add(b);
156
                navPanel.add(b);
154
                final int id = this.parents.get(i);
157
                final int id = this.parents.get(i);
155
                final int index = i;
158
                final int index = i;
156
                if (i < this.parents.size() - 1) {
159
                if (i < this.parents.size() - 1) {
157
                    b.addMouseListener(new MouseAdapter() {
160
                    b.addMouseListener(new MouseAdapter() {
158
                        @Override
161
                        @Override
159
                        public void mousePressed(MouseEvent e) {
162
                        public void mousePressed(MouseEvent e) {
160
                            AttachmentPanel.this.idParent = id;
163
                            AttachmentPanel.this.idParent = id;
161
                            int nb = AttachmentPanel.this.parents.size() - index - 1;
164
                            int nb = AttachmentPanel.this.parents.size() - index - 1;
162
                            for (int n = 0; n < nb; n++) {
165
                            for (int n = 0; n < nb; n++) {
163
                                final int pos = AttachmentPanel.this.parents.size() - 1;
166
                                final int pos = AttachmentPanel.this.parents.size() - 1;
164
                                AttachmentPanel.this.parents.remove(pos);
167
                                AttachmentPanel.this.parents.remove(pos);
165
                                AttachmentPanel.this.parentsNames.remove(pos);
168
                                AttachmentPanel.this.parentsNames.remove(pos);
166
                            }
169
                            }
167
                            clearSelection();
170
                            clearSelection();
168
                            initUI();
171
                            initUI();
169
                        }
172
                        }
170
                    });
173
                    });
171
                }
174
                }
172
                if (i < this.parents.size() - 1) {
175
                if (i < this.parents.size() - 1) {
173
                    b.setDropTarget(new DropTarget() {
176
                    b.setDropTarget(new DropTarget() {
174
 
177
 
175
                        @Override
178
                        @Override
176
                        public synchronized void drop(DropTargetDropEvent dtde) {
179
                        public synchronized void drop(DropTargetDropEvent dtde) {
177
                            dtde.acceptDrop(DnDConstants.ACTION_MOVE);
180
                            dtde.acceptDrop(DnDConstants.ACTION_MOVE);
178
                            Transferable t = dtde.getTransferable();
181
                            Transferable t = dtde.getTransferable();
179
                            try {
182
                            try {
180
                                for (int i = 0; i < t.getTransferDataFlavors().length; i++) {
183
                                for (int i = 0; i < t.getTransferDataFlavors().length; i++) {
181
                                    final DataFlavor dataFlavor = t.getTransferDataFlavors()[i];
184
                                    final DataFlavor dataFlavor = t.getTransferDataFlavors()[i];
182
                                    if (dataFlavor.isMimeTypeEqual(DataFlavor.javaSerializedObjectMimeType)) {
185
                                    if (dataFlavor.isMimeTypeEqual(DataFlavor.javaSerializedObjectMimeType)) {
183
                                        @SuppressWarnings("unchecked")
186
                                        @SuppressWarnings("unchecked")
184
                                        List<Attachment> attachments = (List<Attachment>) t.getTransferData(dataFlavor);
187
                                        List<Attachment> attachments = (List<Attachment>) t.getTransferData(dataFlavor);
185
                                        AttachmentUtils utils = new AttachmentUtils();
188
                                        AttachmentUtils utils = new AttachmentUtils();
186
                                        for (Attachment a : attachments) {
189
                                        for (Attachment a : attachments) {
187
                                            if (a.getParentId() != id) {
190
                                            if (a.getParentId() != id) {
188
                                                utils.move(a, id);
191
                                                utils.move(a, id);
189
                                            }
192
                                            }
190
                                        }
193
                                        }
191
                                        initUI();
194
                                        initUI();
192
                                        break;
195
                                        break;
193
                                    }
196
                                    }
194
                                }
197
                                }
195
 
198
 
196
                            } catch (Exception e) {
199
                            } catch (Exception e) {
197
                                e.printStackTrace();
200
                                e.printStackTrace();
198
                            }
201
                            }
199
                        }
202
                        }
200
                    });
203
                    });
201
                }
204
                }
202
 
205
 
203
            }
206
            }
204
            this.add(navPanel, c);
207
            this.add(navPanel, c);
205
            c.gridy++;
208
            c.gridy++;
206
        }
209
        }
207
 
210
 
208
        // Tools
211
        // Tools
209
        final JPanel toolbar = new JPanel();
212
        final JPanel toolbar = new JPanel();
210
        toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
213
        toolbar.setLayout(new FlowLayout(FlowLayout.LEFT));
211
        final JButton addFolderButton = new JButton("Nouveau dossier");
214
        final JButton addFolderButton = new JButton("Nouveau dossier");
212
        toolbar.add(addFolderButton);
215
        toolbar.add(addFolderButton);
213
        final JButton addFileButton = new JButton("Ajouter un fichier");
216
        final JButton addFileButton = new JButton("Ajouter un fichier");
214
        toolbar.add(addFileButton);
217
        toolbar.add(addFileButton);
-
 
218
        final JButton addURLButton = new JButton("Ajouter une URL");
-
 
219
        toolbar.add(addURLButton);
215
 
220
 
216
        final JProgressBar progressBar = new JProgressBar(0, 100);
221
        final JProgressBar progressBar = new JProgressBar(0, 100);
217
        progressBar.setValue(100);
222
        progressBar.setValue(100);
218
        progressBar.setStringPainted(true);
223
        progressBar.setStringPainted(true);
219
        progressBar.setVisible(false);
224
        progressBar.setVisible(false);
220
        toolbar.add(progressBar);
225
        toolbar.add(progressBar);
221
 
226
 
222
        this.add(toolbar, c);
227
        this.add(toolbar, c);
223
 
228
 
224
        c.gridy++;
229
        c.gridy++;
225
 
230
 
226
        addFolderButton.addActionListener(new ActionListener() {
231
        addFolderButton.addActionListener(new ActionListener() {
227
 
232
 
228
            @Override
233
            @Override
229
            public void actionPerformed(ActionEvent e) {
234
            public void actionPerformed(ActionEvent e) {
230
                AttachmentUtils utils = new AttachmentUtils();
235
                AttachmentUtils utils = new AttachmentUtils();
231
                try {
236
                try {
232
                    utils.createFolder("Nouveau dossier", AttachmentPanel.this.rowSource, AttachmentPanel.this.idParent);
237
                    utils.createFolder("Nouveau dossier", AttachmentPanel.this.rowSource, AttachmentPanel.this.idParent);
233
                } catch (SQLException e1) {
238
                } catch (SQLException e1) {
234
                    JOptionPane.showMessageDialog(null, "Impossible de créer le dossier.", "Erreur", JOptionPane.ERROR_MESSAGE);
239
                    JOptionPane.showMessageDialog(null, "Impossible de créer le dossier.", "Erreur", JOptionPane.ERROR_MESSAGE);
235
                }
240
                }
236
                initUI();
241
                initUI();
237
 
242
 
238
            }
243
            }
239
        });
244
        });
240
 
245
 
241
        addFileButton.addActionListener(new ActionListener() {
246
        addFileButton.addActionListener(new ActionListener() {
242
 
247
 
243
            @Override
248
            @Override
244
            public void actionPerformed(ActionEvent e) {
249
            public void actionPerformed(ActionEvent e) {
245
                final Frame frame = SwingThreadUtils.getAncestorOrSelf(Frame.class, (Component) e.getSource());
250
                final Frame frame = SwingThreadUtils.getAncestorOrSelf(Frame.class, (Component) e.getSource());
246
                final FileDialog fd = new FileDialog(frame, "Ajouter un fichier", FileDialog.LOAD);
251
                final FileDialog fd = new FileDialog(frame, "Ajouter un fichier", FileDialog.LOAD);
247
                fd.setVisible(true);
252
                fd.setVisible(true);
248
                final String fileName = fd.getFile();
253
                final String fileName = fd.getFile();
249
                if (fileName != null) {
254
                if (fileName != null) {
250
                    try {
255
                    try {
251
                        File inFile = new File(fd.getDirectory(), fileName);
256
                        File inFile = new File(fd.getDirectory(), fileName);
252
                        AttachmentUtils utils = new AttachmentUtils();
257
                        AttachmentUtils utils = new AttachmentUtils();
253
                        utils.uploadFile(inFile, AttachmentPanel.this.rowSource, AttachmentPanel.this.idParent);
258
                        utils.uploadFile(inFile, AttachmentPanel.this.rowSource, AttachmentPanel.this.idParent);
254
                        initUI();
259
                        initUI();
255
                    } catch (Exception ex) {
260
                    } catch (Exception ex) {
256
                        ex.printStackTrace();
261
                        ex.printStackTrace();
257
                        JOptionPane.showMessageDialog(AttachmentPanel.this, "Erreur lors de l'ajout du fichier " + fileName + "(" + ex.getMessage() + ")");
262
                        JOptionPane.showMessageDialog(AttachmentPanel.this, "Erreur lors de l'ajout du fichier " + fileName + "(" + ex.getMessage() + ")");
258
                    }
263
                    }
259
                }
264
                }
260
            }
265
            }
261
        });
266
        });
262
 
267
 
-
 
268
        addURLButton.addActionListener(new ActionListener() {
-
 
269
	        @Override
-
 
270
	        public void actionPerformed(ActionEvent e) {
-
 
271
	            final Frame frame = SwingThreadUtils.getAncestorOrSelf(Frame.class, (Component) e.getSource());
-
 
272
	            final String fileName = JOptionPane.showInputDialog(frame, "Veuillez entrer votre URL");
-
 
273
	
-
 
274
	            if (fileName != null) {
-
 
275
	                AttachmentUtils utils = new AttachmentUtils();
-
 
276
	                try {
-
 
277
	                    utils.createURL(fileName, rowSource, idParent);
-
 
278
	                } catch (SQLException e1) {
-
 
279
	                    JOptionPane.showMessageDialog(null, "Impossible de créer l'URL.", "Erreur", JOptionPane.ERROR_MESSAGE);
-
 
280
	                }
-
 
281
	                initUI();
-
 
282
	            }
-
 
283
	        }
-
 
284
	    });
-
 
285
 
263
        ScrollablePanel files = new ScrollablePanel() {
286
        ScrollablePanel files = new ScrollablePanel() {
264
            @Override
287
            @Override
265
            public Dimension getPreferredSize() {
288
            public Dimension getPreferredSize() {
266
                int w = getSize().width;
289
                int w = getSize().width;
267
                int nbPerRow = (w - 5) / (FilePanel.PREFERRED_WIDTH + 5);
290
                int nbPerRow = (w - 5) / (FilePanel.PREFERRED_WIDTH + 5);
268
                if (nbPerRow < 1) {
291
                if (nbPerRow < 1) {
269
                    nbPerRow = 1;
292
                    nbPerRow = 1;
270
                }
293
                }
271
                int nbRow = 1 + (getComponentCount() / nbPerRow);
294
                int nbRow = 1 + (getComponentCount() / nbPerRow);
272
                if (nbRow < 1) {
295
                if (nbRow < 1) {
273
                    nbRow = 1;
296
                    nbRow = 1;
274
                }
297
                }
275
                return new Dimension(w, 5 + nbRow * (FilePanel.PREFERRED_HEIGHT + 5));
298
                return new Dimension(w, 5 + nbRow * (FilePanel.PREFERRED_HEIGHT + 5));
276
            }
299
            }
277
 
300
 
278
        };
301
        };
279
        files.setOpaque(true);
302
        files.setOpaque(true);
280
        files.setBackground(Color.WHITE);
303
        files.setBackground(Color.WHITE);
281
        files.setScrollableWidth(ScrollablePanel.ScrollableSizeHint.FIT);
304
        files.setScrollableWidth(ScrollablePanel.ScrollableSizeHint.FIT);
282
        files.setScrollableHeight(ScrollablePanel.ScrollableSizeHint.NONE);
305
        files.setScrollableHeight(ScrollablePanel.ScrollableSizeHint.NONE);
283
        files.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
306
        files.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
284
        final List<Attachment> attachments = new ArrayList<>(rAttachments.size());
307
        final List<Attachment> attachments = new ArrayList<>(rAttachments.size());
285
        for (final SQLRowValues sqlRowValues : rAttachments) {
308
        for (final SQLRowValues sqlRowValues : rAttachments) {
286
            final Attachment a = new Attachment(sqlRowValues);
309
            final Attachment a = new Attachment(sqlRowValues);
287
            attachments.add(a);
310
            attachments.add(a);
288
        }
311
        }
289
        Collections.sort(attachments, new Comparator<Attachment>() {
312
        Collections.sort(attachments, new Comparator<Attachment>() {
290
 
313
 
291
            @Override
314
            @Override
292
            public int compare(Attachment o1, Attachment o2) {
315
            public int compare(Attachment o1, Attachment o2) {
293
                if (o1.isFolder() && !o2.isFolder()) {
316
                if (o1.isFolder() && !o2.isFolder()) {
294
                    return -1;
317
                    return -1;
295
                }
318
                }
296
                if (!o1.isFolder() && o2.isFolder()) {
319
                if (!o1.isFolder() && o2.isFolder()) {
297
                    return 1;
320
                    return 1;
298
                }
321
                }
299
                return o1.getName().compareTo(o2.getName());
322
                return o1.getName().compareTo(o2.getName());
300
            }
323
            }
301
        });
324
        });
302
 
325
 
303
        // Liste des fichiers
326
        // Liste des fichiers
304
        for (final Attachment a : attachments) {
327
        for (final Attachment a : attachments) {
305
            final FilePanel filePanel = new FilePanel(a, this);
328
            final FilePanel filePanel = new FilePanel(a, this);
306
            this.filePanels.add(filePanel);
329
            this.filePanels.add(filePanel);
307
            filePanel.addMouseListener(new MouseAdapter() {
330
            filePanel.addMouseListener(new MouseAdapter() {
308
                @Override
331
                @Override
309
                public void mousePressed(MouseEvent e) {
332
                public void mousePressed(MouseEvent e) {
310
                    if (e.getClickCount() == 2) {
333
                    if (e.getClickCount() == 2) {
311
                        if (a.isFolder()) {
334
                        if (a.isFolder()) {
312
                            openFolder(a);
335
                            openFolder(a);
-
 
336
                        } else if( a.getMimeType().equals(Attachment.MIMETYPE_URL) ) {
-
 
337
                            try {
-
 
338
                                Desktop.getDesktop().browse(new URI(a.getFileName()));
-
 
339
                            } catch (IOException | URISyntaxException e1) {
-
 
340
                                ExceptionHandler.handle("Malformation URL", e1);
-
 
341
                            }
313
                        } else {
342
                        } else {
314
                            final Thread t = new Thread() {
343
                            final Thread t = new Thread() {
315
                                @Override
344
                                @Override
316
                                public void run() {
345
                                public void run() {
317
                                    AttachmentUtils utils = new AttachmentUtils();
346
                                    AttachmentUtils utils = new AttachmentUtils();
318
                                    File f = utils.getFile(a);
347
                                    File f = utils.getFile(a);
319
                                    if (f == null) {
348
                                    if (f == null) {
320
                                        SwingUtilities.invokeLater(new Runnable() {
349
                                        SwingUtilities.invokeLater(new Runnable() {
321
                                            public void run() {
350
                                            public void run() {
322
                                                JOptionPane.showMessageDialog(null, "Impossible de récupérer le fichier.", "Erreur", JOptionPane.ERROR_MESSAGE);
351
                                                JOptionPane.showMessageDialog(null, "Impossible de récupérer le fichier.", "Erreur", JOptionPane.ERROR_MESSAGE);
323
                                            }
352
                                            }
324
                                        });
353
                                        });
325
                                    } else {
354
                                    } else {
326
                                        try {
355
                                        try {
327
                                            FileUtils.openFile(f);
356
                                            FileUtils.openFile(f);
328
                                        } catch (IOException e1) {
357
                                        } catch (IOException e1) {
329
                                            ExceptionHandler.handle("Erreur lors de l'ouverture du fichier.", e1);
358
                                            ExceptionHandler.handle("Erreur lors de l'ouverture du fichier.", e1);
330
                                        }
359
                                        }
331
 
360
 
332
                                    }
361
                                    }
333
                                }
362
                                }
334
                            };
363
                            };
335
                            t.start();
364
                            t.start();
336
                        }
365
                        }
337
                    }
366
                    }
338
                }
367
                }
339
 
368
 
340
            });
369
            });
341
            files.add(filePanel);
370
            files.add(filePanel);
342
 
371
 
343
        }
372
        }
344
 
373
 
345
        c.gridx = 0;
374
        c.gridx = 0;
346
        c.gridy++;
375
        c.gridy++;
347
        c.weightx = 1;
376
        c.weightx = 1;
348
        c.weighty = 1;
377
        c.weighty = 1;
349
 
378
 
350
        final JScrollPane scroll = new JScrollPane(files);
379
        final JScrollPane scroll = new JScrollPane(files);
351
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
380
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
352
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
381
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
353
        scroll.setMinimumSize(new Dimension((int) (400 * 1.618), 400));
382
        scroll.setMinimumSize(new Dimension((int) (400 * 1.618), 400));
354
        scroll.setPreferredSize(new Dimension((int) (400 * 1.618), 400));
383
        scroll.setPreferredSize(new Dimension((int) (400 * 1.618), 400));
355
        scroll.setBackground(Color.WHITE);
384
        scroll.setBackground(Color.WHITE);
356
        scroll.getViewport().setBackground(Color.WHITE);
385
        scroll.getViewport().setBackground(Color.WHITE);
357
        this.add(scroll, c);
386
        this.add(scroll, c);
358
        scroll.addMouseListener(new MouseAdapter() {
387
        scroll.addMouseListener(new MouseAdapter() {
359
            @Override
388
            @Override
360
            public void mousePressed(MouseEvent e) {
389
            public void mousePressed(MouseEvent e) {
361
                clearSelection();
390
                clearSelection();
362
                updatePanels();
391
                updatePanels();
363
            }
392
            }
364
        });
393
        });
365
 
394
 
366
        this.validate();
395
        this.validate();
367
        this.repaint();
396
        this.repaint();
368
 
397
 
369
        DropTarget dt = new DropTarget() {
398
        DropTarget dt = new DropTarget() {
370
 
399
 
371
            @Override
400
            @Override
372
            public synchronized void drop(DropTargetDropEvent dtde) {
401
            public synchronized void drop(DropTargetDropEvent dtde) {
373
                dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
402
                dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
374
                Transferable t = dtde.getTransferable();
403
                Transferable t = dtde.getTransferable();
375
                try {
404
                try {
376
                    if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
405
                    if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
377
                        @SuppressWarnings("unchecked")
406
                        @SuppressWarnings("unchecked")
378
                        List<File> fileList = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
407
                        List<File> fileList = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
379
                        // TODO faire en arriere plan, mettre une jauge à droite du bouton ajouter
408
                        // TODO faire en arriere plan, mettre une jauge à droite du bouton ajouter
380
                        // et mettre un bouton d'annulation
409
                        // et mettre un bouton d'annulation
381
                        AttachmentUtils utils = new AttachmentUtils();
410
                        AttachmentUtils utils = new AttachmentUtils();
382
                        boolean cancelledByUser = false;
411
                        boolean cancelledByUser = false;
383
                        for (File f : fileList) {
412
                        for (File f : fileList) {
384
                            if (cancelledByUser) {
413
                            if (cancelledByUser) {
385
                                break;
414
                                break;
386
                            }
415
                            }
387
                            try {
416
                            try {
388
                                if (!f.isDirectory()) {
417
                                if (!f.isDirectory()) {
389
                                    utils.uploadFile(f, AttachmentPanel.this.rowSource, AttachmentPanel.this.idParent);
418
                                    utils.uploadFile(f, AttachmentPanel.this.rowSource, AttachmentPanel.this.idParent);
390
                                }
419
                                }
391
                            } catch (Exception ex) {
420
                            } catch (Exception ex) {
392
                                ex.printStackTrace();
421
                                ex.printStackTrace();
393
                                JOptionPane.showMessageDialog(AttachmentPanel.this, "Erreur lors de l'ajout du fichier " + f.getAbsolutePath() + "(" + ex.getMessage() + ")");
422
                                JOptionPane.showMessageDialog(AttachmentPanel.this, "Erreur lors de l'ajout du fichier " + f.getAbsolutePath() + "(" + ex.getMessage() + ")");
394
                            }
423
                            }
395
                        }
424
                        }
396
                        initUI();
425
                        initUI();
397
                    }
426
                    }
398
                } catch (Exception e) {
427
                } catch (Exception e) {
399
                    e.printStackTrace();
428
                    e.printStackTrace();
400
                }
429
                }
401
            }
430
            }
402
        };
431
        };
403
        files.setDropTarget(dt);
432
        files.setDropTarget(dt);
404
        scroll.getViewport().setDropTarget(dt);
433
        scroll.getViewport().setDropTarget(dt);
405
        fireDataChanged();
434
        fireDataChanged();
406
    }
435
    }
407
 
436
 
408
    public Set<Attachment> getSelectedAttachments() {
437
    public Set<Attachment> getSelectedAttachments() {
409
        return this.selectedAttachments;
438
        return this.selectedAttachments;
410
    }
439
    }
411
 
440
 
412
    public void select(Attachment a) {
441
    public void select(Attachment a) {
413
        this.selectedAttachments.add(a);
442
        this.selectedAttachments.add(a);
414
    }
443
    }
415
 
444
 
416
    public void deselect(Attachment a) {
445
    public void deselect(Attachment a) {
417
        this.selectedAttachments.remove(a);
446
        this.selectedAttachments.remove(a);
418
    }
447
    }
419
 
448
 
420
    public boolean isSelected(Attachment a) {
449
    public boolean isSelected(Attachment a) {
421
        return this.selectedAttachments.contains(a);
450
        return this.selectedAttachments.contains(a);
422
    }
451
    }
423
 
452
 
424
    public void clearSelection() {
453
    public void clearSelection() {
425
        this.selectedAttachments.clear();
454
        this.selectedAttachments.clear();
426
    }
455
    }
427
 
456
 
428
    public void updatePanels() {
457
    public void updatePanels() {
429
        for (FilePanel p : this.filePanels) {
458
        for (FilePanel p : this.filePanels) {
430
            p.updateBackgroundFromState();
459
            p.updateBackgroundFromState();
431
        }
460
        }
432
    }
461
    }
433
 
462
 
434
    public void openFolder(final Attachment a) {
463
    public void openFolder(final Attachment a) {
435
        if (!a.isFolder()) {
464
        if (!a.isFolder()) {
436
            throw new IllegalStateException(a.getName() + " is not a folder");
465
            throw new IllegalStateException(a.getName() + " is not a folder");
437
        }
466
        }
438
        clearSelection();
467
        clearSelection();
439
        this.idParent = a.getId();
468
        this.idParent = a.getId();
440
        this.parents.add(a.getId());
469
        this.parents.add(a.getId());
441
        this.parentsNames.add(a.getName());
470
        this.parentsNames.add(a.getName());
442
        initUI();
471
        initUI();
443
    }
472
    }
444
}
473
}