blob: b614ef4290a8a24cf874283e2a030d1de6551dc3 [file] [log] [blame]
Simon Hunt0af1ec32015-07-24 12:17:55 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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;
34 private String typeId;
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
42 * type identifier (icon to display).
43 *
Simon Hunt10618f62017-06-15 19:30:52 -070044 * @param title title text
Simon Hunt629b99e2015-07-27 17:38:33 -070045 * @param typeId type (icon) ID
46 */
Simon Hunt0af1ec32015-07-24 12:17:55 -070047 public PropertyPanel(String title, String typeId) {
48 this.title = title;
49 this.typeId = typeId;
50 }
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 Hunt00a27ff2015-07-28 18:53:40 -0700100 * @param value property value
101 * @return self, for chaining
102 */
103 public PropertyPanel addProp(String key, String value) {
104 properties.add(new Prop(key, value));
105 return this;
106 }
107
108 /**
109 * Adds a property to the panel data, using a decimal formatter.
110 *
Simon Hunt10618f62017-06-15 19:30:52 -0700111 * @param key property key
Simon Hunt00a27ff2015-07-28 18:53:40 -0700112 * @param value property value
113 * @return self, for chaining
114 */
115 public PropertyPanel addProp(String key, int value) {
Simon Huntcd150272015-11-04 14:09:36 -0800116 properties.add(new Prop(key, formatter().format(value)));
Simon Hunt00a27ff2015-07-28 18:53:40 -0700117 return this;
118 }
119
120 /**
121 * Adds a property to the panel data, using a decimal formatter.
122 *
Simon Hunt10618f62017-06-15 19:30:52 -0700123 * @param key property key
Simon Hunt00a27ff2015-07-28 18:53:40 -0700124 * @param value property value
125 * @return self, for chaining
126 */
127 public PropertyPanel addProp(String key, long value) {
Simon Huntcd150272015-11-04 14:09:36 -0800128 properties.add(new Prop(key, formatter().format(value)));
Simon Hunt00a27ff2015-07-28 18:53:40 -0700129 return this;
130 }
131
132 /**
133 * Adds a property to the panel data. Note that the value's
134 * {@link Object#toString toString()} method is used to convert the
135 * value to a string.
136 *
Simon Hunt10618f62017-06-15 19:30:52 -0700137 * @param key property key
Simon Hunt00a27ff2015-07-28 18:53:40 -0700138 * @param value property value
139 * @return self, for chaining
140 */
141 public PropertyPanel addProp(String key, Object value) {
142 properties.add(new Prop(key, value.toString()));
143 return this;
144 }
145
146 /**
147 * Adds a property to the panel data. Note that the value's
148 * {@link Object#toString toString()} method is used to convert the
149 * value to a string, from which the characters defined in the given
150 * regular expression string are stripped.
151 *
Simon Hunt10618f62017-06-15 19:30:52 -0700152 * @param key property key
153 * @param value property value
Simon Hunt00a27ff2015-07-28 18:53:40 -0700154 * @param reStrip regexp characters to strip from value string
155 * @return self, for chaining
156 */
157 public PropertyPanel addProp(String key, Object value, String reStrip) {
158 String val = value.toString().replaceAll(reStrip, "");
159 properties.add(new Prop(key, val));
160 return this;
161 }
162
163 /**
164 * Adds a separator to the panel data.
165 *
166 * @return self, for chaining
167 */
168 public PropertyPanel addSeparator() {
169 properties.add(new Separator());
170 return this;
171 }
Simon Huntb745ca62015-07-28 15:37:11 -0700172
173 /**
Simon Hunt629b99e2015-07-27 17:38:33 -0700174 * Returns the title text.
175 *
176 * @return title text
177 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700178 public String title() {
179 return title;
180 }
181
Simon Hunt629b99e2015-07-27 17:38:33 -0700182 /**
183 * Returns the type identifier.
184 *
185 * @return type identifier
186 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700187 public String typeId() {
188 return typeId;
189 }
190
Simon Hunt629b99e2015-07-27 17:38:33 -0700191 /**
Simon Hunt10618f62017-06-15 19:30:52 -0700192 * Returns the navigation path.
193 *
194 * @return the navigation path
195 */
196 public String navPath() {
197 return navPath;
198 }
199
200 /**
Simon Huntb745ca62015-07-28 15:37:11 -0700201 * Returns the internal ID.
202 *
203 * @return the ID
204 */
205 public String id() {
206 return id;
207 }
208
209 /**
Simon Hunt629b99e2015-07-27 17:38:33 -0700210 * Returns the list of properties to be displayed.
211 *
212 * @return the property list
213 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700214 // TODO: consider protecting this?
215 public List<Prop> properties() {
216 return properties;
217 }
218
Simon Huntfb940112015-07-29 18:36:35 -0700219 /**
220 * Returns the list of button descriptors.
221 *
222 * @return the button list
223 */
224 // TODO: consider protecting this?
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700225 public List<ButtonId> buttons() {
Simon Huntfb940112015-07-29 18:36:35 -0700226 return buttons;
227 }
228
Simon Hunt629b99e2015-07-27 17:38:33 -0700229 // == MUTATORS
230
231 /**
232 * Sets the title text.
233 *
234 * @param title title text
235 * @return self, for chaining
236 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700237 public PropertyPanel title(String title) {
238 this.title = title;
239 return this;
240 }
241
Simon Hunt629b99e2015-07-27 17:38:33 -0700242 /**
243 * Sets the type identifier (icon ID).
244 *
245 * @param typeId type identifier
246 * @return self, for chaining
247 */
248 public PropertyPanel typeId(String typeId) {
249 this.typeId = typeId;
250 return this;
251 }
Simon Hunt0af1ec32015-07-24 12:17:55 -0700252
Simon Hunt629b99e2015-07-27 17:38:33 -0700253 /**
254 * Removes properties with the given keys from the list.
255 *
256 * @param keys keys of properties to remove
257 * @return self, for chaining
258 */
259 public PropertyPanel removeProps(String... keys) {
Simon Hunt3a0598f2015-08-04 19:59:04 -0700260 Set<String> forRemoval = Sets.newHashSet(keys);
261 List<Prop> toKeep = new ArrayList<>();
Simon Hunt10618f62017-06-15 19:30:52 -0700262 for (Prop p : properties) {
Simon Hunt3a0598f2015-08-04 19:59:04 -0700263 if (!forRemoval.contains(p.key())) {
264 toKeep.add(p);
Simon Hunt629b99e2015-07-27 17:38:33 -0700265 }
266 }
Simon Hunt3a0598f2015-08-04 19:59:04 -0700267 properties = toKeep;
Simon Hunt629b99e2015-07-27 17:38:33 -0700268 return this;
269 }
270
271 /**
272 * Removes all currently defined properties.
273 *
274 * @return self, for chaining
275 */
276 public PropertyPanel removeAllProps() {
277 properties.clear();
278 return this;
279 }
Simon Hunt0af1ec32015-07-24 12:17:55 -0700280
Simon Huntfb940112015-07-29 18:36:35 -0700281 /**
Simon Hunt3a0598f2015-08-04 19:59:04 -0700282 * Adds the given button descriptor to the panel data.
Simon Huntfb940112015-07-29 18:36:35 -0700283 *
Simon Hunt3a0598f2015-08-04 19:59:04 -0700284 * @param button button descriptor
Simon Huntfb940112015-07-29 18:36:35 -0700285 * @return self, for chaining
286 */
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700287 public PropertyPanel addButton(ButtonId button) {
Simon Hunt3a0598f2015-08-04 19:59:04 -0700288 buttons.add(button);
289 return this;
290 }
291
292 /**
293 * Removes buttons with the given descriptors from the list.
294 *
295 * @param descriptors descriptors to remove
296 * @return self, for chaining
297 */
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700298 public PropertyPanel removeButtons(ButtonId... descriptors) {
299 Set<ButtonId> forRemoval = Sets.newHashSet(descriptors);
300 List<ButtonId> toKeep = new ArrayList<>();
Simon Hunt10618f62017-06-15 19:30:52 -0700301 for (ButtonId bd : buttons) {
Simon Hunt3a0598f2015-08-04 19:59:04 -0700302 if (!forRemoval.contains(bd)) {
303 toKeep.add(bd);
304 }
305 }
306 buttons = toKeep;
307 return this;
308 }
309
310 /**
311 * Removes all currently defined buttons.
312 *
313 * @return self, for chaining
314 */
315 public PropertyPanel removeAllButtons() {
316 buttons.clear();
Simon Huntfb940112015-07-29 18:36:35 -0700317 return this;
318 }
319
Simon Hunt0af1ec32015-07-24 12:17:55 -0700320 // ====================
321
Simon Huntb745ca62015-07-28 15:37:11 -0700322
Simon Hunt629b99e2015-07-27 17:38:33 -0700323 /**
324 * Simple data carrier for a property, composed of a key/value pair.
325 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700326 public static class Prop {
Simon Hunt629b99e2015-07-27 17:38:33 -0700327 private final String key;
328 private final String value;
Simon Hunt0af1ec32015-07-24 12:17:55 -0700329
Simon Hunt629b99e2015-07-27 17:38:33 -0700330 /**
331 * Constructs a property data value.
332 *
Simon Hunt10618f62017-06-15 19:30:52 -0700333 * @param key property key
Simon Hunt629b99e2015-07-27 17:38:33 -0700334 * @param value property value
335 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700336 public Prop(String key, String value) {
337 this.key = key;
338 this.value = value;
339 }
340
Simon Hunt629b99e2015-07-27 17:38:33 -0700341 /**
342 * Returns the property's key.
343 *
344 * @return the key
345 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700346 public String key() {
347 return key;
348 }
349
Simon Hunt629b99e2015-07-27 17:38:33 -0700350 /**
351 * Returns the property's value.
352 *
353 * @return the value
354 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700355 public String value() {
356 return value;
357 }
Simon Hunt629b99e2015-07-27 17:38:33 -0700358
359 @Override
360 public boolean equals(Object o) {
361 if (this == o) {
362 return true;
363 }
364 if (o == null || getClass() != o.getClass()) {
365 return false;
366 }
367
368 Prop prop = (Prop) o;
369 return key.equals(prop.key) && value.equals(prop.value);
370 }
371
372 @Override
373 public int hashCode() {
374 int result = key.hashCode();
375 result = 31 * result + value.hashCode();
376 return result;
377 }
378
379 @Override
380 public String toString() {
381 return "{" + key + " -> " + value + "}";
382 }
Simon Hunt0af1ec32015-07-24 12:17:55 -0700383 }
384
Simon Hunt629b99e2015-07-27 17:38:33 -0700385 /**
386 * Auxiliary class representing a separator property.
387 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700388 public static class Separator extends Prop {
389 public Separator() {
390 super("-", "");
391 }
392 }
393
Simon Hunt0af1ec32015-07-24 12:17:55 -0700394}