|
@@ -22,7 +22,10 @@
|
22
|
22
|
|
23
|
23
|
package uk.org.ownage.dmdirc.ui.messages;
|
24
|
24
|
|
|
25
|
+import javax.swing.text.AttributeSet;
|
25
|
26
|
import javax.swing.text.BadLocationException;
|
|
27
|
+import javax.swing.text.SimpleAttributeSet;
|
|
28
|
+import javax.swing.text.StyleConstants;
|
26
|
29
|
import javax.swing.text.StyledDocument;
|
27
|
30
|
import uk.org.ownage.dmdirc.logger.ErrorLevel;
|
28
|
31
|
import uk.org.ownage.dmdirc.logger.Logger;
|
|
@@ -39,10 +42,61 @@ public class Styliser {
|
39
|
42
|
|
40
|
43
|
static public void addStyledString(StyledDocument doc, String add) {
|
41
|
44
|
try {
|
42
|
|
- doc.insertString(doc.getLength(), add, null);
|
|
45
|
+ int offset = doc.getLength();
|
|
46
|
+ int position = 0;
|
|
47
|
+ boolean cont = true;
|
|
48
|
+ SimpleAttributeSet attribs = new SimpleAttributeSet();
|
|
49
|
+
|
|
50
|
+ while (position < add.length()) {
|
|
51
|
+ String next = readUntilControl(add.substring(position));
|
|
52
|
+
|
|
53
|
+ System.out.printf("%s", next);
|
|
54
|
+
|
|
55
|
+ doc.insertString(offset, next, attribs);
|
|
56
|
+
|
|
57
|
+ position += next.length();
|
|
58
|
+ offset += next.length();
|
|
59
|
+
|
|
60
|
+ if (position < add.length()) {
|
|
61
|
+ position += readControlChars(add.substring(position), attribs);
|
|
62
|
+ }
|
|
63
|
+ }
|
|
64
|
+
|
43
|
65
|
} catch (BadLocationException ex) {
|
44
|
66
|
Logger.error(ErrorLevel.WARNING, ex);
|
45
|
67
|
}
|
46
|
68
|
}
|
47
|
69
|
|
|
70
|
+ static private String readUntilControl(String input) {
|
|
71
|
+ int pos = input.length();
|
|
72
|
+
|
|
73
|
+ // Bold
|
|
74
|
+ pos = checkChar(pos, input.indexOf(2));
|
|
75
|
+
|
|
76
|
+ return input.substring(0, pos);
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ private static int readControlChars(String string, SimpleAttributeSet attribs) {
|
|
80
|
+ // Bold
|
|
81
|
+ if (string.charAt(0) == 2) {
|
|
82
|
+ toggleAttribute(attribs, StyleConstants.FontConstants.Bold);
|
|
83
|
+ return 1;
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ return 0;
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+ private static int checkChar(int pos, int i) {
|
|
90
|
+ if (i < pos && i != -1) { return i; }
|
|
91
|
+ return pos;
|
|
92
|
+ }
|
|
93
|
+
|
|
94
|
+ private static void toggleAttribute(SimpleAttributeSet attribs, Object attrib) {
|
|
95
|
+ if (attribs.containsAttribute(attrib, Boolean.TRUE)) {
|
|
96
|
+ attribs.removeAttribute(attrib);
|
|
97
|
+ } else {
|
|
98
|
+ attribs.addAttribute(attrib, Boolean.TRUE);
|
|
99
|
+ }
|
|
100
|
+ }
|
|
101
|
+
|
48
|
102
|
}
|