1
2
3
4
5
6
7
8
9
10 package org.gscg.common.gui.statistics;
11
12 import info.clearthought.layout.TableLayout;
13 import info.clearthought.layout.TableLayoutConstants;
14
15 import java.awt.Component;
16 import java.awt.Point;
17
18 import javax.swing.JPanel;
19 import javax.swing.table.DefaultTableModel;
20
21
22 /***
23 * The tablePanel provides an easy to access table. <br>
24 * <p>
25 * (c) copyright 2005 <a href="http://www.simulation.tudelft.nl">Delft
26 * University of Technology </a>, the Netherlands. <br>
27 * See for project information <a
28 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
29 *
30 * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
31 * Delft, the Netherlands. All rights reserved.
32 *
33 * See for project information <a href="http://www.simulation.tudelft.nl/">
34 * www.simulation.tudelft.nl </a>.
35 *
36 * This software is proprietary information of Delft University of Technology
37 * The code is published under the Lesser General Public License
38 *
39 * @author <a href="http://www.tbm.tudelft.nl/webstaf/peterja/index.htm">Peter
40 * Jacobs </a>
41 * @version $Revision: 1.1 $ $Date: 2005/06/16 12:34:00 $
42 * @since 1.1.3
43 */
44 public class TablePanel extends JPanel
45 {
46 /*** the serial version uid */
47 private static final long serialVersionUID = 11L;
48
49 /*** the content of the tablePanel */
50 private DefaultTableModel content = null;
51
52 /***
53 * Constructor for TablePanel.
54 *
55 * @param columns the amount of columns
56 * @param rows the amount of rows
57 */
58 public TablePanel(final int rows, final int columns)
59 {
60 super();
61 this.content = new DefaultTableModel(rows, columns);
62 }
63
64 /***
65 * adds a row to the tablePanel
66 */
67 public void addRow()
68 {
69 this.content.addRow((Object[]) null);
70 this.validate();
71 }
72
73 /***
74 * adds a column to the tablePanel
75 */
76 public void addColumn()
77 {
78 this.content.addColumn((Object[]) null);
79 this.validate();
80 }
81
82 /***
83 * deletes a row from the contentpanel
84 */
85 public void deleteRow()
86 {
87 if (this.content.getRowCount() == 1)
88 {
89 return;
90 }
91 this.content.removeRow(this.content.getRowCount() - 1);
92 this.validate();
93 }
94
95 /***
96 * deletes a column from the tablePanel
97 */
98 public void deleteColumn()
99 {
100 if (this.content.getColumnCount() == 1)
101 {
102 return;
103 }
104 DefaultTableModel result = new DefaultTableModel(this.content
105 .getRowCount(), this.content.getColumnCount() - 1);
106 for (int rows = 0; rows < this.content.getRowCount(); rows++)
107 {
108 for (int cols = 0; cols < this.content.getColumnCount() - 1; cols++)
109 {
110 result.setValueAt(this.content.getValueAt(rows, cols), rows,
111 cols);
112 }
113 }
114 this.content = result;
115 this.validate();
116 }
117
118 /***
119 * returns the row of a specific point
120 *
121 * @param point the xy-point
122 * @return int the row
123 */
124 public int getRow(final Point point)
125 {
126 double heigth = this.getHeight() / (double) this.getRows();
127 return (int) Math.floor(point.getY() / heigth);
128 }
129
130 /***
131 * returns the column of a point on the panel
132 *
133 * @param point the x-y point
134 * @return int the column
135 */
136 public int getColumn(final Point point)
137 {
138 double width = this.getWidth() / (double) this.getColumns();
139 return (int) Math.floor(point.getX() / width);
140 }
141
142 /***
143 * Method setCell.
144 *
145 * @param container the subject of the cell
146 * @param row of the cell
147 * @param column of the cell
148 */
149 public void setCell(final Component container, final int row,
150 final int column)
151 {
152 this.content.setValueAt(container, row, column);
153 this.validate();
154 }
155
156 /***
157 * contains this container
158 *
159 * @param container contains the container..
160 * @return boolean
161 */
162 public boolean contains(final Component container)
163 {
164 for (int c = 0; c < this.getColumns(); c++)
165 {
166 for (int r = 0; r < this.getRows(); r++)
167 {
168 if (this.getCell(r, c).equals(container))
169 {
170 return true;
171 }
172 }
173 }
174 return false;
175 }
176
177 /***
178 * gets the container in a cell
179 *
180 * @param row the row
181 * @param column the column
182 * @return Component
183 */
184 public Component getCell(final int row, final int column)
185 {
186 double height = this.getHeight() / (double) this.getRows();
187 double width = this.getWidth() / (double) this.getColumns();
188 Point point = new Point((int) Math.round((column + 0.5) * width),
189 (int) Math.round((row + 0.5) * height));
190 return this.getComponentAt(point.x, point.y);
191 }
192
193 /***
194 * removes the content of a cell
195 *
196 * @param row the row
197 * @param column the column
198 */
199 public void removeCell(final int row, final int column)
200 {
201 this.remove(this.getCell(row, column));
202 }
203
204 /***
205 * Method refractor computes the size
206 *
207 * @param number refers to the number of rows/columns
208 * @return double[] the result
209 */
210 private double[] refractor(final int number)
211 {
212 double[] result = new double[number];
213 for (int i = 0; i < result.length; i++)
214 {
215 result[i] = 1.0 / number * 1.0;
216 if (result[i] == 1.0)
217 {
218 result[i] = TableLayoutConstants.FILL;
219 }
220 }
221 return result;
222 }
223
224 /***
225 * returns the number of Columns
226 *
227 * @return int
228 */
229 public int getColumns()
230 {
231 return this.content.getColumnCount();
232 }
233
234 /***
235 * returns the number of rows
236 *
237 * @return int the number of rows
238 */
239 public int getRows()
240 {
241 return this.content.getRowCount();
242 }
243
244 /***
245 * @see java.awt.Component#validate()
246 */
247 public void validate()
248 {
249 this.removeAll();
250 double[][] tableDefinition = {
251 this.refractor(this.content.getColumnCount()),
252 this.refractor(this.content.getRowCount())};
253 TableLayout layout = new TableLayout(tableDefinition);
254 this.setLayout(layout);
255 for (int rows = 0; rows < this.content.getRowCount(); rows++)
256 {
257 for (int cols = 0; cols < this.content.getColumnCount(); cols++)
258 {
259 Component component = (Component) this.content.getValueAt(rows,
260 cols);
261 if (component != null)
262 {
263 this.add(component, "" + cols + "," + rows + ",C,C");
264 }
265 }
266 }
267 super.validate();
268 super.repaint();
269 }
270 }