File: NewspaperLayout.java

package info (click to toggle)
libswidgets-java 0.1.4-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny, squeeze
  • size: 356 kB
  • ctags: 667
  • sloc: java: 3,436; xml: 64; makefile: 11
file content (282 lines) | stat: -rw-r--r-- 9,632 bytes parent folder | download
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
// $Id: NewspaperLayout.java,v 1.1 2004/11/03 18:30:05 bobtarling Exp $

package org.tigris.swidgets;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Rectangle;

import javax.swing.JComponent;

/**
 * Similar to {@link GridLayout2} but once the components fill
 * the height of the container they flow into another grid on the
 * right until the full width of the container is filled. Once the
 * containers width is full it flows to the right no more, the grid
 * depths increase instead so that the user scrolls up/down instead of
 * left/right.
 *
 * @author Bob Tarling
 */
public class NewspaperLayout extends GridLayout2 {

    private int gridGap = 0;
    private int preferredX;
    private int preferredY;

    private int gridWidth;

    /**
     * The constructor.
     */
    public NewspaperLayout() {
        this(1, 0, 0, 0, 0);
    }

    /**
     * The constructor.
     * 
     * @param rows the number of rows
     * @param cols the number of columns
     */
    public NewspaperLayout(int rows, int cols) {
        this(rows, cols, 0, 0, 0);
    }

    /**
     * The constructor.
     * 
     * @param rows the number of rows
     * @param cols the number of columns
     * @param hgap the horizontal gap
     * @param vgap the vertical gap
     * @param gg the grid gap
     */
    public NewspaperLayout(int rows, int cols, int hgap, int vgap, int gg)
    {
        super(rows, cols, hgap, vgap, ROWCOLPREFERRED, NONE, NORTHWEST);
        this.gridGap = gg;
    }

    /**
     * @see java.awt.LayoutManager#addLayoutComponent(java.lang.String, 
     * java.awt.Component)
     */
    public void addLayoutComponent(String name, Component comp) {
    }

    /**
     * @see java.awt.LayoutManager#removeLayoutComponent(java.awt.Component)
     */
    public void removeLayoutComponent(Component comp) {
    }

    /**
     * @see java.awt.LayoutManager#preferredLayoutSize(java.awt.Container)
     */
    public Dimension preferredLayoutSize(Container parent) {
        JComponent comp = (JComponent) parent;
        Rectangle rect = comp.getVisibleRect();
        //preferredX = (int) rect.getWidth();
        Insets insets = parent.getInsets();
        layoutContainer(parent);
        if (preferredX < insets.right + gridWidth + insets.left)
	    preferredX = insets.right + gridWidth + insets.left;
        return new Dimension(preferredX, preferredY);
    }

    /**
     * @see java.awt.LayoutManager#minimumLayoutSize(java.awt.Container)
     */
    public Dimension minimumLayoutSize(Container parent) {
        Insets insets = parent.getInsets();
        return new Dimension(insets.right + gridWidth + insets.left, 0);
    }

    /**
     * TODO: This is never used, and not part of the interface LayoutContainer.
     * @param parent the container
     * @return the dimension
     */
    public Dimension maximumLayoutSize(Container parent) {
        return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
    }

    /**
     * @see java.awt.LayoutManager#layoutContainer(java.awt.Container)
     */
    public void layoutContainer(Container parent) {
        synchronized (parent.getTreeLock()) {
            int ncomponents = parent.getComponentCount();
            if (ncomponents == 0) {
                return;
            }
            Insets insets = parent.getInsets();
            int nrows = this.getRows();
            int ncols = this.getColumns();

            if (nrows > 0) {
                ncols = (ncomponents + nrows - 1) / nrows;
            } else {
                nrows = (ncomponents + ncols - 1) / ncols;
            }

            // Determine the width for each column and the height for each row.
            setColWidth(new int[ncols]);
            setRowHeight(new int[nrows]);
            setLargestWidth(0);
            setLargestHeight(0);

            if (getCellSizing() == FITPARENT) {
                int availableWidth =
		    parent.getWidth()
		    - (insets.left + insets.right + (ncols - 1) * getHgap());
                int availableHeight =
		    parent.getHeight()
		    - (insets.top + insets.bottom + (nrows - 1) * getVgap());
                setLargestWidth(availableWidth / ncols);
                setLargestHeight(availableHeight / nrows);
            }
            else {

                for (int c = 0; c < ncols; ++c) {
                    for (int r = 0; r < nrows; ++r) {
                        int i = r * ncols + c;
                        if (parent.getComponent(i).getPreferredSize().getWidth()
                                > getColWidth()[c]) {
                            getColWidth()[c] = (int) parent.getComponent(i)
				    .getPreferredSize().getWidth();
                            if (getColWidth()[c] > getLargestWidth())
				setLargestWidth(getColWidth()[c]);
                        }
                        if ((parent.getComponent(i).getPreferredSize()
                                .getHeight()) > getRowHeight()[r]) {
                            getRowHeight()[r] = (int) parent.getComponent(i)
				    .getPreferredSize().getHeight();
                            if (getRowHeight()[r] > getLargestHeight()) {
                                setLargestHeight(getRowHeight()[r]);
			    }
                        }
                    }
                }
            }

            // Calculate width
            gridWidth = (ncols - 1) * getHgap();
            for (int c = 0; c < ncols; ++c) {
                gridWidth += getColWidth()[c];
            }

            // Calculate Height
            int gridHeight = (nrows - 1) * getVgap();
            for (int r = 0; r < nrows; ++r) {
                gridHeight += getRowHeight()[r];
            }

            int numberOfGrids =
		positionComponentsInternal(parent, getColWidth(), 
		    getRowHeight(), gridHeight, nrows, ncols);
            if (numberOfGrids > 0) {
                positionComponentsExternal(parent, getColWidth(), 
                    getRowHeight(), gridHeight, nrows, ncols, numberOfGrids);
            }
        }
    }

    private int positionComponentsInternal(Container parent,
					   int colWidth[], int rowHeight[],
					   int gridHeight, int nrows, int ncols)
    {
        JComponent parentComp = (JComponent) parent;
        int visibleHeight = (int) parentComp.getVisibleRect().getHeight();
        int visibleWidth = (int) parentComp.getVisibleRect().getWidth();
        int ncomponents = parent.getComponentCount();
        Insets insets = parent.getInsets();
        int newsColumn = 0;
        int highestY = 0;
        int y = insets.top;
        int cellHeight;
        int cellWidth;
        for (int r = 0; r < nrows; ++r) {

            cellHeight = getComponentCellHeight(r);

            if (y + cellHeight + insets.bottom > visibleHeight ) {
                y = insets.top;
                newsColumn++;
                if ((insets.left
		     + insets.right
		     + newsColumn * (gridWidth + gridGap)
		     + gridWidth)
		    > visibleWidth)
		    return newsColumn;
            }

            int x = insets.left + newsColumn * (gridWidth + gridGap);
            for (int c = 0; c < ncols; ++c) {
                cellWidth = getComponentCellWidth(c);

                int i = r * ncols + c;
                if (i < ncomponents) {
                    positionComponentInCell(parent.getComponent(i), x, y,
					    cellWidth, cellHeight);
                    if (y + cellHeight > highestY) highestY = y + cellHeight;
                }
                x += cellWidth + getHgap();
            }
            y += cellHeight + getVgap();
        }
        preferredY = highestY + insets.bottom;
        return -1;
    }


    private boolean positionComponentsExternal(Container parent,
					       int colWidth[], int rowHeight[],
					       int gridHeight,
					       int nrows, int ncols,
					       int maxGrids) {
        int ncomponents = parent.getComponentCount();
        Insets insets = parent.getInsets();
        int newsColumn = 0;
        int targetHeight = gridHeight / maxGrids;
        int highestY = 0;
        int y = insets.top;
        int componentCellHeight;
        int componentCellWidth;
        for (int r = 0; r < nrows; ++r) {
            if (getCellSizing() != ROWCOLPREFERRED)
		componentCellHeight = getLargestHeight();
            else componentCellHeight = rowHeight[r];

            int x = insets.left + newsColumn * (gridWidth + gridGap);
            for (int c = 0; c < ncols; ++c) {
                if (getCellSizing() != ROWCOLPREFERRED)
		    componentCellWidth = getLargestWidth();
                else componentCellWidth = colWidth[c];

                int i = r * ncols + c;
                if (i < ncomponents) {
                    positionComponentInCell(parent.getComponent(i),
					    x, y,
					    componentCellWidth,
					    componentCellHeight);
                    if (y + componentCellHeight > highestY)
			highestY = y + componentCellHeight;
                }
                x += componentCellWidth + getHgap();
            }
            y += componentCellHeight + getVgap();
            if (y >= targetHeight + insets.top) {
                y = insets.top;
                newsColumn++;
            }
        }
        preferredY = highestY + insets.bottom;

        return true;
    }
}