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