Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 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 Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package org.onosproject.ui.topo; |
| 18 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
| 20 | |
Simon Hunt | cd15027 | 2015-11-04 14:09:36 -0800 | [diff] [blame] | 21 | import java.text.NumberFormat; |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 22 | import java.util.ArrayList; |
| 23 | import java.util.List; |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 24 | import java.util.Set; |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Models a panel displayed on the Topology View. |
| 28 | */ |
| 29 | public class PropertyPanel { |
| 30 | |
Simon Hunt | cd15027 | 2015-11-04 14:09:36 -0800 | [diff] [blame] | 31 | private static final NumberFormat NF = NumberFormat.getInstance(); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 32 | |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 33 | private String title; |
| 34 | private String typeId; |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 35 | private String id; |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 36 | private String navPath; |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 37 | private List<Prop> properties = new ArrayList<>(); |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 38 | private List<ButtonId> buttons = new ArrayList<>(); |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 39 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 40 | /** |
| 41 | * Constructs a property panel model with the given title and |
| 42 | * type identifier (icon to display). |
| 43 | * |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 44 | * @param title title text |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 45 | * @param typeId type (icon) ID |
| 46 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 47 | public PropertyPanel(String title, String typeId) { |
| 48 | this.title = title; |
| 49 | this.typeId = typeId; |
| 50 | } |
| 51 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 52 | /** |
Simon Hunt | cd15027 | 2015-11-04 14:09:36 -0800 | [diff] [blame] | 53 | * Returns a number formatter to use for formatting integer and long |
| 54 | * property values. |
| 55 | * <p> |
| 56 | * This default implementation uses a formatter for the default |
| 57 | * locale. For example: |
| 58 | * <pre> |
| 59 | * Locale.ENGLISH : 1000 -> "1,000" |
| 60 | * Locale.FRENCH : 1000 -> "1 000" |
| 61 | * Locale.GERMAN : 1000 -> "1.000" |
| 62 | * </pre> |
| 63 | * |
| 64 | * @return the number formatter |
| 65 | */ |
| 66 | protected NumberFormat formatter() { |
| 67 | return NF; |
| 68 | } |
| 69 | |
| 70 | /** |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 71 | * Adds a navigation path field to the panel data, to be included in |
| 72 | * the returned JSON data to the client. This is typically used to |
| 73 | * configure the topology view with the appropriate navigation path for |
| 74 | * a hot-link to some other view. |
| 75 | * |
| 76 | * @param navPath the navigation path |
| 77 | * @return self for chaining |
| 78 | */ |
| 79 | public PropertyPanel navPath(String navPath) { |
| 80 | this.navPath = navPath; |
| 81 | return this; |
| 82 | } |
| 83 | |
| 84 | /** |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 85 | * Adds an ID field to the panel data, to be included in |
| 86 | * the returned JSON data to the client. |
| 87 | * |
| 88 | * @param id the identifier |
| 89 | * @return self, for chaining |
| 90 | */ |
| 91 | public PropertyPanel id(String id) { |
| 92 | this.id = id; |
| 93 | return this; |
| 94 | } |
| 95 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 96 | /** |
| 97 | * Adds a property to the panel data. |
| 98 | * |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 99 | * @param key property key |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 100 | * @param value property value |
| 101 | * @return self, for chaining |
| 102 | */ |
| 103 | public PropertyPanel addProp(String key, String value) { |
| 104 | properties.add(new Prop(key, value)); |
| 105 | return this; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Adds a property to the panel data, using a decimal formatter. |
| 110 | * |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 111 | * @param key property key |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 112 | * @param value property value |
| 113 | * @return self, for chaining |
| 114 | */ |
| 115 | public PropertyPanel addProp(String key, int value) { |
Simon Hunt | cd15027 | 2015-11-04 14:09:36 -0800 | [diff] [blame] | 116 | properties.add(new Prop(key, formatter().format(value))); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 117 | return this; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Adds a property to the panel data, using a decimal formatter. |
| 122 | * |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 123 | * @param key property key |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 124 | * @param value property value |
| 125 | * @return self, for chaining |
| 126 | */ |
| 127 | public PropertyPanel addProp(String key, long value) { |
Simon Hunt | cd15027 | 2015-11-04 14:09:36 -0800 | [diff] [blame] | 128 | properties.add(new Prop(key, formatter().format(value))); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 129 | return this; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Adds a property to the panel data. Note that the value's |
| 134 | * {@link Object#toString toString()} method is used to convert the |
| 135 | * value to a string. |
| 136 | * |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 137 | * @param key property key |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 138 | * @param value property value |
| 139 | * @return self, for chaining |
| 140 | */ |
| 141 | public PropertyPanel addProp(String key, Object value) { |
| 142 | properties.add(new Prop(key, value.toString())); |
| 143 | return this; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Adds a property to the panel data. Note that the value's |
| 148 | * {@link Object#toString toString()} method is used to convert the |
| 149 | * value to a string, from which the characters defined in the given |
| 150 | * regular expression string are stripped. |
| 151 | * |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 152 | * @param key property key |
| 153 | * @param value property value |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 154 | * @param reStrip regexp characters to strip from value string |
| 155 | * @return self, for chaining |
| 156 | */ |
| 157 | public PropertyPanel addProp(String key, Object value, String reStrip) { |
| 158 | String val = value.toString().replaceAll(reStrip, ""); |
| 159 | properties.add(new Prop(key, val)); |
| 160 | return this; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Adds a separator to the panel data. |
| 165 | * |
| 166 | * @return self, for chaining |
| 167 | */ |
| 168 | public PropertyPanel addSeparator() { |
| 169 | properties.add(new Separator()); |
| 170 | return this; |
| 171 | } |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 172 | |
| 173 | /** |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 174 | * Returns the title text. |
| 175 | * |
| 176 | * @return title text |
| 177 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 178 | public String title() { |
| 179 | return title; |
| 180 | } |
| 181 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 182 | /** |
| 183 | * Returns the type identifier. |
| 184 | * |
| 185 | * @return type identifier |
| 186 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 187 | public String typeId() { |
| 188 | return typeId; |
| 189 | } |
| 190 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 191 | /** |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 192 | * Returns the navigation path. |
| 193 | * |
| 194 | * @return the navigation path |
| 195 | */ |
| 196 | public String navPath() { |
| 197 | return navPath; |
| 198 | } |
| 199 | |
| 200 | /** |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 201 | * Returns the internal ID. |
| 202 | * |
| 203 | * @return the ID |
| 204 | */ |
| 205 | public String id() { |
| 206 | return id; |
| 207 | } |
| 208 | |
| 209 | /** |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 210 | * Returns the list of properties to be displayed. |
| 211 | * |
| 212 | * @return the property list |
| 213 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 214 | // TODO: consider protecting this? |
| 215 | public List<Prop> properties() { |
| 216 | return properties; |
| 217 | } |
| 218 | |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 219 | /** |
| 220 | * Returns the list of button descriptors. |
| 221 | * |
| 222 | * @return the button list |
| 223 | */ |
| 224 | // TODO: consider protecting this? |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 225 | public List<ButtonId> buttons() { |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 226 | return buttons; |
| 227 | } |
| 228 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 229 | // == MUTATORS |
| 230 | |
| 231 | /** |
| 232 | * Sets the title text. |
| 233 | * |
| 234 | * @param title title text |
| 235 | * @return self, for chaining |
| 236 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 237 | public PropertyPanel title(String title) { |
| 238 | this.title = title; |
| 239 | return this; |
| 240 | } |
| 241 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 242 | /** |
| 243 | * Sets the type identifier (icon ID). |
| 244 | * |
| 245 | * @param typeId type identifier |
| 246 | * @return self, for chaining |
| 247 | */ |
| 248 | public PropertyPanel typeId(String typeId) { |
| 249 | this.typeId = typeId; |
| 250 | return this; |
| 251 | } |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 252 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 253 | /** |
| 254 | * Removes properties with the given keys from the list. |
| 255 | * |
| 256 | * @param keys keys of properties to remove |
| 257 | * @return self, for chaining |
| 258 | */ |
| 259 | public PropertyPanel removeProps(String... keys) { |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 260 | Set<String> forRemoval = Sets.newHashSet(keys); |
| 261 | List<Prop> toKeep = new ArrayList<>(); |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 262 | for (Prop p : properties) { |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 263 | if (!forRemoval.contains(p.key())) { |
| 264 | toKeep.add(p); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 267 | properties = toKeep; |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 268 | return this; |
| 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Removes all currently defined properties. |
| 273 | * |
| 274 | * @return self, for chaining |
| 275 | */ |
| 276 | public PropertyPanel removeAllProps() { |
| 277 | properties.clear(); |
| 278 | return this; |
| 279 | } |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 280 | |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 281 | /** |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 282 | * Adds the given button descriptor to the panel data. |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 283 | * |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 284 | * @param button button descriptor |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 285 | * @return self, for chaining |
| 286 | */ |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 287 | public PropertyPanel addButton(ButtonId button) { |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 288 | buttons.add(button); |
| 289 | return this; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Removes buttons with the given descriptors from the list. |
| 294 | * |
| 295 | * @param descriptors descriptors to remove |
| 296 | * @return self, for chaining |
| 297 | */ |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 298 | public PropertyPanel removeButtons(ButtonId... descriptors) { |
| 299 | Set<ButtonId> forRemoval = Sets.newHashSet(descriptors); |
| 300 | List<ButtonId> toKeep = new ArrayList<>(); |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 301 | for (ButtonId bd : buttons) { |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 302 | if (!forRemoval.contains(bd)) { |
| 303 | toKeep.add(bd); |
| 304 | } |
| 305 | } |
| 306 | buttons = toKeep; |
| 307 | return this; |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Removes all currently defined buttons. |
| 312 | * |
| 313 | * @return self, for chaining |
| 314 | */ |
| 315 | public PropertyPanel removeAllButtons() { |
| 316 | buttons.clear(); |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 317 | return this; |
| 318 | } |
| 319 | |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 320 | // ==================== |
| 321 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 322 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 323 | /** |
| 324 | * Simple data carrier for a property, composed of a key/value pair. |
| 325 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 326 | public static class Prop { |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 327 | private final String key; |
| 328 | private final String value; |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 329 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 330 | /** |
| 331 | * Constructs a property data value. |
| 332 | * |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 333 | * @param key property key |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 334 | * @param value property value |
| 335 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 336 | public Prop(String key, String value) { |
| 337 | this.key = key; |
| 338 | this.value = value; |
| 339 | } |
| 340 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 341 | /** |
| 342 | * Returns the property's key. |
| 343 | * |
| 344 | * @return the key |
| 345 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 346 | public String key() { |
| 347 | return key; |
| 348 | } |
| 349 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 350 | /** |
| 351 | * Returns the property's value. |
| 352 | * |
| 353 | * @return the value |
| 354 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 355 | public String value() { |
| 356 | return value; |
| 357 | } |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 358 | |
| 359 | @Override |
| 360 | public boolean equals(Object o) { |
| 361 | if (this == o) { |
| 362 | return true; |
| 363 | } |
| 364 | if (o == null || getClass() != o.getClass()) { |
| 365 | return false; |
| 366 | } |
| 367 | |
| 368 | Prop prop = (Prop) o; |
| 369 | return key.equals(prop.key) && value.equals(prop.value); |
| 370 | } |
| 371 | |
| 372 | @Override |
| 373 | public int hashCode() { |
| 374 | int result = key.hashCode(); |
| 375 | result = 31 * result + value.hashCode(); |
| 376 | return result; |
| 377 | } |
| 378 | |
| 379 | @Override |
| 380 | public String toString() { |
| 381 | return "{" + key + " -> " + value + "}"; |
| 382 | } |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 383 | } |
| 384 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 385 | /** |
| 386 | * Auxiliary class representing a separator property. |
| 387 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 388 | public static class Separator extends Prop { |
| 389 | public Separator() { |
| 390 | super("-", ""); |
| 391 | } |
| 392 | } |
| 393 | |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 394 | } |