/
PropertyPanel.java
414 lines (370 loc) · 10.5 KB
/
PropertyPanel.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
* Copyright 2015-present Open Networking Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.ui.topo;
import com.google.common.collect.Sets;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* Models a panel displayed on the Topology View.
*/
public class PropertyPanel {
private static final NumberFormat NF = NumberFormat.getInstance();
private String title;
private String glyphId;
private String id;
private String navPath;
private List<Prop> properties = new ArrayList<>();
private List<ButtonId> buttons = new ArrayList<>();
/**
* Constructs a property panel model with the given title and
* glyph identifier (icon to display).
*
* @param title title text
* @param glyphId glyph ID
*/
public PropertyPanel(String title, String glyphId) {
this.title = title;
this.glyphId = glyphId;
}
/**
* Returns a number formatter to use for formatting integer and long
* property values.
* <p>
* This default implementation uses a formatter for the default
* locale. For example:
* <pre>
* Locale.ENGLISH : 1000 -> "1,000"
* Locale.FRENCH : 1000 -> "1 000"
* Locale.GERMAN : 1000 -> "1.000"
* </pre>
*
* @return the number formatter
*/
protected NumberFormat formatter() {
return NF;
}
/**
* Adds a navigation path field to the panel data, to be included in
* the returned JSON data to the client. This is typically used to
* configure the topology view with the appropriate navigation path for
* a hot-link to some other view.
*
* @param navPath the navigation path
* @return self for chaining
*/
public PropertyPanel navPath(String navPath) {
this.navPath = navPath;
return this;
}
/**
* Adds an ID field to the panel data, to be included in
* the returned JSON data to the client.
*
* @param id the identifier
* @return self, for chaining
*/
public PropertyPanel id(String id) {
this.id = id;
return this;
}
/**
* Adds a property to the panel data.
*
* @param key property key
* @param label property label (localized)
* @param value property value
* @return self, for chaining
*/
public PropertyPanel addProp(String key, String label, String value) {
properties.add(new Prop(key, label, value));
return this;
}
/**
* Adds a property to the panel data, using a decimal formatter.
*
* @param key property key
* @param label property label (localized)
* @param value property value
* @return self, for chaining
*/
public PropertyPanel addProp(String key, String label, int value) {
properties.add(new Prop(key, label, formatter().format(value)));
return this;
}
/**
* Adds a property to the panel data, using a decimal formatter.
*
* @param key property key
* @param label property label (localized)
* @param value property value
* @return self, for chaining
*/
public PropertyPanel addProp(String key, String label, long value) {
properties.add(new Prop(key, label, formatter().format(value)));
return this;
}
/**
* Adds a property to the panel data. Note that the value's
* {@link Object#toString toString()} method is used to convert the
* value to a string.
*
* @param key property key
* @param label property label (localized)
* @param value property value
* @return self, for chaining
*/
public PropertyPanel addProp(String key, String label, Object value) {
properties.add(new Prop(key, label, value.toString()));
return this;
}
/**
* Adds a property to the panel data. Note that the value's
* {@link Object#toString toString()} method is used to convert the
* value to a string, from which the characters defined in the given
* regular expression string are stripped.
*
* @param key property key
* @param label property label (localized)
* @param value property value
* @param reStrip regexp characters to strip from value string
* @return self, for chaining
*/
public PropertyPanel addProp(String key, String label,
Object value, String reStrip) {
String val = value.toString().replaceAll(reStrip, "");
properties.add(new Prop(key, label, val));
return this;
}
/**
* Adds a separator to the panel data.
*
* @return self, for chaining
*/
public PropertyPanel addSeparator() {
properties.add(new Separator());
return this;
}
/**
* Returns the title text.
*
* @return title text
*/
public String title() {
return title;
}
/**
* Returns the glyph identifier.
*
* @return glyph identifier
*/
public String glyphId() {
return glyphId;
}
/**
* Returns the navigation path.
*
* @return the navigation path
*/
public String navPath() {
return navPath;
}
/**
* Returns the internal ID.
*
* @return the ID
*/
public String id() {
return id;
}
/**
* Returns the list of properties to be displayed.
*
* @return the property list
*/
public List<Prop> properties() {
return properties;
}
/**
* Returns the list of button descriptors.
*
* @return the button list
*/
public List<ButtonId> buttons() {
return buttons;
}
// == MUTATORS
/**
* Sets the title text.
*
* @param title title text
* @return self, for chaining
*/
public PropertyPanel title(String title) {
this.title = title;
return this;
}
/**
* Sets the glyph identifier.
*
* @param glyphId glyph identifier
* @return self, for chaining
*/
public PropertyPanel glyphId(String glyphId) {
this.glyphId = glyphId;
return this;
}
/**
* Removes properties with the given keys from the list.
*
* @param keys keys of properties to remove
* @return self, for chaining
*/
public PropertyPanel removeProps(String... keys) {
Set<String> forRemoval = Sets.newHashSet(keys);
List<Prop> toKeep = new ArrayList<>();
for (Prop p : properties) {
if (!forRemoval.contains(p.key())) {
toKeep.add(p);
}
}
properties = toKeep;
return this;
}
/**
* Removes all currently defined properties.
*
* @return self, for chaining
*/
public PropertyPanel removeAllProps() {
properties.clear();
return this;
}
/**
* Adds the given button descriptor to the panel data.
*
* @param button button descriptor
* @return self, for chaining
*/
public PropertyPanel addButton(ButtonId button) {
buttons.add(button);
return this;
}
/**
* Removes buttons with the given descriptors from the list.
*
* @param descriptors descriptors to remove
* @return self, for chaining
*/
public PropertyPanel removeButtons(ButtonId... descriptors) {
Set<ButtonId> forRemoval = Sets.newHashSet(descriptors);
List<ButtonId> toKeep = new ArrayList<>();
for (ButtonId bd : buttons) {
if (!forRemoval.contains(bd)) {
toKeep.add(bd);
}
}
buttons = toKeep;
return this;
}
/**
* Removes all currently defined buttons.
*
* @return self, for chaining
*/
public PropertyPanel removeAllButtons() {
buttons.clear();
return this;
}
// ====================
/**
* Simple data carrier for a property, composed of a key/label/value trio.
*/
public static class Prop {
private final String key;
private final String label;
private final String value;
/**
* Constructs a property data value.
*
* @param key property key (localization key)
* @param label property label (localization value)
* @param value property value
*/
public Prop(String key, String label, String value) {
this.key = key;
this.label = label;
this.value = value;
}
/**
* Returns the property's key.
*
* @return the key
*/
public String key() {
return key;
}
/**
* Returns the property's (localized) label.
*
* @return the label
*/
public String label() {
return label;
}
/**
* Returns the property's value.
*
* @return the value
*/
public String value() {
return value;
}
/*
* NOTE: equals/hashCode are only expressed in terms of key and value.
*/
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Prop prop = (Prop) o;
return key.equals(prop.key) && value.equals(prop.value);
}
@Override
public int hashCode() {
int result = key.hashCode();
result = 31 * result + value.hashCode();
return result;
}
@Override
public String toString() {
return "{" + key + "(" + label + ") -> " + value + "}";
}
}
/**
* Auxiliary class representing a separator property.
*/
static class Separator extends Prop {
Separator() {
super("-", "-", "");
}
}
}