001package jmri.jmrit.logixng.expressions.swing;
002
003import java.util.List;
004
005import javax.annotation.CheckForNull;
006import javax.annotation.Nonnull;
007import javax.swing.*;
008
009import jmri.InstanceManager;
010import jmri.jmrit.logixng.*;
011import jmri.jmrit.logixng.expressions.FileAsFlag;
012import jmri.jmrit.logixng.expressions.FileAsFlag.DeleteOrKeep;
013import jmri.jmrit.logixng.swing.SwingConfiguratorInterface;
014import jmri.jmrit.logixng.util.swing.LogixNG_SelectEnumSwing;
015import jmri.jmrit.logixng.util.swing.LogixNG_SelectStringSwing;
016import jmri.util.FileUtil;
017
018/**
019 * Configures an FileAsFlag object with a Swing JPanel.
020 *
021 * @author Daniel Bergqvist 2023
022 */
023public class FileAsFlagSwing extends AbstractDigitalExpressionSwing {
024
025    private LogixNG_SelectStringSwing _selectFilenameSwing;
026    private LogixNG_SelectEnumSwing<DeleteOrKeep> _selectDeleteOrKeepSwing;
027
028
029    @Override
030    protected void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
031        FileAsFlag expression = (FileAsFlag)object;
032
033        if (expression == null) {
034            // Create a temporary expression
035            expression = new FileAsFlag("IQDE1", null);
036        }
037
038        _selectFilenameSwing = new LogixNG_SelectStringSwing(getJDialog(), this);
039        _selectDeleteOrKeepSwing = new LogixNG_SelectEnumSwing<>(getJDialog(), this);
040
041        panel = new JPanel();
042
043        JPanel tabbedPaneNamedBean;
044        JPanel tabbedPaneState;
045
046        tabbedPaneNamedBean = _selectFilenameSwing.createFilenamePanel(expression.getSelectFilename(), FileUtil.getUserFilesPath());
047        tabbedPaneState = _selectDeleteOrKeepSwing.createPanel(expression.getSelectDeleteOrKeep(), DeleteOrKeep.values());
048
049        JComponent[] components = new JComponent[]{
050            tabbedPaneNamedBean,
051            tabbedPaneState};
052
053        List<JComponent> componentList = SwingConfiguratorInterface.parseMessage(
054                Bundle.getMessage("FileAsFlag_Components"), components);
055
056        for (JComponent c : componentList) panel.add(c);
057    }
058
059    /** {@inheritDoc} */
060    @Override
061    public boolean validate(@Nonnull List<String> errorMessages) {
062        // Create a temporary expression to test formula
063        FileAsFlag expression = new FileAsFlag("IQDE1", null);
064
065        _selectFilenameSwing.validate(expression.getSelectFilename(), errorMessages);
066        _selectDeleteOrKeepSwing.validate(expression.getSelectDeleteOrKeep(), errorMessages);
067
068        return errorMessages.isEmpty();
069    }
070
071    /** {@inheritDoc} */
072    @Override
073    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
074        FileAsFlag expression = new FileAsFlag(systemName, userName);
075        updateObject(expression);
076        return InstanceManager.getDefault(DigitalExpressionManager.class).registerExpression(expression);
077    }
078
079    /** {@inheritDoc} */
080    @Override
081    public void updateObject(@Nonnull Base object) {
082        if (! (object instanceof FileAsFlag)) {
083            throw new IllegalArgumentException("object must be an FileAsFlag but is a: "+object.getClass().getName());
084        }
085        FileAsFlag expression = (FileAsFlag)object;
086
087        _selectFilenameSwing.updateObject(expression.getSelectFilename());
088        _selectDeleteOrKeepSwing.updateObject(expression.getSelectDeleteOrKeep());
089    }
090
091    /** {@inheritDoc} */
092    @Override
093    public String toString() {
094        return Bundle.getMessage("FileAsFlag_Short");
095    }
096
097    @Override
098    public void dispose() {
099    }
100
101
102//    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(FileAsFlagSwing.class);
103
104}