blob: f8c439406bd0b4132557342ef4d79293257d1a41 [file] [log] [blame]
Simon Hunt83c5b832015-10-20 14:18:24 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Hunt83c5b832015-10-20 14:18:24 -07003 *
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
17package org.onosproject.ui.topo;
18
19/**
20 * Designates a badge to be applied to a node in the topology view.
21 */
22public final class NodeBadge {
23
24 private static final String EMPTY = "";
25
Simon Hunte9343f32015-10-21 18:07:46 -070026 /** Designates the badge status. */
27 public enum Status {
Simon Hunt83c5b832015-10-20 14:18:24 -070028 INFO("i"),
29 WARN("w"),
Simon Hunte9343f32015-10-21 18:07:46 -070030 ERROR("e");
Simon Hunt83c5b832015-10-20 14:18:24 -070031
32 private String code;
33
Simon Hunte9343f32015-10-21 18:07:46 -070034 Status(String code) {
Simon Hunt83c5b832015-10-20 14:18:24 -070035 this.code = code;
36 }
37
38 @Override
39 public String toString() {
40 return "{" + code + "}";
41 }
42
Ray Milkeye3026a42015-10-27 10:39:42 -070043 /* Returns the status code in string form. */
Simon Hunt83c5b832015-10-20 14:18:24 -070044 public String code() {
45 return code;
46 }
47 }
48
Simon Hunte9343f32015-10-21 18:07:46 -070049 private final Status status;
50 private final boolean isGlyph;
51 private final String text;
Simon Hunt83c5b832015-10-20 14:18:24 -070052 private final String message;
53
54 // only instantiated through static methods.
Simon Hunte9343f32015-10-21 18:07:46 -070055 private NodeBadge(Status status, boolean isGlyph, String text, String message) {
56 this.status = status == null ? Status.INFO : status;
57 this.isGlyph = isGlyph;
58 this.text = text;
Simon Hunt83c5b832015-10-20 14:18:24 -070059 this.message = message;
60 }
61
62 @Override
63 public String toString() {
Simon Hunte9343f32015-10-21 18:07:46 -070064 return "{Badge " + status +
65 " (" + text + ")" +
66 (isGlyph ? "*G " : " ") +
67 "\"" + message + "\"}";
Simon Hunt83c5b832015-10-20 14:18:24 -070068 }
69
70 /**
Simon Hunte9343f32015-10-21 18:07:46 -070071 * Returns the badge status.
Simon Hunt83c5b832015-10-20 14:18:24 -070072 *
Simon Hunte9343f32015-10-21 18:07:46 -070073 * @return badge status
Simon Hunt83c5b832015-10-20 14:18:24 -070074 */
Simon Hunte9343f32015-10-21 18:07:46 -070075 public Status status() {
76 return status;
77 }
78
79 /**
80 * Returns true if the text for this badge designates a glyph ID.
81 *
82 * @return true if badge uses glyph
83 */
84 public boolean isGlyph() {
85 return isGlyph;
86 }
87
88 /**
89 * Returns the text for the badge.
90 * Note that if {@link #isGlyph} is true, the text is a glyph ID, otherwise
91 * the text is displayed verbatim in the badge.
92 *
93 * @return text for badge
94 */
95 public String text() {
96 return text;
Simon Hunt83c5b832015-10-20 14:18:24 -070097 }
98
99 /**
100 * Returns the message associated with the badge.
101 *
102 * @return associated message
103 */
104 public String message() {
105 return message;
106 }
107
108 private static String nonNull(String s) {
109 return s == null ? EMPTY : s;
110 }
111
112 /**
Simon Hunte9343f32015-10-21 18:07:46 -0700113 * Returns an arbitrary text badge, with default status.
Simon Hunt83c5b832015-10-20 14:18:24 -0700114 *
Simon Hunte9343f32015-10-21 18:07:46 -0700115 * @param txt the text
116 * @return node badge to display text
Simon Hunt83c5b832015-10-20 14:18:24 -0700117 */
Simon Hunte9343f32015-10-21 18:07:46 -0700118 public static NodeBadge text(String txt) {
119 // TODO: consider length constraint on txt (3 chars?)
120 return new NodeBadge(Status.INFO, false, nonNull(txt), null);
Simon Hunt83c5b832015-10-20 14:18:24 -0700121 }
122
123 /**
Simon Hunte9343f32015-10-21 18:07:46 -0700124 * Returns a glyph badge, with default status.
Simon Hunt83c5b832015-10-20 14:18:24 -0700125 *
Simon Hunte9343f32015-10-21 18:07:46 -0700126 * @param gid the glyph ID
127 * @return node badge to display glyph
Simon Hunt83c5b832015-10-20 14:18:24 -0700128 */
Simon Hunte9343f32015-10-21 18:07:46 -0700129 public static NodeBadge glyph(String gid) {
130 return new NodeBadge(Status.INFO, true, nonNull(gid), null);
Simon Hunt83c5b832015-10-20 14:18:24 -0700131 }
132
133 /**
Simon Hunte9343f32015-10-21 18:07:46 -0700134 * Returns a numeric badge, with default status.
Simon Hunt83c5b832015-10-20 14:18:24 -0700135 *
136 * @param n the number
Simon Hunte9343f32015-10-21 18:07:46 -0700137 * @return node badge to display a number
Simon Hunt83c5b832015-10-20 14:18:24 -0700138 */
139 public static NodeBadge number(int n) {
Simon Hunte9343f32015-10-21 18:07:46 -0700140 // TODO: consider constraints, e.g. 1 <= n <= 999
141 return new NodeBadge(Status.INFO, false, Integer.toString(n), null);
142 }
143
144 /**
145 * Returns an arbitrary text badge, with the given status.
146 *
147 * @param s the status
148 * @param txt the text
149 * @return node badge to display text
150 */
151 public static NodeBadge text(Status s, String txt) {
152 // TODO: consider length constraint on txt (3 chars?)
153 return new NodeBadge(s, false, nonNull(txt), null);
154 }
155
156 /**
157 * Returns a glyph badge, with the given status.
158 *
159 * @param s the status
160 * @param gid the glyph ID
161 * @return node badge to display glyph
162 */
163 public static NodeBadge glyph(Status s, String gid) {
164 return new NodeBadge(s, true, nonNull(gid), null);
165 }
166
167
168 /**
169 * Returns a numeric badge, with the given status and optional message.
170 *
171 * @param s the status
172 * @param n the number
173 * @return node badge to display a number
174 */
175 public static NodeBadge number(Status s, int n) {
176 // TODO: consider constraints, e.g. 1 <= n <= 999
177 return new NodeBadge(s, false, Integer.toString(n), null);
178 }
179
180 /**
181 * Returns an arbitrary text badge, with the given status and optional
182 * message.
183 *
184 * @param s the status
185 * @param txt the text
186 * @param msg the optional message
187 * @return node badge to display text
188 */
189 public static NodeBadge text(Status s, String txt, String msg) {
190 // TODO: consider length constraint on txt (3 chars?)
191 return new NodeBadge(s, false, nonNull(txt), msg);
192 }
193
194 /**
195 * Returns a glyph badge, with the given status and optional message.
196 *
197 * @param s the status
198 * @param gid the glyph ID
199 * @param msg the optional message
200 * @return node badge to display glyph
201 */
202 public static NodeBadge glyph(Status s, String gid, String msg) {
203 return new NodeBadge(s, true, nonNull(gid), msg);
204 }
205
206
207 /**
208 * Returns a numeric badge, with the given status and optional message.
209 *
210 * @param s the status
211 * @param n the number
212 * @param msg the optional message
213 * @return node badge to display a number
214 */
215 public static NodeBadge number(Status s, int n, String msg) {
216 // TODO: consider constraints, e.g. 1 <= n <= 999
217 return new NodeBadge(s, false, Integer.toString(n), msg);
Simon Hunt83c5b832015-10-20 14:18:24 -0700218 }
219
220}