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

org.ikasan.dashboard.ui.general.component.AbstractEntityViewDialog Maven / Gradle / Ivy

There is a newer version: 4.0.1
Show newest version
package org.ikasan.dashboard.ui.general.component;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.select.Select;
import de.f0rce.ace.AceEditor;
import de.f0rce.ace.enums.AceMode;
import de.f0rce.ace.enums.AceTheme;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.StringReader;
import java.io.StringWriter;

public abstract class AbstractEntityViewDialog extends AbstractCloseableResizableDialog
{
    protected DocumentBuilder documentBuilder;
    protected Transformer transformer;
    protected AceEditor aceEditor;
    protected boolean initialised = false;
    protected ObjectMapper objectMapper;
    protected Select select;
    protected String rawContent;

    protected VerticalLayout content;

    public abstract Component getEntityDetailsLayout();

    public abstract void populate(ENTITY entity);

    public AbstractEntityViewDialog()
    {
        this.setWidth("1px");
        this.setSizeFull();
        setDraggable(true);
        setModal(false);
        setResizable(true);
        setCloseOnEsc(true);

        try
        {
            documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            objectMapper = new ObjectMapper();
        }
        catch (Exception e)
        {
            throw  new IllegalStateException("Could not construct EventViewDialog!", e);
        }

        initialiseEditor();

        select = new Select<>();
        select.setItems(getTranslation("label.none", UI.getCurrent().getLocale()), "XML", "JSON");
        select.setLabel(getTranslation("label.format", UI.getCurrent().getLocale()));
        select.addValueChangeListener(componentValueChangeEvent -> {
            if(componentValueChangeEvent.getValue().equals("XML")) {
                this.aceEditor.setValue(this.formatXml(this.aceEditor.getValue()));
            }
            else if(componentValueChangeEvent.getValue().equals("JSON")) {
                this.aceEditor.setValue(this.formatJson(this.aceEditor.getValue()));
            }
            else {
                this.aceEditor.setValue(this.rawContent);
            }
        });
    }

    protected void init()
    {
        content = new VerticalLayout(this.getEntityDetailsLayout(), this.aceEditor);
        content.setMargin(false);
        content.setSpacing(false);
        content.addClassName("dialog-content");
        content.setAlignItems(FlexComponent.Alignment.STRETCH);

        add(content);
    }

    public void open(String event)
    {
        if(!initialised)
        {
            init();
            initialised = true;
        }

        open();

        aceEditor.setValue(event);
        this.rawContent = event;
    }

    protected String formatXml(String event)
    {
        String xmlString;
        try
        {
            Document doc = this.documentBuilder
                .parse(new InputSource(new StringReader(event)));

            StreamResult result = new StreamResult(new StringWriter());
            DOMSource source = new DOMSource(doc);
            transformer.transform(source, result);
            xmlString = result.getWriter().toString();
        }
        catch (Exception e)
        {
            xmlString = event;
        }

        return xmlString;
    }

    protected String formatJson(String event)
    {
        try {
            Object json = objectMapper.readValue(event, Object.class);
            return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
        }
        catch (Exception e) {
            return event;
        }
    }

    protected void initialiseEditor()
    {
        aceEditor = new AceEditor();

        aceEditor.setTheme(AceTheme.dracula);
        aceEditor.setMode(AceMode.text);
        aceEditor.setFontSize(11);
        aceEditor.setTabSize(4);
        aceEditor.setWidth("auto");
        aceEditor.setHeight("50vh");
        aceEditor.setReadOnly(true);
        aceEditor.setWrap(false);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy