001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.layout;
011:
012: import org.eclipse.swt.widgets.Control;
013:
014: /**
015: * Computes and the positions of controls in a CellLayout, based on their span.
016: */
017: class GridInfo {
018: private int cols = 0;
019:
020: private int rows = 0;
021:
022: private int[] gridInfo;
023:
024: int[] controlRow;
025:
026: int[] controlCol;
027:
028: private CellData[] cellData;
029:
030: Control[] controls;
031:
032: /**
033: * Initialize the grid
034: *
035: * @param newControls
036: * @param cols
037: */
038: public void initGrid(Control[] newControls, CellLayout layout) {
039: cols = layout.getColumns();
040: controls = newControls;
041:
042: int area = 0;
043: int totalWidth = 0;
044:
045: controlRow = new int[controls.length];
046: controlCol = new int[controls.length];
047:
048: // Get the CellData objects for each control, and compute
049: // the total number of cells spanned by controls in this layout.
050: cellData = new CellData[controls.length];
051: for (int idx = 0; idx < controls.length; idx++) {
052: if (controls[idx] == null) {
053: continue;
054: }
055:
056: CellData next = CellLayoutUtil.getData(controls[idx]);
057: cellData[idx] = next;
058: area += next.horizontalSpan * next.verticalSpan;
059: totalWidth += next.horizontalSpan;
060: }
061:
062: // Compute the number of columns
063: if (cols == 0) {
064: cols = totalWidth;
065: }
066:
067: // Compute the number of rows
068: rows = area / cols;
069: if (area % cols > 0) {
070: // Ensure we count any partial rows
071: rows++;
072: }
073:
074: area = rows * cols;
075:
076: // Allocate the gridInfo array
077: gridInfo = new int[area];
078: for (int idx = 0; idx < area; idx++) {
079: gridInfo[idx] = -1;
080: }
081:
082: int infoIdx = 0;
083: // Compute the positions of each control
084: for (int idx = 0; idx < controls.length; idx++) {
085: CellData data = cellData[idx];
086:
087: // Find an empty position to insert the control
088: while (gridInfo[infoIdx] >= 0) {
089: infoIdx++;
090: }
091:
092: controlRow[idx] = infoIdx / cols;
093: controlCol[idx] = infoIdx % cols;
094:
095: // Insert the new control here
096: for (int rowIdx = 0; rowIdx < data.verticalSpan; rowIdx++) {
097: for (int colIdx = 0; colIdx < data.horizontalSpan; colIdx++) {
098: gridInfo[infoIdx + rowIdx * cols + colIdx] = idx;
099: }
100: }
101:
102: infoIdx += data.horizontalSpan;
103: }
104: }
105:
106: public int getRows() {
107: return rows;
108: }
109:
110: public int getStartPos(int control, boolean row) {
111: if (row) {
112: return controlRow[control];
113: } else {
114: return controlCol[control];
115: }
116: }
117:
118: /**
119: * Returns the number of rows or columns
120: *
121: * @param isRow if true, returns the number of rows. If false, returns the number of columns
122: * @return
123: */
124: public int getNumRows(boolean isRow) {
125: if (isRow) {
126: return rows;
127: } else {
128: return cols;
129: }
130: }
131:
132: public void getRow(int[] result, int rowId, boolean horizontal) {
133: if (horizontal) {
134: int prev = -1;
135: for (int colIdx = 0; colIdx < cols; colIdx++) {
136: int next = gridInfo[cols * rowId + colIdx];
137:
138: if (prev == next) {
139: result[colIdx] = -1;
140: } else {
141: result[colIdx] = next;
142: }
143:
144: prev = next;
145: }
146: } else {
147: int prev = -1;
148: for (int rowIdx = 0; rowIdx < rows; rowIdx++) {
149: int next = gridInfo[cols * rowIdx + rowId];
150:
151: if (prev == next) {
152: result[rowIdx] = -1;
153: } else {
154: result[rowIdx] = next;
155: }
156:
157: prev = next;
158: }
159: }
160: }
161:
162: public CellData getCellData(int controlId) {
163: return cellData[controlId];
164: }
165:
166: public int getCols() {
167: return cols;
168: }
169: }
|