blob: 05565f7e360467ffd29b203de30b5bbc53dc1129 [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 Hunt0af1ec32015-07-24 12:17:55 -070036 private List<Prop> properties = new ArrayList<>();
Simon Hunt8d22c4b2015-08-06 16:24:43 -070037 private List<ButtonId> buttons = new ArrayList<>();
Simon Hunt0af1ec32015-07-24 12:17:55 -070038
Simon Hunt629b99e2015-07-27 17:38:33 -070039 /**
40 * Constructs a property panel model with the given title and
41 * type identifier (icon to display).
42 *
43 * @param title title text
44 * @param typeId type (icon) ID
45 */
Simon Hunt0af1ec32015-07-24 12:17:55 -070046 public PropertyPanel(String title, String typeId) {
47 this.title = title;
48 this.typeId = typeId;
49 }
50
Simon Hunt629b99e2015-07-27 17:38:33 -070051 /**
Simon Huntcd150272015-11-04 14:09:36 -080052 * Returns a number formatter to use for formatting integer and long
53 * property values.
54 * <p>
55 * This default implementation uses a formatter for the default
56 * locale. For example:
57 * <pre>
58 * Locale.ENGLISH : 1000 -&gt; "1,000"
59 * Locale.FRENCH : 1000 -&gt; "1 000"
60 * Locale.GERMAN : 1000 -&gt; "1.000"
61 * </pre>
62 *
63 * @return the number formatter
64 */
65 protected NumberFormat formatter() {
66 return NF;
67 }
68
69 /**
Simon Huntb745ca62015-07-28 15:37:11 -070070 * Adds an ID field to the panel data, to be included in
71 * the returned JSON data to the client.
72 *
73 * @param id the identifier
74 * @return self, for chaining
75 */
76 public PropertyPanel id(String id) {
77 this.id = id;
78 return this;
79 }
80
Simon Hunt00a27ff2015-07-28 18:53:40 -070081 /**
82 * Adds a property to the panel data.
83 *
84 * @param key property key
85 * @param value property value
86 * @return self, for chaining
87 */
88 public PropertyPanel addProp(String key, String value) {
89 properties.add(new Prop(key, value));
90 return this;
91 }
92
93 /**
94 * Adds a property to the panel data, using a decimal formatter.
95 *
96 * @param key property key
97 * @param value property value
98 * @return self, for chaining
99 */
100 public PropertyPanel addProp(String key, int value) {
Simon Huntcd150272015-11-04 14:09:36 -0800101 properties.add(new Prop(key, formatter().format(value)));
Simon Hunt00a27ff2015-07-28 18:53:40 -0700102 return this;
103 }
104
105 /**
106 * Adds a property to the panel data, using a decimal formatter.
107 *
108 * @param key property key
109 * @param value property value
110 * @return self, for chaining
111 */
112 public PropertyPanel addProp(String key, long value) {
Simon Huntcd150272015-11-04 14:09:36 -0800113 properties.add(new Prop(key, formatter().format(value)));
Simon Hunt00a27ff2015-07-28 18:53:40 -0700114 return this;
115 }
116
117 /**
118 * Adds a property to the panel data. Note that the value's
119 * {@link Object#toString toString()} method is used to convert the
120 * value to a string.
121 *
122 * @param key property key
123 * @param value property value
124 * @return self, for chaining
125 */
126 public PropertyPanel addProp(String key, Object value) {
127 properties.add(new Prop(key, value.toString()));
128 return this;
129 }
130
131 /**
132 * Adds a property to the panel data. Note that the value's
133 * {@link Object#toString toString()} method is used to convert the
134 * value to a string, from which the characters defined in the given
135 * regular expression string are stripped.
136 *
137 * @param key property key
138 * @param value property value
139 * @param reStrip regexp characters to strip from value string
140 * @return self, for chaining
141 */
142 public PropertyPanel addProp(String key, Object value, String reStrip) {
143 String val = value.toString().replaceAll(reStrip, "");
144 properties.add(new Prop(key, val));
145 return this;
146 }
147
148 /**
149 * Adds a separator to the panel data.
150 *
151 * @return self, for chaining
152 */
153 public PropertyPanel addSeparator() {
154 properties.add(new Separator());
155 return this;
156 }
Simon Huntb745ca62015-07-28 15:37:11 -0700157
158 /**
Simon Hunt629b99e2015-07-27 17:38:33 -0700159 * Returns the title text.
160 *
161 * @return title text
162 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700163 public String title() {
164 return title;
165 }
166
Simon Hunt629b99e2015-07-27 17:38:33 -0700167 /**
168 * Returns the type identifier.
169 *
170 * @return type identifier
171 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700172 public String typeId() {
173 return typeId;
174 }
175
Simon Hunt629b99e2015-07-27 17:38:33 -0700176 /**
Simon Huntb745ca62015-07-28 15:37:11 -0700177 * Returns the internal ID.
178 *
179 * @return the ID
180 */
181 public String id() {
182 return id;
183 }
184
185 /**
Simon Hunt629b99e2015-07-27 17:38:33 -0700186 * Returns the list of properties to be displayed.
187 *
188 * @return the property list
189 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700190 // TODO: consider protecting this?
191 public List<Prop> properties() {
192 return properties;
193 }
194
Simon Huntfb940112015-07-29 18:36:35 -0700195 /**
196 * Returns the list of button descriptors.
197 *
198 * @return the button list
199 */
200 // TODO: consider protecting this?
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700201 public List<ButtonId> buttons() {
Simon Huntfb940112015-07-29 18:36:35 -0700202 return buttons;
203 }
204
Simon Hunt629b99e2015-07-27 17:38:33 -0700205 // == MUTATORS
206
207 /**
208 * Sets the title text.
209 *
210 * @param title title text
211 * @return self, for chaining
212 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700213 public PropertyPanel title(String title) {
214 this.title = title;
215 return this;
216 }
217
Simon Hunt629b99e2015-07-27 17:38:33 -0700218 /**
219 * Sets the type identifier (icon ID).
220 *
221 * @param typeId type identifier
222 * @return self, for chaining
223 */
224 public PropertyPanel typeId(String typeId) {
225 this.typeId = typeId;
226 return this;
227 }
Simon Hunt0af1ec32015-07-24 12:17:55 -0700228
Simon Hunt629b99e2015-07-27 17:38:33 -0700229 /**
230 * Removes properties with the given keys from the list.
231 *
232 * @param keys keys of properties to remove
233 * @return self, for chaining
234 */
235 public PropertyPanel removeProps(String... keys) {
Simon Hunt3a0598f2015-08-04 19:59:04 -0700236 Set<String> forRemoval = Sets.newHashSet(keys);
237 List<Prop> toKeep = new ArrayList<>();
Simon Hunt629b99e2015-07-27 17:38:33 -0700238 for (Prop p: properties) {
Simon Hunt3a0598f2015-08-04 19:59:04 -0700239 if (!forRemoval.contains(p.key())) {
240 toKeep.add(p);
Simon Hunt629b99e2015-07-27 17:38:33 -0700241 }
242 }
Simon Hunt3a0598f2015-08-04 19:59:04 -0700243 properties = toKeep;
Simon Hunt629b99e2015-07-27 17:38:33 -0700244 return this;
245 }
246
247 /**
248 * Removes all currently defined properties.
249 *
250 * @return self, for chaining
251 */
252 public PropertyPanel removeAllProps() {
253 properties.clear();
254 return this;
255 }
Simon Hunt0af1ec32015-07-24 12:17:55 -0700256
Simon Huntfb940112015-07-29 18:36:35 -0700257 /**
Simon Hunt3a0598f2015-08-04 19:59:04 -0700258 * Adds the given button descriptor to the panel data.
Simon Huntfb940112015-07-29 18:36:35 -0700259 *
Simon Hunt3a0598f2015-08-04 19:59:04 -0700260 * @param button button descriptor
Simon Huntfb940112015-07-29 18:36:35 -0700261 * @return self, for chaining
262 */
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700263 public PropertyPanel addButton(ButtonId button) {
Simon Hunt3a0598f2015-08-04 19:59:04 -0700264 buttons.add(button);
265 return this;
266 }
267
268 /**
269 * Removes buttons with the given descriptors from the list.
270 *
271 * @param descriptors descriptors to remove
272 * @return self, for chaining
273 */
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700274 public PropertyPanel removeButtons(ButtonId... descriptors) {
275 Set<ButtonId> forRemoval = Sets.newHashSet(descriptors);
276 List<ButtonId> toKeep = new ArrayList<>();
277 for (ButtonId bd: buttons) {
Simon Hunt3a0598f2015-08-04 19:59:04 -0700278 if (!forRemoval.contains(bd)) {
279 toKeep.add(bd);
280 }
281 }
282 buttons = toKeep;
283 return this;
284 }
285
286 /**
287 * Removes all currently defined buttons.
288 *
289 * @return self, for chaining
290 */
291 public PropertyPanel removeAllButtons() {
292 buttons.clear();
Simon Huntfb940112015-07-29 18:36:35 -0700293 return this;
294 }
295
Simon Hunt0af1ec32015-07-24 12:17:55 -0700296 // ====================
297
Simon Huntb745ca62015-07-28 15:37:11 -0700298
Simon Hunt629b99e2015-07-27 17:38:33 -0700299 /**
300 * Simple data carrier for a property, composed of a key/value pair.
301 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700302 public static class Prop {
Simon Hunt629b99e2015-07-27 17:38:33 -0700303 private final String key;
304 private final String value;
Simon Hunt0af1ec32015-07-24 12:17:55 -0700305
Simon Hunt629b99e2015-07-27 17:38:33 -0700306 /**
307 * Constructs a property data value.
308 *
309 * @param key property key
310 * @param value property value
311 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700312 public Prop(String key, String value) {
313 this.key = key;
314 this.value = value;
315 }
316
Simon Hunt629b99e2015-07-27 17:38:33 -0700317 /**
318 * Returns the property's key.
319 *
320 * @return the key
321 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700322 public String key() {
323 return key;
324 }
325
Simon Hunt629b99e2015-07-27 17:38:33 -0700326 /**
327 * Returns the property's value.
328 *
329 * @return the value
330 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700331 public String value() {
332 return value;
333 }
Simon Hunt629b99e2015-07-27 17:38:33 -0700334
335 @Override
336 public boolean equals(Object o) {
337 if (this == o) {
338 return true;
339 }
340 if (o == null || getClass() != o.getClass()) {
341 return false;
342 }
343
344 Prop prop = (Prop) o;
345 return key.equals(prop.key) && value.equals(prop.value);
346 }
347
348 @Override
349 public int hashCode() {
350 int result = key.hashCode();
351 result = 31 * result + value.hashCode();
352 return result;
353 }
354
355 @Override
356 public String toString() {
357 return "{" + key + " -> " + value + "}";
358 }
Simon Hunt0af1ec32015-07-24 12:17:55 -0700359 }
360
Simon Hunt629b99e2015-07-27 17:38:33 -0700361 /**
362 * Auxiliary class representing a separator property.
363 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700364 public static class Separator extends Prop {
365 public Separator() {
366 super("-", "");
367 }
368 }
369
Simon Hunt0af1ec32015-07-24 12:17:55 -0700370}