/
Icon.java
300 lines (258 loc) · 8.14 KB
/
Icon.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package com.storedobject.vaadin;
import com.vaadin.flow.component.*;
import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.component.dependency.NpmPackage;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.dom.Element;
import com.vaadin.flow.shared.Registration;
import java.util.HashMap;
import java.util.Map;
/**
* Enhancements to Vaadin's Icon to handle other collections in a generic way.
*
* @author Syam
*/
public class Icon extends Composite<Component> implements HasStyle, HasIcon, HasSquareElement, ClickNotifier<Icon> {
private static final Map<String, Class<? extends IconInterface>> iconCollections = new HashMap<>();
static {
iconCollections.put("vaadin", VIcon.class);
iconCollections.put("icons", IIcon.class);
iconCollections.put("av", IIcon.class);
iconCollections.put("communication", IIcon.class);
iconCollections.put("device", IIcon.class);
iconCollections.put("editor", IIcon.class);
iconCollections.put("hardware", IIcon.class);
iconCollections.put("image", IIcon.class);
iconCollections.put("maps", IIcon.class);
iconCollections.put("notification", IIcon.class);
iconCollections.put("social", IIcon.class);
iconCollections.put("places", IIcon.class);
}
final IconInterface icon;
/**
* Create from a Vaadin icon
* @param icon Vaadin icon
*/
public Icon(VaadinIcon icon) {
this(create(icon));
}
/**
* Create from an Iron icon.
* @param icon Name of the Iron icon
*/
public Icon(String icon) {
this(create(icon));
}
private Icon(IconInterface icon) {
this.icon = icon;
}
@Override
protected Component initContent() {
return (Component) icon;
}
@Override
public Element getInternalElement() {
return icon.getElement();
}
@Override
public void setIcon(VaadinIcon icon) {
setIcon(HasIcon.getIconName(icon));
}
@Override
public void setIcon(String icon) {
if(icon == null || icon.isBlank()) {
return;
}
icon = HasIcon.getIconName(icon);
Class<? extends IconInterface> iClass = iconCollections.get(icon.substring(0, icon.lastIndexOf(':')));
if(this.icon.getClass() == iClass) {
this.icon.setIcon(icon);
}
}
private static IconInterface create(VaadinIcon icon) {
IconInterface ii = new VIcon();
ii.setIcon(icon);
return ii;
}
private static IconInterface create(String icon) {
IconInterface ii;
if(icon == null || icon.isBlank()) {
return new EmptyIcon();
} else {
icon = HasIcon.getIconName(icon);
if(icon == null || icon.isBlank()) {
return new EmptyIcon();
}
Class<? extends IconInterface> iClass = iconCollections.get(icon.substring(0, icon.lastIndexOf(':')));
ii = create(iClass);
ii.setIcon(icon);
}
return ii;
}
private static IconInterface create(Class<?> klass) {
if(klass == IIcon.class) {
return new IIcon();
}
return new VIcon();
}
@Override
public String getIcon() {
return icon.getIcon();
}
@Override
public void setSize(String size) {
HasSquareElement.super.setSize(size);
icon.setSize(size);
}
@Override
public String getSize() {
return icon.getSize();
}
@Override
public void setWidth(String width) {
HasSquareElement.super.setWidth(width);
icon.setWidth(width);
}
@Override
public void setMinWidth(String minWidth) {
HasSquareElement.super.setMinWidth(minWidth);
icon.setMinWidth(minWidth);
}
@Override
public void setMaxWidth(String maxWidth) {
HasSquareElement.super.setMaxWidth(maxWidth);
icon.setMaxWidth(maxWidth);
}
@Override
public String getWidth() {
return icon.getWidth();
}
@Override
public String getMinWidth() {
return icon.getMinWidth();
}
@Override
public String getMaxWidth() {
return icon.getMaxWidth();
}
@Override
public void setHeight(String height) {
HasSquareElement.super.setHeight(height);
icon.setHeight(height);
}
@Override
public void setMinHeight(String minHeight) {
HasSquareElement.super.setMinHeight(minHeight);
icon.setMinHeight(minHeight);
}
@Override
public void setMaxHeight(String maxHeight) {
HasSquareElement.super.setMaxHeight(maxHeight);
icon.setMaxHeight(maxHeight);
}
@Override
public String getHeight() {
return icon.getHeight();
}
@Override
public String getMinHeight() {
return icon.getMinHeight();
}
@Override
public String getMaxHeight() {
return icon.getMaxHeight();
}
/**
* Set the color.
*
* @param color Color to set.
*/
public void setColor(String color) {
icon.setColor(color);
}
/**
* Get the current color.
*
* @return Current color.
*/
public String getColor() {
return icon.getColor();
}
/**
* Set a style value.
*
* @param name Name of the style.
* @param value Value of the style.
*/
public void setStyle(String name, String value) {
icon.getStyle().set(name, value);
}
/**
* Set an attribute value.
*
* @param name Name of the attribute.
* @param value Value of the attribute.
*/
public void setAttribute(String name, String value) {
icon.setAttribute(name, value);
}
/**
* Add click handler.
*
* @param clickHandler Click handler
* @return Registration.
*/
public Registration addClickHandler(ClickHandler clickHandler) {
ClickNotifier<?> clickNotifier = (ClickNotifier<?>) icon;
return clickNotifier.addClickListener(e -> {
@SuppressWarnings({"rawtypes", "unchecked"})
ClickEvent<Icon> ce = new ClickHandler.ModifiedClickEvent(e, this);
clickHandler.onComponentEvent(ce);
});
}
@Override
public Registration addClickListener(ComponentEventListener<ClickEvent<Icon>> listener) {
ClickNotifier<?> clickNotifier = (ClickNotifier<?>) icon;
return clickNotifier.addClickListener(e -> {
@SuppressWarnings({"rawtypes", "unchecked"})
ClickEvent<Icon> ce = new ClickHandler.ModifiedClickEvent(e, this);
listener.onComponentEvent(ce);
});
}
private static class VIcon extends com.vaadin.flow.component.icon.Icon implements IconInterface {
}
@NpmPackage(value = "@polymer/iron-icons", version = "^3.0.1")
@JsModule("@polymer/iron-icons/iron-icons.js")
@JsModule("@polymer/iron-icons/av-icons.js")
@JsModule("@polymer/iron-icons/communication-icons.js")
@JsModule("@polymer/iron-icons/device-icons.js")
@JsModule("@polymer/iron-icons/editor-icons.js")
@JsModule("@polymer/iron-icons/hardware-icons.js")
@JsModule("@polymer/iron-icons/image-icons.js")
@JsModule("@polymer/iron-icons/maps-icons.js")
@JsModule("@polymer/iron-icons/notification-icons.js")
@JsModule("@polymer/iron-icons/social-icons.js")
@JsModule("@polymer/iron-icons/places-icons.js")
@Tag("iron-icon")
private static class IIcon extends Component implements IconInterface, ClickNotifier<IIcon> {
}
private static class EmptyIcon extends Span implements IconInterface {
}
private interface IconInterface extends HasStyle, HasIcon, HasSquareElement {
default void setColor(String color) {
if (color == null) {
this.getStyle().remove("fill");
} else {
this.getStyle().set("fill", color);
}
}
default String getColor() {
return this.getStyle().get("fill");
}
@Override
default Element getInternalElement() {
return HasStyle.super.getInternalElement();
}
}
}