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

cdc.perfs.ui.swing.SourcesTableModel Maven / Gradle / Ivy

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

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

import javax.swing.table.AbstractTableModel;

import cdc.perfs.Context;
import cdc.perfs.Environment;
import cdc.perfs.EnvironmentKind;
import cdc.perfs.EnvironmentListener;
import cdc.perfs.Measure;
import cdc.perfs.api.MeasureLevel;
import cdc.perfs.api.Source;

/**
 * Implementation of a table model for sources used by an environment.
 *
 * @author Damien Carbonne
 *
 */
final class SourcesTableModel extends AbstractTableModel {
    private static final long serialVersionUID = 1L;
    private final transient Environment environment;
    private static final String[] COLUMN_NAMES = { "Source", "Max Level" };
    protected final transient List sources = new CopyOnWriteArrayList<>();

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

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

        @Override
        public void processSourceCreated(Source source) {
            final int row = sources.size();
            sources.add(source);
            fireTableRowsInserted(row, row);
        }

        @Override
        public void processSourceChanged(Source source) {
            final int row = getRowIndex(source);
            if (row >= 0) {
                fireTableRowsUpdated(row, row);
            }
        }

        @Override
        public void processMeasureCreated(Measure measure) {
            // Ignore
        }

        @Override
        public void processMeasureChanged(Measure measure) {
            // Ignore
        }
    };

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

    private Source getSource(int rowIndex) {
        return sources.get(rowIndex);
    }

    protected int getRowIndex(Source source) {
        return sources.indexOf(source);
    }

    public Environment getEnvironment() {
        return environment;
    }

    public List getSources() {
        return sources;
    }

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

    @Override
    public int getColumnCount() {
        return COLUMN_NAMES.length;
    }

    @Override
    public String getColumnName(int column) {
        if (column >= 0 && column < getColumnCount()) {
            return COLUMN_NAMES[column];
        } else {
            return null;
        }
    }

    @Override
    public Class getColumnClass(int columnIndex) {
        switch (columnIndex) {
        case 0:
            return Source.class;
        case 1:
            return MeasureLevel.class;
        default:
            return null;
        }
    }

    @Override
    public boolean isCellEditable(int rowIndex,
                                  int columnIndex) {
        return getEnvironment().getKind() == EnvironmentKind.RUNTIME && columnIndex == 1;
    }

    @Override
    public Object getValueAt(int rowIndex,
                             int columnIndex) {
        final Source source = getSource(rowIndex);

        switch (columnIndex) {
        case 0:
            return source;
        case 1:
            return source.getMaxLevel();
        default:
            return null;
        }
    }

    @Override
    public void setValueAt(Object value,
                           int rowIndex,
                           int columnIndex) {
        if (columnIndex == 1) {
            final Source source = getSource(rowIndex);
            source.setMaxLevel((MeasureLevel) value);
            fireTableRowsUpdated(rowIndex, rowIndex);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy