blob: 30b4ce7a95d72203c8287bdc2a5f5299d5da42b8 [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 Hunt0af1ec32015-07-24 12:17:55 -070022import java.util.ArrayList;
23import java.util.List;
Simon Hunt629b99e2015-07-27 17:38:33 -070024import java.util.Set;
Simon Hunt0af1ec32015-07-24 12:17:55 -070025
26/**
27 * Models a panel displayed on the Topology View.
28 */
29public class PropertyPanel {
30
31 private String title;
32 private String typeId;
33 private List<Prop> properties = new ArrayList<>();
34
Simon Hunt629b99e2015-07-27 17:38:33 -070035 /**
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 Hunt0af1ec32015-07-24 12:17:55 -070042 public PropertyPanel(String title, String typeId) {
43 this.title = title;
44 this.typeId = typeId;
45 }
46
Simon Hunt629b99e2015-07-27 17:38:33 -070047 /**
48 * Adds a property to the panel.
49 *
50 * @param p the property
51 * @return self, for chaining
52 */
Simon Hunt0af1ec32015-07-24 12:17:55 -070053 public PropertyPanel add(Prop p) {
54 properties.add(p);
55 return this;
56 }
57
Simon Hunt629b99e2015-07-27 17:38:33 -070058 /**
59 * Returns the title text.
60 *
61 * @return title text
62 */
Simon Hunt0af1ec32015-07-24 12:17:55 -070063 public String title() {
64 return title;
65 }
66
Simon Hunt629b99e2015-07-27 17:38:33 -070067 /**
68 * Returns the type identifier.
69 *
70 * @return type identifier
71 */
Simon Hunt0af1ec32015-07-24 12:17:55 -070072 public String typeId() {
73 return typeId;
74 }
75
Simon Hunt629b99e2015-07-27 17:38:33 -070076 /**
77 * Returns the list of properties to be displayed.
78 *
79 * @return the property list
80 */
Simon Hunt0af1ec32015-07-24 12:17:55 -070081 // TODO: consider protecting this?
82 public List<Prop> properties() {
83 return properties;
84 }
85
Simon Hunt629b99e2015-07-27 17:38:33 -070086 // == MUTATORS
87
88 /**
89 * Sets the title text.
90 *
91 * @param title title text
92 * @return self, for chaining
93 */
Simon Hunt0af1ec32015-07-24 12:17:55 -070094 public PropertyPanel title(String title) {
95 this.title = title;
96 return this;
97 }
98
Simon Hunt629b99e2015-07-27 17:38:33 -070099 /**
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 Hunt0af1ec32015-07-24 12:17:55 -0700109
Simon Hunt629b99e2015-07-27 17:38:33 -0700110 /**
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 Hunt0af1ec32015-07-24 12:17:55 -0700137
138 // ====================
139
Simon Hunt629b99e2015-07-27 17:38:33 -0700140 /**
141 * Simple data carrier for a property, composed of a key/value pair.
142 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700143 public static class Prop {
Simon Hunt629b99e2015-07-27 17:38:33 -0700144 private final String key;
145 private final String value;
Simon Hunt0af1ec32015-07-24 12:17:55 -0700146
Simon Hunt629b99e2015-07-27 17:38:33 -0700147 /**
148 * Constructs a property data value.
149 *
150 * @param key property key
151 * @param value property value
152 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700153 public Prop(String key, String value) {
154 this.key = key;
155 this.value = value;
156 }
157
Simon Hunt629b99e2015-07-27 17:38:33 -0700158 /**
159 * Returns the property's key.
160 *
161 * @return the key
162 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700163 public String key() {
164 return key;
165 }
166
Simon Hunt629b99e2015-07-27 17:38:33 -0700167 /**
168 * Returns the property's value.
169 *
170 * @return the value
171 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700172 public String value() {
173 return value;
174 }
Simon Hunt629b99e2015-07-27 17:38:33 -0700175
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 Hunt0af1ec32015-07-24 12:17:55 -0700200 }
201
Simon Hunt629b99e2015-07-27 17:38:33 -0700202 /**
203 * Auxiliary class representing a separator property.
204 */
Simon Hunt0af1ec32015-07-24 12:17:55 -0700205 public static class Separator extends Prop {
206 public Separator() {
207 super("-", "");
208 }
209 }
210
211}