1 package SnowMailClient.model; 2 3 import snow.utils.storage.*; 4 5 import java.util.*; 6 import javax.swing.*; 7 import javax.swing.text.*; 8 import java.awt.*; 9 import java.awt.event.*; 10 11 12 17 public final class StyledTextDocument 18 { 19 20 public final DefaultStyledDocument doc = new DefaultStyledDocument(); 21 22 23 public int maxLength = 5000; 25 26 public StyledTextDocument() 27 { 28 initializeStyles(); 30 } 31 32 33 public void appendReadLine(String str, boolean wasEncrypted) 34 { 35 if(wasEncrypted) 36 { 37 appendStyled("R> "+str+"\n", "readEncrypted"); 38 } 39 else 40 { 41 appendStyled("R> "+str+"\n", "readNotEncrypted"); 42 } 43 } 44 45 46 public void appendSendLine(String str, boolean wasEncrypted) 47 { 48 if(wasEncrypted) 49 { 50 appendStyled("S> "+str+"\n", "sendEncrypted"); 51 } 52 else 53 { 54 appendStyled("S> "+str+"\n", "sendNotEncrypted"); 55 } 56 } 57 58 59 60 public void appendComment(String str) 61 { 62 appendStyled(str+"\n","comment"); 63 } 64 65 66 public void appendError(String str) 67 { 68 appendStyled(str+"\n","error"); 69 } 70 71 72 public void appendLine(String str) 73 { 74 appendStyled(str+"\n","regular"); 75 } 76 77 public void appendSmallLine(String str) 78 { 79 appendStyled(str+"\n","small"); 80 } 81 82 public void append(String str) 83 { 84 appendStyled(str,"regular"); 85 } 86 87 88 public void appendStyled(final String str, final String style) 89 { 90 EventQueue.invokeLater(new Runnable () 91 { 92 public void run() 93 { 94 if (doc != null) 95 { 96 try 97 { 98 doc.insertString(doc.getLength(), str, doc.getStyle(style)); 99 } 100 catch (BadLocationException e) { e.printStackTrace(); } 101 checkLength(); 102 } 103 } 104 }); 105 } 106 107 108 109 public void checkLength() 110 { 111 if(doc.getLength()>maxLength) 112 { 113 try 114 { 115 doc.remove(0, doc.getLength()-maxLength); 116 } 117 catch (BadLocationException e) { e.printStackTrace();} 118 } 119 } 120 121 122 123 127 public void initializeStyles() 128 { 129 130 Style def = StyleContext.getDefaultStyleContext(). 132 getStyle(StyleContext.DEFAULT_STYLE); 133 134 StyleConstants.setFontSize(def, 11); 135 136 Style regular = doc.addStyle("regular", def); 137 StyleConstants.setFontFamily(def, "SansSerif"); 138 139 Style s = doc.addStyle("italic", regular); 140 StyleConstants.setItalic(s, true); 141 142 s = doc.addStyle("bold", regular); 143 StyleConstants.setBold(s, true); 144 145 s = doc.addStyle("small", regular); 146 StyleConstants.setFontSize(s, 9); 147 148 s = doc.addStyle("large", regular); 149 StyleConstants.setFontSize(s, 16); 150 151 s = doc.addStyle("readEncrypted", regular); 152 StyleConstants.setForeground(s, new Color(0f,0.8f,0f)); 153 154 s = doc.addStyle("sendEncrypted", regular); 155 StyleConstants.setForeground(s, new Color(0f,0.8f,0f)); 156 StyleConstants.setBold(s, true); 157 StyleConstants.setItalic(s, true); 158 159 s = doc.addStyle("comment", regular); 160 StyleConstants.setForeground(s, new Color(0f,0.0f,0.8f)); 161 StyleConstants.setBold(s, true); 162 StyleConstants.setItalic(s, true); 163 164 s = doc.addStyle("error", regular); 165 StyleConstants.setForeground(s, new Color(0.9f,0.0f,0.0f)); 166 StyleConstants.setBold(s, true); 167 StyleConstants.setItalic(s, true); 168 169 s = doc.addStyle("readNotEncrypted", regular); 170 StyleConstants.setForeground(s, new Color(255,153,0)); 171 172 s = doc.addStyle("sendNotEncrypted", regular); 173 StyleConstants.setForeground(s, new Color(255,153,0)); 174 StyleConstants.setBold(s, true); 175 StyleConstants.setItalic(s, true); 176 177 191 } 192 193 194 } | Popular Tags |