blob: 03ff2a3b1536c5898e7ef81ccd4e6dc8bf14f8f8 [file] [log] [blame]
Simon Hunt0af1ec32015-07-24 12:17:55 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
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.
15 *
16 */
17
18package org.onosproject.ui.topo;
19
Simon Hunt629b99e2015-07-27 17:38:33 -070020import com.google.common.collect.Sets;
21
Simon Huntb745ca62015-07-28 15:37:11 -070022import java.text.DecimalFormat;
Simon Hunt0af1ec32015-07-24 12:17:55 -070023import java.util.ArrayList;
24import java.util.List;
Simon Hunt629b99e2015-07-27 17:38:33 -070025import java.util.Set;
Simon Hunt0af1ec32015-07-24 12:17:55 -070026
27/**
28 * Models a panel displayed on the Topology View.
29 */
30public class PropertyPanel {
31
Simon Hunt00a27ff2015-07-28 18:53:40 -070032 private static final DecimalFormat DF0 = new DecimalFormat("#,###");
33
Simon Hunt0af1ec32015-07-24 12:17:55 -070034 private String title;
35 private String typeId;
Simon Huntb745ca62015-07-28 15:37:11 -070036 private String id;
Simon Hunt0af1ec32015-07-24 12:17:55 -070037 private List<Prop> properties = new ArrayList<>();
Simon Huntfb940112015-07-29 18:36:35 -070038 private List<Button> 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 *
44 * @param title title text
45 * @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 Huntb745ca62015-07-28 15:37:11 -070053 * Adds an ID field to the panel data, to be included in
54 * the returned JSON data to the client.
55 *
56 * @param id the identifier
57 * @return self, for chaining
58 */
59 public PropertyPanel id(String id) {
60 this.id = id;
61 return this;
62 }
63
Simon Hunt00a27ff2015-07-28 18:53:40 -070064 /**
65 * Adds a property to the panel data.
66 *
67 * @param key property key
68 * @param value property value
69 * @return self, for chaining
70 */
71 public PropertyPanel addProp(String key, String value) {
72 properties.add(new Prop(key, value));
73 return this;
74 }
75
76 /**
77 * Adds a property to the panel data, using a decimal formatter.
78 *
79 * @param key property key
80 * @param value property value
81 * @return self, for chaining
82 */
83 public PropertyPanel addProp(String key, int value) {
84 properties.add(new Prop(key, DF0.format(value)));
85 return this;
86 }
87
88 /**
89 * Adds a property to the panel data, using a decimal formatter.
90 *
91 * @param key property key
92 * @param value property value
93 * @return self, for chaining
94 */
95 public PropertyPanel addProp(String key, long value) {
96 properties.add(new Prop(key, DF0.format(value)));
97 return this;
98 }
99
100 /**
101 * Adds a property to the panel data. Note that the value's
102 * {@link Object#toString toString()} method is used to convert the
103 * value to a string.
104 *
105 * @param key property key
106 * @param value property value
107 * @return self, for chaining
108 */
109 public PropertyPanel addProp(String key, Object value) {
110 properties.add(new Prop(key, value.toString()));
111 return this;
112 }
113
114 /**
115 * Adds a property to the panel data. Note that the value's
116 * {@link Object#toString toString()} method is used to convert the
117 * value to a string, from which the characters defined in the given
118 * regular expression string are stripped.
119 *
120 * @param key property key
121 * @param value property value
122 * @param reStrip regexp characters to strip from value string
123 * @return self, for chaining
124 */
125 public PropertyPanel addProp(String key, Object value, String reStrip) {
126 String val = value.toString().replaceAll(reStrip, "");
127 properties.add(new Prop(key, val));
128 return this;
129 }
130
131 /**
132 * Adds a separator to the panel data.
133 *
134 * @return self, for chaining
135 */
136 public PropertyPanel addSeparator() {
137 properties.add(new Separator());
138 return this;
139 }
Simon Huntb745ca62015-07-28 15:37:11 -0700140
141 /**
Simon Hunt629b99e2015-07-27 17:38:33 -0700142 * Returns the title text.
143 *
144 * @return title text
145 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700146 public String title() {
147 return title;
148 }
149
Simon Hunt629b99e2015-07-27 17:38:33 -0700150 /**
151 * Returns the type identifier.
152 *
153 * @return type identifier
154 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700155 public String typeId() {
156 return typeId;
157 }
158
Simon Hunt629b99e2015-07-27 17:38:33 -0700159 /**
Simon Huntb745ca62015-07-28 15:37:11 -0700160 * Returns the internal ID.
161 *
162 * @return the ID
163 */
164 public String id() {
165 return id;
166 }
167
168 /**
Simon Hunt629b99e2015-07-27 17:38:33 -0700169 * Returns the list of properties to be displayed.
170 *
171 * @return the property list
172 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700173 // TODO: consider protecting this?
174 public List<Prop> properties() {
175 return properties;
176 }
177
Simon Huntfb940112015-07-29 18:36:35 -0700178 /**
179 * Returns the list of button descriptors.
180 *
181 * @return the button list
182 */
183 // TODO: consider protecting this?
184 public List<Button> buttons() {
185 return buttons;
186 }
187
Simon Hunt629b99e2015-07-27 17:38:33 -0700188 // == MUTATORS
189
190 /**
191 * Sets the title text.
192 *
193 * @param title title text
194 * @return self, for chaining
195 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700196 public PropertyPanel title(String title) {
197 this.title = title;
198 return this;
199 }
200
Simon Hunt629b99e2015-07-27 17:38:33 -0700201 /**
202 * Sets the type identifier (icon ID).
203 *
204 * @param typeId type identifier
205 * @return self, for chaining
206 */
207 public PropertyPanel typeId(String typeId) {
208 this.typeId = typeId;
209 return this;
210 }
Simon Hunt0af1ec32015-07-24 12:17:55 -0700211
Simon Hunt629b99e2015-07-27 17:38:33 -0700212 /**
213 * Removes properties with the given keys from the list.
214 *
215 * @param keys keys of properties to remove
216 * @return self, for chaining
217 */
218 public PropertyPanel removeProps(String... keys) {
219 Set<String> keysForRemoval = Sets.newHashSet(keys);
220 List<Prop> propsToKeep = new ArrayList<>();
221 for (Prop p: properties) {
222 if (!keysForRemoval.contains(p.key())) {
223 propsToKeep.add(p);
224 }
225 }
226 properties = propsToKeep;
227 return this;
228 }
229
230 /**
231 * Removes all currently defined properties.
232 *
233 * @return self, for chaining
234 */
235 public PropertyPanel removeAllProps() {
236 properties.clear();
237 return this;
238 }
Simon Hunt0af1ec32015-07-24 12:17:55 -0700239
Simon Huntfb940112015-07-29 18:36:35 -0700240 /**
241 * Adds a button descriptor with the given identifier, to the panel data.
242 *
243 * @param id button identifier
244 * @return self, for chaining
245 */
246 public PropertyPanel addButton(String id) {
247 buttons.add(new Button(id));
248 return this;
249 }
250
Simon Hunt0af1ec32015-07-24 12:17:55 -0700251 // ====================
252
Simon Huntb745ca62015-07-28 15:37:11 -0700253
Simon Hunt629b99e2015-07-27 17:38:33 -0700254 /**
255 * Simple data carrier for a property, composed of a key/value pair.
256 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700257 public static class Prop {
Simon Hunt629b99e2015-07-27 17:38:33 -0700258 private final String key;
259 private final String value;
Simon Hunt0af1ec32015-07-24 12:17:55 -0700260
Simon Hunt629b99e2015-07-27 17:38:33 -0700261 /**
262 * Constructs a property data value.
263 *
264 * @param key property key
265 * @param value property value
266 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700267 public Prop(String key, String value) {
268 this.key = key;
269 this.value = value;
270 }
271
Simon Hunt629b99e2015-07-27 17:38:33 -0700272 /**
273 * Returns the property's key.
274 *
275 * @return the key
276 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700277 public String key() {
278 return key;
279 }
280
Simon Hunt629b99e2015-07-27 17:38:33 -0700281 /**
282 * Returns the property's value.
283 *
284 * @return the value
285 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700286 public String value() {
287 return value;
288 }
Simon Hunt629b99e2015-07-27 17:38:33 -0700289
290 @Override
291 public boolean equals(Object o) {
292 if (this == o) {
293 return true;
294 }
295 if (o == null || getClass() != o.getClass()) {
296 return false;
297 }
298
299 Prop prop = (Prop) o;
300 return key.equals(prop.key) && value.equals(prop.value);
301 }
302
303 @Override
304 public int hashCode() {
305 int result = key.hashCode();
306 result = 31 * result + value.hashCode();
307 return result;
308 }
309
310 @Override
311 public String toString() {
312 return "{" + key + " -> " + value + "}";
313 }
Simon Hunt0af1ec32015-07-24 12:17:55 -0700314 }
315
Simon Hunt629b99e2015-07-27 17:38:33 -0700316 /**
317 * Auxiliary class representing a separator property.
318 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700319 public static class Separator extends Prop {
320 public Separator() {
321 super("-", "");
322 }
323 }
324
Simon Huntfb940112015-07-29 18:36:35 -0700325 /**
326 * Button descriptor. Note that these work in conjunction with
327 * "buttons" defined in the JavaScript code for the overlay.
328 */
329 public static class Button {
330 private final String id;
331
332 /**
333 * Constructs a button descriptor with the given identifier.
334 *
335 * @param id button identifier
336 */
337 public Button(String id) {
338 this.id = id;
339 }
340
341 /**
342 * Returns the identifier for this button.
343 *
344 * @return button identifier
345 */
346 public String id() {
347 return id;
348 }
349 }
Simon Hunt0af1ec32015-07-24 12:17:55 -0700350}