001package jmri.jmrit.logixng.implementation.swing;
002
003import java.awt.GridBagConstraints;
004import java.util.List;
005
006import javax.annotation.CheckForNull;
007import javax.annotation.Nonnull;
008import javax.swing.*;
009
010import jmri.*;
011import jmri.jmrit.logixng.*;
012import jmri.jmrit.logixng.MaleSocket.ErrorHandlingType;
013import jmri.jmrit.logixng.implementation.AbstractMaleSocket;
014import jmri.jmrit.logixng.swing.AbstractSwingConfigurator;
015import jmri.util.swing.JComboBoxUtil;
016
017/**
018 * Abstract class for SwingConfiguratorInterface
019 */
020public abstract class AbstractMaleSocketSwing extends AbstractSwingConfigurator {
021
022    private JPanel panel;
023    private final JLabel errorHandlingLabel = new JLabel(Bundle.getMessage("MaleSocket_ErrorHandlingLabel"));
024    private final JLabel catchAbortExecutionLabel = new JLabel(Bundle.getMessage("MaleSocket_CatchAbortExecutionCheckBox"));
025    private final JComboBox<ErrorHandlingType> errorHandlingComboBox = new JComboBox<>();
026    private final JCheckBox catchAbortExecutionCheckBox = new JCheckBox();
027    private JPanel subPanel;
028    
029    
030    protected JPanel getSubPanel(@CheckForNull Base object) {
031        return null;
032    }
033    
034    /** {@inheritDoc} */
035    @Override
036    public BaseManager<? extends NamedBean> getManager() {
037        throw new UnsupportedOperationException("Not supported");
038    }
039    
040    /** {@inheritDoc} */
041    @Override
042    public final JPanel getConfigPanel(@Nonnull JPanel buttonPanel) throws IllegalArgumentException {
043        createPanel(null, buttonPanel);
044        return panel;
045    }
046    
047    /** {@inheritDoc} */
048    @Override
049    public final JPanel getConfigPanel(@Nonnull Base object, @Nonnull JPanel buttonPanel) throws IllegalArgumentException {
050        createPanel(object, buttonPanel);
051        return panel;
052    }
053    
054    protected final void createPanel(@CheckForNull Base object, @Nonnull JPanel buttonPanel) {
055        if ((object != null) && (! (object instanceof AbstractMaleSocket))) {
056            throw new IllegalArgumentException("object is not an AbstractMaleSocket: " + object.getClass().getName());
057        }
058        
059        panel = new JPanel();
060        
061        AbstractMaleSocket maleSocket = (AbstractMaleSocket)object;
062        
063        panel.setLayout(new java.awt.GridBagLayout());
064        java.awt.GridBagConstraints c = new java.awt.GridBagConstraints();
065        c.gridwidth = 1;
066        c.gridheight = 1;
067        c.gridx = 0;
068        c.gridy = 0;
069        c.anchor = java.awt.GridBagConstraints.EAST;
070        panel.add(errorHandlingLabel, c);
071        
072        c.gridx = 1;
073        for (ErrorHandlingType type : ErrorHandlingType.values()) {
074            errorHandlingComboBox.addItem(type);
075            if ((maleSocket != null) && (maleSocket.getErrorHandlingType() == type)) {
076                errorHandlingComboBox.setSelectedItem(type);
077            }
078        }
079        JComboBoxUtil.setupComboBoxMaxRows(errorHandlingComboBox);
080        
081        panel.add(errorHandlingComboBox, c);
082        
083        c.gridx = 0;
084        c.gridy = 1;
085        panel.add(catchAbortExecutionLabel, c);
086        
087        c.gridx = 1;
088        c.fill = GridBagConstraints.HORIZONTAL;
089        if (maleSocket != null) {
090            catchAbortExecutionCheckBox.setSelected(maleSocket.getCatchAbortExecution());
091        }
092//        catchAbortExecutionCheckBox.setAlignmentX(0);
093        catchAbortExecutionLabel.setLabelFor(catchAbortExecutionCheckBox);
094        panel.add(catchAbortExecutionCheckBox, c);
095        
096        subPanel = getSubPanel(object);
097        if (subPanel != null) {
098            JPanel thisPanel = panel;
099            panel = new JPanel();
100            panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
101            panel.add(thisPanel);
102            panel.add(subPanel);
103        }
104    }
105    
106    /** {@inheritDoc} */
107    @Override
108    public final boolean validate(@Nonnull List<String> errorMessages) {
109        return true;
110    }
111    
112    /** {@inheritDoc} */
113    @Override
114    public MaleSocket createNewObject(@Nonnull String systemName, @CheckForNull String userName) {
115        // Male sockets of this type is created by the system, not by the user
116        throw new UnsupportedOperationException("Not supported");
117    }
118    
119    protected void updateObjectForSubPanel(@Nonnull Base object) {
120        // Do nothing
121    }
122    
123    /** {@inheritDoc} */
124    @Override
125    public final void updateObject(@Nonnull Base object) {
126        if (! (object instanceof AbstractMaleSocket)) {
127            throw new IllegalArgumentException("object is not an AbstractMaleSocket: " + object.getClass().getName());
128        }
129
130        AbstractMaleSocket maleSocket = (AbstractMaleSocket)object;
131        maleSocket.setErrorHandlingType(errorHandlingComboBox.getItemAt(errorHandlingComboBox.getSelectedIndex()));
132        maleSocket.setCatchAbortExecution(catchAbortExecutionCheckBox.isSelected());
133        
134        updateObjectForSubPanel(object);
135    }
136    
137    /** {@inheritDoc} */
138    @Override
139    public String getExampleSystemName() {
140        throw new UnsupportedOperationException("Not supported");
141    }
142    
143    /** {@inheritDoc} */
144    @Override
145    public String getAutoSystemName() {
146        throw new UnsupportedOperationException("Not supported");
147    }
148    
149    /** {@inheritDoc} */
150    @Override
151    public String toString() {
152        throw new UnsupportedOperationException("Not supported");
153    }
154    
155    @Override
156    public final void dispose() {
157    }
158    
159    /*.*
160     * Dispose the sub panel and remove all the listeners that this class may
161     * have registered.
162     *./
163    public void disposeSubPanel() {
164    }
165*/    
166}