KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > swing > text > html > ParagraphView


1 /*
2  * @(#)ParagraphView.java 1.29 04/09/14
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package javax.swing.text.html;
8
9 import java.awt.*;
10 import javax.swing.SizeRequirements JavaDoc;
11 import javax.swing.event.DocumentEvent JavaDoc;
12 import javax.swing.text.Document JavaDoc;
13 import javax.swing.text.Element JavaDoc;
14 import javax.swing.text.AttributeSet JavaDoc;
15 import javax.swing.text.StyleConstants JavaDoc;
16 import javax.swing.text.View JavaDoc;
17 import javax.swing.text.ViewFactory JavaDoc;
18 import javax.swing.text.BadLocationException JavaDoc;
19 import javax.swing.text.JTextComponent JavaDoc;
20
21 /**
22  * Displays the a paragraph, and uses css attributes for its
23  * configuration.
24  *
25  * @author Timothy Prinzing
26  * @version 1.29 09/14/04
27  */

28
29 public class ParagraphView extends javax.swing.text.ParagraphView JavaDoc {
30
31     /**
32      * Constructs a ParagraphView for the given element.
33      *
34      * @param elem the element that this view is responsible for
35      */

36     public ParagraphView(Element JavaDoc elem) {
37     super(elem);
38     }
39
40     /**
41      * Establishes the parent view for this view. This is
42      * guaranteed to be called before any other methods if the
43      * parent view is functioning properly.
44      * <p>
45      * This is implemented
46      * to forward to the superclass as well as call the
47      * <a HREF="#setPropertiesFromAttributes">setPropertiesFromAttributes</a>
48      * method to set the paragraph properties from the css
49      * attributes. The call is made at this time to ensure
50      * the ability to resolve upward through the parents
51      * view attributes.
52      *
53      * @param parent the new parent, or null if the view is
54      * being removed from a parent it was previously added
55      * to
56      */

57     public void setParent(View JavaDoc parent) {
58     super.setParent(parent);
59         if (parent != null) {
60         setPropertiesFromAttributes();
61         }
62     }
63
64     /**
65      * Fetches the attributes to use when rendering. This is
66      * implemented to multiplex the attributes specified in the
67      * model with a StyleSheet.
68      */

69     public AttributeSet JavaDoc getAttributes() {
70     if (attr == null) {
71         StyleSheet JavaDoc sheet = getStyleSheet();
72         attr = sheet.getViewAttributes(this);
73     }
74     return attr;
75     }
76
77     /**
78      * Sets up the paragraph from css attributes instead of
79      * the values found in StyleConstants (i.e. which are used
80      * by the superclass). Since
81      */

82     protected void setPropertiesFromAttributes() {
83     StyleSheet JavaDoc sheet = getStyleSheet();
84     attr = sheet.getViewAttributes(this);
85     painter = sheet.getBoxPainter(attr);
86     if (attr != null) {
87         super.setPropertiesFromAttributes();
88         setInsets((short) painter.getInset(TOP, this),
89               (short) painter.getInset(LEFT, this),
90               (short) painter.getInset(BOTTOM, this),
91               (short) painter.getInset(RIGHT, this));
92         Object JavaDoc o = attr.getAttribute(CSS.Attribute.TEXT_ALIGN);
93         if (o != null) {
94         // set horizontal alignment
95
String JavaDoc ta = o.toString();
96         if (ta.equals("left")) {
97             setJustification(StyleConstants.ALIGN_LEFT);
98         } else if (ta.equals("center")) {
99             setJustification(StyleConstants.ALIGN_CENTER);
100         } else if (ta.equals("right")) {
101             setJustification(StyleConstants.ALIGN_RIGHT);
102         } else if (ta.equals("justify")) {
103             setJustification(StyleConstants.ALIGN_JUSTIFIED);
104         }
105         }
106             // Get the width/height
107
cssWidth = (CSS.LengthValue JavaDoc)attr.getAttribute(
108                                         CSS.Attribute.WIDTH);
109             cssHeight = (CSS.LengthValue JavaDoc)attr.getAttribute(
110                                          CSS.Attribute.HEIGHT);
111     }
112     }
113
114     protected StyleSheet JavaDoc getStyleSheet() {
115     HTMLDocument JavaDoc doc = (HTMLDocument JavaDoc) getDocument();
116     return doc.getStyleSheet();
117     }
118
119
120     /**
121      * Calculate the needs for the paragraph along the minor axis.
122      * This implemented to use the requirements of the superclass,
123      * modified slightly to set a minimum span allowed. Typical
124      * html rendering doesn't let the view size shrink smaller than
125      * the length of the longest word.
126      */

127     protected SizeRequirements JavaDoc calculateMinorAxisRequirements(int axis, SizeRequirements JavaDoc r) {
128     r = super.calculateMinorAxisRequirements(axis, r);
129
130         if (!BlockView.spanSetFromAttributes(axis, r, cssWidth, cssHeight)) {
131             // PENDING(prinz) Need to make this better so it doesn't require
132
// InlineView and works with font changes within the word.
133

134             // find the longest minimum span.
135
float min = 0;
136             int n = getLayoutViewCount();
137             for (int i = 0; i < n; i++) {
138                 View JavaDoc v = getLayoutView(i);
139                 if (v instanceof InlineView JavaDoc) {
140                     float wordSpan = ((InlineView JavaDoc) v).getLongestWordSpan();
141                     min = Math.max(wordSpan, min);
142                 } else {
143                     min = Math.max(v.getMinimumSpan(axis), min);
144                 }
145             }
146             r.minimum = Math.max(r.minimum, (int) min);
147             r.preferred = Math.max(r.minimum, r.preferred);
148             r.maximum = Math.max(r.preferred, r.maximum);
149         }
150         else {
151             // Offset by the margins so that pref/min/max return the
152
// right value.
153
int margin = (axis == X_AXIS) ? getLeftInset() + getRightInset() :
154                                             getTopInset() + getBottomInset();
155             r.minimum -= margin;
156             r.preferred -= margin;
157             r.maximum -= margin;
158         }
159     return r;
160     }
161
162
163     /**
164      * Indicates whether or not this view should be
165      * displayed. If none of the children wish to be
166      * displayed and the only visible child is the
167      * break that ends the paragraph, the paragraph
168      * will not be considered visible. Otherwise,
169      * it will be considered visible and return true.
170      *
171      * @return true if the paragraph should be displayed
172      */

173     public boolean isVisible() {
174     
175     int n = getLayoutViewCount() - 1;
176     for (int i = 0; i < n; i++) {
177         View JavaDoc v = getLayoutView(i);
178         if (v.isVisible()) {
179         return true;
180         }
181     }
182     if (n > 0) {
183         View JavaDoc v = getLayoutView(n);
184         if ((v.getEndOffset() - v.getStartOffset()) == 1) {
185         return false;
186         }
187     }
188     // If it's the last paragraph and not editable, it shouldn't
189
// be visible.
190
if (getStartOffset() == getDocument().getLength()) {
191         boolean editable = false;
192         Component JavaDoc c = getContainer();
193         if (c instanceof JTextComponent JavaDoc) {
194         editable = ((JTextComponent JavaDoc)c).isEditable();
195         }
196         if (!editable) {
197         return false;
198         }
199     }
200     return true;
201     }
202
203     /**
204      * Renders using the given rendering surface and area on that
205      * surface. This is implemented to delgate to the superclass
206      * after stashing the base coordinate for tab calculations.
207      *
208      * @param g the rendering surface to use
209      * @param a the allocated region to render into
210      * @see View#paint
211      */

212     public void paint(Graphics g, Shape a) {
213         if (a == null) {
214             return;
215         }
216
217     Rectangle r;
218     if (a instanceof Rectangle) {
219         r = (Rectangle) a;
220     } else {
221         r = a.getBounds();
222     }
223     painter.paint(g, r.x, r.y, r.width, r.height, this);
224         super.paint(g, a);
225     }
226
227     /**
228      * Determines the preferred span for this view. Returns
229      * 0 if the view is not visible, otherwise it calls the
230      * superclass method to get the preferred span.
231      * axis.
232      *
233      * @param axis may be either View.X_AXIS or View.Y_AXIS
234      * @return the span the view would like to be rendered into;
235      * typically the view is told to render into the span
236      * that is returned, although there is no guarantee;
237      * the parent may choose to resize or break the view
238      * @see javax.swing.text.ParagraphView#getPreferredSpan
239      */

240     public float getPreferredSpan(int axis) {
241     if (!isVisible()) {
242         return 0;
243     }
244     return super.getPreferredSpan(axis);
245     }
246
247     /**
248      * Determines the minimum span for this view along an
249      * axis. Returns 0 if the view is not visible, otherwise
250      * it calls the superclass method to get the minimum span.
251      *
252      * @param axis may be either <code>View.X_AXIS</code> or
253      * <code>View.Y_AXIS</code>
254      * @return the minimum span the view can be rendered into
255      * @see javax.swing.text.ParagraphView#getMinimumSpan
256      */

257     public float getMinimumSpan(int axis) {
258     if (!isVisible()) {
259         return 0;
260     }
261     return super.getMinimumSpan(axis);
262     }
263
264     /**
265      * Determines the maximum span for this view along an
266      * axis. Returns 0 if the view is not visible, otherwise
267      * it calls the superclass method ot get the maximum span.
268      *
269      * @param axis may be either <code>View.X_AXIS</code> or
270      * <code>View.Y_AXIS</code>
271      * @return the maximum span the view can be rendered into
272      * @see javax.swing.text.ParagraphView#getMaximumSpan
273      */

274     public float getMaximumSpan(int axis) {
275     if (!isVisible()) {
276         return 0;
277     }
278     return super.getMaximumSpan(axis);
279     }
280
281     private AttributeSet JavaDoc attr;
282     private StyleSheet.BoxPainter JavaDoc painter;
283     private CSS.LengthValue JavaDoc cssWidth;
284     private CSS.LengthValue JavaDoc cssHeight;
285 }
286
287
Popular Tags