blob: 87b6839282ad8e5d4a7ef66e2a727a704977ebe6 [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<>();
38
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 Hunt629b99e2015-07-27 17:38:33 -0700177 // == MUTATORS
178
179 /**
180 * Sets the title text.
181 *
182 * @param title title text
183 * @return self, for chaining
184 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700185 public PropertyPanel title(String title) {
186 this.title = title;
187 return this;
188 }
189
Simon Hunt629b99e2015-07-27 17:38:33 -0700190 /**
191 * Sets the type identifier (icon ID).
192 *
193 * @param typeId type identifier
194 * @return self, for chaining
195 */
196 public PropertyPanel typeId(String typeId) {
197 this.typeId = typeId;
198 return this;
199 }
Simon Hunt0af1ec32015-07-24 12:17:55 -0700200
Simon Hunt629b99e2015-07-27 17:38:33 -0700201 /**
202 * Removes properties with the given keys from the list.
203 *
204 * @param keys keys of properties to remove
205 * @return self, for chaining
206 */
207 public PropertyPanel removeProps(String... keys) {
208 Set<String> keysForRemoval = Sets.newHashSet(keys);
209 List<Prop> propsToKeep = new ArrayList<>();
210 for (Prop p: properties) {
211 if (!keysForRemoval.contains(p.key())) {
212 propsToKeep.add(p);
213 }
214 }
215 properties = propsToKeep;
216 return this;
217 }
218
219 /**
220 * Removes all currently defined properties.
221 *
222 * @return self, for chaining
223 */
224 public PropertyPanel removeAllProps() {
225 properties.clear();
226 return this;
227 }
Simon Hunt0af1ec32015-07-24 12:17:55 -0700228
229 // ====================
230
Simon Huntb745ca62015-07-28 15:37:11 -0700231
Simon Hunt629b99e2015-07-27 17:38:33 -0700232 /**
233 * Simple data carrier for a property, composed of a key/value pair.
234 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700235 public static class Prop {
Simon Hunt629b99e2015-07-27 17:38:33 -0700236 private final String key;
237 private final String value;
Simon Hunt0af1ec32015-07-24 12:17:55 -0700238
Simon Hunt629b99e2015-07-27 17:38:33 -0700239 /**
240 * Constructs a property data value.
241 *
242 * @param key property key
243 * @param value property value
244 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700245 public Prop(String key, String value) {
246 this.key = key;
247 this.value = value;
248 }
249
Simon Hunt629b99e2015-07-27 17:38:33 -0700250 /**
251 * Returns the property's key.
252 *
253 * @return the key
254 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700255 public String key() {
256 return key;
257 }
258
Simon Hunt629b99e2015-07-27 17:38:33 -0700259 /**
260 * Returns the property's value.
261 *
262 * @return the value
263 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700264 public String value() {
265 return value;
266 }
Simon Hunt629b99e2015-07-27 17:38:33 -0700267
268 @Override
269 public boolean equals(Object o) {
270 if (this == o) {
271 return true;
272 }
273 if (o == null || getClass() != o.getClass()) {
274 return false;
275 }
276
277 Prop prop = (Prop) o;
278 return key.equals(prop.key) && value.equals(prop.value);
279 }
280
281 @Override
282 public int hashCode() {
283 int result = key.hashCode();
284 result = 31 * result + value.hashCode();
285 return result;
286 }
287
288 @Override
289 public String toString() {
290 return "{" + key + " -> " + value + "}";
291 }
Simon Hunt0af1ec32015-07-24 12:17:55 -0700292 }
293
Simon Hunt629b99e2015-07-27 17:38:33 -0700294 /**
295 * Auxiliary class representing a separator property.
296 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700297 public static class Separator extends Prop {
298 public Separator() {
299 super("-", "");
300 }
301 }
302
303}