blob: d4589a48a9492483008b02e886d6e219ae89f9ff [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
Thomas Vachuska529db0a2015-02-23 16:42:14 -080030 private static final String VIEW_PREFIX = "app/view/";
31
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080032 private final String prefix;
33 private final ClassLoader classLoader;
34 private final List<UiView> views;
Thomas Vachuska3553b302015-03-07 14:49:43 -080035 private final UiMessageHandlerFactory messageHandlerFactory;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080036
37 /**
38 * Creates a user interface extension for loading CSS and JS injections
39 * from {@code css.html} and {@code js.html} resources, respectively.
40 *
Thomas Vachuska3553b302015-03-07 14:49:43 -080041 * @param views list of contributed views
42 * @param messageHandlerFactory optional message handler factory
43 * @param classLoader class-loader for user interface resources
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080044 */
Thomas Vachuska3553b302015-03-07 14:49:43 -080045 public UiExtension(List<UiView> views,
46 UiMessageHandlerFactory messageHandlerFactory,
47 ClassLoader classLoader) {
48 this(views, messageHandlerFactory, null, classLoader);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080049 }
50
51 /**
52 * Creates a user interface extension using custom resource prefix. It
53 * loads CSS and JS injections from {@code path/css.html} and
54 * {@code prefix/js.html} resources, respectively.
55 *
Thomas Vachuska3553b302015-03-07 14:49:43 -080056 * @param views list of user interface views
57 * @param messageHandlerFactory optional message handler factory
58 * @param path resource path prefix
59 * @param classLoader class-loader for user interface resources
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080060 */
Thomas Vachuska3553b302015-03-07 14:49:43 -080061 public UiExtension(List<UiView> views,
62 UiMessageHandlerFactory messageHandlerFactory,
63 String path, ClassLoader classLoader) {
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080064 this.views = checkNotNull(ImmutableList.copyOf(views), "Views cannot be null");
Thomas Vachuska3553b302015-03-07 14:49:43 -080065 this.messageHandlerFactory = messageHandlerFactory;
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -080066 this.prefix = path != null ? (path + "/") : "";
67 this.classLoader = checkNotNull(classLoader, "Class loader must be specified");
68 }
69
70 /**
71 * Returns input stream containing CSS inclusion statements.
72 *
73 * @return CSS inclusion statements
74 */
75 public InputStream css() {
76 return classLoader.getResourceAsStream(prefix + "css.html");
77 }
78
79 /**
80 * Returns input stream containing JavaScript inclusion statements.
81 *
82 * @return JavaScript inclusion statements
83 */
84 public InputStream js() {
85 return classLoader.getResourceAsStream(prefix + "js.html");
86 }
87
88 /**
89 * Returns list of user interface views contributed by this extension.
90 *
91 * @return contributed view descriptors
92 */
93 public List<UiView> views() {
94 return views;
95 }
96
97 /**
98 * Returns input stream containing specified view-specific resource.
99 *
100 * @param viewId view identifier
101 * @param path resource path, relative to the view directory
102 * @return resource input stream
103 */
104 public InputStream resource(String viewId, String path) {
Thomas Vachuska529db0a2015-02-23 16:42:14 -0800105 InputStream is = classLoader.getResourceAsStream(VIEW_PREFIX + viewId + "/" + path);
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800106 return is;
107 }
108
Thomas Vachuska3553b302015-03-07 14:49:43 -0800109 /**
110 * Returns message handler factory.
111 *
112 * @return message handlers
113 */
114 public UiMessageHandlerFactory messageHandlerFactory() {
115 return messageHandlerFactory;
116 }
Thomas Vachuskafe8c98a2015-02-04 01:24:32 -0800117}