Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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; |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 34 | private String glyphId; |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 35 | private String id; |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 36 | private String navPath; |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 37 | private List<Prop> properties = new ArrayList<>(); |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 38 | private List<ButtonId> 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 |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 42 | * glyph identifier (icon to display). |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 43 | * |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 44 | * @param title title text |
| 45 | * @param glyphId glyph ID |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 46 | */ |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 47 | public PropertyPanel(String title, String glyphId) { |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 48 | this.title = title; |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 49 | this.glyphId = glyphId; |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 50 | } |
| 51 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 52 | /** |
Simon Hunt | cd15027 | 2015-11-04 14:09:36 -0800 | [diff] [blame] | 53 | * Returns a number formatter to use for formatting integer and long |
| 54 | * property values. |
| 55 | * <p> |
| 56 | * This default implementation uses a formatter for the default |
| 57 | * locale. For example: |
| 58 | * <pre> |
| 59 | * Locale.ENGLISH : 1000 -> "1,000" |
| 60 | * Locale.FRENCH : 1000 -> "1 000" |
| 61 | * Locale.GERMAN : 1000 -> "1.000" |
| 62 | * </pre> |
| 63 | * |
| 64 | * @return the number formatter |
| 65 | */ |
| 66 | protected NumberFormat formatter() { |
| 67 | return NF; |
| 68 | } |
| 69 | |
| 70 | /** |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 71 | * Adds a navigation path field to the panel data, to be included in |
| 72 | * the returned JSON data to the client. This is typically used to |
| 73 | * configure the topology view with the appropriate navigation path for |
| 74 | * a hot-link to some other view. |
| 75 | * |
| 76 | * @param navPath the navigation path |
| 77 | * @return self for chaining |
| 78 | */ |
| 79 | public PropertyPanel navPath(String navPath) { |
| 80 | this.navPath = navPath; |
| 81 | return this; |
| 82 | } |
| 83 | |
| 84 | /** |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 85 | * Adds an ID field to the panel data, to be included in |
| 86 | * the returned JSON data to the client. |
| 87 | * |
| 88 | * @param id the identifier |
| 89 | * @return self, for chaining |
| 90 | */ |
| 91 | public PropertyPanel id(String id) { |
| 92 | this.id = id; |
| 93 | return this; |
| 94 | } |
| 95 | |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 96 | /** |
| 97 | * Adds a property to the panel data. |
| 98 | * |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 99 | * @param key property key |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 100 | * @param label property label (localized) |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 101 | * @param value property value |
| 102 | * @return self, for chaining |
| 103 | */ |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 104 | public PropertyPanel addProp(String key, String label, String value) { |
| 105 | properties.add(new Prop(key, label, value)); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 106 | return this; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Adds a property to the panel data, using a decimal formatter. |
| 111 | * |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 112 | * @param key property key |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 113 | * @param label property label (localized) |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 114 | * @param value property value |
| 115 | * @return self, for chaining |
| 116 | */ |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 117 | public PropertyPanel addProp(String key, String label, int value) { |
| 118 | properties.add(new Prop(key, label, formatter().format(value))); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 119 | return this; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Adds a property to the panel data, using a decimal formatter. |
| 124 | * |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 125 | * @param key property key |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 126 | * @param label property label (localized) |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 127 | * @param value property value |
| 128 | * @return self, for chaining |
| 129 | */ |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 130 | public PropertyPanel addProp(String key, String label, long value) { |
| 131 | properties.add(new Prop(key, label, formatter().format(value))); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 132 | return this; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Adds a property to the panel data. Note that the value's |
| 137 | * {@link Object#toString toString()} method is used to convert the |
| 138 | * value to a string. |
| 139 | * |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 140 | * @param key property key |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 141 | * @param label property label (localized) |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 142 | * @param value property value |
| 143 | * @return self, for chaining |
| 144 | */ |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 145 | public PropertyPanel addProp(String key, String label, Object value) { |
| 146 | properties.add(new Prop(key, label, value.toString())); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 147 | return this; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Adds a property to the panel data. Note that the value's |
| 152 | * {@link Object#toString toString()} method is used to convert the |
| 153 | * value to a string, from which the characters defined in the given |
| 154 | * regular expression string are stripped. |
| 155 | * |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 156 | * @param key property key |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 157 | * @param label property label (localized) |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 158 | * @param value property value |
| 159 | * @param reStrip regexp characters to strip from value string |
| 160 | * @return self, for chaining |
| 161 | */ |
| 162 | public PropertyPanel addProp(String key, String label, |
| 163 | Object value, String reStrip) { |
| 164 | String val = value.toString().replaceAll(reStrip, ""); |
| 165 | properties.add(new Prop(key, label, val)); |
| 166 | return this; |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * The following degenerate forms of addProp(...) for backward compatibility. |
| 171 | */ |
| 172 | |
| 173 | /** |
| 174 | * Adds a property to the panel data. |
| 175 | * Note that the key is used as the label. |
| 176 | * |
| 177 | * @param key property key (also used as display label) |
| 178 | * @param value property value |
| 179 | * @return self, for chaining |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 180 | * @deprecated as of Loon (1.11) in deference to the localized version |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 181 | */ |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 182 | @Deprecated |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 183 | public PropertyPanel addProp(String key, String value) { |
| 184 | return addProp(key, key, value); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Adds a property to the panel data, using a decimal formatter. |
| 189 | * Note that the key is used as the label. |
| 190 | * |
| 191 | * @param key property key (also used as display label) |
| 192 | * @param value property value |
| 193 | * @return self, for chaining |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 194 | * @deprecated as of Loon (1.11) in deference to the localized version |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 195 | */ |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 196 | @Deprecated |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 197 | public PropertyPanel addProp(String key, int value) { |
| 198 | return addProp(key, key, value); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Adds a property to the panel data, using a decimal formatter. |
| 203 | * Note that the key is used as the label. |
| 204 | * |
| 205 | * @param key property key (also used as display label) |
| 206 | * @param value property value |
| 207 | * @return self, for chaining |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 208 | * @deprecated as of Loon (1.11) in deference to the localized version |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 209 | */ |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 210 | @Deprecated |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 211 | public PropertyPanel addProp(String key, long value) { |
| 212 | return addProp(key, key, value); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Adds a property to the panel data. Note that the value's |
| 217 | * {@link Object#toString toString()} method is used to convert the |
| 218 | * value to a string. |
| 219 | * Note also that the key is used as the label. |
| 220 | * |
| 221 | * @param key property key (also used as display label) |
| 222 | * @param value property value |
| 223 | * @return self, for chaining |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 224 | * @deprecated as of Loon (1.11) in deference to the localized version |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 225 | */ |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 226 | @Deprecated |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 227 | public PropertyPanel addProp(String key, Object value) { |
| 228 | return addProp(key, key, value); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Adds a property to the panel data. Note that the value's |
| 233 | * {@link Object#toString toString()} method is used to convert the |
| 234 | * value to a string, from which the characters defined in the given |
| 235 | * regular expression string are stripped. |
| 236 | * Note also that the key is used as the label. |
| 237 | * |
| 238 | * @param key property key (also used as display label) |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 239 | * @param value property value |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 240 | * @param reStrip regexp characters to strip from value string |
| 241 | * @return self, for chaining |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 242 | * @deprecated as of Loon (1.11) in deference to the localized version |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 243 | */ |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 244 | @Deprecated |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 245 | public PropertyPanel addProp(String key, Object value, String reStrip) { |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 246 | return addProp(key, key, value, reStrip); |
Simon Hunt | 00a27ff | 2015-07-28 18:53:40 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Adds a separator to the panel data. |
| 251 | * |
| 252 | * @return self, for chaining |
| 253 | */ |
| 254 | public PropertyPanel addSeparator() { |
| 255 | properties.add(new Separator()); |
| 256 | return this; |
| 257 | } |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 258 | |
| 259 | /** |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 260 | * Returns the title text. |
| 261 | * |
| 262 | * @return title text |
| 263 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 264 | public String title() { |
| 265 | return title; |
| 266 | } |
| 267 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 268 | /** |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 269 | * Returns the glyph identifier. |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 270 | * |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 271 | * @return glyph identifier |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 272 | */ |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 273 | public String glyphId() { |
| 274 | return glyphId; |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 275 | } |
| 276 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 277 | /** |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 278 | * Returns the navigation path. |
| 279 | * |
| 280 | * @return the navigation path |
| 281 | */ |
| 282 | public String navPath() { |
| 283 | return navPath; |
| 284 | } |
| 285 | |
| 286 | /** |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 287 | * Returns the internal ID. |
| 288 | * |
| 289 | * @return the ID |
| 290 | */ |
| 291 | public String id() { |
| 292 | return id; |
| 293 | } |
| 294 | |
| 295 | /** |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 296 | * Returns the list of properties to be displayed. |
| 297 | * |
| 298 | * @return the property list |
| 299 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 300 | public List<Prop> properties() { |
| 301 | return properties; |
| 302 | } |
| 303 | |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 304 | /** |
| 305 | * Returns the list of button descriptors. |
| 306 | * |
| 307 | * @return the button list |
| 308 | */ |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 309 | public List<ButtonId> buttons() { |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 310 | return buttons; |
| 311 | } |
| 312 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 313 | // == MUTATORS |
| 314 | |
| 315 | /** |
| 316 | * Sets the title text. |
| 317 | * |
| 318 | * @param title title text |
| 319 | * @return self, for chaining |
| 320 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 321 | public PropertyPanel title(String title) { |
| 322 | this.title = title; |
| 323 | return this; |
| 324 | } |
| 325 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 326 | /** |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 327 | * Sets the glyph identifier. |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 328 | * |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 329 | * @param glyphId glyph identifier |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 330 | * @return self, for chaining |
| 331 | */ |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 332 | public PropertyPanel glyphId(String glyphId) { |
| 333 | this.glyphId = glyphId; |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 334 | return this; |
| 335 | } |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 336 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 337 | /** |
| 338 | * Removes properties with the given keys from the list. |
| 339 | * |
| 340 | * @param keys keys of properties to remove |
| 341 | * @return self, for chaining |
| 342 | */ |
| 343 | public PropertyPanel removeProps(String... keys) { |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 344 | Set<String> forRemoval = Sets.newHashSet(keys); |
| 345 | List<Prop> toKeep = new ArrayList<>(); |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 346 | for (Prop p : properties) { |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 347 | if (!forRemoval.contains(p.key())) { |
| 348 | toKeep.add(p); |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 349 | } |
| 350 | } |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 351 | properties = toKeep; |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 352 | return this; |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Removes all currently defined properties. |
| 357 | * |
| 358 | * @return self, for chaining |
| 359 | */ |
| 360 | public PropertyPanel removeAllProps() { |
| 361 | properties.clear(); |
| 362 | return this; |
| 363 | } |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 364 | |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 365 | /** |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 366 | * Adds the given button descriptor to the panel data. |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 367 | * |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 368 | * @param button button descriptor |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 369 | * @return self, for chaining |
| 370 | */ |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 371 | public PropertyPanel addButton(ButtonId button) { |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 372 | buttons.add(button); |
| 373 | return this; |
| 374 | } |
| 375 | |
| 376 | /** |
| 377 | * Removes buttons with the given descriptors from the list. |
| 378 | * |
| 379 | * @param descriptors descriptors to remove |
| 380 | * @return self, for chaining |
| 381 | */ |
Simon Hunt | 8d22c4b | 2015-08-06 16:24:43 -0700 | [diff] [blame] | 382 | public PropertyPanel removeButtons(ButtonId... descriptors) { |
| 383 | Set<ButtonId> forRemoval = Sets.newHashSet(descriptors); |
| 384 | List<ButtonId> toKeep = new ArrayList<>(); |
Simon Hunt | 10618f6 | 2017-06-15 19:30:52 -0700 | [diff] [blame] | 385 | for (ButtonId bd : buttons) { |
Simon Hunt | 3a0598f | 2015-08-04 19:59:04 -0700 | [diff] [blame] | 386 | if (!forRemoval.contains(bd)) { |
| 387 | toKeep.add(bd); |
| 388 | } |
| 389 | } |
| 390 | buttons = toKeep; |
| 391 | return this; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Removes all currently defined buttons. |
| 396 | * |
| 397 | * @return self, for chaining |
| 398 | */ |
| 399 | public PropertyPanel removeAllButtons() { |
| 400 | buttons.clear(); |
Simon Hunt | fb94011 | 2015-07-29 18:36:35 -0700 | [diff] [blame] | 401 | return this; |
| 402 | } |
| 403 | |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 404 | // ==================== |
| 405 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame] | 406 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 407 | /** |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 408 | * Simple data carrier for a property, composed of a key/label/value trio. |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 409 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 410 | public static class Prop { |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 411 | private final String key; |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 412 | private final String label; |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 413 | private final String value; |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 414 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 415 | /** |
| 416 | * Constructs a property data value. |
| 417 | * |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 418 | * @param key property key (localization key) |
| 419 | * @param label property label (localization value) |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 420 | * @param value property value |
| 421 | */ |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 422 | public Prop(String key, String label, String value) { |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 423 | this.key = key; |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 424 | this.label = label; |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 425 | this.value = value; |
| 426 | } |
| 427 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 428 | /** |
| 429 | * Returns the property's key. |
| 430 | * |
| 431 | * @return the key |
| 432 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 433 | public String key() { |
| 434 | return key; |
| 435 | } |
| 436 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 437 | /** |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 438 | * Returns the property's (localized) label. |
| 439 | * |
| 440 | * @return the label |
| 441 | */ |
| 442 | public String label() { |
| 443 | return label; |
| 444 | } |
| 445 | |
| 446 | /** |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 447 | * Returns the property's value. |
| 448 | * |
| 449 | * @return the value |
| 450 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 451 | public String value() { |
| 452 | return value; |
| 453 | } |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 454 | |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 455 | /* |
| 456 | * NOTE: equals/hashCode are only expressed in terms of key and value. |
| 457 | */ |
| 458 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 459 | @Override |
| 460 | public boolean equals(Object o) { |
| 461 | if (this == o) { |
| 462 | return true; |
| 463 | } |
| 464 | if (o == null || getClass() != o.getClass()) { |
| 465 | return false; |
| 466 | } |
| 467 | |
| 468 | Prop prop = (Prop) o; |
| 469 | return key.equals(prop.key) && value.equals(prop.value); |
| 470 | } |
| 471 | |
| 472 | @Override |
| 473 | public int hashCode() { |
| 474 | int result = key.hashCode(); |
| 475 | result = 31 * result + value.hashCode(); |
| 476 | return result; |
| 477 | } |
| 478 | |
| 479 | @Override |
| 480 | public String toString() { |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 481 | return "{" + key + "(" + label + ") -> " + value + "}"; |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 482 | } |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 485 | /** |
| 486 | * Auxiliary class representing a separator property. |
| 487 | */ |
Simon Hunt | a58d894 | 2017-08-11 12:51:14 -0700 | [diff] [blame] | 488 | static class Separator extends Prop { |
| 489 | Separator() { |
Simon Hunt | 879ce45 | 2017-08-10 23:32:00 -0700 | [diff] [blame] | 490 | super("-", "-", ""); |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 494 | } |