Skip to content

Commit

Permalink
Remove references to 'doc', this is instead set when LineFormatter is…
Browse files Browse the repository at this point in the history
… initialized.
  • Loading branch information
matty-r committed Dec 11, 2023
1 parent b8c73cf commit e8bee1c
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 68 deletions.
11 changes: 6 additions & 5 deletions src/urChatBasic/base/IRCRoomBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ private void setupMainTextArea()
channelTextArea.setEditable(false);
channelTextArea.setFont(getFontPanel().getFont());
channelTextArea.setEditorKit(new StyledEditorKit());
// This is needed because the channelTextArea isn't the same after it was initialized.
resetLineFormatter();
}

private void setupUsersList()
Expand Down Expand Up @@ -458,8 +460,8 @@ public void run()
String line = messagePair.getLine();
String fromUser = messagePair.getUser();

Document document = channelTextArea.getDocument();
Element root = document.getDefaultRootElement();
Document document = lineFormatter.getDocument();
Element root = lineFormatter.getDocument().getDefaultRootElement();

int lineLimit = gui.getLimitChannelLinesCount();

Expand Down Expand Up @@ -519,7 +521,7 @@ public void run()

if (fromUser.equals(Constants.EVENT_USER) || !fromIRCUser.isMuted())
{
lineFormatter.formattedDocument(doc, new Date(), fromIRCUser, fromUser, line);
lineFormatter.formattedDocument(new Date(), fromIRCUser, fromUser, line);

if (server.getNick() != null && line.indexOf(server.getNick()) > -1)
{
Expand Down Expand Up @@ -951,8 +953,7 @@ public void run()
{

fontDialog.getFontPanel().setDefaultFont(f);
lineFormatter.setFont((StyledDocument) channelTextArea.getDocument(),
fontDialog.getFontPanel().getFont());
lineFormatter.setFont(fontDialog.getFontPanel().getFont());
// TODO: Should this updateStyles if the font is changed?
// lineFormatter.updateStyles((StyledDocument) channelTextArea.getDocument(), 0);
}
Expand Down
93 changes: 62 additions & 31 deletions src/urChatBasic/frontend/LineFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class LineFormatter
private URStyle mediumStyle;
private URStyle lowStyle;
private JTextPane docOwner;
private StyledDocument doc;
public URStyle myStyle;
private Map<String, URStyle> formatterStyles = new HashMap<>();

Expand All @@ -62,6 +63,7 @@ public LineFormatter(URStyle baseStyle, JTextPane docOwner ,final IRCServerBase

// The JTextPane is technically 'disabled', so we need to change the colour to be the enabled colour.
this.docOwner.setBackground(UIManager.getColor(Constants.DEFAULT_BACKGROUND_STRING));
doc = this.docOwner.getStyledDocument();

if (null != server)
{
Expand Down Expand Up @@ -103,11 +105,11 @@ public LineFormatter(URStyle baseStyle, JTextPane docOwner ,final IRCServerBase
formatterStyles.put(lowStyle.getName(), lowStyle);
}

public void setFont(StyledDocument doc, Font newFont)
public void setFont(Font newFont)
{
targetStyle.setFont(newFont);
if (doc.getLength() > 0)
updateStyles(doc, 0);
updateStyles(0);
}

public URStyle defaultStyle(String name, boolean load)
Expand Down Expand Up @@ -359,7 +361,7 @@ public void actionPerformed(ActionEvent e)
}

// Inserts the string at the position
private void insertString(StyledDocument doc, String insertedString, SimpleAttributeSet style, int position)
private void insertString(String insertedString, SimpleAttributeSet style, int position)
throws BadLocationException
{
// remove the existing attributes
Expand All @@ -373,12 +375,12 @@ private void insertString(StyledDocument doc, String insertedString, SimpleAttri
}

// Adds the string (with all needed attributes) to the end of the document
private void appendString(StyledDocument doc, String insertedString, SimpleAttributeSet style)
private void appendString(String insertedString, SimpleAttributeSet style)
throws BadLocationException
{
int position = doc.getLength();

insertString(doc, insertedString, style, position);
insertString(insertedString, style, position);
}

public URStyle getStyleDefault(String styleName)
Expand Down Expand Up @@ -419,10 +421,9 @@ public URStyle getStyle(String styleName, boolean load)

/**
* Reloads all the styles, then updates the doc
* @param doc
* @param startPosition
*/
public void updateStyles(StyledDocument doc, int startPosition)
public void updateStyles(int startPosition)
{
targetStyle.load(formatterPrefs);

Expand All @@ -431,10 +432,10 @@ public void updateStyles(StyledDocument doc, int startPosition)
}

Constants.LOGGER.log(Level.INFO, "Updating styles.");
updateDocStyles(doc, startPosition);
updateDocStyles(startPosition);
}

private void updateDocStyles(StyledDocument doc, int startPosition)
private void updateDocStyles(int startPosition)
{
SimpleAttributeSet textStyle = new SimpleAttributeSet(doc.getCharacterElement(startPosition).getAttributes());

Expand Down Expand Up @@ -472,7 +473,7 @@ private void updateDocStyles(StyledDocument doc, int startPosition)
SimpleAttributeSet timeStyle = getStyle(styleName, false);
timeStyle.addAttribute("date", lineDate);
timeStyle.addAttribute("type", "time");
insertString(doc, newTimeString, timeStyle, styleStart);
insertString(newTimeString, timeStyle, styleStart);
styleLength = newTimeString.length();
} else
{
Expand Down Expand Up @@ -524,10 +525,37 @@ private void updateDocStyles(StyledDocument doc, int startPosition)
doc.setCharacterAttributes(styleStart, styleLength, matchingStyle, true);

if ((styleStart + styleLength) < doc.getLength())
updateDocStyles(doc, (styleStart + styleLength));
updateDocStyles((styleStart + styleLength));
}

public String getLatestLine(StyledDocument doc) throws BadLocationException
public String getFirstLine() throws BadLocationException
{
Element root = doc.getDefaultRootElement();
int linePos = 0;

String finalLine = "";

while (finalLine.isEmpty())
{

if (linePos < 0)
break;

Element line = root.getElement(linePos++);

if (null == line)
continue;

int start = line.getStartOffset();
int end = line.getEndOffset();
String text = doc.getText(start, end - start);
finalLine = text.trim();
}

return finalLine;
}

public String getLatestLine() throws BadLocationException
{
Element root = doc.getDefaultRootElement();
int lines = root.getElementCount();
Expand All @@ -554,7 +582,7 @@ public String getLatestLine(StyledDocument doc) throws BadLocationException
return finalLine;
}

private int getLinePosition(StyledDocument doc, String targetLine) throws BadLocationException
private int getLinePosition(String targetLine) throws BadLocationException
{
Element root = doc.getDefaultRootElement();
int lines = root.getElementCount();
Expand All @@ -579,25 +607,25 @@ private int getLinePosition(StyledDocument doc, String targetLine) throws BadLoc
return 0;
}

public SimpleAttributeSet getStyleAtPosition(StyledDocument doc, int position, String relativeLine)
public SimpleAttributeSet getStyleAtPosition(int position, String relativeLine)
throws BadLocationException
{
if (!relativeLine.isBlank())
position = position + getLinePosition(doc, relativeLine);
position = position + getLinePosition(relativeLine);

AttributeSet textStyle = doc.getCharacterElement(position).getAttributes();

return new SimpleAttributeSet(textStyle);
}

private void parseClickableText(StyledDocument doc, IRCUser fromUser, String line, URStyle defaultStyle)
private void parseClickableText(IRCUser fromUser, String line, URStyle defaultStyle)
throws BadLocationException
{
HashMap<String, URStyle> regexStrings = new HashMap<>();
regexStrings.put(Constants.URL_REGEX, urlStyle);
regexStrings.put(Constants.CHANNEL_REGEX, channelStyle);
// final String line = getLatestLine(doc);
final int relativePosition = getLinePosition(doc, getLatestLine(doc));
final int relativePosition = getLinePosition(getLatestLine());

ArrayList<URStyle> clickableLines = new ArrayList<URStyle>();

Expand Down Expand Up @@ -645,25 +673,23 @@ private void parseClickableText(StyledDocument doc, IRCUser fromUser, String lin
int nextLineLength = Integer.parseInt(nextLine.getAttribute("styleLength").toString());

// Append the string that comes before the next clickable text
appendString(doc, remainingLine.substring(0, nextLineStart - offset), defaultStyle);
appendString(remainingLine.substring(0, nextLineStart - offset), defaultStyle);

appendString(doc, nextLine.getAttribute("clickableText").toString(), nextLine);
appendString(nextLine.getAttribute("clickableText").toString(), nextLine);

remainingLine = remainingLine.substring((nextLineStart + nextLineLength) - offset);
}

appendString(doc, remainingLine, defaultStyle);
appendString(remainingLine, defaultStyle);
}

/**
* Inserts a string onto the end of the doc.
*
* @param doc
* @param timeLine
* @param fromUser
* @param line
* @param timeLine
*/
public void formattedDocument(StyledDocument doc, Date lineDate, IRCUser fromUser, String fromString, String line)
public void formattedDocument(Date lineDate, IRCUser fromUser, String fromString, String line)
{
// build the timeLine string
String timeLine = UserGUI.getTimeLineString(lineDate);
Expand Down Expand Up @@ -708,15 +734,15 @@ public void formattedDocument(StyledDocument doc, Date lineDate, IRCUser fromUse
timePositionStyle.addAttribute("date", lineDate);
timePositionStyle.removeAttribute("type");
timePositionStyle.addAttribute("type", "time");
appendString(doc, timeLine + " ", timePositionStyle);
appendString(timeLine + " ", timePositionStyle);
timePositionStyle.removeAttribute("type");
linePositionStyle.removeAttribute("date");
} else
{
linePositionStyle.addAttribute("date", lineDate);
}

appendString(doc, "<", linePositionStyle);
appendString("<", linePositionStyle);
linePositionStyle.removeAttribute("date");

if (fromUser != null)
Expand All @@ -727,25 +753,30 @@ public void formattedDocument(StyledDocument doc, Date lineDate, IRCUser fromUse
new ClickableText(fromUser.toString(), nickPositionStyle, fromUser));

// doc.insertString(doc.getLength(), fromUser.toString(), clickableNameStyle);
appendString(doc, fromUser.toString(), clickableNameStyle);
appendString(fromUser.toString(), clickableNameStyle);
} else
{
appendString(doc, fromString, nickPositionStyle);
appendString(fromString, nickPositionStyle);
}

appendString(doc, ">", linePositionStyle);
appendString(">", linePositionStyle);

// print the remaining text
// appendString(doc, " "+line, lineStyle);

// parse the outputted line for clickable text
parseClickableText(doc, fromUser, " " + line, linePositionStyle);
parseClickableText(fromUser, " " + line, linePositionStyle);

appendString(doc, System.getProperty("line.separator"), linePositionStyle);
appendString(System.getProperty("line.separator"), linePositionStyle);
} catch (BadLocationException e)
{
Constants.LOGGER.log(Level.SEVERE, e.getLocalizedMessage());
}
}

public StyledDocument getDocument()
{
return doc;
}

}
22 changes: 9 additions & 13 deletions src/urChatBasic/frontend/UserGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -920,19 +920,14 @@ public void updatePreviewTextArea()
IRCUser tempUser = new IRCUser(null, "matty_r");
IRCUser tempUser2 = new IRCUser(null, System.getProperty("user.name"));
previewLineFormatter.setNick(System.getProperty("user.name"));
previewLineFormatter.formattedDocument(previewDoc, new Date(), null, Constants.EVENT_USER,
"urChat has loaded - this is an Event");
previewLineFormatter.formattedDocument(previewDoc, new Date(), tempUser, "matty_r",
"Normal line. Hello, world!");
previewLineFormatter.formattedDocument(previewDoc, new Date(), tempUser, "matty_r",
"This is what it looks like when your nick is mentioned, " + System.getProperty("user.name") + "!");
previewLineFormatter.formattedDocument(previewDoc, new Date(), tempUser2, System.getProperty("user.name"),
"Go to https://github.com/matty-r/urChat");
previewLineFormatter.formattedDocument(previewDoc, new Date(), tempUser2, System.getProperty("user.name"),
"Join #urchatclient on irc.libera.chat or #anotherroom");
previewLineFormatter.formattedDocument(new Date(), null, Constants.EVENT_USER, "urChat has loaded - this is an Event");
previewLineFormatter.formattedDocument(new Date(), tempUser, "matty_r", "Normal line. Hello, world!");
previewLineFormatter.formattedDocument(new Date(), tempUser, "matty_r", "This is what it looks like when your nick is mentioned, " + System.getProperty("user.name") + "!");
previewLineFormatter.formattedDocument(new Date(), tempUser2, System.getProperty("user.name"), "Go to https://github.com/matty-r/urChat");
previewLineFormatter.formattedDocument(new Date(), tempUser2, System.getProperty("user.name"), "Join #urchatclient on irc.libera.chat or #anotherroom");
} else
{
previewLineFormatter.updateStyles(previewDoc, 0);
previewLineFormatter.updateStyles(0);
}
}

Expand All @@ -955,7 +950,7 @@ public void mouseClicked(MouseEvent mouseEvent)
// List<ActionListener> actionListeners = styleFontDialog.getFontPanel().getActionListeners();
// TODO: Need to save attributes and updateStyles after..
// Currently runs the save after updateStyles
previewLineFormatter.updateStyles(doc, 0);
previewLineFormatter.updateStyles(0);
});

// styleFontDialog.addResetListener(new ActionListener() {
Expand Down Expand Up @@ -1901,7 +1896,7 @@ public void actionPerformed(ActionEvent arg0)
}

// previewLineFormatter.setFont(previewTextArea.getStyledDocument(), clientFontPanel.getFont());
previewLineFormatter.updateStyles(previewTextArea.getStyledDocument(), 0);
previewLineFormatter.updateStyles(0);
}
}

Expand Down Expand Up @@ -2070,6 +2065,7 @@ private void updateExtras()
((IRCRoomBase) tab).getFontPanel().setDefaultFont(clientFontPanel.getFont());
SwingUtilities.updateComponentTreeUI(((IRCRoomBase) tab).myMenu);
SwingUtilities.updateComponentTreeUI(((IRCRoomBase) tab).getFontPanel());
// TODO: Update styles ((IRCRoomBase) tab).getChannelTextPane()
}

}
Expand Down
Loading

0 comments on commit e8bee1c

Please sign in to comment.