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

org.bidib.wizard.dmx.client.model.DmxDataRow Maven / Gradle / Ivy

There is a newer version: 2.0.29
Show newest version
package org.bidib.wizard.dmx.client.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jgoodies.common.base.Objects;
import com.jidesoft.grid.DefaultExpandableRow;

/**
 * The {@code DmxDataRow} contains the time data and the configuration variables.
 */
public class DmxDataRow extends DefaultExpandableRow {

    private static final Logger LOGGER = LoggerFactory.getLogger(DmxDataRow.class);

    private final DmxDataIndex dmxDataIndex;

    private final List cvValues = new ArrayList<>();

    private boolean dirty;

    public DmxDataRow(final DmxDataIndex timeIndex) {
        this.dmxDataIndex = timeIndex;
    }

    @Override
    public Object getValueAt(int columnIndex) {
        switch (columnIndex) {
            case DmxDataSetTreeTableModel.COLUMN_TIME_INDEX:
                return dmxDataIndex;
            default:
                if (cvValues.size() > 0 && columnIndex < (cvValues.size() + 1)) {
                    Integer val = cvValues.get(columnIndex - 1).getNewValue();
                    if (val == null) {
                        val = cvValues.get(columnIndex - 1).getValue();
                    }
                    return val;
                }
                else if (columnIndex == cvValues.size() + 1) {
                    // COLUMN_ACTION
                }
                else {
                    LOGGER.warn("getValueAt, column out of range: {}", columnIndex);
                }
                break;
        }

        return null;
    }

    @Override
    public void setValueAt(Object value, int columnIndex) {

        switch (columnIndex) {
            case DmxDataSetTreeTableModel.COLUMN_TIME_INDEX:
                break;
            default:
                if (cvValues.size() > 0 && columnIndex < (cvValues.size() + 1)) {
                    DmxChannelValue currentValue = cvValues.get(columnIndex - 1);

                    if ((!Objects.equals(currentValue.getValue(), value) && currentValue.getNewValue() == null)
                        || (currentValue.getNewValue() != null && !Objects.equals(currentValue.getNewValue(), value))) {

                        currentValue.setNewValue(value != null ? (Integer) value : null);
                        LOGGER.info("Value has changed, columnIndex: {}, value: {}", columnIndex, value);
                        setDirty(true);
                    }
                }
                else if (columnIndex == cvValues.size() + 1) {
                    // COLUMN_ACTION
                }
                else {
                    LOGGER.warn("setValueAt, column out of range: {}, value: {}", columnIndex, value);
                }
                break;
        }

        super.setValueAt(value, columnIndex);
    }

    @Override
    public boolean isCellEditable(int columnIndex) {

        switch (columnIndex) {
            default:
                if (cvValues.size() > 0 && columnIndex > 0 && columnIndex < (cvValues.size() + 1)) {
                    return true;
                }
                else if (columnIndex == cvValues.size() + 1) {
                    // COLUMN_ACTION
                    return true;
                }
                break;
        }
        return false;
    }

    @Override
    public Class getCellClassAt(int columnIndex) {
        switch (columnIndex) {
            case DmxDataSetTreeTableModel.COLUMN_TIME_INDEX:
                return DmxDataIndex.class;

            default:
                if (cvValues.size() > 0 && columnIndex > 0 && columnIndex < (cvValues.size() + 1)) {
                    return Integer.class;
                }
                else if (columnIndex == (cvValues.size() + 1)) {
                    // COLUMN_ACTION
                    return MappingAction.class;
                }
                break;
        }
        return super.getCellClassAt(columnIndex);
    }

    /**
     * @return the dataIndex
     */
    public DmxDataIndex getDataIndex() {
        return dmxDataIndex;
    }

    public List getCvValues() {
        return Collections.unmodifiableList(cvValues);
    }

    public void addCvValue(DmxChannelValue channel) {
        this.cvValues.add(channel);
    }

    public void removeChannelAtIndex(int channelIndex) {
        this.cvValues.remove(channelIndex);
    }

    public void clearChannels() {
        this.cvValues.clear();
    }

    /**
     * @return the dirty
     */
    public boolean isDirty() {
        return dirty;
    }

    /**
     * @param dirty
     *            the dirty to set
     */
    public void setDirty(boolean dirty) {
        this.dirty = dirty;
        if (!dirty) {
            LOGGER.debug("The dirty flag ist reset. Reset the new values.");
            // reset the new values
            for (DmxChannelValue value : this.cvValues) {
                value.setNewValue(null);
            }
        }
    }

    public boolean isRowValid() {
        for (DmxChannelValue value : cvValues) {
            if (value.getValue() == null) {
                return false;
            }
        }
        return true;
    }

    public Integer getBrightness(int channelIndex) {
        return (Integer) getValueAt(channelIndex + 1);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy