View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2007 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // GenomeBrowser Project
18  //
19  // TableData.java
20  // Since: Jun 25, 2007
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.client.util;
26  
27  import java.util.HashMap;
28  import java.util.Set;
29  
30  import org.utgenome.gwt.utgb.client.UTGBClientException;
31  
32  import com.google.gwt.core.client.GWT;
33  
34  /**
35   * TableData holds table data, the key of which is a column with a name "id"
36   * 
37   * @author leo
38   *
39   */
40  public class TableData {
41  	
42  	private String[] _columnName;
43  	private HashMap<Object, Object[]> _rowTable = new HashMap<Object, Object[]>();
44  	
45  	public final static String DEFAULT_KEY_COLUMN_NAME = "id"; 
46  	private int _keyColumn = -1;
47  	
48  	public TableData(String[] columnName)
49  	{
50  		this._columnName = columnName;
51  		// search key column 
52  		for(int i=0; i<_columnName.length; i++)
53  		{
54  			if(_columnName[i].equals(DEFAULT_KEY_COLUMN_NAME))
55  				_keyColumn = i;
56  		}
57  
58  		verifyKeyColumn();
59  	}
60  	
61  	public TableData(String[] columnName, int keyColumn)
62  	{
63  		this._columnName = columnName;
64  		_keyColumn = keyColumn;
65  		verifyKeyColumn();
66  	}
67  	
68  	private void verifyKeyColumn()
69  	{
70  		if(_keyColumn < 0 || _keyColumn >= _columnName.length)
71  		{
72  			GWT.log("key column is not found in " + _columnName, new UTGBClientException());
73  			_keyColumn = 0; // default
74  		}
75  	}
76  	
77  	
78  	
79  	/**
80  	 * @param row
81  	 * @return key
82  	 */
83  	public Object addRow(Object[] row)
84  	{
85  		Object key = row[_keyColumn];
86  		_rowTable.put(key, row);
87  		return key;
88  	}
89  	
90  	/**
91  	 * key set of Integer  
92  	 * @return
93  	 */
94  	public Set<Object> keySet()
95  	{
96  		return _rowTable.keySet();
97  	}
98  	
99  	public Object[] getRow(Object key)
100 	{
101 		return _rowTable.get(key);
102 	}
103 	
104 	public int getColumnCount()
105 	{
106 		return _columnName.length;
107 	}
108 	
109 	
110 	public String[] getColumnLabel()
111 	{
112 		return _columnName;
113 	}
114 	public String getColumnLabel(int column)
115 	{
116 		return _columnName[column];
117 	}
118 
119 	public void clear() {
120 		_rowTable.clear();
121 	}
122 
123 	public int getKeyColumn() {
124 		return _keyColumn;
125 	}
126 	
127 }
128 
129 
130 
131