OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 149 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
142 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
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
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.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.erp.core.edm;
15
 
16
import org.openconcerto.erp.core.common.ui.ScrollablePanel;
17
import org.openconcerto.sql.model.SQLRowAccessor;
18
import org.openconcerto.sql.model.SQLRowValues;
19
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
20
import org.openconcerto.sql.model.SQLTable;
21
import org.openconcerto.sql.model.Where;
22
import org.openconcerto.ui.DefaultGridBagConstraints;
23
import org.openconcerto.ui.SwingThreadUtils;
24
import org.openconcerto.utils.ExceptionHandler;
25
import org.openconcerto.utils.FileUtils;
26
 
27
import java.awt.Color;
28
import java.awt.Component;
29
import java.awt.Dimension;
30
import java.awt.FileDialog;
31
import java.awt.FlowLayout;
32
import java.awt.Frame;
33
import java.awt.GridBagConstraints;
34
import java.awt.GridBagLayout;
35
import java.awt.datatransfer.DataFlavor;
36
import java.awt.datatransfer.Transferable;
37
import java.awt.dnd.DnDConstants;
38
import java.awt.dnd.DropTarget;
39
import java.awt.dnd.DropTargetDropEvent;
40
import java.awt.event.ActionEvent;
41
import java.awt.event.ActionListener;
42
import java.awt.event.MouseAdapter;
43
import java.awt.event.MouseEvent;
44
import java.io.File;
45
import java.io.IOException;
46
import java.util.ArrayList;
47
import java.util.List;
48
 
49
import javax.swing.JButton;
50
import javax.swing.JOptionPane;
51
import javax.swing.JPanel;
52
import javax.swing.JProgressBar;
53
import javax.swing.JScrollPane;
54
import javax.swing.SwingUtilities;
55
import javax.swing.event.ListDataEvent;
56
import javax.swing.event.ListDataListener;
57
 
58
public class AttachmentPanel extends JPanel {
59
 
60
    private final SQLRowAccessor rowSource;
61
    private List<ListDataListener> listeners = new ArrayList<ListDataListener>();
62
 
63
    public AttachmentPanel(SQLRowAccessor rowSource) {
64
        super();
65
        this.rowSource = rowSource;
66
        this.setLayout(new GridBagLayout());
67
        initUI();
68
        setFocusable(true);
69
    }
70
 
71
    public void addListener(ListDataListener l) {
72
        this.listeners.add(l);
73
    }
74
 
75
    public void removeListener(ListDataListener l) {
76
        this.listeners.remove(l);
77
    }
78
 
79
    public void fireDataChanged() {
80
        for (ListDataListener listDataListener : listeners) {
81
            listDataListener.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, 0));
82
        }
83
    }
84
 
85
    public void initUI() {
86
        this.invalidate();
87
        this.removeAll();
88
        GridBagConstraints c = new DefaultGridBagConstraints();
89
 
90
        // Recupération de la liste des fichiers
91
 
92
        // TODO requete dans un SwingWorker
93
        final SQLTable tableAttachment = rowSource.getTable().getTable("ATTACHMENT");
94
        SQLRowValues rowVals = new SQLRowValues(tableAttachment);
95
        rowVals.putNulls(tableAttachment.getFieldsName());
96
 
97
        SQLRowValuesListFetcher fetcher = SQLRowValuesListFetcher.create(rowVals);
98
        Where where = new Where(tableAttachment.getField("SOURCE_TABLE"), "=", this.rowSource.getTable().getName());
99
        where = where.and(new Where(tableAttachment.getField("SOURCE_ID"), "=", this.rowSource.getID()));
100
        // TODO en premier les dossier, puis trier par nom
101
        List<SQLRowValues> attachments = fetcher.fetch(where);
102
 
103
        // AJout d'un fichier
104
        final JButton addButton = new JButton("Ajouter un fichier");
105
        this.add(addButton, c);
106
 
107
        c.gridx++;
108
        c.weightx = 1;
109
        c.fill = GridBagConstraints.HORIZONTAL;
110
        final JProgressBar progressBar = new JProgressBar(0, 100);
111
        progressBar.setValue(100);
112
        progressBar.setStringPainted(true);
113
        progressBar.setVisible(false);
114
        this.add(progressBar, c);
115
        c.fill = GridBagConstraints.BOTH;
116
        c.gridx = 0;
117
        c.gridy++;
118
 
119
        addButton.addActionListener(new ActionListener() {
120
 
121
            @Override
122
            public void actionPerformed(ActionEvent e) {
123
                final Frame frame = SwingThreadUtils.getAncestorOrSelf(Frame.class, (Component) e.getSource());
124
 
125
                final FileDialog fd = new FileDialog(frame, "Ajouter un fichier", FileDialog.LOAD);
126
                fd.setVisible(true);
127
                final String fileName = fd.getFile();
128
                if (fileName != null) {
129
                    File inFile = new File(fd.getDirectory(), fileName);
130
                    AttachmentUtils utils = new AttachmentUtils();
131
                    utils.uploadFile(inFile, rowSource);
132
                    initUI();
133
                }
134
            }
135
        });
136
 
137
        ScrollablePanel files = new ScrollablePanel() {
138
            @Override
139
            public Dimension getPreferredSize() {
140
                int w = getSize().width;
141
                int nbPerRow = (w - 5) / (FilePanel.WIDTH + 5);
142
                if (nbPerRow < 1) {
143
                    nbPerRow = 1;
144
                }
145
                int nbRow = 1 + (getComponentCount() / nbPerRow);
146
                if (nbRow < 1) {
147
                    nbRow = 1;
148
                }
149
                return new Dimension(w, 5 + nbRow * (FilePanel.HEIGHT + 5));
150
            }
151
 
152
        };
153
        files.setOpaque(true);
154
        files.setBackground(Color.WHITE);
155
        files.setScrollableWidth(ScrollablePanel.ScrollableSizeHint.FIT);
156
        files.setScrollableHeight(ScrollablePanel.ScrollableSizeHint.NONE);
157
        files.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
158
        // Liste des fichiers
159
        for (final SQLRowValues sqlRowValues : attachments) {
160
 
161
            final FilePanel filePanel = new FilePanel(sqlRowValues, this);
162
            filePanel.addMouseListener(new MouseAdapter() {
163
                @Override
164
                public void mousePressed(MouseEvent e) {
165
                    if (e.getClickCount() == 2) {
166
                        Thread t = new Thread() {
167
                            @Override
168
                            public void run() {
169
                                AttachmentUtils utils = new AttachmentUtils();
170
                                File f = utils.getFile(sqlRowValues);
171
                                if (f == null) {
172
                                    SwingUtilities.invokeLater(new Runnable() {
173
                                        public void run() {
174
                                            JOptionPane.showMessageDialog(null, "Impossible de récupérer le fichier.", "Erreur", JOptionPane.ERROR_MESSAGE);
175
                                        }
176
                                    });
177
                                    System.err.println("Impossible de récupérer le fichier.");
178
                                } else {
179
                                    try {
180
                                        FileUtils.openFile(f);
181
                                    } catch (IOException e1) {
182
                                        ExceptionHandler.handle("Erreur lors de l'ouverture du fichier.", e1);
183
                                    }
184
                                }
185
                            }
186
                        };
187
                        t.start();
188
                    }
189
                }
190
            });
191
            files.add(filePanel);
192
 
193
        }
194
        c.gridwidth = 2;
195
        c.gridx = 0;
196
        c.gridy++;
197
        c.weightx = 1;
198
        c.weighty = 1;
199
        JScrollPane scroll = new JScrollPane(files);
200
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
201
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
202
        scroll.setMinimumSize(new Dimension((int) (400 * 1.618), 400));
203
        scroll.setPreferredSize(new Dimension((int) (400 * 1.618), 400));
204
        scroll.setBackground(Color.WHITE);
205
        scroll.getViewport().setBackground(Color.WHITE);
206
        this.add(scroll, c);
207
 
208
        this.validate();
209
        this.repaint();
210
 
211
        DropTarget dt = new DropTarget() {
212
            @Override
213
            public synchronized void drop(DropTargetDropEvent dtde) {
214
                dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
215
                Transferable t = dtde.getTransferable();
216
                try {
217
 
218
                    if (t.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
219
                        @SuppressWarnings("unchecked")
220
                        List<File> fileList = (List<File>) t.getTransferData(DataFlavor.javaFileListFlavor);
221
                        // TODO faire en arriere plan, mettre une jauge à droite du bouton ajouter
222
                        // et mettre un bouton d'annulation
223
                        AttachmentUtils utils = new AttachmentUtils();
224
                        boolean cancelledByUser = false;
225
                        for (File f : fileList) {
226
                            if (cancelledByUser) {
227
                                break;
228
                            }
229
                            utils.uploadFile(f, rowSource);
230
                        }
231
                        initUI();
232
                    }
233
                } catch (Exception e) {
234
                    // TODO Auto-generated catch block
235
                    e.printStackTrace();
236
                }
237
            }
238
        };
239
        files.setDropTarget(dt);
240
        scroll.getViewport().setDropTarget(dt);
241
        fireDataChanged();
242
    }
243
 
244
}