50830
|
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 HelloWorld {
|
|
52 |
|
|
53 |
HelloWorld(DefaultStyledDocument doc, StyleContext styles) {
|
|
54 |
this.doc = doc;
|
|
55 |
this.styles = styles;
|
|
56 |
runAttr = new HashMap<String, Style>();
|
|
57 |
}
|
|
58 |
|
|
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 |
|
|
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 |
|
|
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 = new ImageIcon(resources.getString("aliceGif"));
|
|
105 |
StyleConstants.setIcon(s, alice);
|
|
106 |
runAttr.put("alice", s); // alice
|
|
107 |
|
|
108 |
s = styles.addStyle(null, null);
|
|
109 |
Icon caterpillar = new ImageIcon(resources.getString(
|
|
110 |
"caterpillarGif"));
|
|
111 |
StyleConstants.setIcon(s, caterpillar);
|
|
112 |
runAttr.put("caterpillar", s); // caterpillar
|
|
113 |
|
|
114 |
s = styles.addStyle(null, null);
|
|
115 |
Icon hatter = new ImageIcon(resources.getString("hatterGif"));
|
|
116 |
StyleConstants.setIcon(s, hatter);
|
|
117 |
runAttr.put("hatter", s); // hatter
|
|
118 |
|
|
119 |
|
|
120 |
} catch (MissingResourceException mre) {
|
|
121 |
// can't display image
|
|
122 |
}
|
|
123 |
|
|
124 |
Style def = styles.getStyle(StyleContext.DEFAULT_STYLE);
|
|
125 |
|
|
126 |
Style heading = styles.addStyle("heading", def);
|
|
127 |
//StyleConstants.setFontFamily(heading, "SansSerif");
|
|
128 |
StyleConstants.setBold(heading, true);
|
|
129 |
StyleConstants.setAlignment(heading, StyleConstants.ALIGN_CENTER);
|
|
130 |
StyleConstants.setSpaceAbove(heading, 10);
|
|
131 |
StyleConstants.setSpaceBelow(heading, 10);
|
|
132 |
StyleConstants.setFontSize(heading, 18);
|
|
133 |
|
|
134 |
// Title
|
|
135 |
Style sty = styles.addStyle("title", heading);
|
|
136 |
StyleConstants.setFontSize(sty, 32);
|
|
137 |
|
|
138 |
// edition
|
|
139 |
sty = styles.addStyle("edition", heading);
|
|
140 |
StyleConstants.setFontSize(sty, 16);
|
|
141 |
|
|
142 |
// author
|
|
143 |
sty = styles.addStyle("author", heading);
|
|
144 |
StyleConstants.setItalic(sty, true);
|
|
145 |
StyleConstants.setSpaceBelow(sty, 25);
|
|
146 |
|
|
147 |
// subtitle
|
|
148 |
sty = styles.addStyle("subtitle", heading);
|
|
149 |
StyleConstants.setSpaceBelow(sty, 35);
|
|
150 |
|
|
151 |
// normal
|
|
152 |
sty = styles.addStyle("normal", def);
|
|
153 |
StyleConstants.setLeftIndent(sty, 10);
|
|
154 |
StyleConstants.setRightIndent(sty, 10);
|
|
155 |
//StyleConstants.setFontFamily(sty, "SansSerif");
|
|
156 |
StyleConstants.setFontSize(sty, 14);
|
|
157 |
StyleConstants.setSpaceAbove(sty, 4);
|
|
158 |
StyleConstants.setSpaceBelow(sty, 4);
|
|
159 |
}
|
|
160 |
DefaultStyledDocument doc;
|
|
161 |
StyleContext styles;
|
|
162 |
HashMap<String, Style> runAttr;
|
|
163 |
|
|
164 |
|
|
165 |
static class Paragraph {
|
|
166 |
|
|
167 |
Paragraph(String logical, Run[] data) {
|
|
168 |
this.logical = logical;
|
|
169 |
this.data = data;
|
|
170 |
}
|
|
171 |
String logical;
|
|
172 |
Run[] data;
|
|
173 |
}
|
|
174 |
|
|
175 |
|
|
176 |
static class Run {
|
|
177 |
|
|
178 |
Run(String attr, String content) {
|
|
179 |
this.attr = attr;
|
|
180 |
this.content = content;
|
|
181 |
}
|
|
182 |
String attr;
|
|
183 |
String content;
|
|
184 |
}
|
|
185 |
Paragraph[] data = new Paragraph[] {
|
|
186 |
new Paragraph("title", new Run[] {
|
|
187 |
new Run("none", "Hello from Cupertino")
|
|
188 |
}),
|
|
189 |
new Paragraph("title", new Run[] {
|
|
190 |
new Run("none", "\u53F0\u5317\u554F\u5019\u60A8\u0021")
|
|
191 |
}),
|
|
192 |
new Paragraph("title", new Run[] {
|
|
193 |
new Run("none", "\u0391\u03B8\u03B7\u03BD\u03B1\u03B9\u0020" // Greek
|
|
194 |
+ "\u03B1\u03C3\u03C0\u03B1\u03B6\u03BF\u03BD"
|
|
195 |
+ "\u03C4\u03B1\u03B9\u0020\u03C5\u03BC\u03B1"
|
|
196 |
+ "\u03C2\u0021")
|
|
197 |
}),
|
|
198 |
new Paragraph("title", new Run[] {
|
|
199 |
new Run("none", "\u6771\u4eac\u304b\u3089\u4eca\u65e5\u306f")
|
|
200 |
}),
|
|
201 |
new Paragraph("title", new Run[] {
|
|
202 |
new Run("none", "\u05e9\u05dc\u05d5\u05dd \u05de\u05d9\u05e8\u05d5"
|
|
203 |
+ "\u05e9\u05dc\u05d9\u05dd")
|
|
204 |
}),
|
|
205 |
new Paragraph("title", new Run[] {
|
|
206 |
new Run("none", "\u0633\u0644\u0627\u0645")
|
|
207 |
}), };
|
|
208 |
}
|