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

cdc.perfs.ui.fx.ContextsTableModel Maven / Gradle / Ivy

There is a newer version: 0.52.0
Show newest version
package cdc.perfs.ui.fx;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import cdc.perfs.Context;
import cdc.perfs.Environment;
import cdc.perfs.EnvironmentListener;
import cdc.perfs.Source;
import javafx.beans.Observable;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

final class ContextsTableModel {
    private final Environment environment;
    private final List contexts = new CopyOnWriteArrayList<>();
    private final ObservableList delegate = FXCollections.observableArrayList(record -> new Observable[] {
            record.contextProperty(),
            record.visibleProperty()
    });

    private final EnvironmentListener listener = new EnvironmentListener() {
        @Override
        public void processContextCreated(Context context) {
            createRecord(context);
        }

        @Override
        public void processContextChanged(Context context) {
            updateRecord(context);
        }

        @Override
        public void processSourceCreated(Source source) {
            // Ignore
        }

        @Override
        public void processSourceChanged(Source source) {
            // Ignore
        }
    };

    public ContextsTableModel(Environment environment) {
        this.environment = environment;
        for (final Context context : environment.getContexts()) {
            createRecord(context);
        }
        environment.addListener(listener);
    }

    public static final class Record {
        private final ObjectProperty context = new SimpleObjectProperty<>(null);
        private final SimpleBooleanProperty visible = new SimpleBooleanProperty(true);

        public Record(Context context,
                      boolean visible) {
            this.context.set(context);
            this.visible.set(visible);
        }

        public Context getContext() {
            return context.get();
        }

        public ObjectProperty contextProperty() {
            return context;
        }

        public boolean isVisible() {
            return visible.get();
        }

        public void setVisible(boolean value) {
            visible.set(value);
        }

        public SimpleBooleanProperty visibleProperty() {
            return visible;
        }
    }

    protected synchronized void createRecord(Context context) {
        final Record record = new Record(context, true);
        contexts.add(context);
        delegate.addAll(record);
        record.visibleProperty().addListener((observable,
                                              oldValue,
                                              newValue) -> updateRecord(context));
    }

    protected synchronized void updateRecord(Context context) {
        final Record record = getRecord(context);
        if (record != null) {
            record.contextProperty().set(null);
            record.contextProperty().set(context);
        }
    }

    protected int getIndex(Context context) {
        return contexts.indexOf(context);
    }

    private Record getRecord(Context context) {
        final int index = getIndex(context);
        return index < delegate.size() ? delegate.get(index) : null;
    }

    public ObservableList asObservableList() {
        return delegate;
    }

    public Environment getEnvironment() {
        return environment;
    }

    public List getContexts() {
        return contexts;
    }

    public Context getContext(int id) {
        for (final Context context : contexts) {
            if (context.getId() == id) {
                return context;
            }
        }
        return null;
    }

    public boolean isVisible(Context context) {
        final Record record = getRecord(context);
        return record != null && record.isVisible();
    }

    public void setVisible(Context context,
                           boolean visible) {
        final Record record = getRecord(context);
        if (record != null) {
            record.setVisible(visible);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy