[Refactoring] small refactoring

This commit is contained in:
Thibaud Gasser 2019-10-06 12:41:09 +02:00
parent 96f120d943
commit e94c2a50d2
5 changed files with 15 additions and 12 deletions

View File

@ -8,7 +8,7 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
public class ImageController<T> extends Controller<List<Image>> implements ActionListener {
public class ImageController extends Controller<List<Image>> implements ActionListener {
private int index;

View File

@ -8,6 +8,7 @@ import java.awt.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
@ -27,9 +28,9 @@ public class ImageLoader extends SwingWorker<List<Image>, Integer> {
private void loadImages(File current) throws NullPointerException {
if (current.isFile()) {
final Image icon = ImageLib.createScaledImage(current, this.iconWidth, -1);
if (icon != null) {
images.add(icon);
final Optional<Image> icon = ImageLib.createScaledImage(current, this.iconWidth, -1);
if (icon.isPresent()) {
images.add(icon.get());
publish(images.size());
}
return;
@ -43,7 +44,7 @@ public class ImageLoader extends SwingWorker<List<Image>, Integer> {
}
@Override
protected List<Image> doInBackground() throws Exception {
protected List<Image> doInBackground() {
loadImages(root);
return images;
}
@ -51,7 +52,7 @@ public class ImageLoader extends SwingWorker<List<Image>, Integer> {
@Override
protected void process(List<Integer> data) {
if (this.isCancelled()) return;
app.setStatus(String.valueOf(data.get(data.size() - 1)) + " images loaded");
app.setStatus(data.get(data.size() - 1) + " images loaded");
}
@Override

View File

@ -5,19 +5,20 @@ import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Optional;
public final class ImageLib {
public static Image createScaledImage(File file, int iconWidth, int iconHeight) {
public static Optional<Image> createScaledImage(File file, int iconWidth, int iconHeight) {
final BufferedImage img;
try {
if ((img = ImageIO.read(file)) != null) {
return img.getScaledInstance(iconWidth, iconHeight, Image.SCALE_DEFAULT);
return Optional.of(img.getScaledInstance(iconWidth, iconHeight, Image.SCALE_DEFAULT));
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
return Optional.empty();
}
private ImageLib() {}

View File

@ -7,7 +7,7 @@ import javax.swing.*;
public class View<T> extends JPanel {
private final T model;
private Controller controller;
private Controller<T> controller;
protected View(T model) {
this.model = model;
@ -17,7 +17,7 @@ public class View<T> extends JPanel {
return model;
}
protected Controller getController() {
protected Controller<T> getController() {
return controller;
}

View File

@ -16,12 +16,13 @@ public class AStarWorker extends SwingWorker<List<Node>, List<Node>> {
private final Node start;
private final Node goal;
private final Grid target;
private List<Node> path;
private List<Node> path = Collections.emptyList();
public AStarWorker(Node start, Node goal, Grid target) {
this.start = start;
this.goal = goal;
this.target = target;
Thread.currentThread().setName("AStarWorker");
}
@Override