Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -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. |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package org.onosproject.ui.topo; |
| 18 | |
| 19 | import java.util.Collections; |
| 20 | import java.util.Set; |
| 21 | import java.util.TreeSet; |
| 22 | |
| 23 | import static com.google.common.base.Preconditions.checkNotNull; |
| 24 | |
| 25 | /** |
| 26 | * Denotes the highlighting to be applied to a link. |
| 27 | * {@link Flavor} is a closed set of NO-, PRIMARY-, or SECONDARY- highlighting. |
| 28 | * {@link Mod} is an open ended set of additional modifications (CSS classes) |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 29 | * that may also be applied. |
| 30 | * Note that {@link #MOD_OPTICAL} and {@link #MOD_ANIMATED} are pre-defined mods. |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 31 | * Label text may be set, which will also be displayed on the link. |
| 32 | */ |
| 33 | public class LinkHighlight extends AbstractHighlight { |
| 34 | |
| 35 | private static final String PLAIN = "plain"; |
| 36 | private static final String PRIMARY = "primary"; |
| 37 | private static final String SECONDARY = "secondary"; |
| 38 | private static final String EMPTY = ""; |
| 39 | private static final String SPACE = " "; |
| 40 | |
| 41 | private final Flavor flavor; |
| 42 | private final Set<Mod> mods = new TreeSet<>(); |
| 43 | private String label = EMPTY; |
| 44 | |
| 45 | /** |
| 46 | * Constructs a link highlight entity. |
| 47 | * |
| 48 | * @param linkId the link identifier |
| 49 | * @param flavor the highlight flavor |
| 50 | */ |
| 51 | public LinkHighlight(String linkId, Flavor flavor) { |
| 52 | super(TopoElementType.LINK, linkId); |
| 53 | this.flavor = checkNotNull(flavor); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Adds a highlighting modification to this link highlight. |
| 58 | * |
| 59 | * @param mod mod to be added |
| 60 | * @return self, for chaining |
| 61 | */ |
| 62 | public LinkHighlight addMod(Mod mod) { |
| 63 | mods.add(checkNotNull(mod)); |
| 64 | return this; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Adds a label to be displayed on the link. |
| 69 | * |
| 70 | * @param label the label text |
| 71 | * @return self, for chaining |
| 72 | */ |
| 73 | public LinkHighlight setLabel(String label) { |
| 74 | this.label = label == null ? EMPTY : label; |
| 75 | return this; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Returns the highlight flavor. |
| 80 | * |
| 81 | * @return highlight flavor |
| 82 | */ |
| 83 | public Flavor flavor() { |
| 84 | return flavor; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns the highlight modifications. |
| 89 | * |
| 90 | * @return highlight modifications |
| 91 | */ |
| 92 | public Set<Mod> mods() { |
| 93 | return Collections.unmodifiableSet(mods); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Generates the CSS classes string from the {@link #flavor} and |
| 98 | * any optional {@link #mods}. |
| 99 | * |
| 100 | * @return CSS classes string |
| 101 | */ |
| 102 | public String cssClasses() { |
| 103 | StringBuilder sb = new StringBuilder(flavor.toString()); |
| 104 | mods.forEach(m -> sb.append(SPACE).append(m)); |
| 105 | return sb.toString(); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Returns the label text. |
| 110 | * |
| 111 | * @return label text |
| 112 | */ |
| 113 | public String label() { |
| 114 | return label; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Link highlighting flavor. |
| 119 | */ |
| 120 | public enum Flavor { |
| 121 | NO_HIGHLIGHT(PLAIN), |
| 122 | PRIMARY_HIGHLIGHT(PRIMARY), |
| 123 | SECONDARY_HIGHLIGHT(SECONDARY); |
| 124 | |
| 125 | private String cssName; |
| 126 | |
| 127 | Flavor(String s) { |
| 128 | cssName = s; |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public String toString() { |
| 133 | return cssName; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 138 | * Denotes a link to be tagged as an optical link. |
| 139 | */ |
| 140 | public static final Mod MOD_OPTICAL = new Mod("optical"); |
| 141 | |
| 142 | /** |
| 143 | * Denotes a link to be tagged with animated traffic ("marching ants"). |
| 144 | */ |
| 145 | public static final Mod MOD_ANIMATED = new Mod("animated"); |
| 146 | } |