1 11 12 package org.eclipse.ui.internal.layout; 13 14 import org.eclipse.swt.widgets.Control; 15 16 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 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 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 if (cols == 0) { 66 cols = totalWidth; 67 } 68 69 rows = area / cols; 71 if (area % cols > 0) { 72 rows++; 74 } 75 76 area = rows * cols; 77 78 gridInfo = new int[area]; 80 for (int idx = 0; idx < area; idx++) { 81 gridInfo[idx] = -1; 82 } 83 84 int infoIdx = 0; 85 for (int idx = 0; idx < controls.length; idx++) { 87 CellData data = cellData[idx]; 88 89 while (gridInfo[infoIdx] >= 0) { 91 infoIdx++; 92 } 93 94 controlRow[idx] = infoIdx / cols; 95 controlCol[idx] = infoIdx % cols; 96 97 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 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 |