Procházet zdrojové kódy

The styliser now supports bold

git-svn-id: http://svn.dmdirc.com/trunk@253 00569f92-eb28-0410-84fd-f71c24880f
tags/0.2
Chris Smith před 17 roky
rodič
revize
1e58875319
1 změnil soubory, kde provedl 55 přidání a 1 odebrání
  1. 55
    1
      src/uk/org/ownage/dmdirc/ui/messages/Styliser.java

+ 55
- 1
src/uk/org/ownage/dmdirc/ui/messages/Styliser.java Zobrazit soubor

@@ -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
 }

Načítá se…
Zrušit
Uložit