blob: 83757e6762f8b8b28c8dc1efcc6f040f55f6067b [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 *
Thomas Vachuskad32bfdc2015-02-21 16:39:25 -080038 * @param views list of contributed views
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080039 * @param classLoader class-loader for user interface resources
40 */
41 public UiExtension(List<UiView> views, ClassLoader classLoader) {
42 this(views, null, classLoader);
43 }
44
45 /**
46 * Creates a user interface extension using custom resource prefix. It
47 * loads CSS and JS injections from {@code path/css.html} and
48 * {@code prefix/js.html} resources, respectively.
49 *
50 * @param views list of user interface views
51 * @param path resource path prefix
52 * @param classLoader class-loader for user interface resources
53 */
54 public UiExtension(List<UiView> views, String path, ClassLoader classLoader) {
55 this.views = checkNotNull(ImmutableList.copyOf(views), "Views cannot be null");
56 this.prefix = path != null ? (path + "/") : "";
57 this.classLoader = checkNotNull(classLoader, "Class loader must be specified");
58 }
59
60 /**
61 * Returns input stream containing CSS inclusion statements.
62 *
63 * @return CSS inclusion statements
64 */
65 public InputStream css() {
66 return classLoader.getResourceAsStream(prefix + "css.html");
67 }
68
69 /**
70 * Returns input stream containing JavaScript inclusion statements.
71 *
72 * @return JavaScript inclusion statements
73 */
74 public InputStream js() {
75 return classLoader.getResourceAsStream(prefix + "js.html");
76 }
77
78 /**
79 * Returns list of user interface views contributed by this extension.
80 *
81 * @return contributed view descriptors
82 */
83 public List<UiView> views() {
84 return views;
85 }
86
87 /**
88 * Returns input stream containing specified view-specific resource.
89 *
90 * @param viewId view identifier
91 * @param path resource path, relative to the view directory
92 * @return resource input stream
93 */
94 public InputStream resource(String viewId, String path) {
Thomas Vachuskaa0509892015-02-21 22:18:41 -080095 InputStream is = classLoader.getResourceAsStream(viewId + "/" + path);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080096 return is;
97 }
98
99}