forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListBox.java
More file actions
156 lines (137 loc) · 3.21 KB
/
Copy pathListBox.java
File metadata and controls
156 lines (137 loc) · 3.21 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
package javaforce.ui;
/** List Box
*
* @author pquiring
*/
public class ListBox extends Column {
private Font font;
private boolean multiSelection;
private ChangeListener change;
public ListBox() {
font = Font.getSystemFont();
setBorderStyle(LineStyle.SOLID);
}
public Dimension getMinSize() {
int width = 0;
int height = 0;
for(ListItem item : getChildren()) {
Dimension size = item.getMinSize();
if (size.width > width) {
width = size.width;
}
height += size.height;
}
if (getBorderStyle() != LineStyle.NONE) {
width += 2;
height += 2;
}
return new Dimension(width, height);
}
public int getItemHeight() {
return font.getMaxHeight();
}
public boolean isMultiSelection() {
return multiSelection;
}
public void setMultiSelection(boolean state) {
multiSelection = state;
setSelectedIndex(-1);
}
private void clearSelectionExcept(ListItem selected) {
for(ListItem item : getChildren()) {
if (item == selected) continue;
if (item.isSelected()) {
item.setSelected(false);
}
}
}
public void addItem(String text) {
removeItem(text); //no duplicates
ListItem item = new ListItem(text);
item.setFont(font);
item.setActionListener((Component cmp) -> {
if ((!multiSelection) || (!getKeyState(KeyCode.VK_CONTROL_L) && !getKeyState(KeyCode.VK_CONTROL_R))) {
clearSelectionExcept((ListItem)cmp);
}
if (change != null) {
change.changed(this);
}
});
add(item);
}
public void setChangeListener(ChangeListener listener) {
change = listener;
}
public void removeItem(String text) {
for(ListItem item : getChildren(new ListItem[getChildCount()])) {
if (item.getText().equals(text)) {
remove(item);
return;
}
}
}
public ListItem[] getChildren() {
return getChildren(new ListItem[getChildCount()]);
}
public Font getFont() {
return font;
}
public void setFont(Font font) {
this.font = font;
for(ListItem item : getChildren()) {
item.setFont(font);
}
}
public int getSelectedIndex() {
int idx = 0;
for(ListItem item : getChildren()) {
if (item.isSelected()) {
return idx;
}
idx++;
}
return -1;
}
public int[] getSelectedIndexes() {
int idx = 0;
int cnt = 0;
for(ListItem item : getChildren()) {
if (item.isSelected()) {
cnt++;
}
}
int[] list = new int[cnt];
int pos = 0;
for(ListItem item : getChildren()) {
if (item.isSelected()) {
list[pos++] = idx;
}
idx++;
}
return list;
}
public String getSelectedItem() {
int idx = getSelectedIndex();
if (idx == -1) return null;
ListItem item = (ListItem)getChild(idx);
return item.getText();
}
public void setSelectedIndex(int index) {
int idx = 0;
for(ListItem item : getChildren()) {
if (idx == index) {
if (!item.isSelected()) {
item.setSelected(true);
}
} else {
if (item.isSelected()) {
item.setSelected(false);
}
}
idx++;
}
}
public String toString() {
return "ListBox";
}
}