1 23 24 29 30 package com.sun.enterprise.tools.common.ui; 31 import com.sun.enterprise.tools.common.util.diagnostics.Reporter; 32 33 38 public class GenericTableInfo 39 { 40 public GenericTableInfo(int nc) 41 { 42 numCols = nc; 43 Reporter.assertIt(nc > 0); 45 columnNames = new String [numCols]; 46 isEditable = new boolean[numCols]; 47 48 for(int i = 0; i < numCols; i++) 49 { 50 columnNames[i] = "Column " + i; isEditable[i] = true; } 53 } 54 55 57 public GenericTableInfo(int nr, int nc) 58 { 59 numCols = nc; 60 numRows = nr; 61 Reporter.assertIt(nc > 0); Reporter.assertIt(nr >= 0); 65 data = new String [numCols][numRows]; 66 columnNames = new String [numCols]; 67 isEditable = new boolean[numCols]; 68 69 for(int i = 0; i < numCols; i++) 70 { 71 columnNames[i] = "Column " + i; isEditable[i] = true; } 74 } 75 76 78 public void setColumnName(int col, String name) 79 { 80 checkColumnNumber(col); 81 columnNames[col] = name; 82 } 83 84 86 public String getColumnName(int col) 87 { 88 checkColumnNumber(col); 89 return columnNames[col]; 90 } 91 92 94 public void setString(int row, int col, String name) 95 { 96 checkColumnNumber(col); 97 checkRowNumber(row); 98 99 data[col][row] = name; 100 } 101 102 104 public String getString(int row, int col) 105 { 106 checkColumnNumber(col); 107 checkRowNumber(row); 108 return data[col][row]; 110 } 111 112 114 public int getColumnCount() 115 { 116 return numCols; 117 } 118 119 121 public int getRowCount() 122 { 123 return numRows; 124 } 125 126 128 public void setColumnReadOnly(int c) 129 { 130 checkColumnNumber(c); 131 isEditable[c] = false; 132 } 133 134 136 public boolean isColumnEditable(int c) 137 { 138 checkColumnNumber(c); 140 return isEditable[c]; 141 } 142 143 145 public String toString() 146 { 147 String s = ""; 149 for(int c = 0; c < numCols; c++) 150 { 151 s += "Column Name " + c + ": " + columnNames[c] + "\n"; } 153 154 for(int r = 0; r < numRows; r++) 155 { 156 for(int c = 0; c < numCols; c++) 157 { 158 s += "row " + r + ", col " + c + ": " + data[c][r] + "\n"; } 160 } 161 return s; 162 } 163 164 166 private void checkColumnNumber(int col) 167 { 168 if(col < 0 || col >= numCols) 169 throw new IllegalArgumentException ("column number must be between 0 and " + (numCols - 1) + " -- attempted to use non-existant column # " + col); } 171 172 174 private void checkRowNumber(int row) 175 { 176 if(row < 0 || row >= numRows) 177 throw new IllegalArgumentException ("Row number must be between 0 and " + (numRows - 1) + " -- attempted to use non-existant row # " + row); } 179 180 182 private int numCols = 0; 183 private int numRows = 0; 184 private String [][] data = null; 185 private String [] columnNames = null; 186 private boolean[] isEditable = null; 187 188 190 public static void main(String [] args) 191 { 192 GenericTableInfo gti = new GenericTableInfo(2, 3); 193 gti.setColumnName(0, "Col 0 here!"); gti.setColumnName(1, "Col 1 here!"); 196 for(int r = 0; r < 2; r++) 197 { 198 for(int c = 0; c < 3; c++) 199 { 200 gti.setString(r, c, "c" + c + "r" + r); } 202 } 203 System.out.println("" + gti); } 205 } 206 | Popular Tags |