[TASK] Fix various exceptions
This commit is contained in:
1 parent
7269663572
commit
1dfb6d1542
4 files changed
+55
-16
No files matched your search
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user