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; |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 35 | private final UiMessageHandlerFactory messageHandlerFactory; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 36 | |
| 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 Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 41 | * @param views list of contributed views |
| 42 | * @param messageHandlerFactory optional message handler factory |
| 43 | * @param classLoader class-loader for user interface resources |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 44 | */ |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 45 | public UiExtension(List<UiView> views, |
| 46 | UiMessageHandlerFactory messageHandlerFactory, |
| 47 | ClassLoader classLoader) { |
| 48 | this(views, messageHandlerFactory, null, classLoader); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 49 | } |
| 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 Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 56 | * @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 Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 60 | */ |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 61 | public UiExtension(List<UiView> views, |
| 62 | UiMessageHandlerFactory messageHandlerFactory, |
| 63 | String path, ClassLoader classLoader) { |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 64 | this.views = checkNotNull(ImmutableList.copyOf(views), "Views cannot be null"); |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 65 | this.messageHandlerFactory = messageHandlerFactory; |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 66 | 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 Vachuska | 529db0a | 2015-02-23 16:42:14 -0800 | [diff] [blame] | 105 | InputStream is = classLoader.getResourceAsStream(VIEW_PREFIX + viewId + "/" + path); |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 106 | return is; |
| 107 | } |
| 108 | |
Thomas Vachuska | 3553b30 | 2015-03-07 14:49:43 -0800 | [diff] [blame] | 109 | /** |
| 110 | * Returns message handler factory. |
| 111 | * |
| 112 | * @return message handlers |
| 113 | */ |
| 114 | public UiMessageHandlerFactory messageHandlerFactory() { |
| 115 | return messageHandlerFactory; |
| 116 | } |
Thomas Vachuska | fe8c98a | 2015-02-04 01:24:32 -0800 | [diff] [blame] | 117 | } |