1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45:
46:
52: public class ParagraphView extends FlowView implements TabExpander
53: {
54:
58: class Row extends BoxView
59: {
60:
63: Row(Element el)
64: {
65: super(el, X_AXIS);
66: }
67:
68:
72: public short getLeftInset()
73: {
74: short leftInset = super.getLeftInset();
75: View parent = getParent();
76: if (parent != null)
77: {
78: if (parent.getView(0) == this)
79: leftInset += firstLineIndent;
80: }
81: return leftInset;
82: }
83:
84: public float getAlignment(int axis)
85: {
86: float align;
87: if (axis == X_AXIS)
88: switch (justification)
89: {
90: case StyleConstants.ALIGN_RIGHT:
91: align = 1.0F;
92: break;
93: case StyleConstants.ALIGN_CENTER:
94: case StyleConstants.ALIGN_JUSTIFIED:
95: align = 0.5F;
96: break;
97: case StyleConstants.ALIGN_LEFT:
98: default:
99: align = 0.0F;
100: }
101: else
102: align = super.getAlignment(axis);
103: return align;
104: }
105:
106:
110: protected int getViewIndexAtPosition(int pos)
111: {
112: int index = -1;
113: if (pos >= getStartOffset() && pos < getEndOffset())
114: {
115: int nviews = getViewCount();
116: for (int i = 0; i < nviews && index == -1; i++)
117: {
118: View child = getView(i);
119: if (pos >= child.getStartOffset() && pos < child.getEndOffset())
120: index = i;
121: }
122: }
123: return index;
124: }
125:
126:
127:
131: protected void layoutMinorAxis(int targetSpan, int axis, int[] offsets,
132: int[] spans)
133: {
134: baselineLayout(targetSpan, axis, offsets, spans);
135: }
136:
137:
141: protected SizeRequirements calculateMinorAxisRequirements(int axis,
142: SizeRequirements r)
143: {
144: return baselineRequirements(axis, r);
145: }
146:
147: protected void loadChildren(ViewFactory vf)
148: {
149:
150: }
151:
152:
155: public int getStartOffset()
156: {
157:
158: int offset = Integer.MAX_VALUE;
159: int n = getViewCount();
160: for (int i = 0; i < n; i++)
161: {
162: View v = getView(i);
163: offset = Math.min(offset, v.getStartOffset());
164: }
165: return offset;
166: }
167:
168:
171: public int getEndOffset()
172: {
173:
174: int offset = 0;
175: int n = getViewCount();
176: for (int i = 0; i < n; i++)
177: {
178: View v = getView(i);
179: offset = Math.max(offset, v.getEndOffset());
180: }
181: return offset;
182: }
183: }
184:
185:
188: protected int firstLineIndent;
189:
190:
193: private int justification;
194:
195:
198: private float lineSpacing;
199:
200:
203: private TabSet tabSet;
204:
205:
211: public ParagraphView(Element element)
212: {
213: super(element, Y_AXIS);
214: }
215:
216: public float nextTabStop(float x, int tabOffset)
217: {
218: throw new InternalError("Not implemented yet");
219: }
220:
221:
226: protected View createRow()
227: {
228: return new Row(getElement());
229: }
230:
231:
241: public float getAlignment(int axis)
242: {
243: float align;
244: if (axis == X_AXIS)
245: align = 0.5F;
246: else if (getViewCount() > 0)
247: {
248: float prefHeight = getPreferredSpan(Y_AXIS);
249: float firstRowHeight = getView(0).getPreferredSpan(Y_AXIS);
250: align = (firstRowHeight / 2.F) / prefHeight;
251: }
252: else
253: align = 0.5F;
254: return align;
255: }
256:
257:
266: public void changedUpdate(DocumentEvent ev, Shape a, ViewFactory vf)
267: {
268: setPropertiesFromAttributes();
269: layoutChanged(X_AXIS);
270: layoutChanged(Y_AXIS);
271: super.changedUpdate(ev, a, vf);
272: }
273:
274:
277: protected void setPropertiesFromAttributes()
278: {
279: Element el = getElement();
280: AttributeSet atts = el.getAttributes();
281: setFirstLineIndent(StyleConstants.getFirstLineIndent(atts));
282: setLineSpacing(StyleConstants.getLineSpacing(atts));
283: setJustification(StyleConstants.getAlignment(atts));
284: tabSet = StyleConstants.getTabSet(atts);
285: }
286:
287:
292: protected void setFirstLineIndent(float i)
293: {
294: firstLineIndent = (int) i;
295: }
296:
297:
302: protected void setJustification(int j)
303: {
304: justification = j;
305: }
306:
307:
312: protected void setLineSpacing(float s)
313: {
314: lineSpacing = s;
315: }
316:
317:
324: protected View getLayoutView(int i)
325: {
326: return layoutPool.getView(i);
327: }
328:
329:
334: protected int getLayoutViewCount()
335: {
336: return layoutPool.getViewCount();
337: }
338:
339:
344: protected TabSet getTabSet()
345: {
346: return tabSet;
347: }
348:
349:
360: protected int findOffsetToCharactersInString(char[] string, int start)
361: {
362: int offset = -1;
363: Document doc = getDocument();
364: Segment text = new Segment();
365: try
366: {
367: doc.getText(start, doc.getLength() - start, text);
368: int index = start;
369:
370: searchLoop:
371: while (true)
372: {
373: char ch = text.next();
374: if (ch == Segment.DONE)
375: break;
376:
377: for (int j = 0; j < string.length; ++j)
378: {
379: if (string[j] == ch)
380: {
381: offset = index;
382: break searchLoop;
383: }
384: }
385: index++;
386: }
387: }
388: catch (BadLocationException ex)
389: {
390:
391: }
392: return offset;
393: }
394:
395: protected int getClosestPositionTo(int pos, Position.Bias bias, Shape a,
396: int direction, Position.Bias[] biasRet,
397: int rowIndex, int x)
398: throws BadLocationException
399: {
400:
401:
402: return pos;
403: }
404:
405:
418: protected float getPartialSize(int startOffset, int endOffset)
419: {
420: int startIndex = getViewIndex(startOffset, Position.Bias.Backward);
421: int endIndex = getViewIndex(endOffset, Position.Bias.Forward);
422: float span;
423: if (startIndex == endIndex)
424: {
425: View child = getView(startIndex);
426: if (child instanceof TabableView)
427: {
428: TabableView tabable = (TabableView) child;
429: span = tabable.getPartialSpan(startOffset, endOffset);
430: }
431: else
432: span = child.getPreferredSpan(X_AXIS);
433: }
434: else if (endIndex - startIndex == 1)
435: {
436: View child1 = getView(startIndex);
437: if (child1 instanceof TabableView)
438: {
439: TabableView tabable = (TabableView) child1;
440: span = tabable.getPartialSpan(startOffset, child1.getEndOffset());
441: }
442: else
443: span = child1.getPreferredSpan(X_AXIS);
444: View child2 = getView(endIndex);
445: if (child2 instanceof TabableView)
446: {
447: TabableView tabable = (TabableView) child2;
448: span += tabable.getPartialSpan(child2.getStartOffset(), endOffset);
449: }
450: else
451: span += child2.getPreferredSpan(X_AXIS);
452: }
453: else
454: {
455:
456: View child1 = getView(startIndex);
457: if (child1 instanceof TabableView)
458: {
459: TabableView tabable = (TabableView) child1;
460: span = tabable.getPartialSpan(startOffset, child1.getEndOffset());
461: }
462: else
463: span = child1.getPreferredSpan(X_AXIS);
464:
465:
466: for (int i = startIndex + 1; i < endIndex; i++)
467: {
468: View child = getView(i);
469: span += child.getPreferredSpan(X_AXIS);
470: }
471:
472:
473: View child2 = getView(endIndex);
474: if (child2 instanceof TabableView)
475: {
476: TabableView tabable = (TabableView) child2;
477: span += tabable.getPartialSpan(child2.getStartOffset(), endOffset);
478: }
479: else
480: span += child2.getPreferredSpan(X_AXIS);
481: }
482: return span;
483: }
484:
485:
491: protected float getTabBase()
492: {
493: return 0.0F;
494: }
495:
496:
503: protected void adjustRow(Row r, int desiredSpan, int x)
504: {
505: }
506:
507:
512: public View breakView(int axis, float len, Shape a)
513: {
514:
515: return null;
516: }
517:
518:
523: public int getBreakWeight(int axis, float len)
524: {
525:
526: return 0;
527: }
528: }