View Javadoc

1   /*
2    * Copyright (c) 2003-2008 by Cosylab d. d.
3    *
4    * This file is part of CosyBeans-Common.
5    *
6    * CosyBeans-Common is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * CosyBeans-Common is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with CosyBeans-Common.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  
20  package com.cosylab.gui.components.table;
21  
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.List;
25  
26  
27  /**
28   * <code>TableRowArray</code> is support  implementation of
29   * <code>TableRow</code> build around <code>ArrayList</code>.
30   *
31   * @author <a href="mailto:igor.kriznar@cosylab.com">Igor Kriznar</a>
32   * @version $Id: TableRowArray.java,v 1.7 2008-04-22 12:28:40 jbobnar Exp $
33   *
34   * @since May 5, 2004.
35   */
36  public class TableRowArray implements TableRow
37  {
38  	protected List<Object> values;
39  	protected TableRowModel parentModel;
40  
41  	/**
42  	 * Creates new instance.
43  	 */
44  	public TableRowArray()
45  	{
46  		super();
47  		values = new ArrayList<Object>();
48  	}
49  
50  	/**
51  	 * Creates new instance.
52  	 *
53  	 * @param l values as array
54  	 */
55  	public TableRowArray(Object[] l)
56  	{
57  		super();
58  		values = Arrays.asList(l);
59  	}
60  
61  	/**
62  	 * Creates new instance.
63  	 *
64  	 * @param l values as List
65  	 */
66  	public TableRowArray(List<Object> l)
67  	{
68  		super();
69  		values = l;
70  	}
71  
72  	/* (non-Javadoc)
73  	 * @see com.cosylab.gui.components.table.TableRow#getValueAt(int)
74  	 */
75  	public Object getValueAt(int column)
76  	{
77  		return values.get(column);
78  	}
79  
80  	/* (non-Javadoc)
81  	 * @see com.cosylab.gui.components.table.TableRow#getValueCount()
82  	 */
83  	public int getValueCount()
84  	{
85  		return values.size();
86  	}
87  
88  	/* (non-Javadoc)
89  	 * @see com.cosylab.gui.components.table.TableRow#setParentModel(com.cosylab.gui.components.table.TableRowModel)
90  	 */
91  	public void setParentModel(TableRowModel model)
92  	{
93  		parentModel = model;
94  	}
95  
96  	/* (non-Javadoc)
97  	 * @see com.cosylab.gui.components.table.TableRow#getParentModel()
98  	 */
99  	public TableRowModel getParentModel()
100 	{
101 		return parentModel;
102 	}
103 
104 	/**
105 	 * Adds new value to the end of this row.
106 	 *
107 	 * @param value the value added to the end of this row
108 	 */
109 	public void addValue(Object value)
110 	{
111 		values.add(value);
112 	}
113 
114 	/**
115 	 * Inserts new value to this row at specified position.
116 	 *
117 	 * @param index where to insert value
118 	 * @param value new inserted value
119 	 */
120 	public void addValue(int index, Object value)
121 	{
122 		values.add(index, value);
123 	}
124 
125 	/**
126 	 * Replaces value at specified index.
127 	 *
128 	 * @param index where value is set
129 	 * @param newValue new value
130 	 *
131 	 * @return old value at index
132 	 */
133 	public Object setValue(int index, Object newValue)
134 	{
135 		return values.set(index, newValue);
136 	}
137 
138 	/*
139 	 * Fires update event for entire row.
140 	 */
141 	protected void fireUpdate()
142 	{
143 		fireUpdate(TableRowModelEvent.ALL_VALUES);
144 	}
145 
146 	/*
147 	 * Fires update event for value in row.
148 	 */
149 	protected void fireUpdate(int index)
150 	{
151 		if (parentModel != null) {
152 			parentModel.fireRowUpdate(this, index);
153 		}
154 	}
155 
156 	/* (non-Javadoc)
157 	 * @see com.cosylab.gui.components.table.TableRow#isEditable(int)
158 	 */
159 	public boolean isValueSettable(int index)
160 	{
161 		return false;
162 	}
163 
164 	/* (non-Javadoc)
165 	 * @see com.cosylab.gui.components.table.TableRow#compateTo(com.cosylab.gui.components.table.TableRow, int)
166 	 */
167 	public int compareTo(TableRow row, int valueIndex)
168 	{
169 		Object o1 = getValueAt(valueIndex);
170 		Object o2 = row.getValueAt(valueIndex);
171 
172 		if (o1 instanceof Comparable) {
173 			return ((Comparable)o1).compareTo(o2);
174 		}
175 
176 		return 0;
177 	}
178 }
179 
180 /* __oOo__ */