1 36 37 40 41 42 43 import java.util.Locale ; 44 import java.util.MissingResourceException ; 45 import java.util.ResourceBundle ; 46 47 import java.net.URL ; 48 import java.util.Hashtable ; 49 import java.awt.Color ; 50 import javax.swing.*; 51 import javax.swing.text.*; 52 53 56 public class Wonderland { 57 58 Wonderland(DefaultStyledDocument doc, StyleContext styles) { 59 this.doc = doc; 60 this.styles = styles; 61 runAttr = new Hashtable (); 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 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 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 (153,153,102)); 97 runAttr.put("cquote", s); 99 s = styles.addStyle(null, null); 100 StyleConstants.setItalic(s, true); 101 StyleConstants.setForeground(s, new Color (51,102,153)); 102 runAttr.put("aquote", s); 104 try { 105 ResourceBundle 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); 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); 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); 128 129 } catch (MissingResourceException mre) { 130 } 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 Style sty = styles.addStyle("title", heading); 145 StyleConstants.setFontSize(sty, 32); 146 147 sty = styles.addStyle("edition", heading); 149 StyleConstants.setFontSize(sty, 16); 150 151 sty = styles.addStyle("author", heading); 153 StyleConstants.setItalic(sty, true); 154 StyleConstants.setSpaceBelow(sty, 25); 155 156 sty = styles.addStyle("subtitle", heading); 158 StyleConstants.setSpaceBelow(sty, 35); 159 160 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 runAttr; 173 174 static class Paragraph { 175 Paragraph(String logical, Run[] data) { 176 this.logical = logical; 177 this.data = data; 178 } 179 String logical; 180 Run[] data; 181 } 182 183 static class Run { 184 Run(String attr, String content) { 185 this.attr = attr; 186 this.content = content; 187 } 188 String attr; 189 String 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 |