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

sdmxdl.desktop.JValueAsText Maven / Gradle / Ivy

The newest version!
package sdmxdl.desktop;

import lombok.Getter;
import sdmxdl.HasPersistence;

import javax.swing.*;
import java.awt.*;
import java.beans.PropertyChangeEvent;

public final class JValueAsText extends JComponent implements HasModel {

    @Getter
    private T model = null;

    public void setModel(T model) {
        firePropertyChange(MODEL_PROPERTY, this.model, this.model = model);
    }

    private final JTextArea textArea = new JTextArea();

    public JValueAsText() {
        initComponents();
    }

    private void initComponents() {
        textArea.setEditable(false);

        setLayout(new BorderLayout());
        add(new JScrollPane(textArea), BorderLayout.CENTER);

        addPropertyChangeListener(MODEL_PROPERTY, this::onModelChange);
    }

    private void onModelChange(PropertyChangeEvent evt) {
        updateText();
    }

    private void updateText() {
        textArea.setText(getValueAsText());
        textArea.setCaretPosition(0);
    }

    private String getValueAsText() {
        return model != null ? Sdmxdl.INSTANCE.formatAsJson((Class) model.getClass(), model) : "No structure";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy