24 */
25 package javax.swing.text;
26
27 import java.awt.*;
28 import java.lang.ref.SoftReference;
29 import javax.swing.event.*;
30
31 /**
32 * View of plain text (text with only one font and color)
33 * that does line-wrapping. This view expects that its
34 * associated element has child elements that represent
35 * the lines it should be wrapping. It is implemented
36 * as a vertical box that contains logical line views.
37 * The logical line views are nested classes that render
38 * the logical line as multiple physical line if the logical
39 * line is too wide to fit within the allocation. The
40 * line views draw upon the outer class for its state
41 * to reduce their memory requirements.
42 * <p>
43 * The line views do all of their rendering through the
44 * <code>drawLine</code> method which in turn does all of
45 * its rendering through the <code>drawSelectedText</code>
46 * and <code>drawUnselectedText</code> methods. This
47 * enables subclasses to easily specialize the rendering
48 * without concern for the layout aspects.
49 *
50 * @author Timothy Prinzing
51 * @see View
52 */
53 public class WrappedPlainView extends BoxView implements TabExpander {
54
55 /**
56 * Creates a new WrappedPlainView. Lines will be wrapped
57 * on character boundaries.
58 *
59 * @param elem the element underlying the view
60 */
61 public WrappedPlainView(Element elem) {
62 this(elem, false);
63 }
64
65 /**
66 * Creates a new WrappedPlainView. Lines can be wrapped on
72 */
73 public WrappedPlainView(Element elem, boolean wordWrap) {
74 super(elem, Y_AXIS);
75 this.wordWrap = wordWrap;
76 }
77
78 /**
79 * Returns the tab size set for the document, defaulting to 8.
80 *
81 * @return the tab size
82 */
83 protected int getTabSize() {
84 Integer i = (Integer) getDocument().getProperty(PlainDocument.tabSizeAttribute);
85 int size = (i != null) ? i.intValue() : 8;
86 return size;
87 }
88
89 /**
90 * Renders a line of text, suppressing whitespace at the end
91 * and expanding any tabs. This is implemented to make calls
92 * to the methods <code>drawUnselectedText</code> and
93 * <code>drawSelectedText</code> so that the way selected and
94 * unselected text are rendered can be customized.
95 *
96 * @param p0 the starting document location to use >= 0
97 * @param p1 the ending document location to use >= p1
98 * @param g the graphics context
99 * @param x the starting X position >= 0
100 * @param y the starting Y position >= 0
101 * @see #drawUnselectedText
102 * @see #drawSelectedText
103 */
104 protected void drawLine(int p0, int p1, Graphics g, int x, int y) {
105 Element lineMap = getElement();
106 Element line = lineMap.getElement(lineMap.getElementIndex(p0));
107 Element elem;
108
109 try {
110 if (line.isLeaf()) {
111 drawText(line, p0, p1, g, x, y);
112 } else {
113 // this line contains the composed text.
235 protected int calculateBreakPosition(int p0, int p1) {
236 int p;
237 Segment segment = SegmentCache.getSharedSegment();
238 loadText(segment, p0, p1);
239 int currentWidth = getWidth();
240 if (wordWrap) {
241 p = p0 + Utilities.getBreakLocation(segment, metrics,
242 tabBase, tabBase + currentWidth,
243 this, p0);
244 } else {
245 p = p0 + Utilities.getTabbedTextOffset(segment, metrics,
246 tabBase, tabBase + currentWidth,
247 this, p0, false);
248 }
249 SegmentCache.releaseSharedSegment(segment);
250 return p;
251 }
252
253 /**
254 * Loads all of the children to initialize the view.
255 * This is called by the <code>setParent</code> method.
256 * Subclasses can reimplement this to initialize their
257 * child views in a different manner. The default
258 * implementation creates a child view for each
259 * child element.
260 *
261 * @param f the view factory
262 */
263 protected void loadChildren(ViewFactory f) {
264 Element e = getElement();
265 int n = e.getElementCount();
266 if (n > 0) {
267 View[] added = new View[n];
268 for (int i = 0; i < n; i++) {
269 added[i] = new WrappedLine(e.getElement(i));
270 }
271 replace(0, 0, added);
272 }
273 }
274
275 /**
|
24 */
25 package javax.swing.text;
26
27 import java.awt.*;
28 import java.lang.ref.SoftReference;
29 import javax.swing.event.*;
30
31 /**
32 * View of plain text (text with only one font and color)
33 * that does line-wrapping. This view expects that its
34 * associated element has child elements that represent
35 * the lines it should be wrapping. It is implemented
36 * as a vertical box that contains logical line views.
37 * The logical line views are nested classes that render
38 * the logical line as multiple physical line if the logical
39 * line is too wide to fit within the allocation. The
40 * line views draw upon the outer class for its state
41 * to reduce their memory requirements.
42 * <p>
43 * The line views do all of their rendering through the
44 * {@code drawLine} method which in turn does all of
45 * its rendering through the {@code drawSelectedText}
46 * and {@code drawUnselectedText} methods. This
47 * enables subclasses to easily specialize the rendering
48 * without concern for the layout aspects.
49 *
50 * @author Timothy Prinzing
51 * @see View
52 */
53 public class WrappedPlainView extends BoxView implements TabExpander {
54
55 /**
56 * Creates a new WrappedPlainView. Lines will be wrapped
57 * on character boundaries.
58 *
59 * @param elem the element underlying the view
60 */
61 public WrappedPlainView(Element elem) {
62 this(elem, false);
63 }
64
65 /**
66 * Creates a new WrappedPlainView. Lines can be wrapped on
72 */
73 public WrappedPlainView(Element elem, boolean wordWrap) {
74 super(elem, Y_AXIS);
75 this.wordWrap = wordWrap;
76 }
77
78 /**
79 * Returns the tab size set for the document, defaulting to 8.
80 *
81 * @return the tab size
82 */
83 protected int getTabSize() {
84 Integer i = (Integer) getDocument().getProperty(PlainDocument.tabSizeAttribute);
85 int size = (i != null) ? i.intValue() : 8;
86 return size;
87 }
88
89 /**
90 * Renders a line of text, suppressing whitespace at the end
91 * and expanding any tabs. This is implemented to make calls
92 * to the methods {@code drawUnselectedText} and
93 * {@code drawSelectedText} so that the way selected and
94 * unselected text are rendered can be customized.
95 *
96 * @param p0 the starting document location to use >= 0
97 * @param p1 the ending document location to use >= p1
98 * @param g the graphics context
99 * @param x the starting X position >= 0
100 * @param y the starting Y position >= 0
101 * @see #drawUnselectedText
102 * @see #drawSelectedText
103 */
104 protected void drawLine(int p0, int p1, Graphics g, int x, int y) {
105 Element lineMap = getElement();
106 Element line = lineMap.getElement(lineMap.getElementIndex(p0));
107 Element elem;
108
109 try {
110 if (line.isLeaf()) {
111 drawText(line, p0, p1, g, x, y);
112 } else {
113 // this line contains the composed text.
235 protected int calculateBreakPosition(int p0, int p1) {
236 int p;
237 Segment segment = SegmentCache.getSharedSegment();
238 loadText(segment, p0, p1);
239 int currentWidth = getWidth();
240 if (wordWrap) {
241 p = p0 + Utilities.getBreakLocation(segment, metrics,
242 tabBase, tabBase + currentWidth,
243 this, p0);
244 } else {
245 p = p0 + Utilities.getTabbedTextOffset(segment, metrics,
246 tabBase, tabBase + currentWidth,
247 this, p0, false);
248 }
249 SegmentCache.releaseSharedSegment(segment);
250 return p;
251 }
252
253 /**
254 * Loads all of the children to initialize the view.
255 * This is called by the {@code setParent} method.
256 * Subclasses can reimplement this to initialize their
257 * child views in a different manner. The default
258 * implementation creates a child view for each
259 * child element.
260 *
261 * @param f the view factory
262 */
263 protected void loadChildren(ViewFactory f) {
264 Element e = getElement();
265 int n = e.getElementCount();
266 if (n > 0) {
267 View[] added = new View[n];
268 for (int i = 0; i < n; i++) {
269 added[i] = new WrappedLine(e.getElement(i));
270 }
271 replace(0, 0, added);
272 }
273 }
274
275 /**
|