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