KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)HRuleView.java 1.32 03/12/19
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.event.DocumentEvent JavaDoc;
11 import javax.swing.text.*;
12 import java.util.Enumeration JavaDoc;
13 import java.lang.Integer JavaDoc;
14
15 /**
16  * A view implementation to display an html horizontal
17  * rule.
18  *
19  * @author Timothy Prinzing
20  * @author Sara Swanson
21  * @version 1.32 12/19/03
22  */

23 class HRuleView extends View {
24
25     /**
26      * Creates a new view that represents an <hr> element.
27      *
28      * @param elem the element to create a view for
29      */

30     public HRuleView(Element elem) {
31     super(elem);
32     setPropertiesFromAttributes();
33     }
34
35     /**
36      * Update any cached values that come from attributes.
37      */

38     protected void setPropertiesFromAttributes() {
39     StyleSheet JavaDoc sheet = ((HTMLDocument JavaDoc)getDocument()).getStyleSheet();
40     AttributeSet eAttr = getElement().getAttributes();
41     attr = sheet.getViewAttributes(this);
42
43     alignment = StyleConstants.ALIGN_CENTER;
44     size = 0;
45     noshade = null;
46     widthValue = null;
47
48     if (attr != null) {
49             // getAlignment() returns ALIGN_LEFT by default, and HR should
50
// use ALIGN_CENTER by default, so we check if the alignment
51
// attribute is actually defined
52
if (attr.getAttribute(StyleConstants.Alignment) != null) {
53             alignment = StyleConstants.getAlignment(attr);
54             }
55
56         noshade = (String JavaDoc)eAttr.getAttribute(HTML.Attribute.NOSHADE);
57         Object JavaDoc value = eAttr.getAttribute(HTML.Attribute.SIZE);
58         if (value != null && (value instanceof String JavaDoc))
59         size = Integer.parseInt((String JavaDoc)value);
60         value = attr.getAttribute(CSS.Attribute.WIDTH);
61         if (value != null && (value instanceof CSS.LengthValue JavaDoc)) {
62         widthValue = (CSS.LengthValue JavaDoc)value;
63         }
64         topMargin = getLength(CSS.Attribute.MARGIN_TOP, attr);
65         bottomMargin = getLength(CSS.Attribute.MARGIN_BOTTOM, attr);
66         leftMargin = getLength(CSS.Attribute.MARGIN_LEFT, attr);
67         rightMargin = getLength(CSS.Attribute.MARGIN_RIGHT, attr);
68     }
69     else {
70         topMargin = bottomMargin = leftMargin = rightMargin = 0;
71     }
72         size = Math.max(2, size);
73     }
74
75     // This will be removed and centralized at some point, need to unify this
76
// and avoid private classes.
77
private float getLength(CSS.Attribute JavaDoc key, AttributeSet a) {
78     CSS.LengthValue JavaDoc lv = (CSS.LengthValue JavaDoc) a.getAttribute(key);
79     float len = (lv != null) ? lv.getValue() : 0;
80     return len;
81     }
82
83     // --- View methods ---------------------------------------------
84

85     /**
86      * Paints the view.
87      *
88      * @param g the graphics context
89      * @param a the allocation region for the view
90      * @see View#paint
91      */

92     public void paint(Graphics g, Shape a) {
93     Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a :
94                           a.getBounds();
95     int x = 0;
96     int y = alloc.y + SPACE_ABOVE + (int)topMargin;
97     int width = alloc.width - (int)(leftMargin + rightMargin);
98     if (widthValue != null) {
99         width = (int)widthValue.getValue((float)width);
100     }
101     int height = alloc.height - (SPACE_ABOVE + SPACE_BELOW +
102                      (int)topMargin + (int)bottomMargin);
103     if (size > 0)
104         height = size;
105
106     // Align the rule horizontally.
107
switch (alignment) {
108         case StyleConstants.ALIGN_CENTER:
109             x = alloc.x + (alloc.width / 2) - (width / 2);
110         break;
111         case StyleConstants.ALIGN_RIGHT:
112             x = alloc.x + alloc.width - width - (int)rightMargin;
113         break;
114         case StyleConstants.ALIGN_LEFT:
115         default:
116             x = alloc.x + (int)leftMargin;
117         break;
118         }
119
120     // Paint either a shaded rule or a solid line.
121
if (noshade != null) {
122             g.setColor(Color.black);
123         g.fillRect(x, y, width, height);
124         }
125     else {
126             Color bg = getContainer().getBackground();
127             Color bottom, top;
128             if (bg == null || bg.equals(Color.white)) {
129                 top = Color.darkGray;
130                 bottom = Color.lightGray;
131             }
132             else {
133                 top = Color.darkGray;
134                 bottom = Color.white;
135             }
136             g.setColor(bottom);
137             g.drawLine(x + width - 1, y, x + width - 1, y + height - 1);
138             g.drawLine(x, y + height - 1, x + width - 1, y + height - 1);
139             g.setColor(top);
140             g.drawLine(x, y, x + width - 1, y);
141             g.drawLine(x, y, x, y + height - 1);
142         }
143
144     }
145
146
147     /**
148      * Calculates the desired shape of the rule... this is
149      * basically the preferred size of the border.
150      *
151      * @param axis may be either X_AXIS or Y_AXIS
152      * @return the desired span
153      * @see View#getPreferredSpan
154      */

155     public float getPreferredSpan(int axis) {
156     switch (axis) {
157     case View.X_AXIS:
158         return 1;
159     case View.Y_AXIS:
160         if (size > 0) {
161             return size + SPACE_ABOVE + SPACE_BELOW + topMargin +
162             bottomMargin;
163         } else {
164         if (noshade != null) {
165             return 2 + SPACE_ABOVE + SPACE_BELOW + topMargin +
166             bottomMargin;
167         } else {
168             return SPACE_ABOVE + SPACE_BELOW + topMargin +bottomMargin;
169         }
170         }
171     default:
172         throw new IllegalArgumentException JavaDoc("Invalid axis: " + axis);
173     }
174     }
175
176     /**
177      * Gets the resize weight for the axis.
178      * The rule is: rigid vertically and flexible horizontally.
179      *
180      * @param axis may be either X_AXIS or Y_AXIS
181      * @return the weight
182      */

183     public int getResizeWeight(int axis) {
184     if (axis == View.X_AXIS) {
185         return 1;
186     } else if (axis == View.Y_AXIS) {
187         return 0;
188     } else {
189         return 0;
190     }
191     }
192
193     /**
194      * Determines how attractive a break opportunity in
195      * this view is. This is implemented to request a forced break.
196      *
197      * @param axis may be either View.X_AXIS or View.Y_AXIS
198      * @param pos the potential location of the start of the
199      * broken view (greater than or equal to zero).
200      * This may be useful for calculating tab
201      * positions.
202      * @param len specifies the relative length from <em>pos</em>
203      * where a potential break is desired. The value must be greater
204      * than or equal to zero.
205      * @return the weight, which should be a value between
206      * ForcedBreakWeight and BadBreakWeight.
207      */

208     public int getBreakWeight(int axis, float pos, float len) {
209     if (axis == X_AXIS) {
210         return ForcedBreakWeight;
211     }
212     return BadBreakWeight;
213     }
214
215     public View breakView(int axis, int offset, float pos, float len) {
216     return null;
217     }
218
219     /**
220      * Provides a mapping from the document model coordinate space
221      * to the coordinate space of the view mapped to it.
222      *
223      * @param pos the position to convert
224      * @param a the allocated region to render into
225      * @return the bounding box of the given position
226      * @exception BadLocationException if the given position does not
227      * represent a valid location in the associated document
228      * @see View#modelToView
229      */

230     public Shape modelToView(int pos, Shape a, Position.Bias b) throws BadLocationException {
231     int p0 = getStartOffset();
232     int p1 = getEndOffset();
233     if ((pos >= p0) && (pos <= p1)) {
234         Rectangle r = a.getBounds();
235         if (pos == p1) {
236         r.x += r.width;
237         }
238         r.width = 0;
239         return r;
240     }
241     return null;
242     }
243
244     /**
245      * Provides a mapping from the view coordinate space to the logical
246      * coordinate space of the model.
247      *
248      * @param x the X coordinate
249      * @param y the Y coordinate
250      * @param a the allocated region to render into
251      * @return the location within the model that best represents the
252      * given point of view
253      * @see View#viewToModel
254      */

255     public int viewToModel(float x, float y, Shape a, Position.Bias[] bias) {
256     Rectangle alloc = (Rectangle) a;
257     if (x < alloc.x + (alloc.width / 2)) {
258         bias[0] = Position.Bias.Forward;
259         return getStartOffset();
260     }
261     bias[0] = Position.Bias.Backward;
262     return getEndOffset();
263     }
264
265     /**
266      * Fetches the attributes to use when rendering. This is
267      * implemented to multiplex the attributes specified in the
268      * model with a StyleSheet.
269      */

270     public AttributeSet getAttributes() {
271     return attr;
272     }
273
274     public void changedUpdate(DocumentEvent JavaDoc changes, Shape a, ViewFactory f) {
275     super.changedUpdate(changes, a, f);
276     int pos = changes.getOffset();
277     if (pos <= getStartOffset() && (pos + changes.getLength()) >=
278         getEndOffset()) {
279         setPropertiesFromAttributes();
280     }
281     }
282
283     // --- variables ------------------------------------------------
284

285     private float topMargin;
286     private float bottomMargin;
287     private float leftMargin;
288     private float rightMargin;
289     private int alignment = StyleConstants.ALIGN_CENTER;
290     private String JavaDoc noshade = null;
291     private int size = 0;
292     private CSS.LengthValue JavaDoc widthValue;
293
294     private static final int SPACE_ABOVE = 3;
295     private static final int SPACE_BELOW = 3;
296
297     /** View Attributes. */
298     private AttributeSet attr;
299 }
300
301
Popular Tags