jaroslav@110: /** jaroslav@358: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. jaroslav@110: * jaroslav@551: * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved. jaroslav@110: * jaroslav@358: * Oracle and Java are registered trademarks of Oracle and/or its affiliates. jaroslav@358: * Other names may be trademarks of their respective owners. jaroslav@110: * jaroslav@358: * The contents of this file are subject to the terms of either the GNU jaroslav@358: * General Public License Version 2 only ("GPL") or the Common jaroslav@358: * Development and Distribution License("CDDL") (collectively, the jaroslav@358: * "License"). You may not use this file except in compliance with the jaroslav@358: * License. You can obtain a copy of the License at jaroslav@358: * http://www.netbeans.org/cddl-gplv2.html jaroslav@358: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the jaroslav@358: * specific language governing permissions and limitations under the jaroslav@358: * License. When distributing the software, include this License Header jaroslav@358: * Notice in each file and include the License file at jaroslav@358: * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this jaroslav@358: * particular file as subject to the "Classpath" exception as provided jaroslav@358: * by Oracle in the GPL Version 2 section of the License file that jaroslav@358: * accompanied this code. If applicable, add the following below the jaroslav@358: * License Header, with the fields enclosed by brackets [] replaced by jaroslav@358: * your own identifying information: jaroslav@358: * "Portions Copyrighted [year] [name of copyright owner]" jaroslav@358: * jaroslav@358: * Contributor(s): jaroslav@358: * jaroslav@358: * The Original Software is NetBeans. The Initial Developer of the Original jaroslav@551: * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved. jaroslav@358: * jaroslav@358: * If you wish your version of this file to be governed by only the CDDL jaroslav@358: * or only the GPL Version 2, indicate your decision by adding jaroslav@358: * "[Contributor] elects to include this software in this distribution jaroslav@358: * under the [CDDL or GPL Version 2] license." If you do not indicate a jaroslav@358: * single choice of license, a recipient has the option to distribute jaroslav@358: * your version of this file under either the CDDL, the GPL Version 2 or jaroslav@358: * to extend the choice of license to its licensees as provided above. jaroslav@358: * However, if you add GPL Version 2 code and therefore, elected the GPL jaroslav@358: * Version 2 license, then the option applies only if the new code is jaroslav@358: * made subject to such option by the copyright holder. jaroslav@110: */ jaroslav@110: package org.apidesign.html.context.spi; jaroslav@110: jaroslav@110: import net.java.html.BrwsrCtx; jaroslav@362: import org.netbeans.html.context.impl.CtxImpl; jaroslav@110: jaroslav@538: /** Factory class to assign various technologies jaroslav@538: * to a {@link BrwsrCtx browser context}. Start with {@link #newBuilder()} jaroslav@538: * and then assign technologies with {@link Builder#register(java.lang.Class, java.lang.Object, int)} jaroslav@538: * method. jaroslav@110: * jaroslav@110: * @author Jaroslav Tulach jaroslav@110: */ jaroslav@110: public final class Contexts { jaroslav@110: private Contexts() { jaroslav@110: } jaroslav@110: jaroslav@110: /** Creates new, empty builder for creation of {@link BrwsrCtx}. At the jaroslav@534: * end call the {@link Builder#build()} method to generate the context. jaroslav@110: * jaroslav@110: * @return new instance of the builder jaroslav@110: */ jaroslav@110: public static Builder newBuilder() { jaroslav@110: return new Builder(); jaroslav@110: } jaroslav@110: jaroslav@110: /** Seeks for the specified technology in the provided context. jaroslav@110: * jaroslav@110: * @param type of the technology jaroslav@110: * @param context the context to seek in jaroslav@110: * (previously filled with ({@link Builder#register(java.lang.Class, java.lang.Object, int)}) jaroslav@110: * @param technology class that identifies the technology jaroslav@534: * @return the technology in the context or null jaroslav@110: */ jaroslav@110: public static Tech find(BrwsrCtx context, Class technology) { jaroslav@110: return CtxImpl.find(context, technology); jaroslav@110: } jaroslav@110: jaroslav@110: /** Implementors of various HTML technologies should jaroslav@534: * register their implementation via {@link java.util.ServiceProvider} so jaroslav@534: * {@link java.util.ServiceProvider} can find them, when their JARs are included jaroslav@110: * on the classpath of the running application. jaroslav@110: * jaroslav@110: * @author Jaroslav Tulach jaroslav@110: */ jaroslav@110: public static interface Provider { jaroslav@110: jaroslav@110: /** Register into the context if suitable technology is jaroslav@110: * available for the requesting class. jaroslav@110: * The provider should check if its own technology is available in current jaroslav@110: * scope (e.g. proper JDK, proper browser, etc.). The provider jaroslav@110: * can also find the right context depending on requestor's classloader, etc. jaroslav@110: *

jaroslav@110: * Providers should use {@link Builder} to enrich appropriately jaroslav@110: * the context. jaroslav@110: * jaroslav@110: * @param context the context builder to fill with technologies jaroslav@110: * @param requestor the application class requesting access the the HTML page jaroslav@110: * @see BrwsrCtx#findDefault(java.lang.Class) jaroslav@110: */ jaroslav@110: void fillContext(Builder context, Class requestor); jaroslav@110: } jaroslav@110: jaroslav@110: /** Support for providers of new {@link BrwsrCtx}. Providers of different jaroslav@110: * technologies should be of particular interest in this class. End users jaroslav@110: * designing their application with existing technologies should rather jaroslav@110: * point their attention to {@link BrwsrCtx} and co. jaroslav@110: * jaroslav@110: * @author Jaroslav Tulach jaroslav@110: */ jaroslav@110: public static final class Builder { jaroslav@256: private final CtxImpl impl = new CtxImpl(); jaroslav@110: jaroslav@110: Builder() { jaroslav@110: } jaroslav@110: jaroslav@110: /** Registers new technology into the context. Each technology is jaroslav@110: * exactly identified by its implementation class and can be associated jaroslav@110: * with (positive) priority. In case of presence of multiple technologies jaroslav@110: * with the same class, the one with higher lower priority takes precedence. jaroslav@256: * @param type of technology to register jaroslav@256: * @param type the real class of the technology type jaroslav@256: * @param impl an instance of the technology class jaroslav@256: * @param position the lower position, the more important implementation jaroslav@256: * which will be consulted sooner when seeking for a {@link Contexts#find(net.java.html.BrwsrCtx, java.lang.Class)} jaroslav@256: * an implementation jaroslav@256: * @return this builder jaroslav@110: */ jaroslav@256: public Builder register(Class type, Tech impl, int position) { jaroslav@256: if (position <= 0) { jaroslav@110: throw new IllegalStateException(); jaroslav@110: } jaroslav@256: this.impl.register(type, impl, position); jaroslav@110: return this; jaroslav@110: } jaroslav@110: jaroslav@110: /** Generates context based on values previously inserted into jaroslav@110: * this builder. jaroslav@110: * jaroslav@110: * @return new, immutable instance of {@link BrwsrCtx} jaroslav@110: */ jaroslav@110: public BrwsrCtx build() { jaroslav@110: return impl.build(); jaroslav@110: } jaroslav@110: } jaroslav@110: }