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

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

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

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

import javax.swing.table.AbstractTableModel;

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

/**
 * Implementation of a table model for contexts.
 *
 * @author Damien Carbonne
 *
 */
final class ContextsTableModel extends AbstractTableModel {
    private static final long serialVersionUID = 1L;
    private final transient Environment environment;
    private static final String[] COLUMN_NAMES = { "Context", "Visibility", "Measures" };
    protected final transient List contexts = new CopyOnWriteArrayList<>();
    protected final transient Map contextsToRecords = new HashMap<>();
    private final transient EnvironmentListener listener = new EnvironmentListener() {
        @Override
        public void processContextCreated(Context context) {
            createRecord(context);
        }

        @Override
        public void processContextChanged(Context context) {
            final int row = getRowIndex(context);
            if (row >= 0) {
                fireTableRowsUpdated(row, row);
            }
        }

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

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

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

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

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

    private static final class Record {
        private final Context context;
        private boolean visible;

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

        public Context getContext() {
            return context;
        }

        public boolean isVisible() {
            return visible;
        }

        public void setVisible(boolean visible) {
            this.visible = visible;
        }
    }

    protected synchronized void createRecord(Context context) {
        final Record record = new Record(context, true);
        contexts.add(context);
        contextsToRecords.put(context, record);
        final int index = getRowIndex(context);
        fireTableRowsInserted(index, index);
    }

    private Record getRecord(Context context) {
        return contextsToRecords.get(context);
    }

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

    private Record getRecord(int rowIndex) {
        return contextsToRecords.get(contexts.get(rowIndex));
    }

    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);
        }
    }

    @Override
    public int getRowCount() {
        return contextsToRecords.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 Context.class;
        case 1:
            return Boolean.class;
        case 2:
            return Integer.class;
        default:
            return null;
        }
    }

    @Override
    public boolean isCellEditable(int rowIndex,
                                  int columnIndex) {
        return columnIndex == 1;
    }

    @Override
    public Object getValueAt(int rowIndex,
                             int columnIndex) {
        final Record record = getRecord(rowIndex);

        switch (columnIndex) {
        case 0:
            return record.getContext();
        case 1:
            return record.isVisible();
        case 2:
            return record.getContext().getMeasuresCount();
        default:
            return null;
        }
    }

    @Override
    public void setValueAt(Object value,
                           int rowIndex,
                           int columnIndex) {
        if (columnIndex == 1) {
            final Record record = getRecord(rowIndex);
            record.setVisible((Boolean) value);
            fireTableRowsUpdated(rowIndex, rowIndex);
        }
    }

    @Override
    public String toString() {
        return getClass().getCanonicalName() + "<" + environment + ">";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy