OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Rev 182 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 149 Rev 180
Line 43... Line 43...
43
import java.awt.event.MouseEvent;
43
import java.awt.event.MouseEvent;
44
import java.io.File;
44
import java.io.File;
45
import java.io.IOException;
45
import java.io.IOException;
46
import java.sql.SQLException;
46
import java.sql.SQLException;
47
import java.util.ArrayList;
47
import java.util.ArrayList;
-
 
48
import java.util.Collection;
48
import java.util.Collections;
49
import java.util.Collections;
49
import java.util.Comparator;
50
import java.util.Comparator;
50
import java.util.HashSet;
51
import java.util.HashSet;
51
import java.util.List;
52
import java.util.List;
52
import java.util.Set;
53
import java.util.Set;
Line 63... Line 64...
63
import javax.swing.event.ListDataListener;
64
import javax.swing.event.ListDataListener;
64
 
65
 
65
public class AttachmentPanel extends JPanel {
66
public class AttachmentPanel extends JPanel {
66
 
67
 
67
    private final SQLRowAccessor rowSource;
68
    private final SQLRowAccessor rowSource;
-
 
69
    private final Collection<SQLRowAccessor> rowSecondaires;
68
    private List<ListDataListener> listeners = new ArrayList<>();
70
    private List<ListDataListener> listeners = new ArrayList<>();
69
 
71
 
70
    private int idParent = 1;
72
    private int idParent = 1;
71
    private List<Integer> parents = new ArrayList<>();
73
    private List<Integer> parents = new ArrayList<>();
72
    private List<String> parentsNames = new ArrayList<>();
74
    private List<String> parentsNames = new ArrayList<>();
73
    private Set<Attachment> selectedAttachments = new HashSet<>();
75
    private Set<Attachment> selectedAttachments = new HashSet<>();
74
    private List<FilePanel> filePanels = new ArrayList<>();
76
    private List<FilePanel> filePanels = new ArrayList<>();
75
 
77
 
76
    public AttachmentPanel(SQLRowAccessor rowSource) {
78
    public AttachmentPanel(SQLRowAccessor rowSource) {
-
 
79
        this(rowSource, Collections.emptyList());
-
 
80
    }
-
 
81
 
-
 
82
    public AttachmentPanel(SQLRowAccessor rowSource, Collection<SQLRowAccessor> rowSecondaires) {
77
        super();
83
        super();
78
        this.rowSource = rowSource;
84
        this.rowSource = rowSource;
-
 
85
        this.rowSecondaires = rowSecondaires;
-
 
86
 
79
        this.setLayout(new GridBagLayout());
87
        this.setLayout(new GridBagLayout());
80
        this.parents.add(1);
88
        this.parents.add(1);
81
        this.parentsNames.add("Racine");
89
        this.parentsNames.add("Racine");
82
        initUI();
90
        initUI();
83
        setFocusable(true);
91
        setFocusable(true);
Line 90... Line 98...
90
    public void removeListener(ListDataListener l) {
98
    public void removeListener(ListDataListener l) {
91
        this.listeners.remove(l);
99
        this.listeners.remove(l);
92
    }
100
    }
93
 
101
 
94
    public void fireDataChanged() {
102
    public void fireDataChanged() {
95
        for (ListDataListener listDataListener : listeners) {
103
        for (ListDataListener listDataListener : this.listeners) {
96
            listDataListener.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, 0));
104
            listDataListener.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, 0));
97
        }
105
        }
98
    }
106
    }
99
 
107
 
-
 
108
    public int getAttachementsSize() {
-
 
109
        return this.filePanels.size();
-
 
110
    }
-
 
111
 
100
    public void initUI() {
112
    public void initUI() {
101
        filePanels.clear();
113
        this.filePanels.clear();
102
        this.invalidate();
114
        this.invalidate();
103
        this.removeAll();
115
        this.removeAll();
104
        GridBagConstraints c = new DefaultGridBagConstraints();
116
        GridBagConstraints c = new DefaultGridBagConstraints();
105
 
117
 
106
        // Recupération de la liste des fichiers
118
        // Recupération de la liste des fichiers
107
 
119
 
108
        // TODO requete dans un SwingWorker
120
        // TODO requete dans un SwingWorker
109
        final SQLTable tableAttachment = rowSource.getTable().getTable("ATTACHMENT");
121
        final SQLTable tableAttachment = this.rowSource.getTable().getTable("ATTACHMENT");
110
        SQLRowValues rowVals = new SQLRowValues(tableAttachment);
122
        SQLRowValues rowVals = new SQLRowValues(tableAttachment);
111
        rowVals.putNulls(tableAttachment.getFieldsName());
123
        rowVals.putNulls(tableAttachment.getFieldsName());
112
 
124
 
113
        SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowVals);
125
        SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowVals);
114
        Where where = new Where(tableAttachment.getField("SOURCE_TABLE"), "=", this.rowSource.getTable().getName());
126
        Where where = new Where(tableAttachment.getField("SOURCE_TABLE"), "=", this.rowSource.getTable().getName());
115
        where = where.and(new Where(tableAttachment.getField("SOURCE_ID"), "=", this.rowSource.getID()));
127
        where = where.and(new Where(tableAttachment.getField("SOURCE_ID"), "=", this.rowSource.getID()));
116
 
128
 
117
        where = where.and(new Where(tableAttachment.getField("ID_PARENT"), "=", this.idParent));
129
        where = where.and(new Where(tableAttachment.getField("ID_PARENT"), "=", this.idParent));
-
 
130
        for (SQLRowAccessor rowSecondaire : this.rowSecondaires) {
-
 
131
 
-
 
132
            Where whereSec = new Where(tableAttachment.getField("SOURCE_TABLE"), "=", rowSecondaire.getTable().getName());
-
 
133
            whereSec = whereSec.and(new Where(tableAttachment.getField("SOURCE_ID"), "=", rowSecondaire.getID()));
118
 
134
 
-
 
135
            where = where.or(whereSec);
-
 
136
        }
119
        final List<SQLRowValues> rAttachments = fetcher.fetch(where);
137
        final List<SQLRowValues> rAttachments = fetcher.fetch(where);
120
        c.fill = GridBagConstraints.BOTH;
138
        c.fill = GridBagConstraints.BOTH;
121
        c.anchor = GridBagConstraints.WEST;
139
        c.anchor = GridBagConstraints.WEST;
122
        c.gridx = 0;
140
        c.gridx = 0;
123
        c.gridy = 0;
141
        c.gridy = 0;
Line 209... Line 227...
209
 
227
 
210
            @Override
228
            @Override
211
            public void actionPerformed(ActionEvent e) {
229
            public void actionPerformed(ActionEvent e) {
212
                AttachmentUtils utils = new AttachmentUtils();
230
                AttachmentUtils utils = new AttachmentUtils();
213
                try {
231
                try {
214
                    utils.createFolder("Nouveau dossier", rowSource, idParent);
232
                    utils.createFolder("Nouveau dossier", AttachmentPanel.this.rowSource, AttachmentPanel.this.idParent);
215
                } catch (SQLException e1) {
233
                } catch (SQLException e1) {
216
                    JOptionPane.showMessageDialog(null, "Impossible de créer le dossier.", "Erreur", JOptionPane.ERROR_MESSAGE);
234
                    JOptionPane.showMessageDialog(null, "Impossible de créer le dossier.", "Erreur", JOptionPane.ERROR_MESSAGE);
217
                }
235
                }
218
                initUI();
236
                initUI();
219
 
237
 
Line 227... Line 245...
227
                final Frame frame = SwingThreadUtils.getAncestorOrSelf(Frame.class, (Component) e.getSource());
245
                final Frame frame = SwingThreadUtils.getAncestorOrSelf(Frame.class, (Component) e.getSource());
228
                final FileDialog fd = new FileDialog(frame, "Ajouter un fichier", FileDialog.LOAD);
246
                final FileDialog fd = new FileDialog(frame, "Ajouter un fichier", FileDialog.LOAD);
229
                fd.setVisible(true);
247
                fd.setVisible(true);
230
                final String fileName = fd.getFile();
248
                final String fileName = fd.getFile();
231
                if (fileName != null) {
249
                if (fileName != null) {
-
 
250
                    try {
232
                    File inFile = new File(fd.getDirectory(), fileName);
251
                        File inFile = new File(fd.getDirectory(), fileName);
233
                    AttachmentUtils utils = new AttachmentUtils();
252
                        AttachmentUtils utils = new AttachmentUtils();
234
                    utils.uploadFile(inFile, rowSource, idParent);
253
                        utils.uploadFile(inFile, AttachmentPanel.this.rowSource, AttachmentPanel.this.idParent);
235
                    initUI();
254
                        initUI();
-
 
255
                    } catch (Exception ex) {
-
 
256
                        ex.printStackTrace();
-
 
257
                        JOptionPane.showMessageDialog(AttachmentPanel.this, "Erreur lors de l'ajout du fichier " + fileName + "(" + ex.getMessage() + ")");
-
 
258
                    }
236
                }
259
                }
237
            }
260
            }
238
        });
261
        });
239
 
262
 
240
        ScrollablePanel files = new ScrollablePanel() {
263
        ScrollablePanel files = new ScrollablePanel() {
Line 348... Line 371...
348
            @Override
371
            @Override
349
            public synchronized void drop(DropTargetDropEvent dtde) {
372
            public synchronized void drop(DropTargetDropEvent dtde) {
350
                dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
373
                dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
351
                Transferable t = dtde.getTransferable();
374
                Transferable t = dtde.getTransferable();
352
                try {
375
                try {
353
 
-
 
354
                    if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
376
                    if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
355
                        @SuppressWarnings("unchecked")
377
                        @SuppressWarnings("unchecked")
356
                        List<File> fileList = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
378
                        List<File> fileList = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
357
                        // TODO faire en arriere plan, mettre une jauge à droite du bouton ajouter
379
                        // TODO faire en arriere plan, mettre une jauge à droite du bouton ajouter
358
                        // et mettre un bouton d'annulation
380
                        // et mettre un bouton d'annulation
Line 360... Line 382...
360
                        boolean cancelledByUser = false;
382
                        boolean cancelledByUser = false;
361
                        for (File f : fileList) {
383
                        for (File f : fileList) {
362
                            if (cancelledByUser) {
384
                            if (cancelledByUser) {
363
                                break;
385
                                break;
364
                            }
386
                            }
-
 
387
                            try {
365
                            if (!f.isDirectory()) {
388
                                if (!f.isDirectory()) {
366
                                utils.uploadFile(f, rowSource, idParent);
389
                                    utils.uploadFile(f, AttachmentPanel.this.rowSource, AttachmentPanel.this.idParent);
-
 
390
                                }
-
 
391
                            } catch (Exception ex) {
-
 
392
                                ex.printStackTrace();
-
 
393
                                JOptionPane.showMessageDialog(AttachmentPanel.this, "Erreur lors de l'ajout du fichier " + f.getAbsolutePath() + "(" + ex.getMessage() + ")");
367
                            }
394
                            }
368
                        }
395
                        }
369
                        initUI();
396
                        initUI();
370
                    }
397
                    }
371
                } catch (Exception e) {
398
                } catch (Exception e) {
Line 377... Line 404...
377
        scroll.getViewport().setDropTarget(dt);
404
        scroll.getViewport().setDropTarget(dt);
378
        fireDataChanged();
405
        fireDataChanged();
379
    }
406
    }
380
 
407
 
381
    public Set<Attachment> getSelectedAttachments() {
408
    public Set<Attachment> getSelectedAttachments() {
382
        return selectedAttachments;
409
        return this.selectedAttachments;
383
    }
410
    }
384
 
411
 
385
    public void select(Attachment a) {
412
    public void select(Attachment a) {
386
        this.selectedAttachments.add(a);
413
        this.selectedAttachments.add(a);
387
    }
414
    }