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