Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 1 | /* |
| 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 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 | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 21 | import java.text.DecimalFormat; |
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 | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 31 | private static final DecimalFormat DF0 = new DecimalFormat("#,###"); |
| 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 | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 52 | * 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 Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 63 | /** |
| 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 Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 139 | |
| 140 | /** |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 141 | * Returns the title text. |
| 142 | * |
| 143 | * @return title text |
| 144 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 145 | public String title() { |
| 146 | return title; |
| 147 | } |
| 148 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 149 | /** |
| 150 | * Returns the type identifier. |
| 151 | * |
| 152 | * @return type identifier |
| 153 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 154 | public String typeId() { |
| 155 | return typeId; |
| 156 | } |
| 157 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 158 | /** |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 159 | * Returns the internal ID. |
| 160 | * |
| 161 | * @return the ID |
| 162 | */ |
| 163 | public String id() { |
| 164 | return id; |
| 165 | } |
| 166 | |
| 167 | /** |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 168 | * Returns the list of properties to be displayed. |
| 169 | * |
| 170 | * @return the property list |
| 171 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 172 | // TODO: consider protecting this? |
| 173 | public List<Prop> properties() { |
| 174 | return properties; |
| 175 | } |
| 176 | |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 177 | /** |
| 178 | * Returns the list of button descriptors. |
| 179 | * |
| 180 | * @return the button list |
| 181 | */ |
| 182 | // TODO: consider protecting this? |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 183 | public List<ButtonId> buttons() { |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 184 | return buttons; |
| 185 | } |
| 186 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 187 | // == MUTATORS |
| 188 | |
| 189 | /** |
| 190 | * Sets the title text. |
| 191 | * |
| 192 | * @param title title text |
| 193 | * @return self, for chaining |
| 194 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 195 | public PropertyPanel title(String title) { |
| 196 | this.title = title; |
| 197 | return this; |
| 198 | } |
| 199 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 200 | /** |
| 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 Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 210 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 211 | /** |
| 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 Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 218 | Set<String> forRemoval = Sets.newHashSet(keys); |
| 219 | List<Prop> toKeep = new ArrayList<>(); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 220 | for (Prop p: properties) { |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 221 | if (!forRemoval.contains(p.key())) { |
| 222 | toKeep.add(p); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 223 | } |
| 224 | } |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 225 | properties = toKeep; |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 226 | 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 Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 238 | |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 239 | /** |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 240 | * Adds the given button descriptor to the panel data. |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 241 | * |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 242 | * @param button button descriptor |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 243 | * @return self, for chaining |
| 244 | */ |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 245 | public PropertyPanel addButton(ButtonId button) { |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 246 | 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 Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 256 | public PropertyPanel removeButtons(ButtonId... descriptors) { |
| 257 | Set<ButtonId> forRemoval = Sets.newHashSet(descriptors); |
| 258 | List<ButtonId> toKeep = new ArrayList<>(); |
| 259 | for (ButtonId bd: buttons) { |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 260 | 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 Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 275 | return this; |
| 276 | } |
| 277 | |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 278 | // ==================== |
| 279 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 280 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 281 | /** |
| 282 | * Simple data carrier for a property, composed of a key/value pair. |
| 283 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 284 | public static class Prop { |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 285 | private final String key; |
| 286 | private final String value; |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 287 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 288 | /** |
| 289 | * Constructs a property data value. |
| 290 | * |
| 291 | * @param key property key |
| 292 | * @param value property value |
| 293 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 294 | public Prop(String key, String value) { |
| 295 | this.key = key; |
| 296 | this.value = value; |
| 297 | } |
| 298 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 299 | /** |
| 300 | * Returns the property's key. |
| 301 | * |
| 302 | * @return the key |
| 303 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 304 | public String key() { |
| 305 | return key; |
| 306 | } |
| 307 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 308 | /** |
| 309 | * Returns the property's value. |
| 310 | * |
| 311 | * @return the value |
| 312 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 313 | public String value() { |
| 314 | return value; |
| 315 | } |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 316 | |
| 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 Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 341 | } |
| 342 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 343 | /** |
| 344 | * Auxiliary class representing a separator property. |
| 345 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 346 | public static class Separator extends Prop { |
| 347 | public Separator() { |
| 348 | super("-", ""); |
| 349 | } |
| 350 | } |
| 351 | |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 352 | } |