-
Notifications
You must be signed in to change notification settings - Fork 1
/
ListView.java
222 lines (178 loc) · 6.83 KB
/
ListView.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* Copyright 2010 Ben Ruijl, Wouter Smeenk
This file is part of Walled In.
Walled In is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
Walled In is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with Walled In; see the file LICENSE. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
*/
package walledin.engine.gui.components;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.apache.log4j.Logger;
import walledin.engine.Font;
import walledin.engine.Renderer;
import walledin.engine.gui.AbstractScreen;
import walledin.engine.gui.FontType;
import walledin.engine.gui.ScreenMouseEvent;
import walledin.engine.gui.ScreenMouseEventListener;
import walledin.engine.math.Rectangle;
import walledin.engine.math.Vector2f;
public class ListView<T> extends AbstractScreen implements
ScreenMouseEventListener {
public static class RowData<T> {
private final T object;
private final String[] data;
public RowData(final T object, final String[] data) {
this.object = object;
this.data = data;
}
public T getObject() {
return object;
}
public String[] getData() {
return data;
}
}
/** Logger. */
private static final Logger LOG = Logger.getLogger(ListView.class);
private static final float START_X_LIST = 10f;
private static final float START_Y_CAPTION = 40f;
private static final float START_Y_LIST = 60f;
private static final float Y_SPACE = 20f;
private final int numColumns;
private final Button[] columns;
private float[] columnWidth;
private final float[] accumulatedColumnWidth;
private final List<RowData<T>> data;
private Comparator<RowData<T>> lastComparator;
private int selected;
public ListView(final AbstractScreen parent, final Rectangle boudingRect,
final int z, final int numColumns, final String names[],
final float[] columnWidth) {
super(parent, boudingRect, z);
data = new ArrayList<RowData<T>>();
this.numColumns = numColumns;
accumulatedColumnWidth = new float[numColumns];
setColumnWidth(columnWidth);
columns = new Button[numColumns];
selected = -1;
float curX = START_X_LIST;
for (int i = 0; i < numColumns; i++) {
columns[i] = new Button(this, names[i], new Vector2f(curX,
START_Y_CAPTION));
curX += columnWidth[i];
columns[i].addMouseEventListener(this);
addChild(columns[i]);
}
lastComparator = getComparator(0);
addMouseEventListener(this);
}
public void setColumnWidth(final float[] columnWidth) {
if (columnWidth.length == numColumns) {
this.columnWidth = columnWidth;
accumulatedColumnWidth[0] = columnWidth[0];
for (int i = 1; i < numColumns; i++) {
accumulatedColumnWidth[i] += accumulatedColumnWidth[i - 1]
+ columnWidth[i];
}
}
}
public void addData(final RowData<T> entry) {
if (entry.getData().length != numColumns) {
LOG.error("Trying to add data to listview which does not have the same amount of columns.");
return;
}
data.add(entry);
}
/**
* Sort data according to the last used comparator.
*/
public final void sortData() {
Collections.sort(data, lastComparator);
}
public void resetData() {
data.clear();
}
protected List<RowData<T>> getData() {
return data;
}
@Override
public void draw(final Renderer renderer) {
final Font font = getManager().getFont(FontType.BUTTON_CAPTION);
if (selected >= 0 && selected < data.size()) {
renderer.setColorRGBA(0.2f, 0.2f, 0.02f, 0.5f);
renderer.drawFilledRect(new Rectangle(START_X_LIST, START_Y_LIST
+ (selected - 1) * Y_SPACE + 2,
accumulatedColumnWidth[numColumns - 1], Y_SPACE + 2));
renderer.setColorRGBA(1, 1, 1, 1);
}
float curX = START_X_LIST;
for (int i = 0; i < numColumns; i++) {
float curY = START_Y_LIST;
for (int j = 0; j < data.size(); j++) {
font.renderText(renderer, data.get(j).getData()[i],
new Vector2f(curX, curY));
curY += Y_SPACE;
}
curX += columnWidth[i];
}
renderer.drawRectOutline(getRectangle());
super.draw(renderer);
}
@Override
public void onMouseDown(final ScreenMouseEvent e) {
}
@Override
public void onMouseHover(final ScreenMouseEvent e) {
for (int i = 0; i < data.size(); i++) {
/* The i -1 is because the text is rendered above the line. */
if (new Rectangle(START_X_LIST, START_Y_LIST + (i - 1) * Y_SPACE,
accumulatedColumnWidth[numColumns - 1], Y_SPACE).translate(
getAbsolutePosition()).containsPoint(e.getPos())) {
selected = i;
return;
}
}
selected = -1;
}
protected Comparator<RowData<T>> getComparator(final int column) {
return new Comparator<RowData<T>>() {
@Override
public int compare(final RowData<T> o1, final RowData<T> o2) {
return o1.getData()[column].compareTo(o2.getData()[column]);
}
};
}
protected void onListItemClicked(final T item) {
}
@Override
public void onMouseClicked(final ScreenMouseEvent e) {
for (int i = 0; i < numColumns; i++) {
if (e.getScreen() == columns[i]) {
final Comparator<RowData<T>> comp = getComparator(i);
Collections.sort(data, comp);
lastComparator = comp;
return;
}
}
for (int i = 0; i < data.size(); i++) {
/* The i -1 is because the text is rendered above the line. */
if (new Rectangle(START_X_LIST, START_Y_LIST + (i - 1) * Y_SPACE,
accumulatedColumnWidth[numColumns - 1], Y_SPACE).translate(
getAbsolutePosition()).containsPoint(e.getPos())) {
onListItemClicked(data.get(i).getObject());
return;
}
}
}
}