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 | |
| 32 | private String title; |
| 33 | private String typeId; |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame^] | 34 | private String id; |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 35 | private List<Prop> properties = new ArrayList<>(); |
| 36 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 37 | /** |
| 38 | * Constructs a property panel model with the given title and |
| 39 | * type identifier (icon to display). |
| 40 | * |
| 41 | * @param title title text |
| 42 | * @param typeId type (icon) ID |
| 43 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 44 | public PropertyPanel(String title, String typeId) { |
| 45 | this.title = title; |
| 46 | this.typeId = typeId; |
| 47 | } |
| 48 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 49 | /** |
| 50 | * Adds a property to the panel. |
| 51 | * |
| 52 | * @param p the property |
| 53 | * @return self, for chaining |
| 54 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 55 | public PropertyPanel add(Prop p) { |
| 56 | properties.add(p); |
| 57 | return this; |
| 58 | } |
| 59 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 60 | /** |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame^] | 61 | * Adds an ID field to the panel data, to be included in |
| 62 | * the returned JSON data to the client. |
| 63 | * |
| 64 | * @param id the identifier |
| 65 | * @return self, for chaining |
| 66 | */ |
| 67 | public PropertyPanel id(String id) { |
| 68 | this.id = id; |
| 69 | return this; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | /** |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 74 | * Returns the title text. |
| 75 | * |
| 76 | * @return title text |
| 77 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 78 | public String title() { |
| 79 | return title; |
| 80 | } |
| 81 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 82 | /** |
| 83 | * Returns the type identifier. |
| 84 | * |
| 85 | * @return type identifier |
| 86 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 87 | public String typeId() { |
| 88 | return typeId; |
| 89 | } |
| 90 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 91 | /** |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame^] | 92 | * Returns the internal ID. |
| 93 | * |
| 94 | * @return the ID |
| 95 | */ |
| 96 | public String id() { |
| 97 | return id; |
| 98 | } |
| 99 | |
| 100 | /** |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 101 | * Returns the list of properties to be displayed. |
| 102 | * |
| 103 | * @return the property list |
| 104 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 105 | // TODO: consider protecting this? |
| 106 | public List<Prop> properties() { |
| 107 | return properties; |
| 108 | } |
| 109 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 110 | // == MUTATORS |
| 111 | |
| 112 | /** |
| 113 | * Sets the title text. |
| 114 | * |
| 115 | * @param title title text |
| 116 | * @return self, for chaining |
| 117 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 118 | public PropertyPanel title(String title) { |
| 119 | this.title = title; |
| 120 | return this; |
| 121 | } |
| 122 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 123 | /** |
| 124 | * Sets the type identifier (icon ID). |
| 125 | * |
| 126 | * @param typeId type identifier |
| 127 | * @return self, for chaining |
| 128 | */ |
| 129 | public PropertyPanel typeId(String typeId) { |
| 130 | this.typeId = typeId; |
| 131 | return this; |
| 132 | } |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 133 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 134 | /** |
| 135 | * Removes properties with the given keys from the list. |
| 136 | * |
| 137 | * @param keys keys of properties to remove |
| 138 | * @return self, for chaining |
| 139 | */ |
| 140 | public PropertyPanel removeProps(String... keys) { |
| 141 | Set<String> keysForRemoval = Sets.newHashSet(keys); |
| 142 | List<Prop> propsToKeep = new ArrayList<>(); |
| 143 | for (Prop p: properties) { |
| 144 | if (!keysForRemoval.contains(p.key())) { |
| 145 | propsToKeep.add(p); |
| 146 | } |
| 147 | } |
| 148 | properties = propsToKeep; |
| 149 | return this; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Removes all currently defined properties. |
| 154 | * |
| 155 | * @return self, for chaining |
| 156 | */ |
| 157 | public PropertyPanel removeAllProps() { |
| 158 | properties.clear(); |
| 159 | return this; |
| 160 | } |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 161 | |
| 162 | // ==================== |
| 163 | |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame^] | 164 | private static final DecimalFormat DF0 = new DecimalFormat("#,###"); |
| 165 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 166 | /** |
| 167 | * Simple data carrier for a property, composed of a key/value pair. |
| 168 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 169 | public static class Prop { |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 170 | private final String key; |
| 171 | private final String value; |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 172 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 173 | /** |
| 174 | * Constructs a property data value. |
| 175 | * |
| 176 | * @param key property key |
| 177 | * @param value property value |
| 178 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 179 | public Prop(String key, String value) { |
| 180 | this.key = key; |
| 181 | this.value = value; |
| 182 | } |
| 183 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 184 | /** |
Simon Hunt | b745ca6 | 2015-07-28 15:37:11 -0700 | [diff] [blame^] | 185 | * Constructs a property data value. |
| 186 | * @param key property key |
| 187 | * @param value property value |
| 188 | */ |
| 189 | public Prop(String key, int value) { |
| 190 | this.key = key; |
| 191 | this.value = DF0.format(value); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Constructs a property data value. |
| 196 | * @param key property key |
| 197 | * @param value property value |
| 198 | */ |
| 199 | public Prop(String key, long value) { |
| 200 | this.key = key; |
| 201 | this.value = DF0.format(value); |
| 202 | } |
| 203 | |
| 204 | /** |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 205 | * Returns the property's key. |
| 206 | * |
| 207 | * @return the key |
| 208 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 209 | public String key() { |
| 210 | return key; |
| 211 | } |
| 212 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 213 | /** |
| 214 | * Returns the property's value. |
| 215 | * |
| 216 | * @return the value |
| 217 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 218 | public String value() { |
| 219 | return value; |
| 220 | } |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 221 | |
| 222 | @Override |
| 223 | public boolean equals(Object o) { |
| 224 | if (this == o) { |
| 225 | return true; |
| 226 | } |
| 227 | if (o == null || getClass() != o.getClass()) { |
| 228 | return false; |
| 229 | } |
| 230 | |
| 231 | Prop prop = (Prop) o; |
| 232 | return key.equals(prop.key) && value.equals(prop.value); |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public int hashCode() { |
| 237 | int result = key.hashCode(); |
| 238 | result = 31 * result + value.hashCode(); |
| 239 | return result; |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public String toString() { |
| 244 | return "{" + key + " -> " + value + "}"; |
| 245 | } |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Simon Hunt | 629b99e | 2015-07-27 17:38:33 -0700 | [diff] [blame] | 248 | /** |
| 249 | * Auxiliary class representing a separator property. |
| 250 | */ |
Simon Hunt | 0af1ec3 | 2015-07-24 12:17:55 -0700 | [diff] [blame] | 251 | public static class Separator extends Prop { |
| 252 | public Separator() { |
| 253 | super("-", ""); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | } |