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