Skip to content

Commit

Permalink
Refactorings to re-use RenamingPath in Kodi Tools
Browse files Browse the repository at this point in the history
  • Loading branch information
kerner1000 committed Oct 23, 2022
1 parent 599360f commit 8af0768
Show file tree
Hide file tree
Showing 20 changed files with 322 additions and 267 deletions.
6 changes: 2 additions & 4 deletions src/main/java/drrename/event/FilePreviewEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@

package drrename.event;

import drrename.model.RenamingEntry;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import drrename.model.RenamingControl;

public record FilePreviewEvent(RenamingEntry renamingEntry) {
public record FilePreviewEvent(RenamingControl renamingControl) {

}
6 changes: 3 additions & 3 deletions src/main/java/drrename/event/FileRenamedEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package drrename.event;

import drrename.model.RenamingEntry;
import drrename.model.RenamingControl;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

Expand All @@ -33,9 +33,9 @@ public class FileRenamedEvent {

private final UUID uuid;

private final List<RenamingEntry> renamedEntries;
private final List<RenamingControl> renamedEntries;

public FileRenamedEvent(UUID uuid, RenamingEntry renamedEntries) {
public FileRenamedEvent(UUID uuid, RenamingControl renamedEntries) {
this(uuid, Collections.singletonList(renamedEntries));
}
}
6 changes: 3 additions & 3 deletions src/main/java/drrename/event/NewRenamingEntryEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package drrename.event;

import drrename.model.RenamingEntry;
import drrename.model.RenamingControl;
import lombok.Data;
import lombok.RequiredArgsConstructor;

Expand All @@ -34,9 +34,9 @@ public class NewRenamingEntryEvent {

private final UUID uuid;

private final List<RenamingEntry> renamingEntries;
private final List<RenamingControl> renamingEntries;

public NewRenamingEntryEvent(UUID uuid, RenamingEntry renamingEntries) {
public NewRenamingEntryEvent(UUID uuid, RenamingControl renamingEntries) {
this(uuid, Collections.singletonList(renamingEntries));
}
}
48 changes: 4 additions & 44 deletions src/main/java/drrename/kodi/KodiToolsController.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package drrename.kodi;

import drrename.RenameUtil;
import drrename.kodi.treeitem.content.check.NfoCheckResultTreeItemContent;
import drrename.kodi.treeitem.KodiTreeRootItem;
import drrename.kodi.treeitem.MovieTreeItemFactory;
import drrename.kodi.treeitem.content.KodiTreeItemContent;
import drrename.kodi.treeitem.content.MovieTreeItemContent;
import drrename.kodi.treeitem.content.check.NfoCheckResultTreeItemContent;
import drrename.ui.FXUtil;
import drrename.ui.mainview.GoCancelButtonsComponentController;
import drrename.ui.mainview.StartDirectoryComponentController;
import javafx.application.Platform;
import javafx.collections.ListChangeListener;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.image.Image;
Expand Down Expand Up @@ -108,37 +106,7 @@ public void initialize(URL url, ResourceBundle resourceBundle) {
onButtonGoEvent(null);
}
});
treeView.setCellFactory(tv -> new TreeCell<>() {

@Override
protected void updateItem(KodiTreeItemContent item, boolean empty) {
super.updateItem(item, empty);
if (item == null) {
setText(null);
setStyle(null);
setGraphic(null);
} else {
setText(item.toString());
List<String> styles = new ArrayList<>();
if (item instanceof MovieTreeItemContent) {
styles.add("-fx-font-size: 14;");
}
if (item.hasWarning()) {
if (item instanceof MovieTreeItemContent) {
styles.add("-fx-font-size: 13;");
}
styles.add("-fx-font-weight: bold;");
styles.add("-fx-background-color: wheat;");
var joinedStylesString = String.join(" ", styles);
setStyle(joinedStylesString);
setGraphic(buildGraphic(item));
} else {
setStyle(null);
setGraphic(null);
}
}
}
});
treeView.setCellFactory(this::treeViewCellFactoryCallback);

treeView.getSelectionModel().getSelectedIndices().addListener((ListChangeListener<Integer>) c -> {
imageStage.close();
Expand All @@ -157,16 +125,8 @@ protected void updateItem(KodiTreeItemContent item, boolean empty) {
});
}

private Node buildGraphic(KodiTreeItemContent item) {
Button button = new Button("Fix");
button.disableProperty().bind(item.fixableProperty().not());
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent actionEvent) {

}
});
return button;
private TreeCell<KodiTreeItemContent> treeViewCellFactoryCallback(TreeView<KodiTreeItemContent> kodiTreeItemContentTreeView) {
return new KodiTreeCell();
}

private void showImage(Path nfoFile) {
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/drrename/kodi/KodiTreeCell.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Dr.Rename - A Minimalistic Batch Renamer
*
* Copyright (C) 2022
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package drrename.kodi;

import drrename.kodi.treeitem.content.KodiTreeItemContent;
import drrename.kodi.treeitem.content.MovieTreeItemContent;
import javafx.scene.control.TreeCell;

import java.util.ArrayList;
import java.util.List;

public class KodiTreeCell extends TreeCell<KodiTreeItemContent> {

@Override
protected void updateItem(KodiTreeItemContent item, boolean empty) {

super.updateItem(item, empty);
if (item == null) {
setText(null);
setStyle(null);
} else {
setText(item.toString());
List<String> styles = new ArrayList<>();
if (item.hasWarning()) {
if (item instanceof MovieTreeItemContent) {
styles.add("-fx-font-size: 13;");
}
styles.add("-fx-font-weight: bold;");
styles.add("-fx-background-color: wheat;");
var joinedStylesString = String.join(" ", styles);
setStyle(joinedStylesString);
} else {
if (item instanceof MovieTreeItemContent) {
styles.add("-fx-font-size: 14;");
} else
setStyle(null);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.util.Objects;

@Deprecated
public class CheckResult {

protected final StringProperty result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import java.util.Objects;

@Deprecated
@RequiredArgsConstructor
public class CheckResultTreeItemContent<T extends CheckResult> extends KodiTreeItemContent {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package drrename.model;

import drrename.strategy.RenamingStrategy;
import drrename.ui.Styles;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.beans.property.*;
import javafx.beans.value.ObservableValue;
Expand All @@ -11,80 +9,28 @@
import javafx.scene.control.Label;
import lombok.extern.slf4j.Slf4j;

import java.io.FileNotFoundException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
import java.util.concurrent.Callable;

@Slf4j
public class RenamingEntry {

private final ObjectProperty<Path> oldPath;

private final StringProperty newPath;

private final ObjectProperty<Throwable> exception;

private final BooleanProperty willChange;

private final BooleanProperty filtered;
public class RenamingControl extends RenamingPath {

private final StringProperty fileType;

private final Control leftControl;

private final Control rightControl;

public RenamingEntry(final Path path) {
public RenamingControl(final Path path) {
super(path);

this.oldPath = new SimpleObjectProperty<>(Objects.requireNonNull(path));
this.newPath = new SimpleStringProperty();
exception = new SimpleObjectProperty<>();
this.willChange = new SimpleBooleanProperty();
this.fileType = new SimpleStringProperty();
this.filtered = new SimpleBooleanProperty();
newPath.addListener((v, o, n) -> willChange.set(!getOldPath().getFileName().toString().equals(n)));
oldPath.addListener((observable, oldValue, newValue) -> willChange.set(!getOldPath().getFileName().toString().equals(getNewPath())));
leftControl = buildLeft();
rightControl = buildRight();
}

public String preview(final RenamingStrategy strategy) {

if (Files.exists(getOldPath())) {
final String s = strategy.getNameNew(getOldPath());
Platform.runLater(() -> newPath.set(s));
return s;
}
var exception = new FileNotFoundException(getOldPath().getFileName().toString());
Platform.runLater(() -> exceptionProperty().set(exception));
return null;
}

public Path rename(final RenamingStrategy strategy) {
if (isFiltered()) {
log.warn("Rename called on filtered entry, skipping {}", this);
return getOldPath();
}
try {
Path newPath = strategy.rename(getOldPath(), null);
Platform.runLater(() -> commitRename(newPath));
return newPath;
} catch (final Exception e) {
log.debug(e.getLocalizedMessage(), e);
Platform.runLater(() -> this.exception.set(e));
return getOldPath();
}
}

public void commitRename(Path newPath) {
setOldPath(newPath);
exceptionProperty().set(null);
// for now set to false to see an immediate effect, preview service should be triggered and should update this any time soon again.
setWillChange(false);
}

@Override
public String toString() {
return getClass().getSimpleName() + "{" + getOldPath().toString() + "}";
Expand Down Expand Up @@ -142,7 +88,7 @@ protected Callable<String> buildTextRight() {
}

protected String calcStyleRight() {
if (willChange()) {
if (isWillChange()) {
return Styles.changingStyle();
}
return Styles.defaultStyle();
Expand All @@ -151,18 +97,6 @@ protected String calcStyleRight() {
// Getter / Setter //


public BooleanProperty filteredProperty() {
return filtered;
}

public boolean isFiltered() {
return filtered.get();
}

public void setFiltered(boolean filtered) {
this.filtered.set(filtered);
}

public StringProperty fileTypeProperty() {
return fileType;
}
Expand All @@ -175,50 +109,6 @@ public void setFileType(String fileType) {
fileTypeProperty().set(fileType);
}

public void setNewPath(String newPath) {
this.newPath.set(newPath);
}

public String getNewPath() {
return newPath.get();
}

public Throwable getException() {
return exception.get();
}

public ObjectProperty<Throwable> exceptionProperty() {
return exception;
}

public StringProperty newPathProperty() {
return newPath;
}

public BooleanProperty willChangeProperty() {
return this.willChange;
}

public boolean willChange() {
return this.willChangeProperty().get();
}

public void setWillChange(final boolean willChange) {
this.willChangeProperty().set(willChange);
}

public ObjectProperty<Path> oldPathProperty() {
return this.oldPath;
}

public Path getOldPath() {
return this.oldPathProperty().get();
}

public void setOldPath(final Path oldPath) {
this.oldPathProperty().set(oldPath);
}

public Control getLeftControl() {
return leftControl;
}
Expand Down
Loading

0 comments on commit 8af0768

Please sign in to comment.