OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 151 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 151 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.ui.DefaultGridBagConstraints;
16
import org.openconcerto.ui.DefaultGridBagConstraints;
17
import org.openconcerto.utils.ExceptionHandler;
17
import org.openconcerto.utils.ExceptionHandler;
18
import org.openconcerto.utils.JImage;
18
import org.openconcerto.utils.JImage;
19
 
19
 
20
import java.awt.BorderLayout;
20
import java.awt.BorderLayout;
21
import java.awt.Color;
21
import java.awt.Color;
22
import java.awt.Dimension;
22
import java.awt.Dimension;
23
import java.awt.GridBagConstraints;
23
import java.awt.GridBagConstraints;
24
import java.awt.GridBagLayout;
24
import java.awt.GridBagLayout;
25
import java.awt.datatransfer.UnsupportedFlavorException;
25
import java.awt.datatransfer.UnsupportedFlavorException;
26
import java.awt.event.ActionEvent;
26
import java.awt.event.ActionEvent;
27
import java.awt.event.ActionListener;
27
import java.awt.event.ActionListener;
28
import java.awt.event.FocusEvent;
28
import java.awt.event.FocusEvent;
29
import java.awt.event.FocusListener;
29
import java.awt.event.FocusListener;
30
import java.awt.event.KeyAdapter;
30
import java.awt.event.KeyAdapter;
31
import java.awt.event.KeyEvent;
31
import java.awt.event.KeyEvent;
32
import java.awt.event.MouseAdapter;
32
import java.awt.event.MouseAdapter;
33
import java.awt.event.MouseEvent;
33
import java.awt.event.MouseEvent;
34
import java.io.IOException;
34
import java.io.IOException;
35
import java.util.ArrayList;
35
import java.util.ArrayList;
36
import java.util.List;
36
import java.util.List;
37
 
37
 
38
import javax.swing.JComponent;
38
import javax.swing.JComponent;
39
import javax.swing.JFrame;
39
import javax.swing.JFrame;
40
import javax.swing.JLabel;
40
import javax.swing.JLabel;
41
import javax.swing.JMenuItem;
41
import javax.swing.JMenuItem;
42
import javax.swing.JOptionPane;
42
import javax.swing.JOptionPane;
43
import javax.swing.JPanel;
43
import javax.swing.JPanel;
44
import javax.swing.JPopupMenu;
44
import javax.swing.JPopupMenu;
45
import javax.swing.JTextField;
45
import javax.swing.JTextField;
46
import javax.swing.SwingConstants;
46
import javax.swing.SwingConstants;
47
import javax.swing.SwingUtilities;
47
import javax.swing.SwingUtilities;
48
import javax.swing.TransferHandler;
48
import javax.swing.TransferHandler;
49
 
49
 
50
public class FilePanel extends JPanel {
50
public class FilePanel extends JPanel {
51
    private static final Color HOVER_COLOR = new Color(230, 240, 255);
51
    private static final Color HOVER_COLOR = new Color(230, 240, 255);
52
    private static final Color SELECTED_COLOR = new Color(193, 220, 252);
52
    private static final Color SELECTED_COLOR = new Color(193, 220, 252);
53
 
53
 
54
    public static final int PREFERRED_WIDTH = 128;
54
    public static final int PREFERRED_WIDTH = 128;
55
    public static final int PREFERRED_HEIGHT = 74;
55
    public static final int PREFERRED_HEIGHT = 74;
56
 
56
 
57
    private final JLabel label;
57
    private final JLabel label;
58
    private JImage image;
58
    private JImage image;
59
    private AttachmentPanel attachmentPanel;
59
    private AttachmentPanel attachmentPanel;
60
    private Attachment attachment;
60
    private Attachment attachment;
61
 
61
 
62
    public FilePanel(final Attachment attachment, final AttachmentPanel panelSource) {
62
    public FilePanel(final Attachment attachment, final AttachmentPanel panelSource) {
63
        this.attachment = attachment;
63
        this.attachment = attachment;
64
        this.attachmentPanel = panelSource;
64
        this.attachmentPanel = panelSource;
65
        final String name = attachment.getName();
65
        final String name = attachment.getName();
66
        this.setOpaque(true);
66
        this.setOpaque(true);
67
        this.setLayout(new BorderLayout());
67
        this.setLayout(new BorderLayout());
68
        try {
68
        try {
69
            String type = attachment.getMimeType();
69
            String type = attachment.getMimeType();
70
            if (type == null || type.trim().isEmpty() || type.equals("application/octet-stream")) {
70
            if (type == null || type.trim().isEmpty() || type.equals("application/octet-stream")) {
71
                image = new JImage(this.getClass().getResource("data-icon.png"));
71
                image = new JImage(this.getClass().getResource("data-icon.png"));
72
            } else if (type.equals("application/msword")) {
72
            } else if (type.equals("application/msword")) {
73
                image = new JImage(this.getClass().getResource("doc-icon.png"));
73
                image = new JImage(this.getClass().getResource("doc-icon.png"));
74
            } else if (type.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
74
            } else if (type.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
75
                image = new JImage(this.getClass().getResource("docx-icon.png"));
75
                image = new JImage(this.getClass().getResource("docx-icon.png"));
76
            } else if (type.equals("application/vnd.oasis.opendocument.text")) {
76
            } else if (type.equals("application/vnd.oasis.opendocument.text")) {
77
                image = new JImage(this.getClass().getResource("odt-icon.png"));
77
                image = new JImage(this.getClass().getResource("odt-icon.png"));
78
            } else if (type.equals("application/pdf")) {
78
            } else if (type.equals("application/pdf")) {
79
                image = new JImage(this.getClass().getResource("pdf-icon.png"));
79
                image = new JImage(this.getClass().getResource("pdf-icon.png"));
80
            } else if (type.equals("image/jpeg")) {
80
            } else if (type.equals("image/jpeg")) {
81
                image = new JImage(this.getClass().getResource("jpg-icon.png"));
81
                image = new JImage(this.getClass().getResource("jpg-icon.png"));
82
            } else if (type.equals("image/png")) {
82
            } else if (type.equals("image/png")) {
83
                image = new JImage(this.getClass().getResource("png-icon.png"));
83
                image = new JImage(this.getClass().getResource("png-icon.png"));
84
            } else if (type.equals("application/vnd.oasis.opendocument.spreadsheet")) {
84
            } else if (type.equals("application/vnd.oasis.opendocument.spreadsheet")) {
85
                image = new JImage(this.getClass().getResource("ods-icon.png"));
85
                image = new JImage(this.getClass().getResource("ods-icon.png"));
86
            } else if (type.equals("application/msexcel") || type.equals("application/vnd.ms-excel") || type.equals("application/xls")) {
86
            } else if (type.equals("application/msexcel") || type.equals("application/vnd.ms-excel") || type.equals("application/xls")) {
87
                image = new JImage(this.getClass().getResource("xls-icon.png"));
87
                image = new JImage(this.getClass().getResource("xls-icon.png"));
88
            } else if (type.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
88
            } else if (type.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) {
89
                image = new JImage(this.getClass().getResource("xlsx-icon.png"));
89
                image = new JImage(this.getClass().getResource("xlsx-icon.png"));
90
            } else if (type.equals(Attachment.MIMETYPE_FOLDER)) {
90
            } else if (type.equals(Attachment.MIMETYPE_FOLDER)) {
91
                image = new JImage(this.getClass().getResource("folder-icon.png"));
91
                image = new JImage(this.getClass().getResource("folder-icon.png"));
-
 
92
            } else if (type.equals(Attachment.MIMETYPE_URL) || type.equals("application/x-mswinurl")) {
-
 
93
                image = new JImage(this.getClass().getResource("url-icon.png"));
92
            } else {
94
            } else {
93
                image = new JImage(this.getClass().getResource("data-icon.png"));
95
                image = new JImage(this.getClass().getResource("data-icon.png"));
94
            }
96
            }
95
            image.setOpaque(true);
97
            image.setOpaque(true);
96
            image.setCenterImage(true);
98
            image.setCenterImage(true);
97
            this.add(image, BorderLayout.CENTER);
99
            this.add(image, BorderLayout.CENTER);
98
        } catch (Exception e) {
100
        } catch (Exception e) {
99
            ExceptionHandler.handle("image error", e);
101
            ExceptionHandler.handle("image error", e);
100
        }
102
        }
101
        setBackground(Color.WHITE);
103
        setBackground(Color.WHITE);
102
        label = new JLabel(name, SwingConstants.CENTER);
104
        label = new JLabel(name, SwingConstants.CENTER);
103
        label.setOpaque(false);
105
        label.setOpaque(false);
104
        label.setPreferredSize(new Dimension(label.getPreferredSize().width, new JTextField("a").getPreferredSize().height));
106
        label.setPreferredSize(new Dimension(label.getPreferredSize().width, new JTextField("a").getPreferredSize().height));
105
        this.add(label, BorderLayout.SOUTH);
107
        this.add(label, BorderLayout.SOUTH);
106
        int fontSize = label.getFont().getSize();
108
        int fontSize = label.getFont().getSize();
107
        this.setPreferredSize(new Dimension(10 * fontSize, PREFERRED_HEIGHT));
109
        this.setPreferredSize(new Dimension(10 * fontSize, PREFERRED_HEIGHT));
108
        this.setMinimumSize(new Dimension(10 * fontSize, PREFERRED_HEIGHT));
110
        this.setMinimumSize(new Dimension(10 * fontSize, PREFERRED_HEIGHT));
109
        updateBackgroundFromState();
111
        updateBackgroundFromState();
110
        this.addMouseMotionListener(new MouseAdapter() {
112
        this.addMouseMotionListener(new MouseAdapter() {
111
            @Override
113
            @Override
112
            public void mouseDragged(MouseEvent e) {
114
            public void mouseDragged(MouseEvent e) {
113
                final JComponent lab = (JComponent) e.getSource();
115
                final JComponent lab = (JComponent) e.getSource();
114
                final TransferHandler handle = lab.getTransferHandler();
116
                final TransferHandler handle = lab.getTransferHandler();
115
                handle.exportAsDrag(lab, e, TransferHandler.MOVE);
117
                handle.exportAsDrag(lab, e, TransferHandler.MOVE);
116
            }
118
            }
117
        });
119
        });
118
        this.addMouseListener(new MouseAdapter() {
120
        this.addMouseListener(new MouseAdapter() {
119
 
121
 
120
            @Override
122
            @Override
121
            public void mouseExited(MouseEvent e) {
123
            public void mouseExited(MouseEvent e) {
122
                updateBackgroundFromState();
124
                updateBackgroundFromState();
123
            }
125
            }
124
 
126
 
125
            @Override
127
            @Override
126
            public void mouseEntered(MouseEvent e) {
128
            public void mouseEntered(MouseEvent e) {
127
                if (panelSource.isSelected(attachment)) {
129
                if (panelSource.isSelected(attachment)) {
128
                    setBackground(SELECTED_COLOR);
130
                    setBackground(SELECTED_COLOR);
129
                } else {
131
                } else {
130
                    setBackground(HOVER_COLOR);
132
                    setBackground(HOVER_COLOR);
131
                }
133
                }
132
            }
134
            }
133
 
135
 
134
            @Override
136
            @Override
135
            public void mousePressed(MouseEvent e) {
137
            public void mousePressed(MouseEvent e) {
136
                final boolean ctrlPressed = (e.getModifiers() & ActionEvent.CTRL_MASK) == ActionEvent.CTRL_MASK;
138
                final boolean ctrlPressed = (e.getModifiers() & ActionEvent.CTRL_MASK) == ActionEvent.CTRL_MASK;
137
                if (!ctrlPressed) {
139
                if (!ctrlPressed) {
138
                    panelSource.select(attachment);
140
                    panelSource.select(attachment);
139
                    updateBackgroundFromState();
141
                    updateBackgroundFromState();
140
                } else {
142
                } else {
141
                    if (!panelSource.isSelected(FilePanel.this.attachment)) {
143
                    if (!panelSource.isSelected(FilePanel.this.attachment)) {
142
                        // Add to selection if selected
144
                        // Add to selection if selected
143
                        panelSource.select(attachment);
145
                        panelSource.select(attachment);
144
                    } else {
146
                    } else {
145
                        panelSource.deselect(attachment);
147
                        panelSource.deselect(attachment);
146
                    }
148
                    }
147
                    updateBackgroundFromState();
149
                    updateBackgroundFromState();
148
                }
150
                }
149
            }
151
            }
150
 
152
 
151
            @Override
153
            @Override
152
            public void mouseReleased(MouseEvent e) {
154
            public void mouseReleased(MouseEvent e) {
153
                final boolean ctrlPressed = (e.getModifiers() & ActionEvent.CTRL_MASK) == ActionEvent.CTRL_MASK;
155
                final boolean ctrlPressed = (e.getModifiers() & ActionEvent.CTRL_MASK) == ActionEvent.CTRL_MASK;
154
                if (!ctrlPressed) {
156
                if (!ctrlPressed) {
155
                    panelSource.clearSelection();
157
                    panelSource.clearSelection();
156
                    panelSource.select(attachment);
158
                    panelSource.select(attachment);
157
                    panelSource.updatePanels();
159
                    panelSource.updatePanels();
158
                }
160
                }
159
            }
161
            }
160
        });
162
        });
161
        initMenu(attachment, panelSource, name);
163
        initMenu(attachment, panelSource, name);
162
 
164
 
163
        this.setTransferHandler(new TransferHandler("id") {
165
        this.setTransferHandler(new TransferHandler("id") {
164
 
166
 
165
            @SuppressWarnings("unchecked")
167
            @SuppressWarnings("unchecked")
166
            @Override
168
            @Override
167
            public boolean canImport(TransferSupport support) {
169
            public boolean canImport(TransferSupport support) {
168
                if (!attachment.isFolder())
170
                if (!attachment.isFolder())
169
                    return false;
171
                    return false;
170
                try {
172
                try {
171
                    final List<Attachment> attachmentsToImport = (List<Attachment>) support.getTransferable().getTransferData(support.getDataFlavors()[0]);
173
                    final List<Attachment> attachmentsToImport = (List<Attachment>) support.getTransferable().getTransferData(support.getDataFlavors()[0]);
172
                    for (Attachment a : attachmentsToImport) {
174
                    for (Attachment a : attachmentsToImport) {
173
                        if (a.getId() == attachment.getId()) {
175
                        if (a.getId() == attachment.getId()) {
174
                            return false;
176
                            return false;
175
                        }
177
                        }
176
                    }
178
                    }
177
                } catch (UnsupportedFlavorException | IOException e) {
179
                } catch (UnsupportedFlavorException | IOException e) {
178
                    e.printStackTrace();
180
                    e.printStackTrace();
179
                }
181
                }
180
                return true;
182
                return true;
181
            }
183
            }
182
 
184
 
183
            @Override
185
            @Override
184
            public boolean importData(TransferSupport support) {
186
            public boolean importData(TransferSupport support) {
185
                if (!canImport(support)) {
187
                if (!canImport(support)) {
186
                    return false;
188
                    return false;
187
                }
189
                }
188
                try {
190
                try {
189
                    @SuppressWarnings("unchecked")
191
                    @SuppressWarnings("unchecked")
190
                    final List<Attachment> attachmentsToImport = (List<Attachment>) support.getTransferable().getTransferData(support.getDataFlavors()[0]);
192
                    final List<Attachment> attachmentsToImport = (List<Attachment>) support.getTransferable().getTransferData(support.getDataFlavors()[0]);
191
                    final AttachmentUtils utils = new AttachmentUtils();
193
                    final AttachmentUtils utils = new AttachmentUtils();
192
                    for (Attachment a : attachmentsToImport) {
194
                    for (Attachment a : attachmentsToImport) {
193
                        utils.move(a, attachment);
195
                        utils.move(a, attachment);
194
                    }
196
                    }
195
                    SwingUtilities.invokeLater(new Runnable() {
197
                    SwingUtilities.invokeLater(new Runnable() {
196
 
198
 
197
                        @Override
199
                        @Override
198
                        public void run() {
200
                        public void run() {
199
                            panelSource.initUI();
201
                            panelSource.initUI();
200
 
202
 
201
                        }
203
                        }
202
                    });
204
                    });
203
                } catch (Exception e) {
205
                } catch (Exception e) {
204
                    e.printStackTrace();
206
                    e.printStackTrace();
205
                }
207
                }
206
 
208
 
207
                return true;
209
                return true;
208
            }
210
            }
209
 
211
 
210
            @Override
212
            @Override
211
            protected java.awt.datatransfer.Transferable createTransferable(JComponent c) {
213
            protected java.awt.datatransfer.Transferable createTransferable(JComponent c) {
212
                return new AttachmentTransferable(new ArrayList<>(attachmentPanel.getSelectedAttachments()));
214
                return new AttachmentTransferable(new ArrayList<>(attachmentPanel.getSelectedAttachments()));
213
            }
215
            }
214
 
216
 
215
            @Override
217
            @Override
216
            public int getSourceActions(JComponent c) {
218
            public int getSourceActions(JComponent c) {
217
                return MOVE;
219
                return MOVE;
218
            }
220
            }
219
 
221
 
220
        }
222
        }
221
 
223
 
222
        );
224
        );
223
 
225
 
224
    }
226
    }
225
 
227
 
226
    public void updateBackgroundFromState() {
228
    public void updateBackgroundFromState() {
227
        if (this.attachmentPanel.isSelected(this.attachment)) {
229
        if (this.attachmentPanel.isSelected(this.attachment)) {
228
            setBackground(SELECTED_COLOR);
230
            setBackground(SELECTED_COLOR);
229
        } else {
231
        } else {
230
            setBackground(Color.WHITE);
232
            setBackground(Color.WHITE);
231
        }
233
        }
232
    }
234
    }
233
 
235
 
234
    private void initMenu(final Attachment attachment, final AttachmentPanel panelSource, final String name) {
236
    private void initMenu(final Attachment attachment, final AttachmentPanel panelSource, final String name) {
235
        final JPopupMenu menu = new JPopupMenu();
237
        final JPopupMenu menu = new JPopupMenu();
236
        final JMenuItem menuItemDelete = new JMenuItem("Supprimer");
238
        final JMenuItem menuItemDelete = new JMenuItem("Supprimer");
237
        menuItemDelete.addActionListener(new ActionListener() {
239
        menuItemDelete.addActionListener(new ActionListener() {
238
 
240
 
239
            @Override
241
            @Override
240
            public void actionPerformed(ActionEvent e) {
242
            public void actionPerformed(ActionEvent e) {
241
                int value = 0;
243
                int value = 0;
242
                if (attachment.isFolder()) {
244
                if (attachment.isFolder()) {
243
                    value = JOptionPane.showConfirmDialog(FilePanel.this, "Voulez-vous vraiment supprimer ce dossier ?\n" + attachment.getName(), "Supprimer le dossier", JOptionPane.YES_NO_OPTION);
245
                    value = JOptionPane.showConfirmDialog(FilePanel.this, "Voulez-vous vraiment supprimer ce dossier ?\n" + attachment.getName(), "Supprimer le dossier", JOptionPane.YES_NO_OPTION);
244
                } else {
246
                } else {
245
                    value = JOptionPane.showConfirmDialog(FilePanel.this,
247
                    value = JOptionPane.showConfirmDialog(FilePanel.this,
246
                            "Voulez-vous vraiment supprimer ce fichier ?\n" + attachment.getName() + "\nFichier original : " + attachment.getFileName() + "\nType : " + attachment.getMimeType(),
248
                            "Voulez-vous vraiment supprimer ce fichier ?\n" + attachment.getName() + "\nFichier original : " + attachment.getFileName() + "\nType : " + attachment.getMimeType(),
247
                            "Supprimer le fichier", JOptionPane.YES_NO_OPTION);
249
                            "Supprimer le fichier", JOptionPane.YES_NO_OPTION);
248
                }
250
                }
249
                if (value == JOptionPane.YES_OPTION) {
251
                if (value == JOptionPane.YES_OPTION) {
250
                    AttachmentUtils utils = new AttachmentUtils();
252
                    AttachmentUtils utils = new AttachmentUtils();
251
                    try {
253
                    try {
252
                        utils.deleteFile(attachment);
254
                        utils.deleteFile(attachment);
253
                        panelSource.initUI();
255
                        panelSource.initUI();
254
                    } catch (Exception e1) {
256
                    } catch (Exception e1) {
255
                        ExceptionHandler.handle("Erreur lors de la suppression", e1);
257
                        ExceptionHandler.handle("Erreur lors de la suppression", e1);
256
                    }
258
                    }
257
                }
259
                }
258
 
260
 
259
            }
261
            }
260
        });
262
        });
261
        menu.add(menuItemDelete);
263
        menu.add(menuItemDelete);
262
        final JMenuItem menuItemRename = new JMenuItem("Renommer");
264
        final JMenuItem menuItemRename = new JMenuItem("Renommer");
263
        menuItemRename.addActionListener(new ActionListener() {
265
        menuItemRename.addActionListener(new ActionListener() {
264
 
266
 
265
            final JTextField text = new JTextField(name);
267
            final JTextField text = new JTextField(name);
266
 
268
 
267
            private void stopNameEditing() {
269
            private void stopNameEditing() {
268
                FilePanel.this.invalidate();
270
                FilePanel.this.invalidate();
269
                FilePanel.this.remove(text);
271
                FilePanel.this.remove(text);
270
                FilePanel.this.add(label, BorderLayout.SOUTH);
272
                FilePanel.this.add(label, BorderLayout.SOUTH);
271
                FilePanel.this.validate();
273
                FilePanel.this.validate();
272
                FilePanel.this.repaint();
274
                FilePanel.this.repaint();
273
            }
275
            }
274
 
276
 
275
            public void validText(final Attachment attachment, final String name, final JTextField text) {
277
            public void validText(final Attachment attachment, final String name, final JTextField text) {
276
                try {
278
                try {
277
                    String newName = text.getText();
279
                    String newName = text.getText();
278
                    if (newName.trim().isEmpty()) {
280
                    if (newName.trim().isEmpty()) {
279
                        newName = name;
281
                        newName = name;
280
                    }
282
                    }
281
                    AttachmentUtils.rename(attachment, newName);
283
                    AttachmentUtils.rename(attachment, newName);
282
                    label.setText(newName);
284
                    label.setText(newName);
283
                } catch (Exception e1) {
285
                } catch (Exception e1) {
284
                    ExceptionHandler.handle("Erreur lors du renommage du fichier!", e1);
286
                    ExceptionHandler.handle("Erreur lors du renommage du fichier!", e1);
285
                }
287
                }
286
            }
288
            }
287
 
289
 
288
            @Override
290
            @Override
289
            public void actionPerformed(ActionEvent e) {
291
            public void actionPerformed(ActionEvent e) {
290
                final String name = attachment.getName();
292
                final String name = attachment.getName();
291
 
293
 
292
                text.addKeyListener(new KeyAdapter() {
294
                text.addKeyListener(new KeyAdapter() {
293
                    @Override
295
                    @Override
294
                    public void keyPressed(KeyEvent e) {
296
                    public void keyPressed(KeyEvent e) {
295
                        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
297
                        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
296
                            validText(attachment, name, text);
298
                            validText(attachment, name, text);
297
                            stopNameEditing();
299
                            stopNameEditing();
298
                        } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
300
                        } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
299
                            stopNameEditing();
301
                            stopNameEditing();
300
                        }
302
                        }
301
                    }
303
                    }
302
 
304
 
303
                });
305
                });
304
                text.addFocusListener(new FocusListener() {
306
                text.addFocusListener(new FocusListener() {
305
 
307
 
306
                    @Override
308
                    @Override
307
                    public void focusLost(FocusEvent e) {
309
                    public void focusLost(FocusEvent e) {
308
                        validText(attachment, name, text);
310
                        validText(attachment, name, text);
309
                        stopNameEditing();
311
                        stopNameEditing();
310
                    }
312
                    }
311
 
313
 
312
                    @Override
314
                    @Override
313
                    public void focusGained(FocusEvent e) {
315
                    public void focusGained(FocusEvent e) {
314
                        // Nothing to do here
316
                        // Nothing to do here
315
                    }
317
                    }
316
 
318
 
317
                });
319
                });
318
                text.addMouseListener(new MouseAdapter() {
320
                text.addMouseListener(new MouseAdapter() {
319
 
321
 
320
                    @Override
322
                    @Override
321
                    public void mouseExited(MouseEvent e) {
323
                    public void mouseExited(MouseEvent e) {
322
                        validText(attachment, name, text);
324
                        validText(attachment, name, text);
323
                        stopNameEditing();
325
                        stopNameEditing();
324
                    }
326
                    }
325
 
327
 
326
                });
328
                });
327
 
329
 
328
                FilePanel.this.invalidate();
330
                FilePanel.this.invalidate();
329
                FilePanel.this.remove(label);
331
                FilePanel.this.remove(label);
330
                FilePanel.this.add(text, BorderLayout.SOUTH);
332
                FilePanel.this.add(text, BorderLayout.SOUTH);
331
                FilePanel.this.validate();
333
                FilePanel.this.validate();
332
                FilePanel.this.repaint();
334
                FilePanel.this.repaint();
333
 
335
 
334
                text.grabFocus();
336
                text.grabFocus();
335
                text.setSelectionStart(0);
337
                text.setSelectionStart(0);
336
                text.setSelectionEnd(name.length());
338
                text.setSelectionEnd(name.length());
337
            }
339
            }
338
 
340
 
339
        });
341
        });
340
        menu.add(menuItemRename);
342
        menu.add(menuItemRename);
341
        if (!attachment.isFolder()) {
343
        if (!attachment.isFolder()) {
342
            menu.addSeparator();
344
            menu.addSeparator();
343
            JMenuItem menuItemProperties = new JMenuItem("Propriétés");
345
            JMenuItem menuItemProperties = new JMenuItem("Propriétés");
344
            menuItemProperties.addActionListener(new ActionListener() {
346
            menuItemProperties.addActionListener(new ActionListener() {
345
 
347
 
346
                @Override
348
                @Override
347
                public void actionPerformed(ActionEvent e) {
349
                public void actionPerformed(ActionEvent e) {
348
                    JFrame f = new JFrame();
350
                    JFrame f = new JFrame();
349
                    f.setTitle("Propriétés de " + attachment.getName());
351
                    f.setTitle("Propriétés de " + attachment.getName());
350
                    JPanel p = new JPanel();
352
                    JPanel p = new JPanel();
351
                    p.setLayout(new GridBagLayout());
353
                    p.setLayout(new GridBagLayout());
352
                    GridBagConstraints c = new DefaultGridBagConstraints();
354
                    GridBagConstraints c = new DefaultGridBagConstraints();
353
                    // Name
355
                    // Name
354
                    c.weightx = 0;
356
                    c.weightx = 0;
355
                    p.add(new JLabel("Nom : ", SwingConstants.RIGHT), c);
357
                    p.add(new JLabel("Nom : ", SwingConstants.RIGHT), c);
356
                    c.gridx++;
358
                    c.gridx++;
357
                    c.weightx = 1;
359
                    c.weightx = 1;
358
                    p.add(new JLabel(attachment.getName()), c);
360
                    p.add(new JLabel(attachment.getName()), c);
359
                    c.gridy++;
361
                    c.gridy++;
360
                    // Type
362
                    // Type
361
                    c.gridx = 0;
363
                    c.gridx = 0;
362
                    c.weightx = 0;
364
                    c.weightx = 0;
363
                    p.add(new JLabel("Type : ", SwingConstants.RIGHT), c);
365
                    p.add(new JLabel("Type : ", SwingConstants.RIGHT), c);
364
                    c.gridx++;
366
                    c.gridx++;
365
                    c.weightx = 1;
367
                    c.weightx = 1;
366
                    p.add(new JLabel(attachment.getMimeType()), c);
368
                    p.add(new JLabel(attachment.getMimeType()), c);
367
                    c.gridy++;
369
                    c.gridy++;
368
                    // FileName
370
                    // FileName
369
                    c.gridx = 0;
371
                    c.gridx = 0;
370
                    c.weightx = 0;
372
                    c.weightx = 0;
-
 
373
                    if (attachment.getMimeType().equals(Attachment.MIMETYPE_URL)) {
-
 
374
                        p.add(new JLabel("URL : ", SwingConstants.RIGHT), c);
-
 
375
                    } else {
371
                    p.add(new JLabel("Fichier original : ", SwingConstants.RIGHT), c);
376
                        p.add(new JLabel("Fichier original : ", SwingConstants.RIGHT), c);
-
 
377
                    }
372
                    c.gridx++;
378
                    c.gridx++;
373
                    c.weightx = 1;
379
                    c.weightx = 1;
374
                    p.add(new JLabel(attachment.getFileName()), c);
380
                    p.add(new JLabel(attachment.getFileName()), c);
375
                    c.gridy++;
381
                    c.gridy++;
376
                    // Size
382
                    // Size
-
 
383
                    if (!attachment.getMimeType().equals(Attachment.MIMETYPE_URL)) {
377
                    c.gridx = 0;
384
                        c.gridx = 0;
378
                    c.weightx = 0;
385
                        c.weightx = 0;
379
                    p.add(new JLabel("Taille : ", SwingConstants.RIGHT), c);
386
                        p.add(new JLabel("Taille : ", SwingConstants.RIGHT), c);
380
                    c.gridx++;
387
                        c.gridx++;
381
                    c.weightx = 1;
388
                        c.weightx = 1;
382
                    p.add(new JLabel(attachment.getFileSize() + " octets"), c);
389
                        p.add(new JLabel(attachment.getFileSize() + " octets"), c);
383
 
-
 
-
 
390
                    }
384
                    // Spacer
391
                    // Spacer
385
                    c.gridx = 1;
392
                    c.gridx = 1;
386
                    c.gridy++;
393
                    c.gridy++;
387
                    c.weightx = 1;
394
                    c.weightx = 1;
388
                    JPanel spacer = new JPanel();
395
                    JPanel spacer = new JPanel();
389
                    spacer.setPreferredSize(new Dimension(300, 1));
396
                    spacer.setPreferredSize(new Dimension(300, 1));
390
                    p.add(spacer, c);
397
                    p.add(spacer, c);
391
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
398
                    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
392
 
399
 
393
                    f.setContentPane(p);
400
                    f.setContentPane(p);
394
                    f.pack();
401
                    f.pack();
395
                    f.setResizable(false);
402
                    f.setResizable(false);
396
                    f.setLocationRelativeTo(FilePanel.this);
403
                    f.setLocationRelativeTo(FilePanel.this);
397
                    f.setVisible(true);
404
                    f.setVisible(true);
398
 
405
 
399
                }
406
                }
400
            });
407
            });
401
            menu.add(menuItemProperties);
408
            menu.add(menuItemProperties);
402
        }
409
        }
403
        setComponentPopupMenu(menu);
410
        setComponentPopupMenu(menu);
404
    }
411
    }
405
 
412
 
406
    @Override
413
    @Override
407
    public void setBackground(Color bg) {
414
    public void setBackground(Color bg) {
408
        super.setBackground(bg);
415
        super.setBackground(bg);
409
        if (image != null) {
416
        if (image != null) {
410
            image.setBackground(bg);
417
            image.setBackground(bg);
411
        }
418
        }
412
    }
419
    }
413
}
420
}