ImageButtonGroup.java
3.29 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
package cn.csbr.app.gui.util;
import cn.csbr.app.gui.component.common.ImageButton;
import javafx.scene.image.Image;
import java.util.ArrayList;
import java.util.List;
import lombok.Getter;
import org.apache.commons.lang.StringUtils;
/**
* 图片按钮组.
*
* @author fenglinz
* @since 2018-10-31
*/
public final class ImageButtonGroup {
/**
* 按钮分组按钮项.
*/
private static class Item {
private String url;
private String currentUrl;
private String highlightUrl;
@Getter
private ImageButton button;
/**
* 构造方法
*
* @param button 按钮
* @param url 按钮当前图像url
*/
public Item(ImageButton button, String url) {
this.url = url;
this.button = button;
this.currentUrl = url;
this.highlightUrl = StringUtils.isNotBlank(url) ? url.replace("_o", "_s") : "";
}
/**
* 构造方法
*
* @param button 按钮
* @param url 按钮当前图像url
* @param highlightUrl 按钮高亮图像url
*/
public Item(ImageButton button, String url, String highlightUrl) {
this.url = url;
this.button = button;
this.currentUrl = url;
this.highlightUrl = highlightUrl;
}
/**
* 更换按钮图像
*
* @param url 要更换的图像url
*/
public void changeButtonImage(String url) {
if (this.button != null) {
this.currentUrl = url;
this.button.setImage(new Image(url));
}
}
}
/** 按钮组项集合 */
private List<Item> items = new ArrayList<>();
public List<Item> getItems(){
return items;
}
/**
* 清空按钮组集合
*/
public void clear(){
this.items.clear();
}
/**
* 添加按钮分组项.
*
* @param button 按钮对象
* @param url 图像url
*
* @return 按钮分组对象
*/
public ImageButtonGroup addItem(ImageButton button, String url) {
this.items.add(new Item(button, url));
return this;
}
/**
* 高亮显示第一个按钮.
*/
public void highlightFirst() {
if (!this.items.isEmpty()) {
this.highlightCurrentItem(this.items.get(0).button);
}
}
/**
* 高亮显示按钮.
*
* @param button 当前按钮
*/
public void highlightCurrentItem(ImageButton button) {
if (!this.items.isEmpty()) {
Item current = this.findCurrentItem(button);
if (current != null) {
current.changeButtonImage(current.highlightUrl);
}
for (Item item : this.items) {
if (item == current) {
continue;
}
item.changeButtonImage(item.url);
}
}
}
/**
* 查找按钮对应的按钮项
*
* @param button 按钮
*
* @return 按钮项
*/
private Item findCurrentItem(ImageButton button) {
for (Item item : this.items) {
if (item.button == button) {
return item;
}
}
return null;
}
}