Skip to content

Commit

Permalink
Fix crash when current directory doesn't have proper project files in…
Browse files Browse the repository at this point in the history
… it.

Fix #1
  • Loading branch information
Tiim committed Jul 18, 2018
1 parent 15ac1d3 commit 1b6856a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
22 changes: 13 additions & 9 deletions src/main/java/ch/scbirs/timetablegen/Persistence.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.awt.event.ActionEvent;
import java.io.*;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;

public class Persistence {

Expand All @@ -26,14 +26,14 @@ public Persistence() {
.create();
config = new JSONProperty(Paths.get(CONFIG));
mainPath = Paths.get(config.get("last-path", "./tables"));
while (!Files.isDirectory(mainPath)) {
if (Files.isRegularFile(mainPath)) {
mainPath = mainPath.getParent();
}
}

public Path save(Model model, Path path) {
saveToFile(model, path);
return path.normalize();
return Paths.get(".").toAbsolutePath().relativize(path.toAbsolutePath());
}

private void saveToFile(Model model, Path path) {
Expand All @@ -45,9 +45,13 @@ private void saveToFile(Model model, Path path) {
}
}

public File[] getFiles() {
mainPath.toFile().mkdirs();
return mainPath.normalize().toFile().listFiles(new PatternFilenameFilter(".*\\.json"));
public File[] getFiles() throws IOException {
if (!Files.isDirectory(mainPath)) {
Files.createDirectories(mainPath);
}
File file = mainPath.normalize().toFile();
File[] files = file.listFiles(new PatternFilenameFilter(".*\\.json"));
return files == null ? new File[0] : files;
}

public Model load(File file) throws IOException {
Expand All @@ -57,7 +61,7 @@ public Model load(File file) throws IOException {
}

public void setFolder(Path folder) {
while (!Files.isDirectory(folder)) {
if (Files.isRegularFile(folder)) {
folder = folder.getParent();
}
this.mainPath = folder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public class FileComboBoxRenderer extends DefaultListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value,
int index, boolean isSelected, boolean cellHasFocus) {

if (value == null) {
value = "";
}
return super.getListCellRendererComponent(list,
FilenameUtils.getName(value.toString()), index, isSelected, cellHasFocus);
}
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/ch/scbirs/timetablegen/gui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ private void init() {
files.setRenderer(new FileComboBoxRenderer());

filesModel = new FileComboBoxModel();
filesModel.set(p.getFiles());
try {
filesModel.set(p.getFiles());
} catch (IOException e) {
e.printStackTrace();
}

if (filesModel.getSize() > 0) {
filesModel.setSelectedItem(0);
Expand Down Expand Up @@ -98,7 +102,11 @@ private void changeFolder() {
if (i == JFileChooser.APPROVE_OPTION) {
File file = f.getSelectedFile();
p.setFolder(file.toPath());
filesModel.set(p.getFiles());
try {
filesModel.set(p.getFiles());
} catch (IOException e) {
e.printStackTrace();
}
loadFirst();
}
setTitle(Lang.translate("window.main.Name") + " " + p.getFolder());
Expand Down Expand Up @@ -135,7 +143,11 @@ private void save() {
model.setName(name);
}
File f = p.save(model, toSave.toPath()).toFile();
filesModel.set(p.getFiles());
try {
filesModel.set(p.getFiles());
} catch (IOException e) {
e.printStackTrace();
}
filesModel.setSelectedItem(f);
}
}
Expand Down

0 comments on commit 1b6856a

Please sign in to comment.