Package org.apache.isis.viewer.html.component.html

Source Code of org.apache.isis.viewer.html.component.html.HtmlComponentFactory

/*
*  Licensed to the Apache Software Foundation (ASF) under one
*  or more contributor license agreements.  See the NOTICE file
*  distributed with this work for additional information
*  regarding copyright ownership.  The ASF licenses this file
*  to you under the Apache License, Version 2.0 (the
*  "License"); you may not use this file except in compliance
*  with the License.  You may obtain a copy of the License at
*
*        http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing,
*  software distributed under the License is distributed on an
*  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
*  KIND, either express or implied.  See the License for the
*  specific language governing permissions and limitations
*  under the License.
*/

package org.apache.isis.viewer.html.component.html;

import static org.apache.isis.viewer.html.HtmlViewerConstants.FOOTER;
import static org.apache.isis.viewer.html.HtmlViewerConstants.FOOTER_FILE;
import static org.apache.isis.viewer.html.HtmlViewerConstants.HEADER;
import static org.apache.isis.viewer.html.HtmlViewerConstants.HEADER_FILE;
import static org.apache.isis.viewer.html.HtmlViewerConstants.STYLE_SHEET;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.isis.core.commons.config.IsisConfiguration;
import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
import org.apache.isis.core.metamodel.facets.multiline.MultiLineFacet;
import org.apache.isis.core.metamodel.spec.ActionType;
import org.apache.isis.core.metamodel.spec.feature.ObjectAssociation;
import org.apache.isis.core.progmodel.facets.value.booleans.BooleanValueFacet;
import org.apache.isis.runtimes.dflt.runtime.system.context.IsisContext;
import org.apache.isis.viewer.html.PathBuilder;
import org.apache.isis.viewer.html.component.Block;
import org.apache.isis.viewer.html.component.Component;
import org.apache.isis.viewer.html.component.ComponentFactory;
import org.apache.isis.viewer.html.component.DebugPane;
import org.apache.isis.viewer.html.component.Form;
import org.apache.isis.viewer.html.component.Page;
import org.apache.isis.viewer.html.component.Table;

public class HtmlComponentFactory implements ComponentFactory {

    protected final String footer;
    protected final String header;
    protected final String styleSheet;
    private final PathBuilder pathBuilder;

    public HtmlComponentFactory(final PathBuilder pathBuilder) {
        this.pathBuilder = pathBuilder;
        final IsisConfiguration configuration = getConfiguration();
        styleSheet = configuration.getString(STYLE_SHEET);
        String file = configuration.getString(HEADER_FILE);
        header = file == null ? configuration.getString(HEADER) : loadFile(file);
        file = configuration.getString(FOOTER_FILE);
        footer = file == null ? configuration.getString(FOOTER) : loadFile(file);
    }

    @Override
    public Block createBlock(final String style, final String description) {
        return new Div(this, style, description);
    }

    @Override
    public Component createBreadCrumbs(final String[] names, final boolean[] isLinked) {
        return new BreadCrumbs(this, names, isLinked);
    }

    @Override
    public Component createCollectionIcon(final ObjectAssociation field, final ObjectAdapter collection, final String id) {
        return new CollectionLink(this, field, collection, field.getDescription(), id);
    }

    @Override
    public DebugPane createDebugPane() {
        return new HtmlDebug();
    }

    @Override
    public Component createEditOption(final String id) {
        return new ActionComponent(this, "edit", "Edit Object", "Edit the current object", id, null, null);
    }

    @Override
    public Component createRemoveOption(final String id, final String elementId, final String fieldName) {
        return new ActionComponent(this, "remove", "Remove", "Remove item from collection", id, elementId, fieldName);
    }

    @Override
    public Component createAddOption(final String id, final String fieldName) {
        return new ActionComponent(this, "add", "Add Item", "Add item to collection", id, null, fieldName);
    }

    @Override
    public Component createErrorMessage(final Exception e, final boolean isDebug) {
        return new ErrorMessage(e, isDebug);
    }

    @Override
    public Form createForm(final String id, final String actionName, final int step, final int noOfPages, final boolean isEditing) {
        return new HtmlForm(this, id, actionName, step, noOfPages, isEditing);
    }

    @Override
    public Component createHeading(final String name) {
        return new Heading(this, name, 4);
    }

    @Override
    public Component createInlineBlock(final String style, final String text, final String description) {
        return new Span(style, text, description);
    }

    @Override
    public Component createCheckboxBlock(final boolean isEditable, final boolean isSet) {
        return new Checkbox(isSet, isEditable);
    }

    @Override
    public Component createSubmenu(final String menuName, final Component[] items) {
        return new Submenu(menuName, items);
    }

    @Override
    public Component createLink(final String link, final String name, final String description) {
        return new Link(this, link, name, description);
    }

    @Override
    public Component createMenuItem(final String actionId, final String name, final String description, final String reasonDisabled, final ActionType type, final boolean hasParameters, final String targetObjectId) {
        return new MenuItem(this, actionId, name, description, reasonDisabled, type, hasParameters, targetObjectId);
    }

    @Override
    public Component createCollectionIcon(final ObjectAdapter collection, final String collectionId) {
        return new CollectionIcon(this, collection, collection.getSpecification().getDescription(), collectionId);
    }

    @Override
    public Component createObjectIcon(final ObjectAdapter object, final String objectId, final String style) {
        return new ObjectIcon(this, object, object.getSpecification().getDescription(), objectId, style);
    }

    @Override
    public Component createObjectIcon(final ObjectAssociation field, final ObjectAdapter object, final String objectId, final String style) {
        return new ObjectIcon(this, object, field.getDescription(), objectId, style);
    }

    @Override
    public Page createPage() {
        return new DynamicHtmlPage(this, styleSheet, header, footer);
    }

    public LogonFormPage createLogonPage(final String user, final String password, final boolean registerLink, final String error) {
        return new LogonFormPage(this, styleSheet, header, footer, user, password, registerLink, error);
    }

    public RegisterFormPage createRegisterPage(final String user, final String password, final String error) {
        return new RegisterFormPage(this, styleSheet, header, footer, user, password, error);
    }

    @Override
    public Component createService(final String objectId, final String title, final String iconName) {
        return new ServiceComponent(this, objectId, title, iconName);
    }

    @Override
    public Table createTable(final int noColumns, final boolean withSelectorColumn) {
        return new HtmlTable(this, noColumns, withSelectorColumn);
    }

    @Override
    public Component createUserSwap(final String name) {
        return new UserSwapLink(this, name);

    }

    @Override
    public Component createParseableField(final ObjectAssociation field, final ObjectAdapter value, final boolean isEditable) {
        final BooleanValueFacet facet = field.getSpecification().getFacet(BooleanValueFacet.class);
        if (facet != null) {
            return createCheckboxBlock(isEditable, facet.isSet(value));
        } else {
            final String titleString = value != null ? value.titleString() : "";

            final MultiLineFacet multiLineFacet = field.getSpecification().getFacet(MultiLineFacet.class);
            final boolean isWrapped = multiLineFacet != null && !multiLineFacet.preventWrapping();

            if (isWrapped) {
                return createInlineBlock("value", "<pre>" + titleString + "</pre>", null);
            } else {
                return createInlineBlock("value", titleString, null);
            }
        }
    }

    private String loadFile(final String file) {
        final StringBuffer content = new StringBuffer();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
            String line;
            while ((line = reader.readLine()) != null) {
                content.append(line);
                content.append("\n");
            }
        } catch (final FileNotFoundException e) {
            throw new WebViewerException("Failed to find file " + file);
        } catch (final IOException e) {
            throw new WebViewerException("Failed to load file " + file, e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (final IOException ignore) {
                }
            }
        }
        return content.toString();
    }

    @Override
    public String getSuffix() {
        return pathBuilder.getSuffix();
    }

    @Override
    public String pathTo(final String prefix) {
        return pathBuilder.pathTo(prefix);
    }

    // /////////////////////////////////////////////////////////////
    // injected services
    // /////////////////////////////////////////////////////////////

    protected IsisConfiguration getConfiguration() {
        return IsisContext.getConfiguration();
    }

}
TOP

Related Classes of org.apache.isis.viewer.html.component.html.HtmlComponentFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.