-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathListBox.java
More file actions
168 lines (146 loc) · 3.56 KB
/
Copy pathListBox.java
File metadata and controls
168 lines (146 loc) · 3.56 KB
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
package javaforce.webui;
import javaforce.webui.event.*;
/** ListBox
*
* @author pquiring
*/
public class ListBox extends ScrollPanel implements Click {
private class Cell extends Block {
public Cell(Component cmp) {
add(cmp);
}
public Component getComponent() {
Component cmp = get(0);
return cmp;
}
private boolean selected;
public void setSelected(boolean state) {
selected = state;
if (state) {
sendEvent("addclass", new String[] {"cls=selected"});
} else {
sendEvent("delclass", new String[] {"cls=selected"});
}
}
public boolean isSelected() {
return selected;
}
}
public ListBox() {
addClass("listbox");
addClass("height100");
}
public void update(int idx, String text) {
Component c = getCell(idx).getComponent();
if (!(c instanceof Label)) return;
Label lbl = (Label)c;
lbl.setText(text);
}
public void add(String item) {
add(new Label(item));
}
public void add(int idx, String item) {
add(idx, new Label(item));
}
public void add(Component item) {
Cell cell = new Cell(item);
cell.addClickListener(this);
super.add(count(), cell);
}
public void add(int idx, Component item) {
Cell cell = new Cell(item);
cell.addClickListener(this);
super.add(idx, cell);
}
private Cell getCell(int idx) {
return (Cell)get(idx);
}
private Component getComponent(int idx) {
return getCell(idx).getComponent();
}
public int getSelectedIndex() {
int cnt = count();
for(int idx=0;idx<cnt;idx++) {
Cell cell = getCell(idx);
if (cell.isSelected()) return idx;
}
return -1;
}
public int getSelectedCount() {
int selCount = 0;
int cnt = count();
for(int idx=0;idx<cnt;idx++) {
Cell cell = getCell(idx);
if (cell.isSelected()) selCount++;
}
return selCount;
}
public int[] getSelectedIndices() {
int selCount = getSelectedCount();
int[] selIdx = new int[selCount];
int selPos = 0;
int cnt = count();
for(int idx=0;idx<cnt;idx++) {
Cell cell = getCell(idx);
if (cell.isSelected()) {
selIdx[selPos++] = idx;
}
}
return selIdx;
}
public String getSelectedItem() {
int idx = getSelectedIndex();
if (idx == -1) return null;
Component cmp = getComponent(idx);
if (cmp instanceof Label) {
Label l = (Label)cmp;
return l.text;
}
return cmp.toString();
}
public String getItem(int idx) {
Component cmp = getComponent(idx);
if (cmp instanceof Label) {
Label l = (Label)cmp;
return l.text;
}
return cmp.toString();
}
public int getCount() {
return count();
}
public boolean contains(String item) {
int cnt = getCount();
for(int i=0;i<cnt;i++) {
String li = getItem(i);
if (li.equals(item)) return true;
}
return false;
}
public Component getSelectedComponent() {
int idx = getSelectedIndex();
if (idx == -1) return null;
return getComponent(idx);
}
public void setSelectedIndex(int selidx) {
int cnt = count();
for(int idx=0;idx<cnt;idx++) {
Cell cell = getCell(idx);
cell.setSelected(idx == selidx);
}
}
public void onClick(MouseEvent me, Component cmp) {
Cell cell = (Cell)cmp;
if (me.ctrlKey) {
cell.setSelected(!cell.isSelected());
} else {
//clear all other items
int cnt = count();
for(int idx=0;idx<cnt;idx++) {
Cell o = getCell(idx);
o.setSelected(o == cell);
}
}
onChanged(null);
}
}