]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.eclipse.ui/src/org/argeo/eclipse/ui/fs/NioFileLabelProvider.java
CMS wizard and error feedback
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui / src / org / argeo / eclipse / ui / fs / NioFileLabelProvider.java
1 package org.argeo.eclipse.ui.fs;
2
3 import java.io.IOException;
4 import java.nio.file.Files;
5 import java.nio.file.Path;
6
7 import org.argeo.eclipse.ui.EclipseUiUtils;
8 import org.eclipse.jface.viewers.ColumnLabelProvider;
9
10 /** Expect a {@link Path} as input element */
11 public class NioFileLabelProvider extends ColumnLabelProvider {
12 private static final long serialVersionUID = 2160026425187796930L;
13 private final String propName;
14
15 public NioFileLabelProvider(String propName) {
16 this.propName = propName;
17 }
18
19 @Override
20 public String getText(Object element) {
21 try {
22 if (element instanceof ParentDir) {
23 switch (propName) {
24 case FsUiConstants.PROPERTY_SIZE:
25 return "-";
26 case FsUiConstants.PROPERTY_LAST_MODIFIED:
27 return Files.getLastModifiedTime(((ParentDir) element).getPath()).toString();
28 case FsUiConstants.PROPERTY_TYPE:
29 return "Folder";
30 }
31 }
32
33 Path path = (Path) element;
34 switch (propName) {
35 case FsUiConstants.PROPERTY_SIZE:
36 if (Files.isDirectory(path))
37 return "-";
38 else
39 return FsUiUtils.humanReadableByteCount(Files.size(path), false);
40 case FsUiConstants.PROPERTY_LAST_MODIFIED:
41 return Files.getLastModifiedTime(path).toString();
42 case FsUiConstants.PROPERTY_TYPE:
43 if (Files.isDirectory(path))
44 return "Folder";
45 else {
46 String mimeType = Files.probeContentType(path);
47 if (EclipseUiUtils.isEmpty(mimeType))
48 return "Unknown";
49 else
50 return mimeType;
51 }
52 default:
53 throw new IllegalArgumentException("Unsupported property " + propName);
54 }
55 } catch (IOException ioe) {
56 throw new FsUiException("Cannot get property " + propName + " on " + element);
57 }
58 }
59 }