1 /*
2  *
3  * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  *   - Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  *
12  *   - Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in the
14  *     documentation and/or other materials provided with the distribution.
15  *
16  *   - Neither the name of Oracle nor the names of its
17  *     contributors may be used to endorse or promote products derived
18  *     from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 
34 import java.awt.Color;
35 import java.util.HashMap;
36 import java.util.Locale;
37 import java.util.MissingResourceException;
38 import java.util.ResourceBundle;
39 import javax.swing.Icon;
40 import javax.swing.ImageIcon;
41 import javax.swing.text.BadLocationException;
42 import javax.swing.text.DefaultStyledDocument;
43 import javax.swing.text.Style;
44 import javax.swing.text.StyleConstants;
45 import javax.swing.text.StyleContext;
46 
47 
48 /**
49  * hack to load attributed content
50  */
51 public class Wonderland {
52 
Wonderland(DefaultStyledDocument doc, StyleContext styles)53     Wonderland(DefaultStyledDocument doc, StyleContext styles) {
54         this.doc = doc;
55         this.styles = styles;
56         runAttr = new HashMap<String, Style>();
57     }
58 
loadDocument()59     void loadDocument() {
60         createStyles();
61         for (int i = 0; i < data.length; i++) {
62             Paragraph p = data[i];
63             addParagraph(p);
64         }
65     }
66 
addParagraph(Paragraph p)67     void addParagraph(Paragraph p) {
68         try {
69             Style s = null;
70             for (int i = 0; i < p.data.length; i++) {
71                 Run run = p.data[i];
72                 s = runAttr.get(run.attr);
73                 doc.insertString(doc.getLength(), run.content, s);
74             }
75 
76             // set logical style
77             Style ls = styles.getStyle(p.logical);
78             doc.setLogicalStyle(doc.getLength() - 1, ls);
79             doc.insertString(doc.getLength(), "\n", null);
80         } catch (BadLocationException e) {
81             System.err.println("Internal error: " + e);
82         }
83     }
84 
createStyles()85     void createStyles() {
86         // no attributes defined
87         Style s = styles.addStyle(null, null);
88         runAttr.put("none", s);
89         s = styles.addStyle(null, null);
90         StyleConstants.setItalic(s, true);
91         StyleConstants.setForeground(s, new Color(153, 153, 102));
92         runAttr.put("cquote", s); // catepillar quote
93 
94         s = styles.addStyle(null, null);
95         StyleConstants.setItalic(s, true);
96         StyleConstants.setForeground(s, new Color(51, 102, 153));
97         runAttr.put("aquote", s); // alice quote
98 
99         try {
100             ResourceBundle resources = ResourceBundle.getBundle(
101                     "resources.Stylepad",
102                     Locale.getDefault());
103             s = styles.addStyle(null, null);
104             Icon alice =
105                     new ImageIcon(getClass().
106                     getResource(resources.getString("aliceGif")));
107             StyleConstants.setIcon(s, alice);
108             runAttr.put("alice", s); // alice
109 
110             s = styles.addStyle(null, null);
111             Icon caterpillar =
112                     new ImageIcon(getClass().
113                     getResource(resources.getString("caterpillarGif")));
114             StyleConstants.setIcon(s, caterpillar);
115             runAttr.put("caterpillar", s); // caterpillar
116 
117             s = styles.addStyle(null, null);
118             Icon hatter =
119                     new ImageIcon(getClass().
120                     getResource(resources.getString("hatterGif")));
121             StyleConstants.setIcon(s, hatter);
122             runAttr.put("hatter", s); // hatter
123 
124 
125         } catch (MissingResourceException mre) {
126             // can't display image
127         }
128 
129         Style def = styles.getStyle(StyleContext.DEFAULT_STYLE);
130 
131         Style heading = styles.addStyle("heading", def);
132         StyleConstants.setFontFamily(heading, "SansSerif");
133         StyleConstants.setBold(heading, true);
134         StyleConstants.setAlignment(heading, StyleConstants.ALIGN_CENTER);
135         StyleConstants.setSpaceAbove(heading, 10);
136         StyleConstants.setSpaceBelow(heading, 10);
137         StyleConstants.setFontSize(heading, 18);
138 
139         // Title
140         Style sty = styles.addStyle("title", heading);
141         StyleConstants.setFontSize(sty, 32);
142 
143         // edition
144         sty = styles.addStyle("edition", heading);
145         StyleConstants.setFontSize(sty, 16);
146 
147         // author
148         sty = styles.addStyle("author", heading);
149         StyleConstants.setItalic(sty, true);
150         StyleConstants.setSpaceBelow(sty, 25);
151 
152         // subtitle
153         sty = styles.addStyle("subtitle", heading);
154         StyleConstants.setSpaceBelow(sty, 35);
155 
156         // normal
157         sty = styles.addStyle("normal", def);
158         StyleConstants.setLeftIndent(sty, 10);
159         StyleConstants.setRightIndent(sty, 10);
160         StyleConstants.setFontFamily(sty, "SansSerif");
161         StyleConstants.setFontSize(sty, 14);
162         StyleConstants.setSpaceAbove(sty, 4);
163         StyleConstants.setSpaceBelow(sty, 4);
164     }
165     DefaultStyledDocument doc;
166     StyleContext styles;
167     HashMap<String, Style> runAttr;
168 
169 
170     static class Paragraph {
171 
Paragraph(String logical, Run[] data)172         Paragraph(String logical, Run[] data) {
173             this.logical = logical;
174             this.data = data;
175         }
176         String logical;
177         Run[] data;
178     }
179 
180 
181     static class Run {
182 
Run(String attr, String content)183         Run(String attr, String content) {
184             this.attr = attr;
185             this.content = content;
186         }
187         String attr;
188         String content;
189     }
190     Paragraph[] data = new Paragraph[] {
191         new Paragraph("title", new Run[] {
192             new Run("none", "ALICE'S ADVENTURES IN WONDERLAND")
193         }),
194         new Paragraph("author", new Run[] {
195             new Run("none", "Lewis Carroll")
196         }),
197         new Paragraph("heading", new Run[] {
198             new Run("alice", " ")
199         }),
200         new Paragraph("edition", new Run[] {
201             new Run("none", "THE MILLENNIUM FULCRUM EDITION 3.0")
202         }),
203         new Paragraph("heading", new Run[] {
204             new Run("none", "CHAPTER V")
205         }),
206         new Paragraph("subtitle", new Run[] {
207             new Run("none", "Advice from a Caterpillar")
208         }),
209         new Paragraph("normal", new Run[] {
210             new Run("none", " "), }),
211         new Paragraph("normal", new Run[] {
212             new Run("none",
213             "The Caterpillar and Alice looked at each other for some time in "
214                     + "silence:  at last the Caterpillar took the hookah out "
215                     + "of its mouth, and addressed her in a languid, sleepy "
216                     + "voice.")
217         }),
218         new Paragraph("normal", new Run[] {
219             new Run("cquote", "Who are YOU?  "),
220             new Run("none", "said the Caterpillar.")
221         }),
222         new Paragraph("normal",
223         new Run[] {
224             new Run("none",
225             "This was not an encouraging opening for a conversation.  Alice "
226                     + "replied, rather shyly, "),
227             new Run("aquote",
228             "I--I hardly know, sir, just at present--at least I know who I WAS "
229                     + "when I got up this morning, but I think I must have "
230                     + "been changed several times since then. "), }),
231         new Paragraph("heading", new Run[] {
232             new Run("caterpillar", " ")
233         }),
234         new Paragraph("normal", new Run[] {
235             new Run("cquote", "What do you mean by that? "),
236             new Run("none", " said the Caterpillar sternly.  "),
237             new Run("cquote", "Explain yourself!"), }),
238         new Paragraph("normal", new Run[] {
239             new Run("aquote", "I can't explain MYSELF, I'm afraid, sir"),
240             new Run("none", " said Alice, "),
241             new Run("aquote", "because I'm not myself, you see."), }),
242         new Paragraph("normal", new Run[] {
243             new Run("cquote", "I don't see,"),
244             new Run("none", " said the Caterpillar."), }),
245         new Paragraph("normal",
246         new Run[] {
247             new Run("aquote", "I'm afraid I can't put it more clearly,  "),
248             new Run("none", "Alice replied very politely, "),
249             new Run("aquote",
250             "for I can't understand it myself to begin with; and being so many "
251                     + "different sizes in a day is very confusing."), }),
252         new Paragraph("normal", new Run[] {
253             new Run("cquote", "It isn't,  "),
254             new Run("none", "said the Caterpillar.")
255         }),
256         new Paragraph("normal", new Run[] {
257             new Run("aquote", "Well, perhaps you haven't found it so yet,"),
258             new Run("none", " said Alice; "),
259             new Run("aquote",
260             "but when you have to turn into a chrysalis--you will some day, "
261                     + "you know--and then after that into a butterfly, I "
262                     + "should think you'll feel it a little queer, won't you?")
263         }),
264         new Paragraph("normal", new Run[] {
265             new Run("cquote", "Not a bit, "),
266             new Run("none", "said the Caterpillar.")
267         }),
268         new Paragraph("normal",
269         new Run[] {
270             new Run("aquote", "Well, perhaps your feelings may be different,"),
271             new Run("none", " said Alice; "),
272             new Run("aquote", "all I know is, it would feel very queer to ME."),
273         }),
274         new Paragraph("normal", new Run[] {
275             new Run("cquote", "You!"),
276             new Run("none", " said the Caterpillar contemptuously.  "),
277             new Run("cquote", "Who are YOU?"), }),
278         new Paragraph("normal", new Run[] {
279             new Run("normal",
280             "Which brought them back again to the beginning of the "
281                     + "conversation.  Alice felt a little irritated at the "
282                     + "Caterpillar's making such VERY short remarks, and she "
283                     + "drew herself up and said, very gravely, "),
284             new Run("aquote",
285             "I think, you ought to tell me who YOU are, first."), }),
286         new Paragraph("normal", new Run[] {
287             new Run("cquote", "Why?  "),
288             new Run("none", "said the Caterpillar."), }),
289         new Paragraph("heading", new Run[] {
290             new Run("hatter", " ")
291         }),
292         new Paragraph("normal", new Run[] {
293             new Run("none", " "), }),
294         new Paragraph("normal", new Run[] {
295             new Run("none", " "), }),
296         new Paragraph("normal", new Run[] {
297             new Run("none", " "), })
298     };
299 }
300