KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > model > StyledTextDocument


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 /** a document container for styled textpanes.
13     The document is the member variable doc.
14     This is actually only used for outputs (logs) => no undo...
15     See MailView for a document with undo function.
16 */

17 public final class StyledTextDocument
18 {
19
20    public final DefaultStyledDocument doc = new DefaultStyledDocument();
21
22
23    public int maxLength = 5000; // some lines to debug
24

25
26    public StyledTextDocument()
27    {
28       //Create the styles we need.
29
initializeStyles();
30    }
31
32
33    public void appendReadLine(String JavaDoc 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 JavaDoc 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 JavaDoc str)
61    {
62       appendStyled(str+"\n","comment");
63    }
64
65
66    public void appendError(String JavaDoc str)
67    {
68       appendStyled(str+"\n","error");
69    }
70
71
72    public void appendLine(String JavaDoc str)
73    {
74       appendStyled(str+"\n","regular");
75    }
76
77    public void appendSmallLine(String JavaDoc str)
78    {
79       appendStyled(str+"\n","small");
80    }
81
82    public void append(String JavaDoc str)
83    {
84       appendStyled(str,"regular");
85    }
86
87
88    public void appendStyled(final String JavaDoc str, final String JavaDoc style)
89    {
90      EventQueue.invokeLater(new Runnable JavaDoc()
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    /** recall this at each UI change. The best way is to add a property listener to the textpane and
124        react on ui changes.
125        NO => this is not necessary, the styles are updated correctly !!
126    */

127    public void initializeStyles()
128    {
129
130       //Initialize some styles.
131
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 /* s = doc.addStyle("icon", regular);
178       StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
179       StyleConstants.setIcon(s, new ImageIcon("images/Pig.PNG"));
180
181       s = doc.addStyle("button", regular);
182       StyleConstants.setAlignment(s, StyleConstants.ALIGN_CENTER);
183       JButton button = new JButton(new ImageIcon("images/sound.PNG"));
184       button.setMargin(new Insets(0,0,0,0));
185       button.addActionListener(new ActionListener() {
186           public void actionPerformed(ActionEvent e) {
187               Toolkit.getDefaultToolkit().beep();
188           }
189       });
190       StyleConstants.setComponent(s, button);*/

191    }
192
193
194 } // StyledTextDocument
Popular Tags