Skip to content

Commit

Permalink
🔀 Merge pull request #23 from Komposten/refactor/12-backend-dialogs
Browse files Browse the repository at this point in the history
â™» Remove UI dialogs from the backend
  • Loading branch information
Komposten authored Aug 2, 2019
2 parents 8581a4f + ad0dc3c commit ebb88a7
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 53 deletions.
4 changes: 3 additions & 1 deletion src/main/java/komposten/vivaldi/Launcher.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package komposten.vivaldi;

import java.io.IOException;

import komposten.vivaldi.backend.Backend;

public class Launcher
{
public static void main(String[] args)
public static void main(String[] args) throws IOException
{
new Backend(null);
}
Expand Down
63 changes: 22 additions & 41 deletions src/main/java/komposten/vivaldi/backend/Backend.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package komposten.vivaldi.backend;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.ClosedWatchServiceException;
import java.nio.file.FileSystems;
Expand All @@ -34,8 +33,6 @@
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import javax.swing.JOptionPane;

import komposten.utilities.data.Settings;
import komposten.utilities.logging.ExceptionHandler;
import komposten.utilities.logging.Level;
Expand All @@ -54,11 +51,12 @@ public class Backend
static final String VERSION_PATTERN = "(\\d+\\.)+\\d+";

private static final String FILE_SETTINGS = "settings.ini";
static final String FILE_CONFIG = "config.ini";
static final String FILE_PATCHED = "PATCHED";
public static final String FILE_PATCHLOG = "patchlog.txt";

private final String configPath;

private List<String> configErrors;

private WatchService watchService;
private Map<WatchKey, File> keyToDirMap;
Expand All @@ -73,12 +71,14 @@ public class Backend
private WorkerThread workerThread;


public Backend(String configPath)
public Backend(String configPath) throws IOException
{
if (!LogUtils.hasInitialised())
LogUtils.writeToFile("log.txt");
if (configPath == null)
throw new NullPointerException("configPath must not be null!");

this.configPath = (configPath != null ? configPath : FILE_CONFIG);
this.configPath = configPath;

workerThread = new WorkerThread();
workerThread.start();
Expand All @@ -93,7 +93,7 @@ public Backend(String configPath)
}


public void start()
public boolean start()
{
boolean configValid = validateModConfig();

Expand All @@ -105,6 +105,8 @@ public void start()
if (appConfig.getBoolean(SETTING_WATCH, true) && addFileWatchers())
startFileWatch();
}

return configValid;
}


Expand All @@ -120,6 +122,12 @@ public ModConfig getModConfig()
}


public List<String> getConfigErrors()
{
return configErrors;
}


public void setModConfig(ModConfig newConfig)
{
workerThread.postRunnable(() ->
Expand All @@ -143,56 +151,29 @@ public void saveModConfig()
}


private void loadConfigs()
private void loadConfigs() throws IOException
{
appConfig = new Settings(FILE_SETTINGS);

try
{
modConfig = new ModConfig(new File(configPath));
}
catch (FileNotFoundException e)
{
//FIXME Don't show UI messages in the backend!
String title = "Could not load the config!";
String msg = String.format("The config file (%s) could not be found!"
+ "%nStarting with an empty config.", configPath);
JOptionPane.showMessageDialog(null, msg, title, JOptionPane.ERROR_MESSAGE);
LogUtils.log(Level.ERROR, getClass().getSimpleName(), msg, e, false);

modConfig = new ModConfig(new File(configPath), null, null, null);
}
catch (IOException e)
{
String title = "Could not load the config!";
String msg = String.format("The config file (%s) could not be read:"
+ "%n%s", configPath, e.getMessage());
JOptionPane.showMessageDialog(null, msg, title, JOptionPane.ERROR_MESSAGE);
LogUtils.log(Level.ERROR, getClass().getSimpleName(), msg, e, false);
}
modConfig = new ModConfig(new File(configPath));
}


private boolean validateModConfig()
{
List<String> errors = modConfig.validate();
configErrors = modConfig.validate();

if (!errors.isEmpty())
if (!configErrors.isEmpty())
{
StringBuilder builder = new StringBuilder();
builder.append(String.format("The config contains %d errors:", errors.size()));
builder.append(String.format("The config contains %d errors:", configErrors.size()));

for (String error : errors)
for (String error : configErrors)
builder.append("\n" + error);

LogUtils.log(Level.ERROR, builder.toString());

//FIXME Don't show UI messages in the backend!
JOptionPane.showMessageDialog(null, builder.toString(), "Invalid config!",
JOptionPane.ERROR_MESSAGE);
}

return errors.isEmpty();
return configErrors.isEmpty();
}


Expand Down
16 changes: 9 additions & 7 deletions src/main/java/komposten/vivaldi/backend/ModConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -42,7 +41,7 @@ public class ModConfig
private List<Instruction> instructions;


public ModConfig(File file) throws FileNotFoundException, IOException
public ModConfig(File file) throws IOException
{
this(file, null, null, null);
parseFile(file);
Expand Down Expand Up @@ -84,14 +83,17 @@ public List<Instruction> getInstructions()
}


private void parseFile(File file) throws FileNotFoundException, IOException
private void parseFile(File file) throws IOException
{
try (BufferedReader reader = new BufferedReader(new FileReader(file)))
if (file.exists())
{
String line;
while ((line = reader.readLine()) != null)
try (BufferedReader reader = new BufferedReader(new FileReader(file)))
{
parseLine(line.trim());
String line;
while ((line = reader.readLine()) != null)
{
parseLine(line.trim());
}
}
}
}
Expand Down
23 changes: 21 additions & 2 deletions src/main/java/komposten/vivaldi/ui/ModPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ public class ModPanel extends JPanel
private EditInstructionDialog editDialog;
private VivaldiDirectoryDialog vivaldiDialog;

public ModPanel(String configPath)
/**
* @throws IOException If an I/O error occurred while reading the config file.
*/
public ModPanel(String configPath) throws IOException
{
super(new GridBagLayout());

Expand Down Expand Up @@ -170,7 +173,23 @@ public ModPanel(String configPath)

addInitialData();

backend.start();
startBackend();
}


private void startBackend()
{
if (!backend.start())
{
StringBuilder builder = new StringBuilder();
builder.append(String.format("The config contains %d errors:", backend.getConfigErrors().size()));

for (String error : backend.getConfigErrors())
builder.append("\n").append(error);

JOptionPane.showMessageDialog(null, builder.toString(), "Invalid config!",
JOptionPane.ERROR_MESSAGE);
}
}


Expand Down
45 changes: 43 additions & 2 deletions src/main/java/komposten/vivaldi/ui/VivaldiModderUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@
package komposten.vivaldi.ui;

import java.awt.Dimension;
import java.io.File;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import komposten.utilities.logging.Level;
import komposten.utilities.logging.LogUtils;

public class VivaldiModderUI extends JFrame
Expand All @@ -34,7 +38,22 @@ public VivaldiModderUI(String configPath)

LogUtils.writeToFile("log.txt");

modPanel = new ModPanel(configPath);
checkConfigExists(configPath);

try
{
modPanel = new ModPanel(configPath);
}
catch (IOException e)
{
String title = "Could not load the config!";
String msg = String.format("The config file (%s) could not be read!"
+ "%nReason: %s"
+ "%n%nThe program will exit.", configPath, e.getMessage());
LogUtils.log(Level.ERROR, getClass().getSimpleName(), title, e, false);
JOptionPane.showMessageDialog(null, msg, title, JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
setContentPane(modPanel);

pack();
Expand All @@ -45,11 +64,33 @@ public VivaldiModderUI(String configPath)
}


private void checkConfigExists(String configPath)
{
File file = new File(configPath);

if (!file.exists())
{
String title = "Could not load the config!";
String msg = String.format("The config file (%s) could not be found!"
+ "%nStarting with an empty config instead.", configPath);
JOptionPane.showMessageDialog(null, msg, title, JOptionPane.ERROR_MESSAGE);
}
else if (!file.isFile())
{
String title = "Could not load the config!";
String msg = String.format("The config file (%s) is not a file!"
+ "%nThe program will exit.", configPath);
JOptionPane.showMessageDialog(null, msg, title, JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
}


public static void main(String[] args)
{
if (args.length > 0)
new VivaldiModderUI(args[0]);
else
new VivaldiModderUI(null);
new VivaldiModderUI("config.ini");
}
}

0 comments on commit ebb88a7

Please sign in to comment.