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