blob: f65cda0f5b75e9ded1aac848a002abac12faa02c [file] [log] [blame]
Simon Hunt0af1ec32015-07-24 12:17:55 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Simon Hunt0af1ec32015-07-24 12:17:55 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Simon Hunt0af1ec32015-07-24 12:17:55 -070015 */
16
17package org.onosproject.ui.topo;
18
Simon Hunt629b99e2015-07-27 17:38:33 -070019import com.google.common.collect.Sets;
20
Simon Huntcd150272015-11-04 14:09:36 -080021import java.text.NumberFormat;
Simon Hunt0af1ec32015-07-24 12:17:55 -070022import java.util.ArrayList;
23import java.util.List;
Simon Hunt629b99e2015-07-27 17:38:33 -070024import java.util.Set;
Simon Hunt0af1ec32015-07-24 12:17:55 -070025
26/**
27 * Models a panel displayed on the Topology View.
28 */
29public class PropertyPanel {
30
Simon Huntcd150272015-11-04 14:09:36 -080031 private static final NumberFormat NF = NumberFormat.getInstance();
Simon Hunt00a27ff2015-07-28 18:53:40 -070032
Simon Hunt0af1ec32015-07-24 12:17:55 -070033 private String title;
Simon Hunta58d8942017-08-11 12:51:14 -070034 private String glyphId;
Simon Huntb745ca62015-07-28 15:37:11 -070035 private String id;
Simon Hunt10618f62017-06-15 19:30:52 -070036 private String navPath;
Simon Hunt0af1ec32015-07-24 12:17:55 -070037 private List<Prop> properties = new ArrayList<>();
Simon Hunt8d22c4b2015-08-06 16:24:43 -070038 private List<ButtonId> buttons = new ArrayList<>();
Simon Hunt0af1ec32015-07-24 12:17:55 -070039
Simon Hunt629b99e2015-07-27 17:38:33 -070040 /**
41 * Constructs a property panel model with the given title and
Simon Hunta58d8942017-08-11 12:51:14 -070042 * glyph identifier (icon to display).
Simon Hunt629b99e2015-07-27 17:38:33 -070043 *
Simon Hunta58d8942017-08-11 12:51:14 -070044 * @param title title text
45 * @param glyphId glyph ID
Simon Hunt629b99e2015-07-27 17:38:33 -070046 */
Simon Hunta58d8942017-08-11 12:51:14 -070047 public PropertyPanel(String title, String glyphId) {
Simon Hunt0af1ec32015-07-24 12:17:55 -070048 this.title = title;
Simon Hunta58d8942017-08-11 12:51:14 -070049 this.glyphId = glyphId;
Simon Hunt0af1ec32015-07-24 12:17:55 -070050 }
51
Simon Hunt629b99e2015-07-27 17:38:33 -070052 /**
Simon Huntcd150272015-11-04 14:09:36 -080053 * Returns a number formatter to use for formatting integer and long
54 * property values.
55 * <p>
56 * This default implementation uses a formatter for the default
57 * locale. For example:
58 * <pre>
59 * Locale.ENGLISH : 1000 -&gt; "1,000"
60 * Locale.FRENCH : 1000 -&gt; "1 000"
61 * Locale.GERMAN : 1000 -&gt; "1.000"
62 * </pre>
63 *
64 * @return the number formatter
65 */
66 protected NumberFormat formatter() {
67 return NF;
68 }
69
70 /**
Simon Hunt10618f62017-06-15 19:30:52 -070071 * Adds a navigation path field to the panel data, to be included in
72 * the returned JSON data to the client. This is typically used to
73 * configure the topology view with the appropriate navigation path for
74 * a hot-link to some other view.
75 *
76 * @param navPath the navigation path
77 * @return self for chaining
78 */
79 public PropertyPanel navPath(String navPath) {
80 this.navPath = navPath;
81 return this;
82 }
83
84 /**
Simon Huntb745ca62015-07-28 15:37:11 -070085 * Adds an ID field to the panel data, to be included in
86 * the returned JSON data to the client.
87 *
88 * @param id the identifier
89 * @return self, for chaining
90 */
91 public PropertyPanel id(String id) {
92 this.id = id;
93 return this;
94 }
95
Simon Hunt00a27ff2015-07-28 18:53:40 -070096 /**
97 * Adds a property to the panel data.
98 *
Simon Hunt10618f62017-06-15 19:30:52 -070099 * @param key property key
Simon Hunt879ce452017-08-10 23:32:00 -0700100 * @param label property label (localized)
Simon Hunt00a27ff2015-07-28 18:53:40 -0700101 * @param value property value
102 * @return self, for chaining
103 */
Simon Hunt879ce452017-08-10 23:32:00 -0700104 public PropertyPanel addProp(String key, String label, String value) {
105 properties.add(new Prop(key, label, value));
Simon Hunt00a27ff2015-07-28 18:53:40 -0700106 return this;
107 }
108
109 /**
110 * Adds a property to the panel data, using a decimal formatter.
111 *
Simon Hunt10618f62017-06-15 19:30:52 -0700112 * @param key property key
Simon Hunt879ce452017-08-10 23:32:00 -0700113 * @param label property label (localized)
Simon Hunt00a27ff2015-07-28 18:53:40 -0700114 * @param value property value
115 * @return self, for chaining
116 */
Simon Hunt879ce452017-08-10 23:32:00 -0700117 public PropertyPanel addProp(String key, String label, int value) {
118 properties.add(new Prop(key, label, formatter().format(value)));
Simon Hunt00a27ff2015-07-28 18:53:40 -0700119 return this;
120 }
121
122 /**
123 * Adds a property to the panel data, using a decimal formatter.
124 *
Simon Hunt10618f62017-06-15 19:30:52 -0700125 * @param key property key
Simon Hunt879ce452017-08-10 23:32:00 -0700126 * @param label property label (localized)
Simon Hunt00a27ff2015-07-28 18:53:40 -0700127 * @param value property value
128 * @return self, for chaining
129 */
Simon Hunt879ce452017-08-10 23:32:00 -0700130 public PropertyPanel addProp(String key, String label, long value) {
131 properties.add(new Prop(key, label, formatter().format(value)));
Simon Hunt00a27ff2015-07-28 18:53:40 -0700132 return this;
133 }
134
135 /**
136 * Adds a property to the panel data. Note that the value's
137 * {@link Object#toString toString()} method is used to convert the
138 * value to a string.
139 *
Simon Hunt10618f62017-06-15 19:30:52 -0700140 * @param key property key
Simon Hunt879ce452017-08-10 23:32:00 -0700141 * @param label property label (localized)
Simon Hunt00a27ff2015-07-28 18:53:40 -0700142 * @param value property value
143 * @return self, for chaining
144 */
Simon Hunt879ce452017-08-10 23:32:00 -0700145 public PropertyPanel addProp(String key, String label, Object value) {
146 properties.add(new Prop(key, label, value.toString()));
Simon Hunt00a27ff2015-07-28 18:53:40 -0700147 return this;
148 }
149
150 /**
151 * Adds a property to the panel data. Note that the value's
152 * {@link Object#toString toString()} method is used to convert the
153 * value to a string, from which the characters defined in the given
154 * regular expression string are stripped.
155 *
Simon Hunt10618f62017-06-15 19:30:52 -0700156 * @param key property key
Simon Hunta58d8942017-08-11 12:51:14 -0700157 * @param label property label (localized)
Simon Hunt879ce452017-08-10 23:32:00 -0700158 * @param value property value
159 * @param reStrip regexp characters to strip from value string
160 * @return self, for chaining
161 */
162 public PropertyPanel addProp(String key, String label,
163 Object value, String reStrip) {
164 String val = value.toString().replaceAll(reStrip, "");
165 properties.add(new Prop(key, label, val));
166 return this;
167 }
168
Simon Hunt00a27ff2015-07-28 18:53:40 -0700169 /**
170 * Adds a separator to the panel data.
171 *
172 * @return self, for chaining
173 */
174 public PropertyPanel addSeparator() {
175 properties.add(new Separator());
176 return this;
177 }
Simon Huntb745ca62015-07-28 15:37:11 -0700178
179 /**
Simon Hunt629b99e2015-07-27 17:38:33 -0700180 * Returns the title text.
181 *
182 * @return title text
183 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700184 public String title() {
185 return title;
186 }
187
Simon Hunt629b99e2015-07-27 17:38:33 -0700188 /**
Simon Hunta58d8942017-08-11 12:51:14 -0700189 * Returns the glyph identifier.
Simon Hunt629b99e2015-07-27 17:38:33 -0700190 *
Simon Hunta58d8942017-08-11 12:51:14 -0700191 * @return glyph identifier
Simon Hunt629b99e2015-07-27 17:38:33 -0700192 */
Simon Hunta58d8942017-08-11 12:51:14 -0700193 public String glyphId() {
194 return glyphId;
Simon Hunt0af1ec32015-07-24 12:17:55 -0700195 }
196
Simon Hunt629b99e2015-07-27 17:38:33 -0700197 /**
Simon Hunt10618f62017-06-15 19:30:52 -0700198 * Returns the navigation path.
199 *
200 * @return the navigation path
201 */
202 public String navPath() {
203 return navPath;
204 }
205
206 /**
Simon Huntb745ca62015-07-28 15:37:11 -0700207 * Returns the internal ID.
208 *
209 * @return the ID
210 */
211 public String id() {
212 return id;
213 }
214
215 /**
Simon Hunt629b99e2015-07-27 17:38:33 -0700216 * Returns the list of properties to be displayed.
217 *
218 * @return the property list
219 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700220 public List<Prop> properties() {
221 return properties;
222 }
223
Simon Huntfb940112015-07-29 18:36:35 -0700224 /**
225 * Returns the list of button descriptors.
226 *
227 * @return the button list
228 */
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700229 public List<ButtonId> buttons() {
Simon Huntfb940112015-07-29 18:36:35 -0700230 return buttons;
231 }
232
Simon Hunt629b99e2015-07-27 17:38:33 -0700233 // == MUTATORS
234
235 /**
236 * Sets the title text.
237 *
238 * @param title title text
239 * @return self, for chaining
240 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700241 public PropertyPanel title(String title) {
242 this.title = title;
243 return this;
244 }
245
Simon Hunt629b99e2015-07-27 17:38:33 -0700246 /**
Simon Hunta58d8942017-08-11 12:51:14 -0700247 * Sets the glyph identifier.
Simon Hunt629b99e2015-07-27 17:38:33 -0700248 *
Simon Hunta58d8942017-08-11 12:51:14 -0700249 * @param glyphId glyph identifier
Simon Hunt629b99e2015-07-27 17:38:33 -0700250 * @return self, for chaining
251 */
Simon Hunta58d8942017-08-11 12:51:14 -0700252 public PropertyPanel glyphId(String glyphId) {
253 this.glyphId = glyphId;
Simon Hunt629b99e2015-07-27 17:38:33 -0700254 return this;
255 }
Simon Hunt0af1ec32015-07-24 12:17:55 -0700256
Simon Hunt629b99e2015-07-27 17:38:33 -0700257 /**
258 * Removes properties with the given keys from the list.
259 *
260 * @param keys keys of properties to remove
261 * @return self, for chaining
262 */
263 public PropertyPanel removeProps(String... keys) {
Simon Hunt3a0598f2015-08-04 19:59:04 -0700264 Set<String> forRemoval = Sets.newHashSet(keys);
265 List<Prop> toKeep = new ArrayList<>();
Simon Hunt10618f62017-06-15 19:30:52 -0700266 for (Prop p : properties) {
Simon Hunt3a0598f2015-08-04 19:59:04 -0700267 if (!forRemoval.contains(p.key())) {
268 toKeep.add(p);
Simon Hunt629b99e2015-07-27 17:38:33 -0700269 }
270 }
Simon Hunt3a0598f2015-08-04 19:59:04 -0700271 properties = toKeep;
Simon Hunt629b99e2015-07-27 17:38:33 -0700272 return this;
273 }
274
275 /**
276 * Removes all currently defined properties.
277 *
278 * @return self, for chaining
279 */
280 public PropertyPanel removeAllProps() {
281 properties.clear();
282 return this;
283 }
Simon Hunt0af1ec32015-07-24 12:17:55 -0700284
Simon Huntfb940112015-07-29 18:36:35 -0700285 /**
Simon Hunt3a0598f2015-08-04 19:59:04 -0700286 * Adds the given button descriptor to the panel data.
Simon Huntfb940112015-07-29 18:36:35 -0700287 *
Simon Hunt3a0598f2015-08-04 19:59:04 -0700288 * @param button button descriptor
Simon Huntfb940112015-07-29 18:36:35 -0700289 * @return self, for chaining
290 */
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700291 public PropertyPanel addButton(ButtonId button) {
Simon Hunt3a0598f2015-08-04 19:59:04 -0700292 buttons.add(button);
293 return this;
294 }
295
296 /**
297 * Removes buttons with the given descriptors from the list.
298 *
299 * @param descriptors descriptors to remove
300 * @return self, for chaining
301 */
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700302 public PropertyPanel removeButtons(ButtonId... descriptors) {
303 Set<ButtonId> forRemoval = Sets.newHashSet(descriptors);
304 List<ButtonId> toKeep = new ArrayList<>();
Simon Hunt10618f62017-06-15 19:30:52 -0700305 for (ButtonId bd : buttons) {
Simon Hunt3a0598f2015-08-04 19:59:04 -0700306 if (!forRemoval.contains(bd)) {
307 toKeep.add(bd);
308 }
309 }
310 buttons = toKeep;
311 return this;
312 }
313
314 /**
315 * Removes all currently defined buttons.
316 *
317 * @return self, for chaining
318 */
319 public PropertyPanel removeAllButtons() {
320 buttons.clear();
Simon Huntfb940112015-07-29 18:36:35 -0700321 return this;
322 }
323
Simon Hunt0af1ec32015-07-24 12:17:55 -0700324 // ====================
325
Simon Huntb745ca62015-07-28 15:37:11 -0700326
Simon Hunt629b99e2015-07-27 17:38:33 -0700327 /**
Simon Hunt879ce452017-08-10 23:32:00 -0700328 * Simple data carrier for a property, composed of a key/label/value trio.
Simon Hunt629b99e2015-07-27 17:38:33 -0700329 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700330 public static class Prop {
Simon Hunt629b99e2015-07-27 17:38:33 -0700331 private final String key;
Simon Hunt879ce452017-08-10 23:32:00 -0700332 private final String label;
Simon Hunt629b99e2015-07-27 17:38:33 -0700333 private final String value;
Simon Hunt0af1ec32015-07-24 12:17:55 -0700334
Simon Hunt629b99e2015-07-27 17:38:33 -0700335 /**
336 * Constructs a property data value.
337 *
Simon Hunt879ce452017-08-10 23:32:00 -0700338 * @param key property key (localization key)
339 * @param label property label (localization value)
Simon Hunt629b99e2015-07-27 17:38:33 -0700340 * @param value property value
341 */
Simon Hunt879ce452017-08-10 23:32:00 -0700342 public Prop(String key, String label, String value) {
Simon Hunt0af1ec32015-07-24 12:17:55 -0700343 this.key = key;
Simon Hunt879ce452017-08-10 23:32:00 -0700344 this.label = label;
Simon Hunt0af1ec32015-07-24 12:17:55 -0700345 this.value = value;
346 }
347
Simon Hunt629b99e2015-07-27 17:38:33 -0700348 /**
349 * Returns the property's key.
350 *
351 * @return the key
352 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700353 public String key() {
354 return key;
355 }
356
Simon Hunt629b99e2015-07-27 17:38:33 -0700357 /**
Simon Hunt879ce452017-08-10 23:32:00 -0700358 * Returns the property's (localized) label.
359 *
360 * @return the label
361 */
362 public String label() {
363 return label;
364 }
365
366 /**
Simon Hunt629b99e2015-07-27 17:38:33 -0700367 * Returns the property's value.
368 *
369 * @return the value
370 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700371 public String value() {
372 return value;
373 }
Simon Hunt629b99e2015-07-27 17:38:33 -0700374
Simon Hunt879ce452017-08-10 23:32:00 -0700375 /*
376 * NOTE: equals/hashCode are only expressed in terms of key and value.
377 */
378
Simon Hunt629b99e2015-07-27 17:38:33 -0700379 @Override
380 public boolean equals(Object o) {
381 if (this == o) {
382 return true;
383 }
384 if (o == null || getClass() != o.getClass()) {
385 return false;
386 }
387
388 Prop prop = (Prop) o;
389 return key.equals(prop.key) && value.equals(prop.value);
390 }
391
392 @Override
393 public int hashCode() {
394 int result = key.hashCode();
395 result = 31 * result + value.hashCode();
396 return result;
397 }
398
399 @Override
400 public String toString() {
Simon Hunt879ce452017-08-10 23:32:00 -0700401 return "{" + key + "(" + label + ") -> " + value + "}";
Simon Hunt629b99e2015-07-27 17:38:33 -0700402 }
Simon Hunt0af1ec32015-07-24 12:17:55 -0700403 }
404
Simon Hunt629b99e2015-07-27 17:38:33 -0700405 /**
406 * Auxiliary class representing a separator property.
407 */
Simon Hunta58d8942017-08-11 12:51:14 -0700408 static class Separator extends Prop {
409 Separator() {
Simon Hunt879ce452017-08-10 23:32:00 -0700410 super("-", "-", "");
Simon Hunt0af1ec32015-07-24 12:17:55 -0700411 }
412 }
413
Simon Hunt0af1ec32015-07-24 12:17:55 -0700414}