Skip to content

Commit

Permalink
Moved all location related classes into locations package.
Browse files Browse the repository at this point in the history
Reworked extraction of last modified time following issue #35.

Here now lastModified and creation time are read. The latter
of both is then used for comparision. There can be cases,
where one of both provides 0L (as long) as result.
  • Loading branch information
Oliver-Loeffler committed Jan 1, 2020
1 parent be27867 commit 46d9a8e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/main/java/net/raumzeitfalle/fx/DemoJavaFxStage.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void start(Stage primaryStage) throws Exception {
PathFilter na0 = PathFilter.forFileExtension(".na0 (LMS binary files)", "n[a-z]\\d");

Path local = Paths.get("./");
FXFileChooserStage fc = FXFileChooserStage.create(Skin.DARK, local,all,xml, xlsx, na0, txt, exe,combined);
FXFileChooserStage fc = FXFileChooserStage.create(Skin.DEFAULT, local,all,xml, xlsx, na0, txt, exe,combined);

Button button = new Button("Show customized Stage: FXFileChooserImpl.class");
button.setOnAction(e -> {
Expand All @@ -79,7 +79,7 @@ public void start(Stage primaryStage) throws Exception {
}

public static void main(String[] args) {
launch(new String[0]);
launch();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.Window;
import net.raumzeitfalle.fx.util.Location;
import net.raumzeitfalle.fx.filechooser.locations.Location;

public class FXFileChooserStage extends Stage implements HideableWindow {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import javafx.collections.ObservableList;
import javafx.collections.ObservableSet;
import javafx.collections.transformation.FilteredList;
import net.raumzeitfalle.fx.util.Location;
import net.raumzeitfalle.fx.filechooser.locations.Location;

final class FileChooserModel {

Expand Down
30 changes: 23 additions & 7 deletions src/main/java/net/raumzeitfalle/fx/filechooser/IndexedPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,45 @@

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.attribute.FileTime;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

class IndexedPath {

private final Path path;

private final FileTime timestamp;

private static final Logger LOGGER = Logger.getLogger(IndexedPath.class.getName());

public static IndexedPath valueOf(Path path) {
FileTime timestamp = getTimestamp(path);
FileTime timestamp;
try {
timestamp = getTimestamp(path);
} catch (IOException e) {
timestamp = FileTime.from(0, TimeUnit.MICROSECONDS);
String message = String.format("Could not determine lastModified timestamp for %s, returning %s instead.", path, timestamp);
LOGGER.log(Level.SEVERE, message, e);
}
return new IndexedPath(path, timestamp);
}

private static FileTime getTimestamp(Path path) {
try {
return Files.getLastModifiedTime(path);
} catch (IOException e) {
Instant fallback = LocalDateTime.MAX.atZone(ZoneId.systemDefault()).toInstant();
return FileTime.from(fallback);
private static FileTime getTimestamp(Path path) throws IOException {
BasicFileAttributes attributes = Files.readAttributes(path, BasicFileAttributes.class);
FileTime lastModified = attributes.lastModifiedTime();
FileTime created = attributes.creationTime();
if (lastModified.compareTo(created) > 0) {
return lastModified;
} else {
return created;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* limitations under the License.
* #L%
*/
package net.raumzeitfalle.fx.util;
package net.raumzeitfalle.fx.filechooser.locations;

import static org.junit.jupiter.api.Assertions.*;

Expand Down

0 comments on commit 46d9a8e

Please sign in to comment.