litesoft
@ 947
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 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.ui.def.nonpublic.support; import org.litesoft.ui.def.*; import org.litesoft.ui.def.nonpublic.*; public class ComponentGrid { private int mColumns; private Row[] mRows; private Row getRow( int pRow ) { return ((0 <= pRow) && (pRow < mRows.length)) ? mRows[pRow] : Row.NULL; } public ComponentGrid( int pColumns, UiDef[] pComponents ) { mColumns = pColumns; createRows( pComponents.length ); ComponentSource cs = new ComponentSource( pComponents ); for ( int r = 0; r < mRows.length; r++ ) { fillRow( r, cs ); } validateNoOverlappingSpans(); } private void createRows( int pComponents ) { int rows = (pComponents + (mColumns - 1)) / mColumns; mRows = new Row[rows]; for ( int i = 0; i < mRows.length; i++ ) { mRows[i] = new Row( mColumns ); } } private void fillRow( int pRow, ComponentSource pComponentSource ) { ComponentGridCell[] cells = getRow( pRow ).mCells; boolean allSpans = true; for ( int col = 0; col < cells.length; col++ ) { UiDef comp = pComponentSource.get(); ComponentGridCell cell = new ComponentGridCell( pRow, col, comp ); cells[col] = cell; if ( comp instanceof UiGridSpanFromLeftDef ) { cell.setSpanedFromLeft( this ); } else if ( comp instanceof UiGridSpanFromAboveDef ) { cell.setSpanedFromAbove( this ); } else { allSpans = false; } } if ( allSpans ) { throw new IllegalStateException( "Row (" + pRow + ") is nothing but Spans" ); } } public int getColumns() { return mColumns; } public int getRows() { return mRows.length; } public ComponentGridCell getCell( int pRow, int pCol ) { return getRow( pRow ).getCell( pCol ); } private static class ComponentSource { private int mIndex = 0; private UiDef[] mComponents; public ComponentSource( UiDef[] pComponents ) { mComponents = pComponents; } public UiDef get() { if ( mIndex < mComponents.length ) { return mComponents[mIndex++]; } return new UiSpacerDef(); } } private static class Row { public static final Row NULL = new Row( 0 ); public ComponentGridCell[] mCells; public Row( int pColumns ) { mCells = new ComponentGridCell[pColumns]; } public ComponentGridCell getCell( int pColumn ) { return ((0 <= pColumn) && (pColumn < mCells.length)) ? mCells[pColumn] : null; } } private void validateNoOverlappingSpans() { for ( int r1 = 0; r1 < mRows.length; r1++ ) { for ( int c1 = 0; c1 < mColumns; c1++ ) { ComponentGridCell cell = getCell( r1, c1 ); for ( int c2 = c1 + 1; c2 < mColumns; c2++ ) { chkOverlaps( cell, r1, c2 ); } for ( int r2 = r1 + 1; r2 < mRows.length; r2++ ) { for ( int c2 = 0; c2 < mColumns; c2++ ) { chkOverlaps( cell, r2, c2 ); } } } } } private void chkOverlaps( ComponentGridCell pCell, int pRow, int pCol ) { ComponentGridCell secondCell = getCell( pRow, pCol ); if ( secondCell.overlapsWith( pCell ) ) { throw new IllegalStateException( secondCell + " overlaps with " + pCell ); } } } |