blob: 43364eab544b934fbb636645fbb5c9520eee9987 [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 Hunt3a0598f2015-08-04 19:59:04 -070038 private List<ButtonDescriptor> 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?
Simon Hunt3a0598f2015-08-04 19:59:04 -0700184 public List<ButtonDescriptor> buttons() {
Simon Huntfb940112015-07-29 18:36:35 -0700185 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) {
Simon Hunt3a0598f2015-08-04 19:59:04 -0700219 Set<String> forRemoval = Sets.newHashSet(keys);
220 List<Prop> toKeep = new ArrayList<>();
Simon Hunt629b99e2015-07-27 17:38:33 -0700221 for (Prop p: properties) {
Simon Hunt3a0598f2015-08-04 19:59:04 -0700222 if (!forRemoval.contains(p.key())) {
223 toKeep.add(p);
Simon Hunt629b99e2015-07-27 17:38:33 -0700224 }
225 }
Simon Hunt3a0598f2015-08-04 19:59:04 -0700226 properties = toKeep;
Simon Hunt629b99e2015-07-27 17:38:33 -0700227 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 /**
Simon Hunt3a0598f2015-08-04 19:59:04 -0700241 * Adds the given button descriptor to the panel data.
Simon Huntfb940112015-07-29 18:36:35 -0700242 *
Simon Hunt3a0598f2015-08-04 19:59:04 -0700243 * @param button button descriptor
Simon Huntfb940112015-07-29 18:36:35 -0700244 * @return self, for chaining
245 */
Simon Hunt3a0598f2015-08-04 19:59:04 -0700246 public PropertyPanel addButton(ButtonDescriptor button) {
247 buttons.add(button);
248 return this;
249 }
250
251 /**
252 * Removes buttons with the given descriptors from the list.
253 *
254 * @param descriptors descriptors to remove
255 * @return self, for chaining
256 */
257 public PropertyPanel removeButtons(ButtonDescriptor... descriptors) {
258 Set<ButtonDescriptor> forRemoval = Sets.newHashSet(descriptors);
259 List<ButtonDescriptor> toKeep = new ArrayList<>();
260 for (ButtonDescriptor bd: buttons) {
261 if (!forRemoval.contains(bd)) {
262 toKeep.add(bd);
263 }
264 }
265 buttons = toKeep;
266 return this;
267 }
268
269 /**
270 * Removes all currently defined buttons.
271 *
272 * @return self, for chaining
273 */
274 public PropertyPanel removeAllButtons() {
275 buttons.clear();
Simon Huntfb940112015-07-29 18:36:35 -0700276 return this;
277 }
278
Simon Hunt0af1ec32015-07-24 12:17:55 -0700279 // ====================
280
Simon Huntb745ca62015-07-28 15:37:11 -0700281
Simon Hunt629b99e2015-07-27 17:38:33 -0700282 /**
283 * Simple data carrier for a property, composed of a key/value pair.
284 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700285 public static class Prop {
Simon Hunt629b99e2015-07-27 17:38:33 -0700286 private final String key;
287 private final String value;
Simon Hunt0af1ec32015-07-24 12:17:55 -0700288
Simon Hunt629b99e2015-07-27 17:38:33 -0700289 /**
290 * Constructs a property data value.
291 *
292 * @param key property key
293 * @param value property value
294 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700295 public Prop(String key, String value) {
296 this.key = key;
297 this.value = value;
298 }
299
Simon Hunt629b99e2015-07-27 17:38:33 -0700300 /**
301 * Returns the property's key.
302 *
303 * @return the key
304 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700305 public String key() {
306 return key;
307 }
308
Simon Hunt629b99e2015-07-27 17:38:33 -0700309 /**
310 * Returns the property's value.
311 *
312 * @return the value
313 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700314 public String value() {
315 return value;
316 }
Simon Hunt629b99e2015-07-27 17:38:33 -0700317
318 @Override
319 public boolean equals(Object o) {
320 if (this == o) {
321 return true;
322 }
323 if (o == null || getClass() != o.getClass()) {
324 return false;
325 }
326
327 Prop prop = (Prop) o;
328 return key.equals(prop.key) && value.equals(prop.value);
329 }
330
331 @Override
332 public int hashCode() {
333 int result = key.hashCode();
334 result = 31 * result + value.hashCode();
335 return result;
336 }
337
338 @Override
339 public String toString() {
340 return "{" + key + " -> " + value + "}";
341 }
Simon Hunt0af1ec32015-07-24 12:17:55 -0700342 }
343
Simon Hunt629b99e2015-07-27 17:38:33 -0700344 /**
345 * Auxiliary class representing a separator property.
346 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700347 public static class Separator extends Prop {
348 public Separator() {
349 super("-", "");
350 }
351 }
352
Simon Hunt0af1ec32015-07-24 12:17:55 -0700353}