KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > layout > GridInfo


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.layout;
13
14 import org.eclipse.swt.widgets.Control;
15
16 /**
17  * Computes and the positions of controls in a CellLayout, based on their span.
18  */

19 class GridInfo {
20     private int cols = 0;
21
22     private int rows = 0;
23
24     private int[] gridInfo;
25
26     int[] controlRow;
27
28     int[] controlCol;
29
30     private CellData[] cellData;
31
32     Control[] controls;
33
34     /**
35      * Initialize the grid
36      *
37      * @param newControls
38      * @param cols
39      */

40     public void initGrid(Control[] newControls, CellLayout layout) {
41         cols = layout.getColumns();
42         controls = newControls;
43
44         int area = 0;
45         int totalWidth = 0;
46
47         controlRow = new int[controls.length];
48         controlCol = new int[controls.length];
49
50         // Get the CellData objects for each control, and compute
51
// the total number of cells spanned by controls in this layout.
52
cellData = new CellData[controls.length];
53         for (int idx = 0; idx < controls.length; idx++) {
54             if (controls[idx] == null) {
55                 continue;
56             }
57
58             CellData next = CellLayoutUtil.getData(controls[idx]);
59             cellData[idx] = next;
60             area += next.horizontalSpan * next.verticalSpan;
61             totalWidth += next.horizontalSpan;
62         }
63
64         // Compute the number of columns
65
if (cols == 0) {
66             cols = totalWidth;
67         }
68
69         // Compute the number of rows
70
rows = area / cols;
71         if (area % cols > 0) {
72             // Ensure we count any partial rows
73
rows++;
74         }
75
76         area = rows * cols;
77
78         // Allocate the gridInfo array
79
gridInfo = new int[area];
80         for (int idx = 0; idx < area; idx++) {
81             gridInfo[idx] = -1;
82         }
83
84         int infoIdx = 0;
85         // Compute the positions of each control
86
for (int idx = 0; idx < controls.length; idx++) {
87             CellData data = cellData[idx];
88
89             // Find an empty position to insert the control
90
while (gridInfo[infoIdx] >= 0) {
91                 infoIdx++;
92             }
93
94             controlRow[idx] = infoIdx / cols;
95             controlCol[idx] = infoIdx % cols;
96
97             // Insert the new control here
98
for (int rowIdx = 0; rowIdx < data.verticalSpan; rowIdx++) {
99                 for (int colIdx = 0; colIdx < data.horizontalSpan; colIdx++) {
100                     gridInfo[infoIdx + rowIdx * cols + colIdx] = idx;
101                 }
102             }
103
104             infoIdx += data.horizontalSpan;
105         }
106     }
107
108     public int getRows() {
109         return rows;
110     }
111
112     public int getStartPos(int control, boolean row) {
113         if (row) {
114             return controlRow[control];
115         } else {
116             return controlCol[control];
117         }
118     }
119
120     /**
121      * Returns the number of rows or columns
122      *
123      * @param isRow if true, returns the number of rows. If false, returns the number of columns
124      * @return
125      */

126     public int getNumRows(boolean isRow) {
127         if (isRow) {
128             return rows;
129         } else {
130             return cols;
131         }
132     }
133
134     public void getRow(int[] result, int rowId, boolean horizontal) {
135         if (horizontal) {
136             int prev = -1;
137             for (int colIdx = 0; colIdx < cols; colIdx++) {
138                 int next = gridInfo[cols * rowId + colIdx];
139
140                 if (prev == next) {
141                     result[colIdx] = -1;
142                 } else {
143                     result[colIdx] = next;
144                 }
145
146                 prev = next;
147             }
148         } else {
149             int prev = -1;
150             for (int rowIdx = 0; rowIdx < rows; rowIdx++) {
151                 int next = gridInfo[cols * rowIdx + rowId];
152
153                 if (prev == next) {
154                     result[rowIdx] = -1;
155                 } else {
156                     result[rowIdx] = next;
157                 }
158
159                 prev = next;
160             }
161         }
162     }
163
164     public CellData getCellData(int controlId) {
165         return cellData[controlId];
166     }
167
168     public int getCols() {
169         return cols;
170     }
171 }
172
Popular Tags