Simon Hunt | 83c5b83 | 2015-10-20 14:18:24 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Simon Hunt | 83c5b83 | 2015-10-20 14:18:24 -0700 | [diff] [blame] | 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 | package org.onosproject.ui.topo; |
| 18 | |
| 19 | /** |
| 20 | * Designates a badge to be applied to a node in the topology view. |
| 21 | */ |
| 22 | public final class NodeBadge { |
| 23 | |
| 24 | private static final String EMPTY = ""; |
| 25 | |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 26 | /** Designates the badge status. */ |
| 27 | public enum Status { |
Simon Hunt | 83c5b83 | 2015-10-20 14:18:24 -0700 | [diff] [blame] | 28 | INFO("i"), |
| 29 | WARN("w"), |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 30 | ERROR("e"); |
Simon Hunt | 83c5b83 | 2015-10-20 14:18:24 -0700 | [diff] [blame] | 31 | |
| 32 | private String code; |
| 33 | |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 34 | Status(String code) { |
Simon Hunt | 83c5b83 | 2015-10-20 14:18:24 -0700 | [diff] [blame] | 35 | this.code = code; |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public String toString() { |
| 40 | return "{" + code + "}"; |
| 41 | } |
| 42 | |
Ray Milkey | e3026a4 | 2015-10-27 10:39:42 -0700 | [diff] [blame] | 43 | /* Returns the status code in string form. */ |
Simon Hunt | 83c5b83 | 2015-10-20 14:18:24 -0700 | [diff] [blame] | 44 | public String code() { |
| 45 | return code; |
| 46 | } |
| 47 | } |
| 48 | |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 49 | private final Status status; |
| 50 | private final boolean isGlyph; |
| 51 | private final String text; |
Simon Hunt | 83c5b83 | 2015-10-20 14:18:24 -0700 | [diff] [blame] | 52 | private final String message; |
| 53 | |
| 54 | // only instantiated through static methods. |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 55 | 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 Hunt | 83c5b83 | 2015-10-20 14:18:24 -0700 | [diff] [blame] | 59 | this.message = message; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public String toString() { |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 64 | return "{Badge " + status + |
| 65 | " (" + text + ")" + |
| 66 | (isGlyph ? "*G " : " ") + |
| 67 | "\"" + message + "\"}"; |
Simon Hunt | 83c5b83 | 2015-10-20 14:18:24 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 71 | * Returns the badge status. |
Simon Hunt | 83c5b83 | 2015-10-20 14:18:24 -0700 | [diff] [blame] | 72 | * |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 73 | * @return badge status |
Simon Hunt | 83c5b83 | 2015-10-20 14:18:24 -0700 | [diff] [blame] | 74 | */ |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 75 | 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 Hunt | 83c5b83 | 2015-10-20 14:18:24 -0700 | [diff] [blame] | 97 | } |
| 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 Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 113 | * Returns an arbitrary text badge, with default status. |
Simon Hunt | 83c5b83 | 2015-10-20 14:18:24 -0700 | [diff] [blame] | 114 | * |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 115 | * @param txt the text |
| 116 | * @return node badge to display text |
Simon Hunt | 83c5b83 | 2015-10-20 14:18:24 -0700 | [diff] [blame] | 117 | */ |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 118 | 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 Hunt | 83c5b83 | 2015-10-20 14:18:24 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | /** |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 124 | * Returns a glyph badge, with default status. |
Simon Hunt | 83c5b83 | 2015-10-20 14:18:24 -0700 | [diff] [blame] | 125 | * |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 126 | * @param gid the glyph ID |
| 127 | * @return node badge to display glyph |
Simon Hunt | 83c5b83 | 2015-10-20 14:18:24 -0700 | [diff] [blame] | 128 | */ |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 129 | public static NodeBadge glyph(String gid) { |
| 130 | return new NodeBadge(Status.INFO, true, nonNull(gid), null); |
Simon Hunt | 83c5b83 | 2015-10-20 14:18:24 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | /** |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 134 | * Returns a numeric badge, with default status. |
Simon Hunt | 83c5b83 | 2015-10-20 14:18:24 -0700 | [diff] [blame] | 135 | * |
| 136 | * @param n the number |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 137 | * @return node badge to display a number |
Simon Hunt | 83c5b83 | 2015-10-20 14:18:24 -0700 | [diff] [blame] | 138 | */ |
| 139 | public static NodeBadge number(int n) { |
Simon Hunt | e9343f3 | 2015-10-21 18:07:46 -0700 | [diff] [blame] | 140 | // 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 Hunt | 83c5b83 | 2015-10-20 14:18:24 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | } |