|
@@ -15,158 +15,172 @@ |
15 |
15 |
|
*/ |
16 |
16 |
|
package com.google.gwt.gen2.table.client; |
17 |
17 |
|
|
18 |
|
- |
import java.util.ArrayList; |
19 |
|
- |
import java.util.HashSet; |
20 |
|
- |
import java.util.Iterator; |
21 |
|
- |
import java.util.List; |
22 |
|
- |
import java.util.Set; |
|
18 |
+ |
import java.util.*; |
23 |
19 |
|
|
24 |
20 |
|
/** |
25 |
21 |
|
* A collection of {@link ColumnDefinition ColumnDefinitions} that define a |
26 |
22 |
|
* table. |
27 |
|
- |
* |
|
23 |
+ |
* |
28 |
24 |
|
* @param <RowType> the type of the row values |
29 |
25 |
|
*/ |
30 |
|
- |
public class DefaultTableDefinition<RowType> implements |
31 |
|
- |
TableDefinition<RowType> { |
32 |
|
- |
/** |
33 |
|
- |
* The default {@link RowRenderer} to use when the |
34 |
|
- |
* {@link DefaultTableDefinition} does not specify one. |
35 |
|
- |
*/ |
36 |
|
- |
private static final RowRenderer DEFAULT_ROW_RENDERER = new DefaultRowRenderer(); |
37 |
|
- |
|
38 |
|
- |
/** |
39 |
|
- |
* The ordered list of {@link ColumnDefinition ColumnDefinitions} used by this |
40 |
|
- |
* renderer. |
41 |
|
- |
*/ |
42 |
|
- |
private List<ColumnDefinition<RowType, ?>> columnDefs; |
43 |
|
- |
|
44 |
|
- |
/** |
45 |
|
- |
* A list of visible columns. |
46 |
|
- |
*/ |
47 |
|
- |
private Set<ColumnDefinition<RowType, ?>> hiddenColumnDefs; |
48 |
|
- |
|
49 |
|
- |
/** |
50 |
|
- |
* The renderer used to render rows. |
51 |
|
- |
*/ |
52 |
|
- |
private RowRenderer<RowType> rowRenderer = DEFAULT_ROW_RENDERER; |
53 |
|
- |
|
54 |
|
- |
/** |
55 |
|
- |
* Create a new {@link TableDefinition}. |
56 |
|
- |
*/ |
57 |
|
- |
public DefaultTableDefinition() { |
58 |
|
- |
this(new ArrayList<ColumnDefinition<RowType, ?>>()); |
59 |
|
- |
} |
60 |
|
- |
|
61 |
|
- |
/** |
62 |
|
- |
* Create a new {@link TableDefinition} with a list of |
63 |
|
- |
* {@link ColumnDefinition ColumnDefinitions}. |
64 |
|
- |
* |
65 |
|
- |
* @param columnDefs the {@link ColumnDefinition ColumnDefinitions} to render |
66 |
|
- |
*/ |
67 |
|
- |
public DefaultTableDefinition(List<ColumnDefinition<RowType, ?>> columnDefs) { |
68 |
|
- |
this.columnDefs = columnDefs; |
69 |
|
- |
hiddenColumnDefs = new HashSet<ColumnDefinition<RowType, ?>>(); |
70 |
|
- |
} |
71 |
|
- |
|
72 |
|
- |
/** |
73 |
|
- |
* Add a {@link ColumnDefinition}. |
74 |
|
- |
* |
75 |
|
- |
* @param columnDef the {@link ColumnDefinition} to add |
76 |
|
- |
*/ |
77 |
|
- |
public void addColumnDefinition(ColumnDefinition<RowType, ?> columnDef) { |
78 |
|
- |
columnDefs.add(columnDef); |
79 |
|
- |
} |
80 |
|
- |
|
81 |
|
- |
/** |
82 |
|
- |
* Insert a {@link ColumnDefinition} at a specific index. |
83 |
|
- |
* |
84 |
|
- |
* @param index the index to place the {@link ColumnDefinition} |
85 |
|
- |
* @param columnDef the {@link ColumnDefinition} to add |
86 |
|
- |
*/ |
87 |
|
- |
public void addColumnDefinition(int index, |
88 |
|
- |
ColumnDefinition<RowType, ?> columnDef) { |
89 |
|
- |
columnDefs.add(index, columnDef); |
90 |
|
- |
} |
91 |
|
- |
|
92 |
|
- |
/** |
93 |
|
- |
* Get the {@link ColumnDefinition} for a given column. |
94 |
|
- |
* |
95 |
|
- |
* @param column the column index |
96 |
|
- |
* @return the {@link ColumnDefinition} for the column |
97 |
|
- |
* @throws IndexOutOfBoundsException if the column is not defined |
98 |
|
- |
*/ |
99 |
|
- |
public ColumnDefinition<RowType, ?> getColumnDefinition(int column) |
100 |
|
- |
throws IndexOutOfBoundsException { |
101 |
|
- |
return columnDefs.get(column); |
102 |
|
- |
} |
103 |
|
- |
|
104 |
|
- |
/** |
105 |
|
- |
* @return the number of {@link ColumnDefinition ColumnDefinitions}. |
106 |
|
- |
*/ |
107 |
|
- |
public int getColumnDefinitionCount() { |
108 |
|
- |
return columnDefs.size(); |
109 |
|
- |
} |
110 |
|
- |
|
111 |
|
- |
public RowRenderer<RowType> getRowRenderer() { |
112 |
|
- |
return rowRenderer; |
113 |
|
- |
} |
114 |
|
- |
|
115 |
|
- |
public List<ColumnDefinition<RowType, ?>> getVisibleColumnDefinitions() { |
116 |
|
- |
List<ColumnDefinition<RowType, ?>> visibleColumns = new ArrayList<ColumnDefinition<RowType, ?>>(); |
117 |
|
- |
for (ColumnDefinition<RowType, ?> columnDef : columnDefs) { |
118 |
|
- |
if (isColumnVisible(columnDef)) { |
119 |
|
- |
visibleColumns.add(columnDef); |
120 |
|
- |
} |
121 |
|
- |
} |
122 |
|
- |
return visibleColumns; |
123 |
|
- |
} |
124 |
|
- |
|
125 |
|
- |
/** |
126 |
|
- |
* Check if a column is visible or not. |
127 |
|
- |
* |
128 |
|
- |
* @param colDef the {@link ColumnDefinition} |
129 |
|
- |
* @return true if visible, false if hidden |
130 |
|
- |
*/ |
131 |
|
- |
public boolean isColumnVisible(ColumnDefinition<RowType, ?> colDef) { |
132 |
|
- |
return !hiddenColumnDefs.contains(colDef); |
133 |
|
- |
} |
134 |
|
- |
|
135 |
|
- |
/** |
136 |
|
- |
* Remove a {@link ColumnDefinition}. |
137 |
|
- |
* |
138 |
|
- |
* @param columnDef the {@link ColumnDefinition} to remove |
139 |
|
- |
*/ |
140 |
|
- |
public void removeColumnDefinition(ColumnDefinition<RowType, ?> columnDef) { |
141 |
|
- |
columnDefs.remove(columnDef); |
142 |
|
- |
} |
143 |
|
- |
|
144 |
|
- |
public void renderRows(int startRowIndex, Iterator<RowType> rowValues, |
145 |
|
- |
AbstractRowView<RowType> view) { |
146 |
|
- |
List<ColumnDefinition<RowType, ?>> visibleColumns = getVisibleColumnDefinitions(); |
147 |
|
- |
view.renderRowsImpl(startRowIndex, rowValues, rowRenderer, visibleColumns); |
148 |
|
- |
} |
149 |
|
- |
|
150 |
|
- |
/** |
151 |
|
- |
* Hide or show a column. |
152 |
|
- |
* |
153 |
|
- |
* @param colDef the {@link ColumnDefinition} |
154 |
|
- |
* @param visible true to show it, false to hide |
155 |
|
- |
*/ |
156 |
|
- |
public void setColumnVisible(ColumnDefinition<RowType, ?> colDef, |
157 |
|
- |
boolean visible) { |
158 |
|
- |
if (visible) { |
159 |
|
- |
hiddenColumnDefs.remove(colDef); |
160 |
|
- |
} else { |
161 |
|
- |
hiddenColumnDefs.add(colDef); |
162 |
|
- |
} |
163 |
|
- |
} |
164 |
|
- |
|
165 |
|
- |
/** |
166 |
|
- |
* Set the {@link RowRenderer} used to render rows. |
167 |
|
- |
*/ |
168 |
|
- |
public void setRowRenderer(RowRenderer<RowType> rowRenderer) { |
169 |
|
- |
assert rowRenderer != null : "rowRenderer cannot be null"; |
170 |
|
- |
this.rowRenderer = rowRenderer; |
171 |
|
- |
} |
|
26 |
+ |
public class DefaultTableDefinition<RowType> implements TableDefinition<RowType> |
|
27 |
+ |
{ |
|
28 |
+ |
/** |
|
29 |
+ |
* The default {@link RowRenderer} to use when the |
|
30 |
+ |
* {@link DefaultTableDefinition} does not specify one. |
|
31 |
+ |
*/ |
|
32 |
+ |
private static final RowRenderer DEFAULT_ROW_RENDERER = new DefaultRowRenderer(); |
|
33 |
+ |
|
|
34 |
+ |
/** |
|
35 |
+ |
* The ordered list of {@link ColumnDefinition ColumnDefinitions} used by this |
|
36 |
+ |
* renderer. |
|
37 |
+ |
*/ |
|
38 |
+ |
private List<ColumnDefinition<RowType, ?>> columnDefs; |
|
39 |
+ |
|
|
40 |
+ |
/** |
|
41 |
+ |
* A list of visible columns. |
|
42 |
+ |
*/ |
|
43 |
+ |
private Set<ColumnDefinition<RowType, ?>> hiddenColumnDefs; |
|
44 |
+ |
|
|
45 |
+ |
/** |
|
46 |
+ |
* The renderer used to render rows. |
|
47 |
+ |
*/ |
|
48 |
+ |
private RowRenderer<RowType> rowRenderer = DEFAULT_ROW_RENDERER; |
|
49 |
+ |
|
|
50 |
+ |
/** |
|
51 |
+ |
* Create a new {@link TableDefinition}. |
|
52 |
+ |
*/ |
|
53 |
+ |
public DefaultTableDefinition() |
|
54 |
+ |
{ |
|
55 |
+ |
this( new ArrayList<ColumnDefinition<RowType, ?>>() ); |
|
56 |
+ |
} |
|
57 |
+ |
|
|
58 |
+ |
/** |
|
59 |
+ |
* Create a new {@link TableDefinition} with a list of |
|
60 |
+ |
* {@link ColumnDefinition ColumnDefinitions}. |
|
61 |
+ |
* |
|
62 |
+ |
* @param columnDefs the {@link ColumnDefinition ColumnDefinitions} to render |
|
63 |
+ |
*/ |
|
64 |
+ |
public DefaultTableDefinition( List<ColumnDefinition<RowType, ?>> columnDefs ) |
|
65 |
+ |
{ |
|
66 |
+ |
this.columnDefs = columnDefs; |
|
67 |
+ |
hiddenColumnDefs = new HashSet<ColumnDefinition<RowType, ?>>(); |
|
68 |
+ |
} |
|
69 |
+ |
|
|
70 |
+ |
/** |
|
71 |
+ |
* Add a {@link ColumnDefinition}. |
|
72 |
+ |
* |
|
73 |
+ |
* @param columnDef the {@link ColumnDefinition} to add |
|
74 |
+ |
*/ |
|
75 |
+ |
public void addColumnDefinition( ColumnDefinition<RowType, ?> columnDef ) |
|
76 |
+ |
{ |
|
77 |
+ |
columnDefs.add( columnDef ); |
|
78 |
+ |
} |
|
79 |
+ |
|
|
80 |
+ |
/** |
|
81 |
+ |
* Insert a {@link ColumnDefinition} at a specific index. |
|
82 |
+ |
* |
|
83 |
+ |
* @param index the index to place the {@link ColumnDefinition} |
|
84 |
+ |
* @param columnDef the {@link ColumnDefinition} to add |
|
85 |
+ |
*/ |
|
86 |
+ |
public void addColumnDefinition( int index, ColumnDefinition<RowType, ?> columnDef ) |
|
87 |
+ |
{ |
|
88 |
+ |
columnDefs.add( index, columnDef ); |
|
89 |
+ |
} |
|
90 |
+ |
|
|
91 |
+ |
/** |
|
92 |
+ |
* Get the {@link ColumnDefinition} for a given column. |
|
93 |
+ |
* |
|
94 |
+ |
* @param column the column index |
|
95 |
+ |
* |
|
96 |
+ |
* @return the {@link ColumnDefinition} for the column |
|
97 |
+ |
* |
|
98 |
+ |
* @throws IndexOutOfBoundsException if the column is not defined |
|
99 |
+ |
*/ |
|
100 |
+ |
public ColumnDefinition<RowType, ?> getColumnDefinition( int column ) |
|
101 |
+ |
throws IndexOutOfBoundsException |
|
102 |
+ |
{ |
|
103 |
+ |
return columnDefs.get( column ); |
|
104 |
+ |
} |
|
105 |
+ |
|
|
106 |
+ |
/** |
|
107 |
+ |
* @return the number of {@link ColumnDefinition ColumnDefinitions}. |
|
108 |
+ |
*/ |
|
109 |
+ |
public int getColumnDefinitionCount() |
|
110 |
+ |
{ |
|
111 |
+ |
return columnDefs.size(); |
|
112 |
+ |
} |
|
113 |
+ |
|
|
114 |
+ |
public RowRenderer<RowType> getRowRenderer() |
|
115 |
+ |
{ |
|
116 |
+ |
return rowRenderer; |
|
117 |
+ |
} |
|
118 |
+ |
|
|
119 |
+ |
public List<ColumnDefinition<RowType, ?>> getVisibleColumnDefinitions() |
|
120 |
+ |
{ |
|
121 |
+ |
List<ColumnDefinition<RowType, ?>> visibleColumns = new ArrayList<ColumnDefinition<RowType, ?>>(); |
|
122 |
+ |
for ( ColumnDefinition<RowType, ?> columnDef : columnDefs ) |
|
123 |
+ |
{ |
|
124 |
+ |
if ( isColumnVisible( columnDef ) ) |
|
125 |
+ |
{ |
|
126 |
+ |
visibleColumns.add( columnDef ); |
|
127 |
+ |
} |
|
128 |
+ |
} |
|
129 |
+ |
return visibleColumns; |
|
130 |
+ |
} |
|
131 |
+ |
|
|
132 |
+ |
/** |
|
133 |
+ |
* Check if a column is visible or not. |
|
134 |
+ |
* |
|
135 |
+ |
* @param colDef the {@link ColumnDefinition} |
|
136 |
+ |
* |
|
137 |
+ |
* @return true if visible, false if hidden |
|
138 |
+ |
*/ |
|
139 |
+ |
public boolean isColumnVisible( ColumnDefinition<RowType, ?> colDef ) |
|
140 |
+ |
{ |
|
141 |
+ |
return !hiddenColumnDefs.contains( colDef ); |
|
142 |
+ |
} |
|
143 |
+ |
|
|
144 |
+ |
/** |
|
145 |
+ |
* Remove a {@link ColumnDefinition}. |
|
146 |
+ |
* |
|
147 |
+ |
* @param columnDef the {@link ColumnDefinition} to remove |
|
148 |
+ |
*/ |
|
149 |
+ |
public void removeColumnDefinition( ColumnDefinition<RowType, ?> columnDef ) |
|
150 |
+ |
{ |
|
151 |
+ |
columnDefs.remove( columnDef ); |
|
152 |
+ |
} |
|
153 |
+ |
|
|
154 |
+ |
public void renderRows( int startRowIndex, Iterator<RowType> rowValues, AbstractRowView<RowType> view ) |
|
155 |
+ |
{ |
|
156 |
+ |
List<ColumnDefinition<RowType, ?>> visibleColumns = getVisibleColumnDefinitions(); |
|
157 |
+ |
view.renderRowsImpl( startRowIndex, rowValues, rowRenderer, visibleColumns ); |
|
158 |
+ |
} |
|
159 |
+ |
|
|
160 |
+ |
/** |
|
161 |
+ |
* Hide or show a column. |
|
162 |
+ |
* |
|
163 |
+ |
* @param colDef the {@link ColumnDefinition} |
|
164 |
+ |
* @param visible true to show it, false to hide |
|
165 |
+ |
*/ |
|
166 |
+ |
public void setColumnVisible( ColumnDefinition<RowType, ?> colDef, boolean visible ) |
|
167 |
+ |
{ |
|
168 |
+ |
if ( visible ) |
|
169 |
+ |
{ |
|
170 |
+ |
hiddenColumnDefs.remove( colDef ); |
|
171 |
+ |
} |
|
172 |
+ |
else |
|
173 |
+ |
{ |
|
174 |
+ |
hiddenColumnDefs.add( colDef ); |
|
175 |
+ |
} |
|
176 |
+ |
} |
|
177 |
+ |
|
|
178 |
+ |
/** |
|
179 |
+ |
* Set the {@link RowRenderer} used to render rows. |
|
180 |
+ |
*/ |
|
181 |
+ |
public void setRowRenderer( RowRenderer<RowType> rowRenderer ) |
|
182 |
+ |
{ |
|
183 |
+ |
assert rowRenderer != null : "rowRenderer cannot be null"; |
|
184 |
+ |
this.rowRenderer = rowRenderer; |
|
185 |
+ |
} |
172 |
186 |
|
} |