blob: 4e2f57062c146283458539ae3810a676af6d08e5 [file] [log] [blame]
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -08001/*
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 */
16package org.onosproject.ui;
17
18import com.google.common.collect.ImmutableList;
19
20import java.io.InputStream;
21import java.util.List;
22
23import static com.google.common.base.Preconditions.checkNotNull;
24
25/**
26 * User interface extension.
27 */
28public class UiExtension {
29
30 private final String prefix;
31 private final ClassLoader classLoader;
32 private final List<UiView> views;
33
34 /**
35 * Creates a user interface extension for loading CSS and JS injections
36 * from {@code css.html} and {@code js.html} resources, respectively.
37 *
38 * @param classLoader class-loader for user interface resources
39 */
40 public UiExtension(List<UiView> views, ClassLoader classLoader) {
41 this(views, null, classLoader);
42 }
43
44 /**
45 * Creates a user interface extension using custom resource prefix. It
46 * loads CSS and JS injections from {@code path/css.html} and
47 * {@code prefix/js.html} resources, respectively.
48 *
49 * @param views list of user interface views
50 * @param path resource path prefix
51 * @param classLoader class-loader for user interface resources
52 */
53 public UiExtension(List<UiView> views, String path, ClassLoader classLoader) {
54 this.views = checkNotNull(ImmutableList.copyOf(views), "Views cannot be null");
55 this.prefix = path != null ? (path + "/") : "";
56 this.classLoader = checkNotNull(classLoader, "Class loader must be specified");
57 }
58
59 /**
60 * Returns input stream containing CSS inclusion statements.
61 *
62 * @return CSS inclusion statements
63 */
64 public InputStream css() {
65 return classLoader.getResourceAsStream(prefix + "css.html");
66 }
67
68 /**
69 * Returns input stream containing JavaScript inclusion statements.
70 *
71 * @return JavaScript inclusion statements
72 */
73 public InputStream js() {
74 return classLoader.getResourceAsStream(prefix + "js.html");
75 }
76
77 /**
78 * Returns list of user interface views contributed by this extension.
79 *
80 * @return contributed view descriptors
81 */
82 public List<UiView> views() {
83 return views;
84 }
85
86 /**
87 * Returns input stream containing specified view-specific resource.
88 *
89 * @param viewId view identifier
90 * @param path resource path, relative to the view directory
91 * @return resource input stream
92 */
93 public InputStream resource(String viewId, String path) {
94 InputStream is = classLoader.getResourceAsStream(prefix + "views/" + viewId + "/" + path);
95 return is;
96 }
97
98}