Path: blob/master/src/demo/share/jfc/Stylepad/HelloWorld.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 content.49*/50public class HelloWorld {5152HelloWorld(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 = new ImageIcon(resources.getString("aliceGif"));104StyleConstants.setIcon(s, alice);105runAttr.put("alice", s); // alice106107s = styles.addStyle(null, null);108Icon caterpillar = new ImageIcon(resources.getString(109"caterpillarGif"));110StyleConstants.setIcon(s, caterpillar);111runAttr.put("caterpillar", s); // caterpillar112113s = styles.addStyle(null, null);114Icon hatter = new ImageIcon(resources.getString("hatterGif"));115StyleConstants.setIcon(s, hatter);116runAttr.put("hatter", s); // hatter117118119} catch (MissingResourceException mre) {120// can't display image121}122123Style def = styles.getStyle(StyleContext.DEFAULT_STYLE);124125Style heading = styles.addStyle("heading", def);126//StyleConstants.setFontFamily(heading, "SansSerif");127StyleConstants.setBold(heading, true);128StyleConstants.setAlignment(heading, StyleConstants.ALIGN_CENTER);129StyleConstants.setSpaceAbove(heading, 10);130StyleConstants.setSpaceBelow(heading, 10);131StyleConstants.setFontSize(heading, 18);132133// Title134Style sty = styles.addStyle("title", heading);135StyleConstants.setFontSize(sty, 32);136137// edition138sty = styles.addStyle("edition", heading);139StyleConstants.setFontSize(sty, 16);140141// author142sty = styles.addStyle("author", heading);143StyleConstants.setItalic(sty, true);144StyleConstants.setSpaceBelow(sty, 25);145146// subtitle147sty = styles.addStyle("subtitle", heading);148StyleConstants.setSpaceBelow(sty, 35);149150// normal151sty = styles.addStyle("normal", def);152StyleConstants.setLeftIndent(sty, 10);153StyleConstants.setRightIndent(sty, 10);154//StyleConstants.setFontFamily(sty, "SansSerif");155StyleConstants.setFontSize(sty, 14);156StyleConstants.setSpaceAbove(sty, 4);157StyleConstants.setSpaceBelow(sty, 4);158}159DefaultStyledDocument doc;160StyleContext styles;161HashMap<String, Style> runAttr;162163164static class Paragraph {165166Paragraph(String logical, Run[] data) {167this.logical = logical;168this.data = data;169}170String logical;171Run[] data;172}173174175static class Run {176177Run(String attr, String content) {178this.attr = attr;179this.content = content;180}181String attr;182String content;183}184Paragraph[] data = new Paragraph[] {185new Paragraph("title", new Run[] {186new Run("none", "Hello from Cupertino")187}),188new Paragraph("title", new Run[] {189new Run("none", "\u53F0\u5317\u554F\u5019\u60A8\u0021")190}),191new Paragraph("title", new Run[] {192new Run("none", "\u0391\u03B8\u03B7\u03BD\u03B1\u03B9\u0020" // Greek193+ "\u03B1\u03C3\u03C0\u03B1\u03B6\u03BF\u03BD"194+ "\u03C4\u03B1\u03B9\u0020\u03C5\u03BC\u03B1"195+ "\u03C2\u0021")196}),197new Paragraph("title", new Run[] {198new Run("none", "\u6771\u4eac\u304b\u3089\u4eca\u65e5\u306f")199}),200new Paragraph("title", new Run[] {201new Run("none", "\u05e9\u05dc\u05d5\u05dd \u05de\u05d9\u05e8\u05d5"202+ "\u05e9\u05dc\u05d9\u05dd")203}),204new Paragraph("title", new Run[] {205new Run("none", "\u0633\u0644\u0627\u0645")206}), };207}208209210