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

cdc.perfs.ui.fx.SourcesTableModel 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.MeasureLevel;
import cdc.perfs.Source;
import javafx.application.Platform;
import javafx.collections.ModifiableObservableListBase;

public final class SourcesTableModel extends ModifiableObservableListBase {
    private final Environment environment;
    private final List delegate = new CopyOnWriteArrayList<>();

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

        @Override
        public void processContextChanged(Context context) {
            // Ignore
        }

        @Override
        public void processSourceCreated(Source source) {
            createRecord(source);
        }

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

    public SourcesTableModel(Environment environment) {
        this.environment = environment;
        for (final Source source : environment.getSources()) {
            createRecord(source);
        }
        environment.addListener(listener);
    }

    public static class Record {
        private final Source source;

        public Record(Source source) {
            this.source = source;
        }

        public Source getSource() {
            return source;
        }

        public MeasureLevel getMaxLevel() {
            return source.getMaxLevel();
        }

        public void setMaxLevel(MeasureLevel level) {
            source.setMaxLevel(level);
        }
    }

    protected synchronized void createRecord(Source source) {
        final Record record = new Record(source);
        Platform.runLater(() -> addAll(record));
    }

    private int getIndex(Source source) {
        // Not efficient, but should be OK (?)
        for (int index = 0; index < size(); index++) {
            if (delegate.get(index).getSource() == source) {
                return index;
            }
        }
        return -1;
    }

    protected synchronized void updateRecord(Source source) {
        final int index = getIndex(source);
        beginChange();
        nextUpdate(index);
        endChange();
    }

    public Environment getEnvironment() {
        return environment;
    }

    @Override
    public Record get(int index) {
        return delegate.get(index);
    }

    @Override
    public int size() {
        return delegate.size();
    }

    @Override
    protected void doAdd(int index,
                         Record element) {
        delegate.add(index, element);
    }

    @Override
    protected Record doSet(int index,
                           Record element) {
        return delegate.set(index, element);
    }

    @Override
    protected Record doRemove(int index) {
        return delegate.remove(index);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy