blob: a734c6b7d53b0da70e12c96da6a3873f8c26da26 [file] [log] [blame]
Simon Hunta17fa672015-08-19 18:42:22 -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.
Simon Hunta17fa672015-08-19 18:42:22 -070015 */
16
17package org.onosproject.ui.topo;
18
19import java.util.Collections;
20import java.util.Set;
21import java.util.TreeSet;
22
23import 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 Hunt4fc86852015-08-20 17:57:52 -070029 * that may also be applied.
30 * Note that {@link #MOD_OPTICAL} and {@link #MOD_ANIMATED} are pre-defined mods.
Simon Hunta17fa672015-08-19 18:42:22 -070031 * Label text may be set, which will also be displayed on the link.
32 */
33public 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 Hunta17fa672015-08-19 18:42:22 -0700138 * 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}