All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.metaeffekt.artifact.analysis.dashboard.SheetParagraph Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2021-2024 the original author or authors.
 *
 * Licensed 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 com.metaeffekt.artifact.analysis.dashboard;

import j2html.tags.DomContent;
import j2html.tags.specialized.DivTag;
import j2html.tags.specialized.SpanTag;

import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import static j2html.TagCreator.*;

public class SheetParagraph {

    private String identifier, title, style;
    private Set classes = new HashSet<>();
    private final SpanTag content = span();

    public void setIdentifier(String identifier) {
        this.identifier = identifier;
    }

    public String getIdentifier() {
        return identifier;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTitle() {
        return title;
    }

    public String getSecureIdentifier() {
        if (identifier == null) identifier = "unser-identifier-" + new Random().nextInt(999999);
        return identifier.toLowerCase().replace("&", " ").replaceAll(" +", " ").replace(" ", "-");
    }

    public String getParagraphClassIdentifier() {
        return "data-sheet-" + getSecureIdentifier();
    }

    public SheetParagraph with(DomContent... tag) {
        for (DomContent addTag : tag) {
            this.content.with(addTag);
        }
        return this;
    }

    public SheetParagraph with(String content) {
        this.content.with(text(content));
        return this;
    }

    public SheetParagraph withClasses(String... classes) {
        Collections.addAll(this.classes, classes);
        return this;
    }

    public SheetParagraph withStyle(String style) {
        this.style = style;
        return this;
    }

    public SpanTag getEditableContent() {
        return content;
    }

    public DivTag getContent() {
        DivTag contentBuilder = div();
        if (classes.size() != 0) contentBuilder.withClasses(classes.toArray(new String[]{}));
        if (this.style != null) contentBuilder.withStyle(this.style);
        contentBuilder.withClass(getParagraphClassIdentifier());
        if (title != null) contentBuilder.with(h2(title));
        contentBuilder.with(content);
        return contentBuilder;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy