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<>(); |
| 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 | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 177 | // == MUTATORS |
| 178 | |
| 179 | /** |
| 180 | * Sets the title text. |
| 181 | * |
| 182 | * @param title title text |
| 183 | * @return self, for chaining |
| 184 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 185 | public PropertyPanel title(String title) { |
| 186 | this.title = title; |
| 187 | return this; |
| 188 | } |
| 189 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 190 | /** |
| 191 | * Sets the type identifier (icon ID). |
| 192 | * |
| 193 | * @param typeId type identifier |
| 194 | * @return self, for chaining |
| 195 | */ |
| 196 | public PropertyPanel typeId(String typeId) { |
| 197 | this.typeId = typeId; |
| 198 | return this; |
| 199 | } |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 200 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 201 | /** |
| 202 | * Removes properties with the given keys from the list. |
| 203 | * |
| 204 | * @param keys keys of properties to remove |
| 205 | * @return self, for chaining |
| 206 | */ |
| 207 | public PropertyPanel removeProps(String... keys) { |
| 208 | Set<String> keysForRemoval = Sets.newHashSet(keys); |
| 209 | List<Prop> propsToKeep = new ArrayList<>(); |
| 210 | for (Prop p: properties) { |
| 211 | if (!keysForRemoval.contains(p.key())) { |
| 212 | propsToKeep.add(p); |
| 213 | } |
| 214 | } |
| 215 | properties = propsToKeep; |
| 216 | return this; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Removes all currently defined properties. |
| 221 | * |
| 222 | * @return self, for chaining |
| 223 | */ |
| 224 | public PropertyPanel removeAllProps() { |
| 225 | properties.clear(); |
| 226 | return this; |
| 227 | } |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 228 | |
| 229 | // ==================== |
| 230 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 231 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 232 | /** |
| 233 | * Simple data carrier for a property, composed of a key/value pair. |
| 234 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 235 | public static class Prop { |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 236 | private final String key; |
| 237 | private final String value; |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 238 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 239 | /** |
| 240 | * Constructs a property data value. |
| 241 | * |
| 242 | * @param key property key |
| 243 | * @param value property value |
| 244 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 245 | public Prop(String key, String value) { |
| 246 | this.key = key; |
| 247 | this.value = value; |
| 248 | } |
| 249 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 250 | /** |
| 251 | * Returns the property's key. |
| 252 | * |
| 253 | * @return the key |
| 254 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 255 | public String key() { |
| 256 | return key; |
| 257 | } |
| 258 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 259 | /** |
| 260 | * Returns the property's value. |
| 261 | * |
| 262 | * @return the value |
| 263 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 264 | public String value() { |
| 265 | return value; |
| 266 | } |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 267 | |
| 268 | @Override |
| 269 | public boolean equals(Object o) { |
| 270 | if (this == o) { |
| 271 | return true; |
| 272 | } |
| 273 | if (o == null || getClass() != o.getClass()) { |
| 274 | return false; |
| 275 | } |
| 276 | |
| 277 | Prop prop = (Prop) o; |
| 278 | return key.equals(prop.key) && value.equals(prop.value); |
| 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public int hashCode() { |
| 283 | int result = key.hashCode(); |
| 284 | result = 31 * result + value.hashCode(); |
| 285 | return result; |
| 286 | } |
| 287 | |
| 288 | @Override |
| 289 | public String toString() { |
| 290 | return "{" + key + " -> " + value + "}"; |
| 291 | } |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 294 | /** |
| 295 | * Auxiliary class representing a separator property. |
| 296 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 297 | public static class Separator extends Prop { |
| 298 | public Separator() { |
| 299 | super("-", ""); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | } |