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 | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame^] | 38 | private List<Button> 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? |
| 184 | public List<Button> buttons() { |
| 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) { |
| 219 | Set<String> keysForRemoval = Sets.newHashSet(keys); |
| 220 | List<Prop> propsToKeep = new ArrayList<>(); |
| 221 | for (Prop p: properties) { |
| 222 | if (!keysForRemoval.contains(p.key())) { |
| 223 | propsToKeep.add(p); |
| 224 | } |
| 225 | } |
| 226 | properties = propsToKeep; |
| 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 | /** |
| 241 | * Adds a button descriptor with the given identifier, to the panel data. |
| 242 | * |
| 243 | * @param id button identifier |
| 244 | * @return self, for chaining |
| 245 | */ |
| 246 | public PropertyPanel addButton(String id) { |
| 247 | buttons.add(new Button(id)); |
| 248 | return this; |
| 249 | } |
| 250 | |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 251 | // ==================== |
| 252 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 253 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 254 | /** |
| 255 | * Simple data carrier for a property, composed of a key/value pair. |
| 256 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 257 | public static class Prop { |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 258 | private final String key; |
| 259 | private final String value; |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 260 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 261 | /** |
| 262 | * Constructs a property data value. |
| 263 | * |
| 264 | * @param key property key |
| 265 | * @param value property value |
| 266 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 267 | public Prop(String key, String value) { |
| 268 | this.key = key; |
| 269 | this.value = value; |
| 270 | } |
| 271 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 272 | /** |
| 273 | * Returns the property's key. |
| 274 | * |
| 275 | * @return the key |
| 276 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 277 | public String key() { |
| 278 | return key; |
| 279 | } |
| 280 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 281 | /** |
| 282 | * Returns the property's value. |
| 283 | * |
| 284 | * @return the value |
| 285 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 286 | public String value() { |
| 287 | return value; |
| 288 | } |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 289 | |
| 290 | @Override |
| 291 | public boolean equals(Object o) { |
| 292 | if (this == o) { |
| 293 | return true; |
| 294 | } |
| 295 | if (o == null || getClass() != o.getClass()) { |
| 296 | return false; |
| 297 | } |
| 298 | |
| 299 | Prop prop = (Prop) o; |
| 300 | return key.equals(prop.key) && value.equals(prop.value); |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public int hashCode() { |
| 305 | int result = key.hashCode(); |
| 306 | result = 31 * result + value.hashCode(); |
| 307 | return result; |
| 308 | } |
| 309 | |
| 310 | @Override |
| 311 | public String toString() { |
| 312 | return "{" + key + " -> " + value + "}"; |
| 313 | } |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 316 | /** |
| 317 | * Auxiliary class representing a separator property. |
| 318 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 319 | public static class Separator extends Prop { |
| 320 | public Separator() { |
| 321 | super("-", ""); |
| 322 | } |
| 323 | } |
| 324 | |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame^] | 325 | /** |
| 326 | * Button descriptor. Note that these work in conjunction with |
| 327 | * "buttons" defined in the JavaScript code for the overlay. |
| 328 | */ |
| 329 | public static class Button { |
| 330 | private final String id; |
| 331 | |
| 332 | /** |
| 333 | * Constructs a button descriptor with the given identifier. |
| 334 | * |
| 335 | * @param id button identifier |
| 336 | */ |
| 337 | public Button(String id) { |
| 338 | this.id = id; |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * Returns the identifier for this button. |
| 343 | * |
| 344 | * @return button identifier |
| 345 | */ |
| 346 | public String id() { |
| 347 | return id; |
| 348 | } |
| 349 | } |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 350 | } |