Improve JCR file system.
[lgpl/argeo-commons.git] / org.argeo.eclipse.ui / src / org / argeo / eclipse / ui / fs / NioFileLabelProvider.java
index 391f250eb70fcff5cf6c94ab2b89a6534a18a85e..2bb65eed06fc130d731ae41c601a21b882ba7faf 100644 (file)
@@ -3,28 +3,61 @@ package org.argeo.eclipse.ui.fs;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.nio.file.attribute.FileTime;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 
 import org.argeo.eclipse.ui.EclipseUiUtils;
 import org.eclipse.jface.viewers.ColumnLabelProvider;
 
 /** Expect a {@link Path} as input element */
 public class NioFileLabelProvider extends ColumnLabelProvider {
+       private final static FileTime EPOCH = FileTime.fromMillis(0);
        private static final long serialVersionUID = 2160026425187796930L;
        private final String propName;
+       private DateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm");
 
+       // TODO use new formatting
+       // DateTimeFormatter formatter =
+       // DateTimeFormatter.ofLocalizedDateTime( FormatStyle.SHORT )
+       // .withLocale( Locale.UK )
+       // .withZone( ZoneId.systemDefault() );
        public NioFileLabelProvider(String propName) {
                this.propName = propName;
        }
 
        @Override
        public String getText(Object element) {
-               Path path = (Path) element;
                try {
+                       Path path;
+                       if (element instanceof ParentDir) {
+//                             switch (propName) {
+//                             case FsUiConstants.PROPERTY_SIZE:
+//                                     return "-";
+//                             case FsUiConstants.PROPERTY_LAST_MODIFIED:
+//                                     return "-";
+//                             // return Files.getLastModifiedTime(((ParentDir) element).getPath()).toString();
+//                             case FsUiConstants.PROPERTY_TYPE:
+//                                     return "Folder";
+//                             }
+                               path = ((ParentDir) element).getPath();
+                       } else
+                               path = (Path) element;
                        switch (propName) {
                        case FsUiConstants.PROPERTY_SIZE:
-                               return FsUiUtils.humanReadableByteCount(Files.size(path), false);
+                               if (Files.isDirectory(path))
+                                       return "-";
+                               else
+                                       return FsUiUtils.humanReadableByteCount(Files.size(path), false);
                        case FsUiConstants.PROPERTY_LAST_MODIFIED:
-                               return Files.getLastModifiedTime(path).toString();
+                               if (Files.isDirectory(path))
+                                       return "-";
+                               FileTime time = Files.getLastModifiedTime(path);
+                               if (time.equals(EPOCH))
+                                       return "-";
+                               else
+                                       return dateFormat.format(new Date(time.toMillis()));
                        case FsUiConstants.PROPERTY_TYPE:
                                if (Files.isDirectory(path))
                                        return "Folder";
@@ -39,7 +72,7 @@ public class NioFileLabelProvider extends ColumnLabelProvider {
                                throw new IllegalArgumentException("Unsupported property " + propName);
                        }
                } catch (IOException ioe) {
-                       throw new FsUiException("Cannot get property " + propName + " on " + path.toString());
+                       throw new FsUiException("Cannot get property " + propName + " on " + element);
                }
        }
 }