[TASK] Fix various exceptions

This commit is contained in:
Jan Philipp Timme 2016-10-03 19:44:07 +02:00
parent 7269663572
commit 1dfb6d1542
4 changed files with 55 additions and 16 deletions

View File

@ -12,7 +12,6 @@ import eu.larkc.csparql.core.engine.CsparqlEngine;
import eu.larkc.csparql.core.engine.CsparqlEngineImpl;
import eu.larkc.csparql.core.engine.CsparqlQueryResultProxy;
import lu.jpt.csparqlproject.gui.FancyTextObserverWindow;
import lu.jpt.csparqlproject.gui.RawTextObserverWindow;
import lu.jpt.csparqlproject.rentacar.RentACarSimulation;
import lu.jpt.csparqlproject.util.CsparqlQueryHelper;
@ -185,11 +184,13 @@ public class SimulationContext {
*/
private Observer createResultObserverWindow(String queryName) {
Observer observer = null;
/*
if(false && queryName.equals("getEvents")) {
observer = new RawTextObserverWindow("[ResultProxy] " + queryName);
} else {
observer = new FancyTextObserverWindow("[ResultProxy] " + queryName);
}
}*/
observer = new FancyTextObserverWindow("[ResultProxy] " + queryName);
return observer;
}

View File

@ -4,6 +4,7 @@ import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionEvent;
@ -52,6 +53,7 @@ public class FancyTextObserverWindow extends JFrame implements Observer {
private JTextPane textPane;
private volatile boolean autoScrollToBottom = true;
private volatile boolean usePrefixManager = true;
private volatile boolean clearInsteadOfEleminatingLines = true;
/**
* Constructor
@ -70,6 +72,15 @@ public class FancyTextObserverWindow extends JFrame implements Observer {
this.autoScrollToBottom = autoScrollToBottom;
}
/**
* Set whether the window contents shall be cleared instead of being
* maintained line by line to enforce a line limit.
* @param clear
*/
public void setClearInsteadOfEliminatingLines(boolean clear) {
this.clearInsteadOfEleminatingLines = clear;
}
/**
* Set whether or not the PrefixManager shall be used to shorten
* uris in strings to prefixes.
@ -247,12 +258,25 @@ public class FancyTextObserverWindow extends JFrame implements Observer {
}
/**
* Internal method to append text to the textpane
* Internal man-in-the-middle method to pass the task to the AWT EventQueue
* @param text to show within the window
* @param addLinebreak whether or not to add a linebreak (usually yes is better)
* @param color to display the text in
*/
private void appendToTextPane(String text, boolean addLinebreak, Color color) {
final FancyTextObserverWindow me = this;
EventQueue.invokeLater(() -> {
me.actuallyAppendTextToTextPane(text, addLinebreak, color);
});
}
/**
* Internal method to append text to the textpane
* @param text to show within the window
* @param addLinebreak whether or not to add a linebreak (usually yes is better)
* @param color to display the text in
*/
private void actuallyAppendTextToTextPane(String text, boolean addLinebreak, Color color) {
if (addLinebreak) text += "\n";
StyleContext styleContext = StyleContext.getDefaultStyleContext();
AttributeSet attributeSet = styleContext.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, color);
@ -271,35 +295,47 @@ public class FancyTextObserverWindow extends JFrame implements Observer {
}
} while(!success);
// This is the place to enforce a line limit
//this.enforceLineLimit();
this.enforceLineLimit();
// TODO: Fix behaviour when autoscroll is disabled!
if(this.autoScrollToBottom) {
Document textPaneDocument = this.textPane.getDocument();
this.textPane.select(textPaneDocument.getLength(), textPaneDocument.getLength());
}
}
}
/**
* Helper method to enforce a line limit for the window.
*/
private void enforceLineLimit() {
int MAX_LINES = 50;
int MAX_LINES = 300;
String[] lines = this.textPane.getText().split("\n");
int totalLines = lines.length;
int characters = 0;
if(totalLines > MAX_LINES) {
int linesToRemove = (totalLines - MAX_LINES) - 1;
if(linesToRemove > 5) {
for(int currentLine = 0; currentLine < linesToRemove; currentLine++) {
characters += lines[currentLine].length() + 1;
}
if(clearInsteadOfEleminatingLines) {
try {
this.textPane.getDocument().remove(0, characters);
this.textPane.getDocument().remove(0, this.textPane.getDocument().getLength());
} catch (BadLocationException e) {
FancyTextObserverWindow.logger.error(e.toString());
FancyTextObserverWindow.logger.error(e.getMessage());
// This has to work. :-/
}
} else {
int linesToRemove = (totalLines - MAX_LINES) - 1;
if(linesToRemove > 0) {
for(int currentLine = 0; currentLine < linesToRemove; currentLine++) {
characters += lines[currentLine].length() + 1;
}
try {
this.textPane.getDocument().remove(0, characters);
} catch (BadLocationException e) {
FancyTextObserverWindow.logger.error(e.toString());
FancyTextObserverWindow.logger.error(e.getMessage());
}
// Bonus: Update caret position
int length = this.textPane.getDocument().getLength();
this.textPane.setCaretPosition(length);
}
}
}
}

View File

@ -32,6 +32,8 @@ import eu.larkc.csparql.core.engine.CsparqlQueryResultProxy;
import lu.jpt.csparqlproject.Main;
/**
* Unused raw text window that uses a textArea instead of a textPane.
* Not implemented completely yet.
* Multi-purpose text window to allow for easy viewing of RdfStream quadruples,
* various logging or observing query results.
*/

View File

@ -42,7 +42,7 @@ public class RentACarSimulation implements Runnable {
public RentACarSimulation() {
this.registerOwnPrefixes();
int numberOfCars = 1;
int numberOfCars = 5;
int numberOfCustomers = 1;
// Create a car pool and drivers
this.carPool = new CarPool(numberOfCars);