blob: 88dad9aedd54ba7bdea0b990c117fa347b97b3ae [file] [log] [blame]
Simon Hunt0af1ec32015-07-24 12:17:55 -07001/*
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
18package org.onosproject.ui.topo;
19
Simon Hunt629b99e2015-07-27 17:38:33 -070020import com.google.common.collect.Sets;
21
Simon Huntb745ca62015-07-28 15:37:11 -070022import java.text.DecimalFormat;
Simon Hunt0af1ec32015-07-24 12:17:55 -070023import java.util.ArrayList;
24import java.util.List;
Simon Hunt629b99e2015-07-27 17:38:33 -070025import java.util.Set;
Simon Hunt0af1ec32015-07-24 12:17:55 -070026
27/**
28 * Models a panel displayed on the Topology View.
29 */
30public class PropertyPanel {
31
32 private String title;
33 private String typeId;
Simon Huntb745ca62015-07-28 15:37:11 -070034 private String id;
Simon Hunt0af1ec32015-07-24 12:17:55 -070035 private List<Prop> properties = new ArrayList<>();
36
Simon Hunt629b99e2015-07-27 17:38:33 -070037 /**
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 Hunt0af1ec32015-07-24 12:17:55 -070044 public PropertyPanel(String title, String typeId) {
45 this.title = title;
46 this.typeId = typeId;
47 }
48
Simon Hunt629b99e2015-07-27 17:38:33 -070049 /**
50 * Adds a property to the panel.
51 *
52 * @param p the property
53 * @return self, for chaining
54 */
Simon Hunt0af1ec32015-07-24 12:17:55 -070055 public PropertyPanel add(Prop p) {
56 properties.add(p);
57 return this;
58 }
59
Simon Hunt629b99e2015-07-27 17:38:33 -070060 /**
Simon Huntb745ca62015-07-28 15:37:11 -070061 * 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 Hunt629b99e2015-07-27 17:38:33 -070074 * Returns the title text.
75 *
76 * @return title text
77 */
Simon Hunt0af1ec32015-07-24 12:17:55 -070078 public String title() {
79 return title;
80 }
81
Simon Hunt629b99e2015-07-27 17:38:33 -070082 /**
83 * Returns the type identifier.
84 *
85 * @return type identifier
86 */
Simon Hunt0af1ec32015-07-24 12:17:55 -070087 public String typeId() {
88 return typeId;
89 }
90
Simon Hunt629b99e2015-07-27 17:38:33 -070091 /**
Simon Huntb745ca62015-07-28 15:37:11 -070092 * Returns the internal ID.
93 *
94 * @return the ID
95 */
96 public String id() {
97 return id;
98 }
99
100 /**
Simon Hunt629b99e2015-07-27 17:38:33 -0700101 * Returns the list of properties to be displayed.
102 *
103 * @return the property list
104 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700105 // TODO: consider protecting this?
106 public List<Prop> properties() {
107 return properties;
108 }
109
Simon Hunt629b99e2015-07-27 17:38:33 -0700110 // == MUTATORS
111
112 /**
113 * Sets the title text.
114 *
115 * @param title title text
116 * @return self, for chaining
117 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700118 public PropertyPanel title(String title) {
119 this.title = title;
120 return this;
121 }
122
Simon Hunt629b99e2015-07-27 17:38:33 -0700123 /**
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 Hunt0af1ec32015-07-24 12:17:55 -0700133
Simon Hunt629b99e2015-07-27 17:38:33 -0700134 /**
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 Hunt0af1ec32015-07-24 12:17:55 -0700161
162 // ====================
163
Simon Huntb745ca62015-07-28 15:37:11 -0700164 private static final DecimalFormat DF0 = new DecimalFormat("#,###");
165
Simon Hunt629b99e2015-07-27 17:38:33 -0700166 /**
167 * Simple data carrier for a property, composed of a key/value pair.
168 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700169 public static class Prop {
Simon Hunt629b99e2015-07-27 17:38:33 -0700170 private final String key;
171 private final String value;
Simon Hunt0af1ec32015-07-24 12:17:55 -0700172
Simon Hunt629b99e2015-07-27 17:38:33 -0700173 /**
174 * Constructs a property data value.
175 *
176 * @param key property key
177 * @param value property value
178 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700179 public Prop(String key, String value) {
180 this.key = key;
181 this.value = value;
182 }
183
Simon Hunt629b99e2015-07-27 17:38:33 -0700184 /**
Simon Huntb745ca62015-07-28 15:37:11 -0700185 * 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 Hunt629b99e2015-07-27 17:38:33 -0700205 * Returns the property's key.
206 *
207 * @return the key
208 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700209 public String key() {
210 return key;
211 }
212
Simon Hunt629b99e2015-07-27 17:38:33 -0700213 /**
214 * Returns the property's value.
215 *
216 * @return the value
217 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700218 public String value() {
219 return value;
220 }
Simon Hunt629b99e2015-07-27 17:38:33 -0700221
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 Hunt0af1ec32015-07-24 12:17:55 -0700246 }
247
Simon Hunt629b99e2015-07-27 17:38:33 -0700248 /**
249 * Auxiliary class representing a separator property.
250 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700251 public static class Separator extends Prop {
252 public Separator() {
253 super("-", "");
254 }
255 }
256
257}