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

org.bidib.wizard.nodes.client.view.AspectMappingTreeTable Maven / Gradle / Ivy

package org.bidib.wizard.nodes.client.view;

import java.util.function.BiConsumer;

import javax.swing.JOptionPane;
import javax.swing.table.TableModel;

import org.bidib.wizard.api.locale.Resources;
import org.bidib.wizard.client.common.view.slider.SliderRenderer;
import org.bidib.wizard.nodes.client.controller.InvalidMappingException;
import org.bidib.wizard.nodes.client.model.AccessoryAspectTreeTableModel;
import org.bidib.wizard.nodes.client.model.AspectRow;
import org.bidib.wizard.nodes.client.model.MappingAction;
import org.oxbow.swingbits.dialog.task.TaskDialog.StandardIcon;
import org.oxbow.swingbits.dialog.task.TaskDialogs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jidesoft.grid.DefaultExpandableRow;
import com.jidesoft.grid.FilterableTableModel;
import com.jidesoft.grid.Row;
import com.jidesoft.grid.SortableTableModel;
import com.jidesoft.grid.TreeTable;

public class AspectMappingTreeTable extends TreeTable {

    private static final long serialVersionUID = 1L;

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

    private final BiConsumer writeConsumer;

    public AspectMappingTreeTable(final TableModel tableModel,
        final BiConsumer writeConsumer) {
        super(tableModel);
        this.writeConsumer = writeConsumer;
    }

    public void adjustRowHeight() {
        // set the correct row height
        final SliderRenderer sliderEditor = new SliderRenderer(0, 255, 10);

        int rowHeight =
            sliderEditor.getTableCellRendererComponent(this, 1, false, false, 0, 0).getPreferredSize().height + 6;
        LOGGER.debug("Set row height: {}", rowHeight);

        setRowHeight(rowHeight);
    }

    public void removeMapping(int rowIndex) {

        final AccessoryAspectTreeTableModel tableModel = getTableModel();
        final Row row = tableModel.getRowAt(rowIndex);
        LOGGER.info("Current rowIndex: {}, row: {}", rowIndex, row);

        if (row instanceof AspectRow) {
            final AspectRow aspectRow = (AspectRow) row;
            LOGGER.info("Current aspectRow: {}", aspectRow);

            if (!aspectRow.hasValidMapping()) {
                TaskDialogs
                    .build(JOptionPane.getFrameForComponent(this),
                        Resources.getString(AspectMappingTreeTable.class, "invalid-mapping.title"),
                        Resources.getString(AspectMappingTreeTable.class, "invalid-mapping.instruction"))
                    .icon(StandardIcon.WARNING).message();
                return;
            }

            boolean confirmed =
                TaskDialogs
                    .isConfirmed(JOptionPane.getFrameForComponent(this),
                        Resources.getString(AspectMappingTreeTable.class, "delete-mapping.title"),
                        Resources
                            .getString(AspectMappingTreeTable.class, "delete-mapping.instruction",
                                aspectRow.getAccessoryLabel(), aspectRow.getAspectNumber(),
                                aspectRow.getDccAccessoryAddress(), aspectRow.getDccAspect()));
            if (confirmed) {
                try {
                    this.writeConsumer.accept(aspectRow, MappingAction.delete);
                }
                catch (InvalidMappingException ex) {
                    LOGGER.warn("Delete mapping failed.", ex);
                }
            }
            else {
                LOGGER.info("User cancelled delete aspect mapping: {}", aspectRow);
            }
        }
    }

    public void writeMapping(int rowIndex) {

        final AccessoryAspectTreeTableModel tableModel = getTableModel();
        final Row row = tableModel.getRowAt(rowIndex);
        LOGGER.info("Current rowIndex: {}, row: {}", rowIndex, row);

        if (row instanceof AspectRow) {
            final AspectRow aspectRow = (AspectRow) row;
            LOGGER.info("Current aspectRow: {}", aspectRow);

            if (!aspectRow.hasValidMapping()) {
                TaskDialogs
                    .build(JOptionPane.getFrameForComponent(this),
                        Resources.getString(AspectMappingTreeTable.class, "invalid-mapping.title"),
                        Resources.getString(AspectMappingTreeTable.class, "invalid-mapping.instruction"))
                    .icon(StandardIcon.WARNING).message();
                return;
            }

            try {
                this.writeConsumer.accept(aspectRow, MappingAction.create);
            }
            catch (InvalidMappingException ex) {
                LOGGER.warn("Create new mapping failed.", ex);
            }
        }
    }

    private AccessoryAspectTreeTableModel getTableModel() {
        TableModel tm = getModel();
        if (tm instanceof FilterableTableModel) {
            tm = ((FilterableTableModel) tm).getActualModel();
        }
        if (tm instanceof SortableTableModel) {
            tm = ((SortableTableModel) tm).getActualModel();
        }
        final AccessoryAspectTreeTableModel tableModel =
            (AccessoryAspectTreeTableModel) tm;
        return tableModel;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy