Skip to content

Commit

Permalink
✨ Notify the user if the graph fails to load and why.
Browse files Browse the repository at this point in the history
Also fixes #7
  • Loading branch information
Komposten committed May 30, 2019
1 parent 2a6ca66 commit 65645ec
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 59 deletions.
72 changes: 55 additions & 17 deletions core/src/komposten/tcs/TetraColourSpace.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import java.util.LinkedList;
import java.util.List;

import javax.swing.JOptionPane;
import javax.xml.parsers.ParserConfigurationException;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
Expand All @@ -46,6 +49,7 @@
import com.badlogic.gdx.utils.ScreenUtils;

import komposten.tcs.backend.Backend;
import komposten.tcs.backend.ParseException;
import komposten.tcs.backend.Style.Colour;
import komposten.tcs.backend.data.Point;
import komposten.tcs.input.Action;
Expand Down Expand Up @@ -118,9 +122,11 @@ public void create()
inputHandler.addListener(inputListener);

createScreenshotBuffer();
loadData();
setupPerspectiveCamera();
updateViewport();
if (loadData())
{
setupPerspectiveCamera();
updateViewport();
}
}


Expand All @@ -136,14 +142,36 @@ private void createScreenshotBuffer()
}


private void loadData()
private boolean loadData()
{
try
{
backend = new Backend(dataFile, logger);
world = new World(backend, camera);
userInterface = new UserInterface(backend, world);

userInterface.attachToInputHandler(inputHandler);
world.attachToInputHandler(inputHandler);
}
catch (IOException | ParserConfigurationException | ParseException e)
{
showErrorDialog();
Gdx.app.exit();
return false;
}

return true;
}


private void showErrorDialog()
{
backend = new Backend(dataFile, logger);
world = new World(backend, camera);
userInterface = new UserInterface(backend, world);
String title = "Load error!";
String msg = String.format("Errors occurred while loading %s!"
+ "%nThe program will terminate."
+ "%n%nSee log.txt for more information.", dataFile.getName());

userInterface.attachToInputHandler(inputHandler);
world.attachToInputHandler(inputHandler);
JOptionPane.showMessageDialog(null, msg, title, JOptionPane.ERROR_MESSAGE);
}


Expand Down Expand Up @@ -255,14 +283,21 @@ private void updateSelectionLog()

private void updateViewport()
{
float ratio = Gdx.graphics.getHeight() / (float)Gdx.graphics.getWidth();
camera.viewportHeight = ratio;
cameraDirty = true;
if (camera != null)
{
float ratio = Gdx.graphics.getHeight() / (float)Gdx.graphics.getWidth();
camera.viewportHeight = ratio;
cameraDirty = true;
}

spriteCamera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
spriteBatch.setProjectionMatrix(spriteCamera.combined);
if (spriteCamera != null)
{
spriteCamera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
spriteBatch.setProjectionMatrix(spriteCamera.combined);
}

userInterface.resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
if (userInterface != null)
userInterface.resize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}


Expand All @@ -278,7 +313,9 @@ public void dispose()
{
screenshotBuffer.dispose();
batch.dispose();
userInterface.dispose();

if (userInterface != null)
userInterface.dispose();
disposeObjects();

if (!selectionLog.isEmpty())
Expand All @@ -300,7 +337,8 @@ private void disposeObjects()
disposable.dispose();
}

world.dispose();
if (world != null)
world.dispose();
}


Expand Down
47 changes: 36 additions & 11 deletions core/src/komposten/tcs/backend/Backend.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
Expand All @@ -43,7 +44,7 @@ public class Backend
private GraphData graph;


public Backend(File dataFile, Logger logger)
public Backend(File dataFile, Logger logger) throws IOException, ParserConfigurationException, ParseException
{
this.dataFile = dataFile;
this.logger = logger;
Expand All @@ -64,7 +65,7 @@ public GraphData getGraph()
}


private void loadDataFromFile()
private void loadDataFromFile() throws IOException, ParserConfigurationException, ParseException
{
try
{
Expand All @@ -79,19 +80,32 @@ private void loadDataFromFile()
}
catch (IOException e)
{
String msg = "Error reading data file: " + dataFile.getPath();
logger.log(Level.FATAL, getClass().getSimpleName(), msg, e, false);
logError("Error reading data file: %s", e, dataFile.getPath());
throw e;
}
catch (ParserConfigurationException e)
{
String msg = "Error creating the XML parser!";
logger.log(Level.FATAL, getClass().getSimpleName(), msg, e, false);
logError("Error creating the XML parser!", e);
throw e;
}
catch (SAXException e)
{
String msg = "Error parsing data file: " + dataFile.getPath();
logger.log(Level.FATAL, getClass().getSimpleName(), msg, e, false);
logError("Error parsing data file %s", e, dataFile.getPath());
throw new ParseException(e);
}
catch (ParseException e)
{
String msg = "Error parsing data file " + dataFile.getPath() + ": " + e.getMessage();
logger.log(Level.FATAL, msg);
throw e;
}
}


private void logError(String formatString, Throwable cause, Object... params)
{
String msg = String.format(formatString, params);
logger.log(Level.FATAL, getClass().getSimpleName(), msg, cause, false);
}


Expand All @@ -105,9 +119,20 @@ private void loadConfig(Element root)
}


private void loadGraph(Element root)
private void loadGraph(Element root) throws ParseException
{
GraphLoader loader = new GraphLoader(style, logger);
graph = loader.load(root);
GraphLoader loader = new GraphLoader(style);
if (!loader.load(root))
{
List<String> errors = loader.getErrors();

StringBuilder message = new StringBuilder();
for (String error : errors)
message.append("\n").append(error);

throw new ParseException(message.toString());
}

graph = loader.getResult();
}
}
Loading

0 comments on commit 65645ec

Please sign in to comment.