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