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