001 /*
002 * Copyright 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025 package javax.swing.text.html;
026
027 import java.util.Enumeration;
028 import java.awt.*;
029 import javax.swing.*;
030 import javax.swing.border.*;
031 import javax.swing.event.*;
032 import javax.swing.text.*;
033
034 /**
035 * A view implementation to display an unwrapped
036 * preformatted line.<p>
037 * This subclasses ParagraphView, but this really only contains one
038 * Row of text.
039 *
040 * @author Timothy Prinzing
041 * @version 1.25 05/05/07
042 */
043 class LineView extends ParagraphView {
044 /** Last place painted at. */
045 int tabBase;
046
047 /**
048 * Creates a LineView object.
049 *
050 * @param elem the element to wrap in a view
051 */
052 public LineView(Element elem) {
053 super (elem);
054 }
055
056 /**
057 * Preformatted lines are not suppressed if they
058 * have only whitespace, so they are always visible.
059 */
060 public boolean isVisible() {
061 return true;
062 }
063
064 /**
065 * Determines the minimum span for this view along an
066 * axis. The preformatted line should refuse to be
067 * sized less than the preferred size.
068 *
069 * @param axis may be either <code>View.X_AXIS</code> or
070 * <code>View.Y_AXIS</code>
071 * @return the minimum span the view can be rendered into
072 * @see View#getPreferredSpan
073 */
074 public float getMinimumSpan(int axis) {
075 return getPreferredSpan(axis);
076 }
077
078 /**
079 * Gets the resize weight for the specified axis.
080 *
081 * @param axis may be either X_AXIS or Y_AXIS
082 * @return the weight
083 */
084 public int getResizeWeight(int axis) {
085 switch (axis) {
086 case View.X_AXIS:
087 return 1;
088 case View.Y_AXIS:
089 return 0;
090 default:
091 throw new IllegalArgumentException("Invalid axis: " + axis);
092 }
093 }
094
095 /**
096 * Gets the alignment for an axis.
097 *
098 * @param axis may be either X_AXIS or Y_AXIS
099 * @return the alignment
100 */
101 public float getAlignment(int axis) {
102 if (axis == View.X_AXIS) {
103 return 0;
104 }
105 return super .getAlignment(axis);
106 }
107
108 /**
109 * Lays out the children. If the layout span has changed,
110 * the rows are rebuilt. The superclass functionality
111 * is called after checking and possibly rebuilding the
112 * rows. If the height has changed, the
113 * <code>preferenceChanged</code> method is called
114 * on the parent since the vertical preference is
115 * rigid.
116 *
117 * @param width the width to lay out against >= 0. This is
118 * the width inside of the inset area.
119 * @param height the height to lay out against >= 0 (not used
120 * by paragraph, but used by the superclass). This
121 * is the height inside of the inset area.
122 */
123 protected void layout(int width, int height) {
124 super .layout(Integer.MAX_VALUE - 1, height);
125 }
126
127 /**
128 * Returns the next tab stop position given a reference position.
129 * This view implements the tab coordinate system, and calls
130 * <code>getTabbedSpan</code> on the logical children in the process
131 * of layout to determine the desired span of the children. The
132 * logical children can delegate their tab expansion upward to
133 * the paragraph which knows how to expand tabs.
134 * <code>LabelView</code> is an example of a view that delegates
135 * its tab expansion needs upward to the paragraph.
136 * <p>
137 * This is implemented to try and locate a <code>TabSet</code>
138 * in the paragraph element's attribute set. If one can be
139 * found, its settings will be used, otherwise a default expansion
140 * will be provided. The base location for for tab expansion
141 * is the left inset from the paragraphs most recent allocation
142 * (which is what the layout of the children is based upon).
143 *
144 * @param x the X reference position
145 * @param tabOffset the position within the text stream
146 * that the tab occurred at >= 0.
147 * @return the trailing end of the tab expansion >= 0
148 * @see TabSet
149 * @see TabStop
150 * @see LabelView
151 */
152 public float nextTabStop(float x, int tabOffset) {
153 // If the text isn't left justified, offset by 10 pixels!
154 if (getTabSet() == null
155 && StyleConstants.getAlignment(getAttributes()) == StyleConstants.ALIGN_LEFT) {
156 return getPreTab(x, tabOffset);
157 }
158 return super .nextTabStop(x, tabOffset);
159 }
160
161 /**
162 * Returns the location for the tab.
163 */
164 protected float getPreTab(float x, int tabOffset) {
165 Document d = getDocument();
166 View v = getViewAtPosition(tabOffset, null);
167 if ((d instanceof StyledDocument) && v != null) {
168 // Assume f is fixed point.
169 Font f = ((StyledDocument) d).getFont(v.getAttributes());
170 Container c = getContainer();
171 FontMetrics fm = (c != null) ? c.getFontMetrics(f)
172 : Toolkit.getDefaultToolkit().getFontMetrics(f);
173 int width = getCharactersPerTab() * fm.charWidth('W');
174 int tb = (int) getTabBase();
175 return (float) ((((int) x - tb) / width + 1) * width + tb);
176 }
177 return 10.0f + x;
178 }
179
180 /**
181 * @return number of characters per tab, 8.
182 */
183 protected int getCharactersPerTab() {
184 return 8;
185 }
186 }
|