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