KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)LineView.java 1.18 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.util.Enumeration JavaDoc;
10 import java.awt.*;
11 import javax.swing.*;
12 import javax.swing.border.*;
13 import javax.swing.event.*;
14 import javax.swing.text.*;
15
16 /**
17  * A view implementation to display an unwrapped
18  * preformatted line.<p>
19  * This subclasses ParagraphView, but this really only contains one
20  * Row of text.
21  *
22  * @author Timothy Prinzing
23  * @version 1.18 12/19/03
24  */

25 class LineView extends ParagraphView JavaDoc {
26     /** Last place painted at. */
27     int tabBase;
28
29     /**
30      * Creates a LineView object.
31      *
32      * @param elem the element to wrap in a view
33      */

34     public LineView(Element elem) {
35     super(elem);
36     }
37
38     /**
39      * Preformatted lines are not suppressed if they
40      * have only whitespace, so they are always visible.
41      */

42     public boolean isVisible() {
43     return true;
44     }
45
46     /**
47      * Determines the minimum span for this view along an
48      * axis. The preformatted line should refuse to be
49      * sized less than the preferred size.
50      *
51      * @param axis may be either <code>View.X_AXIS</code> or
52      * <code>View.Y_AXIS</code>
53      * @return the minimum span the view can be rendered into
54      * @see View#getPreferredSpan
55      */

56     public float getMinimumSpan(int axis) {
57     return getPreferredSpan(axis);
58     }
59
60     /**
61      * Gets the resize weight for the specified axis.
62      *
63      * @param axis may be either X_AXIS or Y_AXIS
64      * @return the weight
65      */

66     public int getResizeWeight(int axis) {
67     switch (axis) {
68     case View.X_AXIS:
69         return 1;
70     case View.Y_AXIS:
71         return 0;
72     default:
73         throw new IllegalArgumentException JavaDoc("Invalid axis: " + axis);
74     }
75     }
76
77     /**
78      * Gets the alignment for an axis.
79      *
80      * @param axis may be either X_AXIS or Y_AXIS
81      * @return the alignment
82      */

83     public float getAlignment(int axis) {
84     if (axis == View.X_AXIS) {
85         return 0;
86     }
87     return super.getAlignment(axis);
88     }
89
90     /**
91      * Lays out the children. If the layout span has changed,
92      * the rows are rebuilt. The superclass functionality
93      * is called after checking and possibly rebuilding the
94      * rows. If the height has changed, the
95      * <code>preferenceChanged</code> method is called
96      * on the parent since the vertical preference is
97      * rigid.
98      *
99      * @param width the width to lay out against >= 0. This is
100      * the width inside of the inset area.
101      * @param height the height to lay out against >= 0 (not used
102      * by paragraph, but used by the superclass). This
103      * is the height inside of the inset area.
104      */

105     protected void layout(int width, int height) {
106     super.layout(Integer.MAX_VALUE - 1, height);
107     }
108
109     /**
110      * Returns the next tab stop position given a reference position.
111      * This view implements the tab coordinate system, and calls
112      * <code>getTabbedSpan</code> on the logical children in the process
113      * of layout to determine the desired span of the children. The
114      * logical children can delegate their tab expansion upward to
115      * the paragraph which knows how to expand tabs.
116      * <code>LabelView</code> is an example of a view that delegates
117      * its tab expansion needs upward to the paragraph.
118      * <p>
119      * This is implemented to try and locate a <code>TabSet</code>
120      * in the paragraph element's attribute set. If one can be
121      * found, its settings will be used, otherwise a default expansion
122      * will be provided. The base location for for tab expansion
123      * is the left inset from the paragraphs most recent allocation
124      * (which is what the layout of the children is based upon).
125      *
126      * @param x the X reference position
127      * @param tabOffset the position within the text stream
128      * that the tab occurred at >= 0.
129      * @return the trailing end of the tab expansion >= 0
130      * @see TabSet
131      * @see TabStop
132      * @see LabelView
133      */

134     public float nextTabStop(float x, int tabOffset) {
135     // If the text isn't left justified, offset by 10 pixels!
136
if (getTabSet() == null &&
137         StyleConstants.getAlignment(getAttributes()) ==
138         StyleConstants.ALIGN_LEFT) {
139         return getPreTab(x, tabOffset);
140     }
141     return super.nextTabStop(x, tabOffset);
142     }
143
144     /**
145      * Returns the location for the tab.
146      */

147     protected float getPreTab(float x, int tabOffset) {
148     Document d = getDocument();
149     View v = getViewAtPosition(tabOffset, null);
150     if ((d instanceof StyledDocument) && v != null) {
151         // Assume f is fixed point.
152
Font f = ((StyledDocument)d).getFont(v.getAttributes());
153             Container c = getContainer();
154         FontMetrics fm = (c != null) ? c.getFontMetrics(f) :
155                 Toolkit.getDefaultToolkit().getFontMetrics(f);
156         int width = getCharactersPerTab() * fm.charWidth('W');
157         int tb = (int)getTabBase();
158         return (float)((((int)x - tb) / width + 1) * width + tb);
159     }
160     return 10.0f + x;
161     }
162
163     /**
164      * @return number of characters per tab, 8.
165      */

166     protected int getCharactersPerTab() {
167     return 8;
168     }
169 }
170
Popular Tags