1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45:
46: import ;
47: import ;
48:
49:
55: public class ParagraphView extends FlowView implements TabExpander
56: {
57:
61: class Row extends BoxView
62: {
63:
66: Row(Element el)
67: {
68: super(el, X_AXIS);
69: }
70:
71:
75: public short getLeftInset()
76: {
77: short leftInset = super.getLeftInset();
78: View parent = getParent();
79: if (parent != null)
80: {
81: if (parent.getView(0) == this)
82: leftInset += firstLineIndent;
83: }
84: return leftInset;
85: }
86:
87: public float getAlignment(int axis)
88: {
89: float align;
90: if (axis == X_AXIS)
91: switch (justification)
92: {
93: case StyleConstants.ALIGN_RIGHT:
94: align = 1.0F;
95: break;
96: case StyleConstants.ALIGN_CENTER:
97: case StyleConstants.ALIGN_JUSTIFIED:
98: align = 0.5F;
99: break;
100: case StyleConstants.ALIGN_LEFT:
101: default:
102: align = 0.0F;
103: }
104: else
105: align = super.getAlignment(axis);
106: return align;
107: }
108:
109:
113: protected int getViewIndexAtPosition(int pos)
114: {
115: int index = -1;
116: if (pos >= getStartOffset() && pos < getEndOffset())
117: {
118: int nviews = getViewCount();
119: for (int i = 0; i < nviews && index == -1; i++)
120: {
121: View child = getView(i);
122: if (pos >= child.getStartOffset() && pos < child.getEndOffset())
123: index = i;
124: }
125: }
126: return index;
127: }
128:
129:
130:
134: protected void layoutMinorAxis(int targetSpan, int axis, int[] offsets,
135: int[] spans)
136: {
137: baselineLayout(targetSpan, axis, offsets, spans);
138: }
139:
140:
144: protected SizeRequirements calculateMinorAxisRequirements(int axis,
145: SizeRequirements r)
146: {
147: return baselineRequirements(axis, r);
148: }
149:
150: protected void loadChildren(ViewFactory vf)
151: {
152:
153: }
154:
155:
158: public int getStartOffset()
159: {
160:
161: int offset = Integer.MAX_VALUE;
162: int n = getViewCount();
163: for (int i = 0; i < n; i++)
164: {
165: View v = getView(i);
166: offset = Math.min(offset, v.getStartOffset());
167: }
168: return offset;
169: }
170:
171:
174: public int getEndOffset()
175: {
176:
177: int offset = 0;
178: int n = getViewCount();
179: for (int i = 0; i < n; i++)
180: {
181: View v = getView(i);
182: offset = Math.max(offset, v.getEndOffset());
183: }
184: return offset;
185: }
186: }
187:
188:
191: protected int firstLineIndent;
192:
193:
196: private int justification;
197:
198:
201: private float lineSpacing;
202:
203:
206: private TabSet tabSet;
207:
208:
214: public ParagraphView(Element element)
215: {
216: super(element, Y_AXIS);
217: }
218:
219: public float nextTabStop(float x, int tabOffset)
220: {
221: throw new InternalError("Not implemented yet");
222: }
223:
224:
229: protected View createRow()
230: {
231: return new Row(getElement());
232: }
233:
234:
244: public float getAlignment(int axis)
245: {
246: float align;
247: if (axis == X_AXIS)
248: align = 0.5F;
249: else if (getViewCount() > 0)
250: {
251: float prefHeight = getPreferredSpan(Y_AXIS);
252: float firstRowHeight = getView(0).getPreferredSpan(Y_AXIS);
253: align = (firstRowHeight / 2.F) / prefHeight;
254: }
255: else
256: align = 0.5F;
257: return align;
258: }
259:
260:
269: public void changedUpdate(DocumentEvent ev, Shape a, ViewFactory vf)
270: {
271: setPropertiesFromAttributes();
272: layoutChanged(X_AXIS);
273: layoutChanged(Y_AXIS);
274: super.changedUpdate(ev, a, vf);
275: }
276:
277:
280: protected void setPropertiesFromAttributes()
281: {
282: Element el = getElement();
283: AttributeSet atts = el.getAttributes();
284: setFirstLineIndent(StyleConstants.getFirstLineIndent(atts));
285: setLineSpacing(StyleConstants.getLineSpacing(atts));
286: setJustification(StyleConstants.getAlignment(atts));
287: tabSet = StyleConstants.getTabSet(atts);
288: }
289:
290:
295: protected void setFirstLineIndent(float i)
296: {
297: firstLineIndent = (int) i;
298: }
299:
300:
305: protected void setJustification(int j)
306: {
307: justification = j;
308: }
309:
310:
315: protected void setLineSpacing(float s)
316: {
317: lineSpacing = s;
318: }
319:
320:
327: protected View getLayoutView(int i)
328: {
329: return layoutPool.getView(i);
330: }
331:
332:
337: protected int getLayoutViewCount()
338: {
339: return layoutPool.getViewCount();
340: }
341:
342:
347: protected TabSet getTabSet()
348: {
349: return tabSet;
350: }
351:
352:
363: protected int findOffsetToCharactersInString(char[] string, int start)
364: {
365: int offset = -1;
366: Document doc = getDocument();
367: Segment text = new Segment();
368: try
369: {
370: doc.getText(start, doc.getLength() - start, text);
371: int index = start;
372:
373: searchLoop:
374: while (true)
375: {
376: char ch = text.next();
377: if (ch == Segment.DONE)
378: break;
379:
380: for (int j = 0; j < string.length; ++j)
381: {
382: if (string[j] == ch)
383: {
384: offset = index;
385: break searchLoop;
386: }
387: }
388: index++;
389: }
390: }
391: catch (BadLocationException ex)
392: {
393:
394: }
395: return offset;
396: }
397:
398: protected int getClosestPositionTo(int pos, Position.Bias bias, Shape a,
399: int direction, Position.Bias[] biasRet,
400: int rowIndex, int x)
401: throws BadLocationException
402: {
403:
404:
405: return pos;
406: }
407:
408:
421: protected float getPartialSize(int startOffset, int endOffset)
422: {
423: int startIndex = getViewIndex(startOffset, Position.Bias.Backward);
424: int endIndex = getViewIndex(endOffset, Position.Bias.Forward);
425: float span;
426: if (startIndex == endIndex)
427: {
428: View child = getView(startIndex);
429: if (child instanceof TabableView)
430: {
431: TabableView tabable = (TabableView) child;
432: span = tabable.getPartialSpan(startOffset, endOffset);
433: }
434: else
435: span = child.getPreferredSpan(X_AXIS);
436: }
437: else if (endIndex - startIndex == 1)
438: {
439: View child1 = getView(startIndex);
440: if (child1 instanceof TabableView)
441: {
442: TabableView tabable = (TabableView) child1;
443: span = tabable.getPartialSpan(startOffset, child1.getEndOffset());
444: }
445: else
446: span = child1.getPreferredSpan(X_AXIS);
447: View child2 = getView(endIndex);
448: if (child2 instanceof TabableView)
449: {
450: TabableView tabable = (TabableView) child2;
451: span += tabable.getPartialSpan(child2.getStartOffset(), endOffset);
452: }
453: else
454: span += child2.getPreferredSpan(X_AXIS);
455: }
456: else
457: {
458:
459: View child1 = getView(startIndex);
460: if (child1 instanceof TabableView)
461: {
462: TabableView tabable = (TabableView) child1;
463: span = tabable.getPartialSpan(startOffset, child1.getEndOffset());
464: }
465: else
466: span = child1.getPreferredSpan(X_AXIS);
467:
468:
469: for (int i = startIndex + 1; i < endIndex; i++)
470: {
471: View child = getView(i);
472: span += child.getPreferredSpan(X_AXIS);
473: }
474:
475:
476: View child2 = getView(endIndex);
477: if (child2 instanceof TabableView)
478: {
479: TabableView tabable = (TabableView) child2;
480: span += tabable.getPartialSpan(child2.getStartOffset(), endOffset);
481: }
482: else
483: span += child2.getPreferredSpan(X_AXIS);
484: }
485: return span;
486: }
487:
488:
494: protected float getTabBase()
495: {
496: return 0.0F;
497: }
498:
499:
506: protected void adjustRow(Row r, int desiredSpan, int x)
507: {
508: }
509:
510:
515: public View breakView(int axis, float len, Shape a)
516: {
517:
518: return null;
519: }
520:
521:
526: public int getBreakWeight(int axis, float len)
527: {
528:
529: return 0;
530: }
531: }