KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Wonderland


1 /*
2  * @(#)Wonderland.java 1.15 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)Wonderland.java 1.15 05/11/17
39  */

40
41
42
43 import java.util.Locale JavaDoc;
44 import java.util.MissingResourceException JavaDoc;
45 import java.util.ResourceBundle JavaDoc;
46
47 import java.net.URL JavaDoc;
48 import java.util.Hashtable JavaDoc;
49 import java.awt.Color JavaDoc;
50 import javax.swing.*;
51 import javax.swing.text.*;
52
53 /**
54  * hack to load attributed content
55  */

56 public class Wonderland {
57
58   Wonderland(DefaultStyledDocument doc, StyleContext styles) {
59     this.doc = doc;
60     this.styles = styles;
61     runAttr = new Hashtable JavaDoc();
62   }
63
64   void loadDocument() {
65     createStyles();
66     for (int i = 0; i < data.length; i++) {
67       Paragraph p = data[i];
68       addParagraph(p);
69     }
70   }
71
72   void addParagraph(Paragraph p) {
73     try {
74       Style s = null;
75       for (int i = 0; i < p.data.length; i++) {
76     Run run = p.data[i];
77     s = (Style) runAttr.get(run.attr);
78     doc.insertString(doc.getLength(), run.content, s);
79       }
80
81       // set logical style
82
Style ls = styles.getStyle(p.logical);
83       doc.setLogicalStyle(doc.getLength() - 1, ls);
84       doc.insertString(doc.getLength(), "\n", null);
85     } catch (BadLocationException e) {
86       System.err.println("Internal error: " + e);
87     }
88   }
89
90   void createStyles() {
91     // no attributes defined
92
Style s = styles.addStyle(null, null);
93     runAttr.put("none", s);
94     s = styles.addStyle(null, null);
95     StyleConstants.setItalic(s, true);
96     StyleConstants.setForeground(s, new Color JavaDoc(153,153,102));
97     runAttr.put("cquote", s); // catepillar quote
98

99     s = styles.addStyle(null, null);
100     StyleConstants.setItalic(s, true);
101     StyleConstants.setForeground(s, new Color JavaDoc(51,102,153));
102     runAttr.put("aquote", s); // alice quote
103

104     try {
105             ResourceBundle JavaDoc resources = ResourceBundle.getBundle("resources.Stylepad",
106                                 Locale.getDefault());
107         s = styles.addStyle(null, null);
108         Icon alice =
109                 new ImageIcon(getClass().
110                               getResource(resources.getString("aliceGif")));
111         StyleConstants.setIcon(s, alice);
112         runAttr.put("alice", s); // alice
113

114         s = styles.addStyle(null, null);
115         Icon caterpillar =
116                 new ImageIcon(getClass().
117                               getResource(resources.getString("caterpillarGif")));
118         StyleConstants.setIcon(s, caterpillar);
119         runAttr.put("caterpillar", s); // caterpillar
120

121         s = styles.addStyle(null, null);
122         Icon hatter =
123                 new ImageIcon(getClass().
124                               getResource(resources.getString("hatterGif")));
125         StyleConstants.setIcon(s, hatter);
126         runAttr.put("hatter", s); // hatter
127

128
129     } catch (MissingResourceException JavaDoc mre) {
130       // can't display image
131
}
132
133     Style def = styles.getStyle(StyleContext.DEFAULT_STYLE);
134
135     Style heading = styles.addStyle("heading", def);
136     StyleConstants.setFontFamily(heading, "SansSerif");
137     StyleConstants.setBold(heading, true);
138     StyleConstants.setAlignment(heading, StyleConstants.ALIGN_CENTER);
139     StyleConstants.setSpaceAbove(heading, 10);
140     StyleConstants.setSpaceBelow(heading, 10);
141     StyleConstants.setFontSize(heading, 18);
142
143     // Title
144
Style sty = styles.addStyle("title", heading);
145     StyleConstants.setFontSize(sty, 32);
146
147     // edition
148
sty = styles.addStyle("edition", heading);
149     StyleConstants.setFontSize(sty, 16);
150
151     // author
152
sty = styles.addStyle("author", heading);
153     StyleConstants.setItalic(sty, true);
154     StyleConstants.setSpaceBelow(sty, 25);
155
156     // subtitle
157
sty = styles.addStyle("subtitle", heading);
158     StyleConstants.setSpaceBelow(sty, 35);
159
160     // normal
161
sty = styles.addStyle("normal", def);
162     StyleConstants.setLeftIndent(sty, 10);
163     StyleConstants.setRightIndent(sty, 10);
164     StyleConstants.setFontFamily(sty, "SansSerif");
165     StyleConstants.setFontSize(sty, 14);
166     StyleConstants.setSpaceAbove(sty, 4);
167     StyleConstants.setSpaceBelow(sty, 4);
168   }
169
170   DefaultStyledDocument doc;
171   StyleContext styles;
172   Hashtable JavaDoc runAttr;
173
174   static class Paragraph {
175     Paragraph(String JavaDoc logical, Run[] data) {
176       this.logical = logical;
177       this.data = data;
178     }
179     String JavaDoc logical;
180     Run[] data;
181   }
182
183   static class Run {
184     Run(String JavaDoc attr, String JavaDoc content) {
185       this.attr = attr;
186       this.content = content;
187     }
188     String JavaDoc attr;
189     String JavaDoc content;
190   }
191
192   Paragraph[] data = new Paragraph[] {
193     new Paragraph("title", new Run[] {
194       new Run("none", "ALICE'S ADVENTURES IN WONDERLAND")
195     }),
196     new Paragraph("author", new Run[] {
197       new Run("none", "Lewis Carroll")
198     }),
199     new Paragraph("heading", new Run[] {
200       new Run("alice", " ")
201     }),
202     new Paragraph("edition", new Run[] {
203       new Run("none", "THE MILLENNIUM FULCRUM EDITION 3.0")
204     }),
205     new Paragraph("heading", new Run[] {
206       new Run("none", "CHAPTER V")
207     }),
208     new Paragraph("subtitle", new Run[] {
209       new Run("none", "Advice from a Caterpillar")
210     }),
211     new Paragraph("normal", new Run[] {
212       new Run("none", " "),
213     }),
214     new Paragraph("normal", new Run[] {
215       new Run("none", "The Caterpillar and Alice looked at each other for some time in silence: at last the Caterpillar took the hookah out of its mouth, and addressed her in a languid, sleepy voice.")
216     }),
217     new Paragraph("normal", new Run[] {
218       new Run("cquote", "Who are YOU? "),
219       new Run("none", "said the Caterpillar.")
220     }),
221     new Paragraph("normal", new Run[] {
222       new Run("none", "This was not an encouraging opening for a conversation. Alice replied, rather shyly, "),
223       new Run("aquote", "I--I hardly know, sir, just at present--at least I know who I WAS when I got up this morning, but I think I must have been changed several times since then. "),
224     }),
225     new Paragraph("heading", new Run[] {
226       new Run("caterpillar", " ")
227     }),
228     new Paragraph("normal", new Run[] {
229       new Run("cquote", "What do you mean by that? "),
230       new Run("none", " said the Caterpillar sternly. "),
231       new Run("cquote", "Explain yourself!"),
232     }),
233     new Paragraph("normal", new Run[] {
234       new Run("aquote", "I can't explain MYSELF, I'm afraid, sir"),
235       new Run("none", " said Alice, "),
236       new Run("aquote", "because I'm not myself, you see."),
237     }),
238     new Paragraph("normal", new Run[] {
239       new Run("cquote", "I don't see,"),
240       new Run("none", " said the Caterpillar."),
241     }),
242     new Paragraph("normal", new Run[] {
243       new Run("aquote", "I'm afraid I can't put it more clearly, "),
244       new Run("none", "Alice replied very politely, "),
245       new Run("aquote", "for I can't understand it myself to begin with; and being so many different sizes in a day is very confusing."),
246     }),
247     new Paragraph("normal", new Run[] {
248       new Run("cquote", "It isn't, "),
249       new Run("none", "said the Caterpillar.")
250     }),
251     new Paragraph("normal", new Run[] {
252       new Run("aquote", "Well, perhaps you haven't found it so yet,"),
253       new Run("none", " said Alice; "),
254       new Run("aquote", "but when you have to turn into a chrysalis--you will some day, you know--and then after that into a butterfly, I should think you'll feel it a little queer, won't you?")
255     }),
256     new Paragraph("normal", new Run[] {
257       new Run("cquote", "Not a bit, "),
258       new Run("none", "said the Caterpillar.")
259     }),
260     new Paragraph("normal", new Run[] {
261       new Run("aquote", "Well, perhaps your feelings may be different,"),
262       new Run("none", " said Alice; "),
263       new Run("aquote", "all I know is, it would feel very queer to ME."),
264     }),
265     new Paragraph("normal", new Run[] {
266       new Run("cquote", "You!"),
267       new Run("none", " said the Caterpillar contemptuously. "),
268       new Run("cquote", "Who are YOU?"),
269     }),
270     new Paragraph("normal", new Run[] {
271       new Run("normal", "Which brought them back again to the beginning of the conversation. Alice felt a little irritated at the Caterpillar's making such VERY short remarks, and she drew herself up and said, very gravely, "),
272       new Run("aquote", "I think, you ought to tell me who YOU are, first."),
273     }),
274     new Paragraph("normal", new Run[] {
275       new Run("cquote", "Why? "),
276       new Run("none", "said the Caterpillar."),
277     }),
278     new Paragraph("heading", new Run[] {
279       new Run("hatter", " ")
280     }),
281     new Paragraph("normal", new Run[] {
282       new Run("none", " "),
283     }),
284     new Paragraph("normal", new Run[] {
285       new Run("none", " "),
286     }),
287     new Paragraph("normal", new Run[] {
288       new Run("none", " "),
289     })
290   };
291
292
293 }
294
Popular Tags