KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > common > ui > GenericTableInfo


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * GenericTableInfo.java
26  *
27  * Created on February 21, 2001, 1:18 AM
28  */

29
30 package com.sun.enterprise.tools.common.ui;
31 import com.sun.enterprise.tools.common.util.diagnostics.Reporter;
32
33 /**
34  *
35  * @author bnevins
36  * @version
37  */

38 public class GenericTableInfo
39 {
40     public GenericTableInfo(int nc)
41     {
42         numCols = nc;
43         Reporter.assertIt(nc > 0); //NOI18N
44

45         columnNames = new String JavaDoc[numCols];
46         isEditable = new boolean[numCols];
47         
48         for(int i = 0; i < numCols; i++)
49         {
50             columnNames[i] = "Column " + i;//NOI18N // just to have a default...
51
isEditable[i] = true; // by default -- all cells are editable...
52
}
53     }
54     
55     ////////////////////////////////////////////////////////////
56

57     public GenericTableInfo(int nr, int nc)
58     {
59         numCols = nc;
60         numRows = nr;
61         // no columns -- bad! no rows -- OK!
62
Reporter.assertIt(nc > 0); //NOI18N
63
Reporter.assertIt(nr >= 0); //NOI18N
64

65         data = new String JavaDoc[numCols][numRows];
66         columnNames = new String JavaDoc[numCols];
67         isEditable = new boolean[numCols];
68         
69         for(int i = 0; i < numCols; i++)
70         {
71             columnNames[i] = "Column " + i;//NOI18N // just to have a default...
72
isEditable[i] = true; // by default -- all cells are editable...
73
}
74     }
75     
76     /////////////////////////////////////////////////
77

78     public void setColumnName(int col, String JavaDoc name)
79     {
80         checkColumnNumber(col);
81         columnNames[col] = name;
82     }
83     
84     /////////////////////////////////////////////////
85

86     public String JavaDoc getColumnName(int col)
87     {
88         checkColumnNumber(col);
89         return columnNames[col];
90     }
91     
92     /////////////////////////////////////////////////
93

94     public void setString(int row, int col, String JavaDoc name)
95     {
96         checkColumnNumber(col);
97         checkRowNumber(row);
98         
99         data[col][row] = name;
100     }
101     
102     /////////////////////////////////////////////////
103

104     public String JavaDoc getString(int row, int col)
105     {
106         checkColumnNumber(col);
107         checkRowNumber(row);
108         //System.out.println("getString[col=" + col + "][r=" + row + "]: " + data[col][row]);//NOI18N
109
return data[col][row];
110     }
111     
112     /////////////////////////////////////////////////
113

114     public int getColumnCount()
115     {
116         return numCols;
117     }
118     
119     /////////////////////////////////////////////////
120

121     public int getRowCount()
122     {
123         return numRows;
124     }
125     
126     //////////////////////////////////////////////////////////////
127

128     public void setColumnReadOnly(int c)
129     {
130         checkColumnNumber(c);
131         isEditable[c] = false;
132     }
133     
134     //////////////////////////////////////////////////////////////
135

136     public boolean isColumnEditable(int c)
137     {
138     /// System.out.println("isColEditable for Column " + c + " --- " + isEditable[c]);//NOI18N
139
checkColumnNumber(c);
140         return isEditable[c];
141     }
142     
143     /////////////////////////////////////////////////
144

145     public String JavaDoc toString()
146     {
147         String JavaDoc s = "";//NOI18N
148

149         for(int c = 0; c < numCols; c++)
150         {
151             s += "Column Name " + c + ": " + columnNames[c] + "\n";//NOI18N
152
}
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";//NOI18N
159
}
160         }
161         return s;
162     }
163     
164     /////////////////////////////////////////////////
165

166     private void checkColumnNumber(int col)
167     {
168         if(col < 0 || col >= numCols)
169             throw new IllegalArgumentException JavaDoc("column number must be between 0 and " + (numCols - 1) + " -- attempted to use non-existant column # " + col);//NOI18N
170
}
171     
172     /////////////////////////////////////////////////
173

174     private void checkRowNumber(int row)
175     {
176         if(row < 0 || row >= numRows)
177             throw new IllegalArgumentException JavaDoc("Row number must be between 0 and " + (numRows - 1) + " -- attempted to use non-existant row # " + row);//NOI18N
178
}
179     
180     /////////////////////////////////////////////////
181

182     private int numCols = 0;
183     private int numRows = 0;
184     private String JavaDoc[][] data = null;
185     private String JavaDoc[] columnNames = null;
186     private boolean[] isEditable = null;
187     
188     /////////////////////////////////////////////////
189

190     public static void main(String JavaDoc[] args)
191     {
192         GenericTableInfo gti = new GenericTableInfo(2, 3);
193         gti.setColumnName(0, "Col 0 here!");//NOI18N
194
gti.setColumnName(1, "Col 1 here!");//NOI18N
195

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);//NOI18N
201
}
202         }
203         System.out.println("" + gti);//NOI18N
204
}
205 }
206
Popular Tags