summaryrefslogtreecommitdiff
path: root/javax/swing/text/html/FrameSetView.java
blob: e3252d79cafd2bf2fb6c20eb81fafe9839caa591 (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
/* FrameSetView.java -- Implements HTML frameset
   Copyright (C) 2006 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.html;

import java.util.StringTokenizer;

import javax.swing.text.AttributeSet;
import javax.swing.text.BoxView;
import javax.swing.text.Element;
import javax.swing.text.View;
import javax.swing.text.ViewFactory;

/**
 * Implements HTML framesets. This is implemented as a vertical box that
 * holds the rows of the frameset. Each row is again a horizontal box that
 * holds the actual columns.
 */
public class FrameSetView
  extends BoxView
{

  /**
   * A row of a frameset.
   */
  private class FrameSetRow
    extends BoxView
  {
    private int row;
    FrameSetRow(Element el, int r)
    {
      super(el, X_AXIS);
      row = r;
    }

    protected void loadChildren(ViewFactory f)
    {
      // Load the columns here.
      Element el = getElement();
      View[] columns = new View[numViews[X_AXIS]];
      int offset = row * numViews[X_AXIS];
      for (int c = 0; c < numViews[X_AXIS]; c++)
        {
          Element child = el.getElement(offset + c);
          columns[c] = f.create(child);
        }
      replace(0, 0, columns);
    }

    protected void layoutMajorAxis(int targetSpan, int axis, int[] offsets,
                                   int[] spans)
    {
      int numRows = numViews[X_AXIS];
      int[] abs = absolute[X_AXIS];
      int[] rel = relative[X_AXIS];
      int[] perc = percent[X_AXIS];
      layoutViews(targetSpan, axis, offsets, spans, numRows, abs, rel, perc);
    }
  }

  /**
   * Holds the absolute layout information for the views along one axis. The
   * indices are absolute[axis][index], where axis is either X_AXIS (columns)
   * or Y_AXIS (rows). Rows or columns that don't have absolute layout have
   * a -1 in this array.
   */
  int[][] absolute;

  /**
   * Holds the relative (*) layout information for the views along one axis.
   * The indices are relative[axis][index], where axis is either X_AXIS
   * (columns) or Y_AXIS (rows). Rows or columns that don't have relative
   * layout have a Float.NaN in this array.
   */
  int[][] relative;

  /**
   * Holds the relative (%) layout information for the views along one axis.
   * The indices are relative[axis][index], where axis is either X_AXIS
   * (columns) or Y_AXIS (rows). Rows or columns that don't have relative
   * layout have a Float.NaN in this array.
   *
   * The percentage is divided by 100 so that we hold the actual fraction here.
   */
  int[][] percent;

  /**
   * The number of children in each direction.
   */
  int[] numViews;

  FrameSetView(Element el)
  {
    super(el, Y_AXIS);
    numViews = new int[2];
    absolute = new int[2][];
    relative = new int[2][];
    percent = new int[2][];
  }

  /**
   * Loads the children and places them inside the grid.
   */
  protected void loadChildren(ViewFactory f)
  {
    parseRowsCols();
    // Set up the rows.
    View[] rows = new View[numViews[Y_AXIS]];
    for (int r = 0; r < numViews[Y_AXIS]; r++)
      {
        rows[r] = new FrameSetRow(getElement(), r);
      }
    replace(0, 0, rows);
  }

  /**
   * Parses the rows and cols attributes and sets up the layout info.
   */
  private void parseRowsCols()
  {
    Element el = getElement();
    AttributeSet atts = el.getAttributes();
    String cols = (String) atts.getAttribute(HTML.Attribute.COLS);
    if (cols == null) // Defaults to '100%' when not specified.
      cols = "100%";
    parseLayout(cols, X_AXIS);
    String rows = (String) atts.getAttribute(HTML.Attribute.ROWS);
    if (rows == null) // Defaults to '100%' when not specified.
      rows = "100%";
    parseLayout(rows, Y_AXIS);
  }

  /**
   * Parses the cols or rows attribute and places the layout info in the
   * appropriate arrays.
   *
   * @param att the attributes to parse
   * @param axis the axis
   */
  private void parseLayout(String att, int axis)
  {
    StringTokenizer tokens = new StringTokenizer(att, ",");
    numViews[axis] = tokens.countTokens();
    absolute[axis] = new int[numViews[axis]];
    relative[axis] = new int[numViews[axis]];
    percent[axis] = new int[numViews[axis]];
    for (int index = 0; tokens.hasMoreTokens(); index++)
      {
        String token = tokens.nextToken();
        int p = token.indexOf('%');
        int s = token.indexOf('*');
        if (p != -1)
          {
            // Percent value.
            String number = token.substring(0, p);
            try
              {
                percent[axis][index] = Integer.parseInt(number);
              }
            catch (NumberFormatException ex)
              {
                // Leave value as 0 then.
              }
          }
        else if (s != -1)
          {
            // Star relative value.
            String number = token.substring(0, s);
            try
              {
                relative[axis][index] = Integer.parseInt(number);
              }
            catch (NumberFormatException ex)
              {
                // Leave value as 0 then.
              }
          }
        else
          {
            // Absolute value.
            try
              {
                absolute[axis][index] = Integer.parseInt(token);
              }
            catch (NumberFormatException ex)
              {
                // Leave value as 0 then.
              }
          }
      }
  }

  protected void layoutMajorAxis(int targetSpan, int axis, int[] offsets,
                                 int[] spans)
  {
    int numRows = numViews[Y_AXIS];
    int[] abs = absolute[Y_AXIS];
    int[] rel = relative[Y_AXIS];
    int[] perc = percent[Y_AXIS];
    layoutViews(targetSpan, axis, offsets, spans, numRows, abs, rel, perc);
  }

  void layoutViews(int targetSpan, int axis, int[] offsets, int[] spans,
                   int numViews, int[] abs, int[] rel, int[] perc)
  {
    // We need two passes. In the first pass we layout the absolute and
    // percent values and accumulate the needed space. In the second pass
    // the relative values are distributed and the offsets are set.
    int total = 0;
    int relTotal = 0;
    for (int i = 0; i < numViews; i++)
      {
        if (abs[i] > 0)
          {
            spans[i] = abs[i];
            total += spans[i];
          }
        else if (perc[i] > 0)
          {
            spans[i] = (targetSpan * perc[i]) / 100;
            total += spans[i];
          }
        else if (rel[i] > 0)
          {
            relTotal += rel[i];
          }
      }
    int offs = 0;
    for (int i = 0; i < numViews; i++)
      {
        if (relTotal > 0 && rel[i] > 0)
          {
            spans[i] = targetSpan * (rel[i] / relTotal);
          }
        offsets[i] = offs;
        offs += spans[i];
      }
  }
}