summaryrefslogtreecommitdiff
path: root/javax/swing/text/ParagraphView.java
blob: 6a13b2a3e5c2f08d178dabc550b73a88fae04b7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/* ParagraphView.java -- A composite View
   Copyright (C) 2005  Free Software Foundation, Inc.

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */


package javax.swing.text;

import java.awt.Shape;

import javax.swing.SizeRequirements;
import javax.swing.event.DocumentEvent;

/**
 * A {@link FlowView} that flows it's children horizontally and boxes the rows
 * vertically.
 *
 * @author Roman Kennke (roman@kennke.org)
 */
public class ParagraphView extends FlowView implements TabExpander
{
  /**
   * A specialized horizontal <code>BoxView</code> that represents exactly
   * one row in a <code>ParagraphView</code>.
   */
  class Row extends BoxView
  {
    /**
     * Creates a new instance of <code>Row</code>.
     */
    Row(Element el)
    {
      super(el, X_AXIS);
    }

    /**
     * Overridden to adjust when we are the first line, and firstLineIndent
     * is not 0.
     */
    public short getLeftInset()
    {
      short leftInset = super.getLeftInset();
      View parent = getParent();
      if (parent != null)
        {
          if (parent.getView(0) == this)
            leftInset += firstLineIndent;
        }
      return leftInset;
    }

    public float getAlignment(int axis)
    {
      float align;
      if (axis == X_AXIS)
        switch (justification)
          {
          case StyleConstants.ALIGN_RIGHT:
            align = 1.0F;
            break;
          case StyleConstants.ALIGN_CENTER:
          case StyleConstants.ALIGN_JUSTIFIED:
            align = 0.5F;
            break;
          case StyleConstants.ALIGN_LEFT:
          default:
            align = 0.0F;
          }
      else
        align = super.getAlignment(axis);
      return align;
    }

    /**
     * Allows rows to span the whole parent view.
     */
    public float getMaximumSpan(int axis)
    {
      float max;
      if (axis == X_AXIS)
        max = Float.MAX_VALUE;
      else
        max = super.getMaximumSpan(axis);
      return max;
    }

    /**
     * Overridden because child views are not necessarily laid out in model
     * order.
     */
    protected int getViewIndexAtPosition(int pos)
    {
      int index = -1;
      if (pos >= getStartOffset() && pos < getEndOffset())
        {
          int nviews = getViewCount();
          for (int i = 0; i < nviews && index == -1; i++)
            {
              View child = getView(i);
              if (pos >= child.getStartOffset() && pos < child.getEndOffset())
                index = i;
            }
        }
      return index;
    }


    /**
     * Overridden to perform a baseline layout. The normal BoxView layout
     * isn't completely suitable for rows.
     */
    protected void layoutMinorAxis(int targetSpan, int axis, int[] offsets,
                                   int[] spans)
    {
      baselineLayout(targetSpan, axis, offsets, spans);
    }

    /**
     * Overridden to perform a baseline layout. The normal BoxView layout
     * isn't completely suitable for rows.
     */
    protected SizeRequirements calculateMinorAxisRequirements(int axis,
                                                            SizeRequirements r)
    {
      return baselineRequirements(axis, r);
    }

    protected void loadChildren(ViewFactory vf)
    {
      // Do nothing here. The children are added while layouting.
    }

    /**
     * Overridden to determine the minimum start offset of the row's children.
     */
    public int getStartOffset()
    {
      // Determine minimum start offset of the children.
      int offset = Integer.MAX_VALUE;
      int n = getViewCount();
      for (int i = 0; i < n; i++)
        {
          View v = getView(i);
          offset = Math.min(offset, v.getStartOffset());
        }
      return offset;
    }

    /**
     * Overridden to determine the maximum end offset of the row's children.
     */
    public int getEndOffset()
    {
      // Determine minimum start offset of the children.
      int offset = 0;
      int n = getViewCount();
      for (int i = 0; i < n; i++)
        {
          View v = getView(i);
          offset = Math.max(offset, v.getEndOffset());
        }
      return offset;
    }
  }

  /**
   * The indentation of the first line of the paragraph.
   */
  protected int firstLineIndent;

  /**
   * The justification of the paragraph.
   */
  private int justification;

  /**
   * The line spacing of this paragraph.
   */
  private float lineSpacing;

  /**
   * The TabSet of this paragraph.
   */
  private TabSet tabSet;

  /**
   * Creates a new <code>ParagraphView</code> for the given
   * <code>Element</code>.
   *
   * @param element the element that is rendered by this ParagraphView
   */
  public ParagraphView(Element element)
  {
    super(element, Y_AXIS);
  }

  public float nextTabStop(float x, int tabOffset)
  {
    throw new InternalError("Not implemented yet");
  }

  /**
   * Creates a new view that represents a row within a flow.
   *
   * @return a view for a new row
   */
  protected View createRow()
  {
    return new Row(getElement());
  }

  /**
   * Returns the alignment for this paragraph view for the specified axis.
   * For the X_AXIS the paragraph view will be aligned at it's left edge
   * (0.0F). For the Y_AXIS the paragraph view will be aligned at the
   * center of it's first row.
   *
   * @param axis the axis which is examined
   *
   * @return the alignment for this paragraph view for the specified axis
   */
  public float getAlignment(int axis)
  {
    float align;
    if (axis == X_AXIS)
      align = 0.5F;
    else if (getViewCount() > 0)
      {
        float prefHeight = getPreferredSpan(Y_AXIS);
        float firstRowHeight = getView(0).getPreferredSpan(Y_AXIS);
        align = (firstRowHeight / 2.F) / prefHeight;
      }
    else
      align = 0.5F;
    return align;
  }

  /**
   * Receives notification when some attributes of the displayed element
   * changes. This triggers a refresh of the cached attributes of this
   * paragraph.
   *
   * @param ev the document event
   * @param a the allocation of this view
   * @param vf the view factory to use for creating new child views
   */
  public void changedUpdate(DocumentEvent ev, Shape a, ViewFactory vf)
  {
    setPropertiesFromAttributes();
    layoutChanged(X_AXIS);
    layoutChanged(Y_AXIS);
    super.changedUpdate(ev, a, vf);
  }

  /**
   * Fetches the cached properties from the element's attributes.
   */
  protected void setPropertiesFromAttributes()
  {
    Element el = getElement();
    AttributeSet atts = el.getAttributes();
    setFirstLineIndent(StyleConstants.getFirstLineIndent(atts));
    setLineSpacing(StyleConstants.getLineSpacing(atts));
    setJustification(StyleConstants.getAlignment(atts));
    tabSet = StyleConstants.getTabSet(atts);
  }

  /**
   * Sets the indentation of the first line of the paragraph.
   *
   * @param i the indentation to set
   */
  protected void setFirstLineIndent(float i)
  {
    firstLineIndent = (int) i;
  }

  /**
   * Sets the justification of the paragraph.
   *
   * @param j the justification to set 
   */
  protected void setJustification(int j)
  {
    justification = j;
  }

  /**
   * Sets the line spacing for this paragraph.
   *
   * @param s the line spacing to set
   */
  protected void setLineSpacing(float s)
  {
    lineSpacing = s;
  }

  /**
   * Returns the i-th view from the logical views, before breaking into rows.
   *
   * @param i the index of the logical view to return
   *
   * @return the i-th view from the logical views, before breaking into rows
   */
  protected View getLayoutView(int i)
  {
    return layoutPool.getView(i);
  }

  /**
   * Returns the number of logical child views.
   *
   * @return the number of logical child views
   */
  protected int getLayoutViewCount()
  {
    return layoutPool.getViewCount();
  }

  /**
   * Returns the TabSet used by this ParagraphView.
   *
   * @return the TabSet used by this ParagraphView
   */
  protected TabSet getTabSet()
  {
    return tabSet;
  }

  /**
   * Finds the next offset in the document that has one of the characters
   * specified in <code>string</code>. If there is no such character found,
   * this returns -1.
   *
   * @param string the characters to search for
   * @param start the start offset
   *
   * @return the next offset in the document that has one of the characters
   *         specified in <code>string</code>
   */
  protected int findOffsetToCharactersInString(char[] string, int start)
  {
    int offset = -1;
    Document doc = getDocument();
    Segment text = new Segment();
    try
      {
        doc.getText(start, doc.getLength() - start, text);
        int index = start;

        searchLoop:
        while (true)
          {
            char ch = text.next();
            if (ch == Segment.DONE)
              break;

            for (int j = 0; j < string.length; ++j)
              {
                if (string[j] == ch)
                  {
                    offset = index;
                    break searchLoop;
                  }
              }
            index++;
          }
      }
    catch (BadLocationException ex)
      {
        // Ignore this and return -1.
      }
    return offset;
  }

  protected int getClosestPositionTo(int pos, Position.Bias bias, Shape a,
                                     int direction, Position.Bias[] biasRet,
                                     int rowIndex, int x)
    throws BadLocationException
  {
    // FIXME: Implement this properly. However, this looks like it might
    // have been replaced by viewToModel.
    return pos;
  }

  /**
   * Returns the size that is used by this view (or it's child views) between
   * <code>startOffset</code> and <code>endOffset</code>. If the child views
   * implement the {@link TabableView} interface, then this is used to
   * determine the span, otherwise we use the preferred span of the child
   * views.
   *
   * @param startOffset the start offset
   * @param endOffset the end offset
   *
   * @return the span used by the view between <code>startOffset</code> and
   *         <code>endOffset</cod>
   */
  protected float getPartialSize(int startOffset, int endOffset)
  {
    int startIndex = getViewIndex(startOffset, Position.Bias.Backward);
    int endIndex = getViewIndex(endOffset, Position.Bias.Forward);
    float span;
    if (startIndex == endIndex)
      {
        View child = getView(startIndex);
        if (child instanceof TabableView)
          {
            TabableView tabable = (TabableView) child;
            span = tabable.getPartialSpan(startOffset, endOffset);
          }
        else
          span = child.getPreferredSpan(X_AXIS);
      }
    else if (endIndex - startIndex == 1)
      {
        View child1 = getView(startIndex);
        if (child1 instanceof TabableView)
          {
            TabableView tabable = (TabableView) child1;
            span = tabable.getPartialSpan(startOffset, child1.getEndOffset());
          }
        else
          span = child1.getPreferredSpan(X_AXIS);
        View child2 = getView(endIndex);
        if (child2 instanceof TabableView)
          {
            TabableView tabable = (TabableView) child2;
            span += tabable.getPartialSpan(child2.getStartOffset(), endOffset);
          }
        else
          span += child2.getPreferredSpan(X_AXIS);
      }
    else
      {
        // Start with the first view.
        View child1 = getView(startIndex);
        if (child1 instanceof TabableView)
          {
            TabableView tabable = (TabableView) child1;
            span = tabable.getPartialSpan(startOffset, child1.getEndOffset());
          }
        else
          span = child1.getPreferredSpan(X_AXIS);

        // Add up the view spans between the start and the end view.
        for (int i = startIndex + 1; i < endIndex; i++)
          {
            View child = getView(i);
            span += child.getPreferredSpan(X_AXIS);
          }

        // Add the span of the last view.
        View child2 = getView(endIndex);
        if (child2 instanceof TabableView)
          {
            TabableView tabable = (TabableView) child2;
            span += tabable.getPartialSpan(child2.getStartOffset(), endOffset);
          }
        else
          span += child2.getPreferredSpan(X_AXIS);
      }
    return span;
  }

  /**
   * Returns the location where the tabs are calculated from. This returns
   * <code>0.0F</code> by default.
   *
   * @return the location where the tabs are calculated from
   */
  protected float getTabBase()
  {
    return 0.0F;
  }

  /**
   * @specnote This method is specified to take a Row parameter, which is a
   *           private inner class of that class, which makes it unusable from
   *           application code. Also, this method seems to be replaced by
   *           {@link FlowStrategy#adjustRow(FlowView, int, int, int)}.
   *
   */
  protected void adjustRow(Row r, int desiredSpan, int x)
  {
  }

  /**
   * @specnote This method's signature differs from the one defined in
   *           {@link View} and is therefore never called. It is probably there
   *           for historical reasons.
   */
  public View breakView(int axis, float len, Shape a)
  {
    // This method is not used.
    return null;
  }

  /**
   * @specnote This method's signature differs from the one defined in
   *           {@link View} and is therefore never called. It is probably there
   *           for historical reasons.
   */
  public int getBreakWeight(int axis, float len)
  {
    // This method is not used.
    return 0;
  }
}