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

org.bidib.wizard.mvc.main.controller.FlagPanelController Maven / Gradle / Ivy

There is a newer version: 2.0.29
Show newest version
package org.bidib.wizard.mvc.main.controller;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import org.bidib.jbidibc.core.schema.bidiblabels.NodeLabels;
import org.bidib.wizard.api.locale.Resources;
import org.bidib.wizard.api.model.NodeInterface;
import org.bidib.wizard.common.labels.BidibLabelUtils;
import org.bidib.wizard.common.labels.LabelsChangedEvent;
import org.bidib.wizard.common.labels.WizardLabelFactory;
import org.bidib.wizard.common.labels.WizardLabelWrapper;
import org.bidib.wizard.mvc.main.model.MainModel;
import org.bidib.wizard.mvc.main.view.exchange.NodeExchangeHelper;
import org.bidib.wizard.mvc.main.view.panel.FlagListPanel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;

public class FlagPanelController {
    private static final Logger LOGGER = LoggerFactory.getLogger(FlagPanelController.class);

    private final MainModel mainModel;

    @Autowired
    private WizardLabelWrapper wizardLabelWrapper;

    private FlagListPanel flagListPanel;

    public FlagPanelController(final MainModel mainModel) {
        this.mainModel = mainModel;
    }

    public FlagListPanel createFlagListPanel() {
        this.flagListPanel = new FlagListPanel(this, mainModel);

        return this.flagListPanel;
    }

    public void setFlagLabel(int flagId, String label) {

        LOGGER.info("Set the flag label: {}", label);

        final NodeInterface node = mainModel.getSelectedNode();
        if (node != null) {
            // update the stored labels in the single base or the master base
            try {
                NodeLabels nodeLabels = getNodeLabels();
                BidibLabelUtils.replaceFlagLabel(nodeLabels, flagId, label);
                saveLabels();
            }
            catch (Exception ex) {
                LOGGER.warn("Save feedback labels failed.", ex);

                String labelPath = ex.getMessage();
                JOptionPane
                    .showMessageDialog(JOptionPane.getFrameForComponent(null),
                        Resources
                            .getString(NodeExchangeHelper.class, "labelfileerror.message", new Object[] { labelPath }),
                        Resources.getString(NodeExchangeHelper.class, "labelfileerror.title"),
                        JOptionPane.ERROR_MESSAGE);

                throw new RuntimeException(ex);
            }
        }
        else {
            LOGGER.warn("No node selected, labels are not stored.");
        }
    }

    private NodeLabels getNodeLabels() {
        final WizardLabelFactory wizardLabelFactory = wizardLabelWrapper.getWizardLabelFactory();

        NodeLabels nodeLabels = wizardLabelFactory.loadLabels(mainModel.getSelectedNode().getUniqueId());
        return nodeLabels;
    }

    private void saveLabels() {
        try {
            long uniqueId = mainModel.getSelectedNode().getUniqueId();
            wizardLabelWrapper.saveNodeLabels(uniqueId);
        }
        catch (Exception e) {
            LOGGER.warn("Save accessory labels failed.", e);
            throw new RuntimeException(e);
        }
    }

    @EventListener(LabelsChangedEvent.class)
    public void labelsChangedEvent(LabelsChangedEvent labelsChangedEvent) {
        LOGGER.info("The labels have changed, node: {}", labelsChangedEvent);

        if (this.flagListPanel != null) {
            SwingUtilities.invokeLater(() -> this.flagListPanel.refreshView());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy