blob: a165be33b5ae66785f02f10a8908e958595bca37 [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.
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 Huntb745ca62015-07-28 15:37:11 -070021import java.text.DecimalFormat;
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 Hunt00a27ff2015-07-28 18:53:40 -070031 private static final DecimalFormat DF0 = new DecimalFormat("#,###");
32
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 Huntb745ca62015-07-28 15:37:11 -070052 * Adds an ID field to the panel data, to be included in
53 * the returned JSON data to the client.
54 *
55 * @param id the identifier
56 * @return self, for chaining
57 */
58 public PropertyPanel id(String id) {
59 this.id = id;
60 return this;
61 }
62
Simon Hunt00a27ff2015-07-28 18:53:40 -070063 /**
64 * Adds a property to the panel data.
65 *
66 * @param key property key
67 * @param value property value
68 * @return self, for chaining
69 */
70 public PropertyPanel addProp(String key, String value) {
71 properties.add(new Prop(key, value));
72 return this;
73 }
74
75 /**
76 * Adds a property to the panel data, using a decimal formatter.
77 *
78 * @param key property key
79 * @param value property value
80 * @return self, for chaining
81 */
82 public PropertyPanel addProp(String key, int value) {
83 properties.add(new Prop(key, DF0.format(value)));
84 return this;
85 }
86
87 /**
88 * Adds a property to the panel data, using a decimal formatter.
89 *
90 * @param key property key
91 * @param value property value
92 * @return self, for chaining
93 */
94 public PropertyPanel addProp(String key, long value) {
95 properties.add(new Prop(key, DF0.format(value)));
96 return this;
97 }
98
99 /**
100 * Adds a property to the panel data. Note that the value's
101 * {@link Object#toString toString()} method is used to convert the
102 * value to a string.
103 *
104 * @param key property key
105 * @param value property value
106 * @return self, for chaining
107 */
108 public PropertyPanel addProp(String key, Object value) {
109 properties.add(new Prop(key, value.toString()));
110 return this;
111 }
112
113 /**
114 * Adds a property to the panel data. Note that the value's
115 * {@link Object#toString toString()} method is used to convert the
116 * value to a string, from which the characters defined in the given
117 * regular expression string are stripped.
118 *
119 * @param key property key
120 * @param value property value
121 * @param reStrip regexp characters to strip from value string
122 * @return self, for chaining
123 */
124 public PropertyPanel addProp(String key, Object value, String reStrip) {
125 String val = value.toString().replaceAll(reStrip, "");
126 properties.add(new Prop(key, val));
127 return this;
128 }
129
130 /**
131 * Adds a separator to the panel data.
132 *
133 * @return self, for chaining
134 */
135 public PropertyPanel addSeparator() {
136 properties.add(new Separator());
137 return this;
138 }
Simon Huntb745ca62015-07-28 15:37:11 -0700139
140 /**
Simon Hunt629b99e2015-07-27 17:38:33 -0700141 * Returns the title text.
142 *
143 * @return title text
144 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700145 public String title() {
146 return title;
147 }
148
Simon Hunt629b99e2015-07-27 17:38:33 -0700149 /**
150 * Returns the type identifier.
151 *
152 * @return type identifier
153 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700154 public String typeId() {
155 return typeId;
156 }
157
Simon Hunt629b99e2015-07-27 17:38:33 -0700158 /**
Simon Huntb745ca62015-07-28 15:37:11 -0700159 * Returns the internal ID.
160 *
161 * @return the ID
162 */
163 public String id() {
164 return id;
165 }
166
167 /**
Simon Hunt629b99e2015-07-27 17:38:33 -0700168 * Returns the list of properties to be displayed.
169 *
170 * @return the property list
171 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700172 // TODO: consider protecting this?
173 public List<Prop> properties() {
174 return properties;
175 }
176
Simon Huntfb940112015-07-29 18:36:35 -0700177 /**
178 * Returns the list of button descriptors.
179 *
180 * @return the button list
181 */
182 // TODO: consider protecting this?
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700183 public List<ButtonId> buttons() {
Simon Huntfb940112015-07-29 18:36:35 -0700184 return buttons;
185 }
186
Simon Hunt629b99e2015-07-27 17:38:33 -0700187 // == MUTATORS
188
189 /**
190 * Sets the title text.
191 *
192 * @param title title text
193 * @return self, for chaining
194 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700195 public PropertyPanel title(String title) {
196 this.title = title;
197 return this;
198 }
199
Simon Hunt629b99e2015-07-27 17:38:33 -0700200 /**
201 * Sets the type identifier (icon ID).
202 *
203 * @param typeId type identifier
204 * @return self, for chaining
205 */
206 public PropertyPanel typeId(String typeId) {
207 this.typeId = typeId;
208 return this;
209 }
Simon Hunt0af1ec32015-07-24 12:17:55 -0700210
Simon Hunt629b99e2015-07-27 17:38:33 -0700211 /**
212 * Removes properties with the given keys from the list.
213 *
214 * @param keys keys of properties to remove
215 * @return self, for chaining
216 */
217 public PropertyPanel removeProps(String... keys) {
Simon Hunt3a0598f2015-08-04 19:59:04 -0700218 Set<String> forRemoval = Sets.newHashSet(keys);
219 List<Prop> toKeep = new ArrayList<>();
Simon Hunt629b99e2015-07-27 17:38:33 -0700220 for (Prop p: properties) {
Simon Hunt3a0598f2015-08-04 19:59:04 -0700221 if (!forRemoval.contains(p.key())) {
222 toKeep.add(p);
Simon Hunt629b99e2015-07-27 17:38:33 -0700223 }
224 }
Simon Hunt3a0598f2015-08-04 19:59:04 -0700225 properties = toKeep;
Simon Hunt629b99e2015-07-27 17:38:33 -0700226 return this;
227 }
228
229 /**
230 * Removes all currently defined properties.
231 *
232 * @return self, for chaining
233 */
234 public PropertyPanel removeAllProps() {
235 properties.clear();
236 return this;
237 }
Simon Hunt0af1ec32015-07-24 12:17:55 -0700238
Simon Huntfb940112015-07-29 18:36:35 -0700239 /**
Simon Hunt3a0598f2015-08-04 19:59:04 -0700240 * Adds the given button descriptor to the panel data.
Simon Huntfb940112015-07-29 18:36:35 -0700241 *
Simon Hunt3a0598f2015-08-04 19:59:04 -0700242 * @param button button descriptor
Simon Huntfb940112015-07-29 18:36:35 -0700243 * @return self, for chaining
244 */
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700245 public PropertyPanel addButton(ButtonId button) {
Simon Hunt3a0598f2015-08-04 19:59:04 -0700246 buttons.add(button);
247 return this;
248 }
249
250 /**
251 * Removes buttons with the given descriptors from the list.
252 *
253 * @param descriptors descriptors to remove
254 * @return self, for chaining
255 */
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700256 public PropertyPanel removeButtons(ButtonId... descriptors) {
257 Set<ButtonId> forRemoval = Sets.newHashSet(descriptors);
258 List<ButtonId> toKeep = new ArrayList<>();
259 for (ButtonId bd: buttons) {
Simon Hunt3a0598f2015-08-04 19:59:04 -0700260 if (!forRemoval.contains(bd)) {
261 toKeep.add(bd);
262 }
263 }
264 buttons = toKeep;
265 return this;
266 }
267
268 /**
269 * Removes all currently defined buttons.
270 *
271 * @return self, for chaining
272 */
273 public PropertyPanel removeAllButtons() {
274 buttons.clear();
Simon Huntfb940112015-07-29 18:36:35 -0700275 return this;
276 }
277
Simon Hunt0af1ec32015-07-24 12:17:55 -0700278 // ====================
279
Simon Huntb745ca62015-07-28 15:37:11 -0700280
Simon Hunt629b99e2015-07-27 17:38:33 -0700281 /**
282 * Simple data carrier for a property, composed of a key/value pair.
283 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700284 public static class Prop {
Simon Hunt629b99e2015-07-27 17:38:33 -0700285 private final String key;
286 private final String value;
Simon Hunt0af1ec32015-07-24 12:17:55 -0700287
Simon Hunt629b99e2015-07-27 17:38:33 -0700288 /**
289 * Constructs a property data value.
290 *
291 * @param key property key
292 * @param value property value
293 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700294 public Prop(String key, String value) {
295 this.key = key;
296 this.value = value;
297 }
298
Simon Hunt629b99e2015-07-27 17:38:33 -0700299 /**
300 * Returns the property's key.
301 *
302 * @return the key
303 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700304 public String key() {
305 return key;
306 }
307
Simon Hunt629b99e2015-07-27 17:38:33 -0700308 /**
309 * Returns the property's value.
310 *
311 * @return the value
312 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700313 public String value() {
314 return value;
315 }
Simon Hunt629b99e2015-07-27 17:38:33 -0700316
317 @Override
318 public boolean equals(Object o) {
319 if (this == o) {
320 return true;
321 }
322 if (o == null || getClass() != o.getClass()) {
323 return false;
324 }
325
326 Prop prop = (Prop) o;
327 return key.equals(prop.key) && value.equals(prop.value);
328 }
329
330 @Override
331 public int hashCode() {
332 int result = key.hashCode();
333 result = 31 * result + value.hashCode();
334 return result;
335 }
336
337 @Override
338 public String toString() {
339 return "{" + key + " -> " + value + "}";
340 }
Simon Hunt0af1ec32015-07-24 12:17:55 -0700341 }
342
Simon Hunt629b99e2015-07-27 17:38:33 -0700343 /**
344 * Auxiliary class representing a separator property.
345 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700346 public static class Separator extends Prop {
347 public Separator() {
348 super("-", "");
349 }
350 }
351
Simon Hunt0af1ec32015-07-24 12:17:55 -0700352}