1 /*
2 * Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25 package javax.swing.text.html;
26
27 import java.awt;
28 import java.text.BreakIterator;
29 import javax.swing.event.DocumentEvent;
30 import javax.swing.text;
31
32 /**
33 * Displays the <dfn>inline element</dfn> styles
34 * based upon css attributes.
35 *
36 * @author Timothy Prinzing
37 */
38 public class InlineView extends LabelView {
39
40 /**
41 * Constructs a new view wrapped on an element.
42 *
43 * @param elem the element
44 */
45 public InlineView(Element elem) {
46 super(elem);
47 StyleSheet sheet = getStyleSheet();
48 attr = sheet.getViewAttributes(this);
49 }
50
51 /**
52 * Gives notification that something was inserted into
53 * the document in a location that this view is responsible for.
54 * If either parameter is <code>null</code>, behavior of this method is
55 * implementation dependent.
56 *
57 * @param e the change information from the associated document
58 * @param a the current allocation of the view
59 * @param f the factory to use to rebuild if the view has children
60 * @since 1.5
61 * @see View#insertUpdate
62 */
63 public void insertUpdate(DocumentEvent e, Shape a, ViewFactory f) {
64 super.insertUpdate(e, a, f);
65 }
66
67 /**
68 * Gives notification that something was removed from the document
69 * in a location that this view is responsible for.
70 * If either parameter is <code>null</code>, behavior of this method is
71 * implementation dependent.
72 *
73 * @param e the change information from the associated document
74 * @param a the current allocation of the view
75 * @param f the factory to use to rebuild if the view has children
76 * @since 1.5
77 * @see View#removeUpdate
78 */
79 public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f) {
80 super.removeUpdate(e, a, f);
81 }
82
83 /**
84 * Gives notification from the document that attributes were changed
85 * in a location that this view is responsible for.
86 *
87 * @param e the change information from the associated document
88 * @param a the current allocation of the view
89 * @param f the factory to use to rebuild if the view has children
90 * @see View#changedUpdate
91 */
92 public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
93 super.changedUpdate(e, a, f);
94 StyleSheet sheet = getStyleSheet();
95 attr = sheet.getViewAttributes(this);
96 preferenceChanged(null, true, true);
97 }
98
99 /**
100 * Fetches the attributes to use when rendering. This is
101 * implemented to multiplex the attributes specified in the
102 * model with a StyleSheet.
103 */
104 public AttributeSet getAttributes() {
105 return attr;
106 }
107
108 /**
109 * Determines how attractive a break opportunity in
110 * this view is. This can be used for determining which
111 * view is the most attractive to call <code>breakView</code>
112 * on in the process of formatting. A view that represents
113 * text that has whitespace in it might be more attractive
114 * than a view that has no whitespace, for example. The
115 * higher the weight, the more attractive the break. A
116 * value equal to or lower than <code>BadBreakWeight</code>
117 * should not be considered for a break. A value greater
118 * than or equal to <code>ForcedBreakWeight</code> should
119 * be broken.
120 * <p>
121 * This is implemented to provide the default behavior
122 * of returning <code>BadBreakWeight</code> unless the length
123 * is greater than the length of the view in which case the
124 * entire view represents the fragment. Unless a view has
125 * been written to support breaking behavior, it is not
126 * attractive to try and break the view. An example of
127 * a view that does support breaking is <code>LabelView</code>.
128 * An example of a view that uses break weight is
129 * <code>ParagraphView</code>.
130 *
131 * @param axis may be either View.X_AXIS or View.Y_AXIS
132 * @param pos the potential location of the start of the
133 * broken view >= 0. This may be useful for calculating tab
134 * positions.
135 * @param len specifies the relative length from <em>pos</em>
136 * where a potential break is desired >= 0.
137 * @return the weight, which should be a value between
138 * ForcedBreakWeight and BadBreakWeight.
139 * @see LabelView
140 * @see ParagraphView
141 * @see javax.swing.text.View#BadBreakWeight
142 * @see javax.swing.text.View#GoodBreakWeight
143 * @see javax.swing.text.View#ExcellentBreakWeight
144 * @see javax.swing.text.View#ForcedBreakWeight
145 */
146 public int getBreakWeight(int axis, float pos, float len) {
147 if (nowrap) {
148 return BadBreakWeight;
149 }
150 return super.getBreakWeight(axis, pos, len);
151 }
152
153 /**
154 * Tries to break this view on the given axis. Refer to
155 * {@link javax.swing.text.View#breakView} for a complete
156 * description of this method.
157 * <p>Behavior of this method is unspecified in case <code>axis</code>
158 * is neither <code>View.X_AXIS</code> nor <code>View.Y_AXIS</code>, and
159 * in case <code>offset</code>, <code>pos</code>, or <code>len</code>
160 * is null.
161 *
162 * @param axis may be either <code>View.X_AXIS</code> or
163 * <code>View.Y_AXIS</code>
164 * @param offset the location in the document model
165 * that a broken fragment would occupy >= 0. This
166 * would be the starting offset of the fragment
167 * returned
168 * @param pos the position along the axis that the
169 * broken view would occupy >= 0. This may be useful for
170 * things like tab calculations
171 * @param len specifies the distance along the axis
172 * where a potential break is desired >= 0
173 * @return the fragment of the view that represents the
174 * given span.
175 * @since 1.5
176 * @see javax.swing.text.View#breakView
177 */
178 public View breakView(int axis, int offset, float pos, float len) {
179 return super.breakView(axis, offset, pos, len);
180 }
181
182
183 /**
184 * Set the cached properties from the attributes.
185 */
186 protected void setPropertiesFromAttributes() {
187 super.setPropertiesFromAttributes();
188 AttributeSet a = getAttributes();
189 Object decor = a.getAttribute(CSS.Attribute.TEXT_DECORATION);
190 boolean u = (decor != null) ?
191 (decor.toString().indexOf("underline") >= 0) : false;
192 setUnderline(u);
193 boolean s = (decor != null) ?
194 (decor.toString().indexOf("line-through") >= 0) : false;
195 setStrikeThrough(s);
196 Object vAlign = a.getAttribute(CSS.Attribute.VERTICAL_ALIGN);
197 s = (vAlign != null) ? (vAlign.toString().indexOf("sup") >= 0) : false;
198 setSuperscript(s);
199 s = (vAlign != null) ? (vAlign.toString().indexOf("sub") >= 0) : false;
200 setSubscript(s);
201
202 Object whitespace = a.getAttribute(CSS.Attribute.WHITE_SPACE);
203 if ((whitespace != null) && whitespace.equals("nowrap")) {
204 nowrap = true;
205 } else {
206 nowrap = false;
207 }
208
209 HTMLDocument doc = (HTMLDocument)getDocument();
210 // fetches background color from stylesheet if specified
211 Color bg = doc.getBackground(a);
212 if (bg != null) {
213 setBackground(bg);
214 }
215 }
216
217
218 protected StyleSheet getStyleSheet() {
219 HTMLDocument doc = (HTMLDocument) getDocument();
220 return doc.getStyleSheet();
221 }
222
223 private boolean nowrap;
224 private AttributeSet attr;
225 }