1 package org.jahia.layout; 2 3 28 import java.util.Vector ; 29 30 public class Grid2D { 31 34 private Vector rowList = new Vector (); 35 private int rowCount = 0; 36 private int columnCount = 0; 37 private int maxRows = -1; 40 private int maxColumns = -1; 41 42 46 private class DummyGridEmptyObject { 47 } 48 49 52 public Grid2D() { 53 } 54 55 61 public Grid2D(int maxRowCount, int maxColumnCount) { 62 maxRows = maxRowCount; 63 maxColumns = maxColumnCount; 64 } 65 66 70 public int getRowCount() { 71 return rowCount; 72 } 73 74 78 public int getColumnCount() { 79 return columnCount; 80 } 81 82 91 private void addRows(int numberOfRows) 92 throws ArrayIndexOutOfBoundsException { 93 if (maxRows >= 0) { 94 if (rowCount + numberOfRows > maxRows) { 95 throw new ArrayIndexOutOfBoundsException ("Grid2D.addRows : number of rows to add would exceed maximum number of rows !"); 96 } 97 } 98 for (int i=0; i < numberOfRows; i++) { 99 Vector curVector = new Vector (); 100 for (int j=0; j < columnCount; j++) { 102 curVector.add(new DummyGridEmptyObject()); 103 } 104 rowList.add(curVector); 105 } 106 rowCount += numberOfRows; 107 } 108 109 118 private void addColumns(int numberOfColumns) 119 throws ArrayIndexOutOfBoundsException { 120 if (maxColumns >= 0) { 121 if (columnCount + numberOfColumns > maxColumns) { 122 throw new ArrayIndexOutOfBoundsException ("Grid2D.addColumns : number of columns to add would exceed maximum number of columns !"); 123 } 124 } 125 for (int i=0; i < rowCount; i++) { 127 Vector curVector = (Vector ) rowList.elementAt(i); 128 for (int j=0; j < numberOfColumns; j++) { 130 curVector.add(new DummyGridEmptyObject()); 131 } 132 } 133 columnCount += numberOfColumns; 134 } 135 136 147 public Object getElementAt(int row, int column) 148 throws ArrayIndexOutOfBoundsException { 149 if ((row < 0) || (row >= rowCount) ) { 150 throw new ArrayIndexOutOfBoundsException ("Grid2D.elementAt : row value " + 151 Integer.toString(row) + 152 " is invalid ! (rowCount=" + 153 Integer.toString(rowCount) + ")"); 154 } 155 if ((column < 0) || (column >= columnCount) ) { 156 throw new ArrayIndexOutOfBoundsException ("Grid2D.elementAt : column value " + 157 Integer.toString(column) + 158 " is invalid ! (rowCount=" + 159 Integer.toString(rowCount) + ")"); 160 } 161 Vector rowVector = (Vector ) rowList.elementAt(row); 162 Object theObject = rowVector.elementAt(column); 163 if (theObject instanceof DummyGridEmptyObject) { 164 return null; 165 } else { 166 return theObject; 167 } 168 } 169 170 187 public void setElementAt(int row, int column, Object theObject) 188 throws ArrayIndexOutOfBoundsException { 189 if ( (row+1) > rowCount ) { 191 addRows(row+1-rowCount); 192 } 193 if ( (column+1) > columnCount ) { 194 addColumns(column+1-columnCount); 195 } 196 Vector rowVector = (Vector ) rowList.elementAt(row); 198 rowVector.setElementAt(theObject, column); 199 } 200 201 209 public void setEmptyElementAt(int row, int column) { 210 setElementAt(row, column, new DummyGridEmptyObject() ); 211 } 212 213 216 public void clear() { 217 rowList.clear(); 218 rowList = null; 219 rowList = new Vector (); 220 rowCount = 0; 221 columnCount = 0; 222 } 223 224 231 public String toString() { 232 StringBuffer result = new StringBuffer (""); 233 for (int i = 0; i < getRowCount() ; i++) { 234 for (int j = 0; j < getColumnCount(); j++) { 235 Object curObject = getElementAt(i, j); 236 if (curObject == null) { 237 result.append("<null>"); 238 } else { 239 result.append(curObject.toString()); 240 } 241 result.append(" "); 242 } 243 result.append('\n'); 244 } 245 return result.toString(); 246 } 247 248 } | Popular Tags |