001package jmri.jmrix.ipocs;
002
003import jmri.jmrix.AbstractConnectionConfig;
004
005import java.awt.event.ActionEvent;
006import java.awt.event.ActionListener;
007import java.awt.event.FocusEvent;
008import java.awt.event.FocusListener;
009
010import javax.swing.JLabel;
011import javax.swing.JPanel;
012import javax.swing.JTextField;
013
014import jmri.util.swing.JmriJOptionPane;
015
016/**
017 * @author Fredrik Elestedt Copyright (C) 2020
018 * @since 4.21.2
019 */
020public class IpocsConnectionConfig extends AbstractConnectionConfig {
021  private IpocsPortController portController;
022  private final JTextField portField = new JTextField(6);
023  private final JLabel portFieldLabel = new JLabel();
024
025  public IpocsConnectionConfig() {
026    super();
027    portField.setText("0");
028  }
029
030  public IpocsConnectionConfig(IpocsPortController portController) {
031    this();
032    this.portController = portController;
033  }
034
035  @Override
036  public String name() {
037    return "IPOCS Connection";
038  }
039
040  @Override
041  protected void setInstance() {
042    if (portController == null) {
043      portController = new IpocsPortController(new IpocsSystemConnectionMemo());
044      portController.configure();
045    }
046  }
047
048  @Override
049  public String getInfo() {
050    return getAdapter().getCurrentPortName();
051  }
052
053  @Override
054  public IpocsPortController getAdapter() {
055    return portController;
056  }
057
058  @Override
059  protected void checkInitDone() {
060
061  }
062
063  @Override
064  public void updateAdapter() {
065    getAdapter().setPort(Short.parseShort(portField.getText()));
066
067    if (getAdapter().getSystemConnectionMemo() != null &&
068        !getAdapter().getSystemConnectionMemo().setSystemPrefix(systemPrefixField.getText())) {
069      systemPrefixField.setText(getAdapter().getSystemConnectionMemo().getSystemPrefix());
070      connectionNameField.setText(getAdapter().getSystemConnectionMemo().getUserName());
071    }
072  }
073
074  public class SystemPrefixFieldActionListener implements ActionListener {
075    @Override
076    public void actionPerformed(java.awt.event.ActionEvent e) {
077      if (!getAdapter().getSystemConnectionMemo().setSystemPrefix(systemPrefixField.getText())) {
078        JmriJOptionPane.showMessageDialog(null, Bundle.getMessage(Bundle.UNABLE_PREFIX));
079        systemPrefixField.setText(getAdapter().getSystemConnectionMemo().getSystemPrefix());
080      }
081    }
082  }
083  public class SystemPrefixFieldFocusListener implements FocusListener {
084    @Override
085    public void focusLost(java.awt.event.FocusEvent e) {
086      if (!getAdapter().getSystemConnectionMemo().setSystemPrefix(systemPrefixField.getText())) {
087        JmriJOptionPane.showMessageDialog(null, Bundle.getMessage(Bundle.UNABLE_PREFIX));
088        systemPrefixField.setText(getAdapter().getSystemConnectionMemo().getSystemPrefix());
089      }
090    }
091
092    @Override
093    public void focusGained(java.awt.event.FocusEvent e) {
094    }
095  }
096
097  public class ConnectionNameFieldActionListener implements ActionListener {
098    @Override
099    public void actionPerformed(java.awt.event.ActionEvent e) {
100      if (!getAdapter().getSystemConnectionMemo().setUserName(connectionNameField.getText())) {
101        JmriJOptionPane.showMessageDialog(null, Bundle.getMessage(Bundle.UNABLE_CONNNAME));
102        connectionNameField.setText(getAdapter().getSystemConnectionMemo().getUserName());
103      }
104    }
105  }
106  public class ConnectionNameFieldFocusListener implements FocusListener {
107    @Override
108    public void focusLost(java.awt.event.FocusEvent e) {
109      if (!getAdapter().getSystemConnectionMemo().setUserName(connectionNameField.getText())) {
110        JmriJOptionPane.showMessageDialog(null, Bundle.getMessage(Bundle.UNABLE_CONNNAME));
111        connectionNameField.setText(getAdapter().getSystemConnectionMemo().getUserName());
112      }
113    }
114
115    @Override
116    public void focusGained(java.awt.event.FocusEvent e) {
117    }
118  }
119
120  public class PortFieldActionListener implements ActionListener {
121    @Override
122    public void actionPerformed(ActionEvent e) {
123      try {
124        getAdapter().setPort(Short.parseShort(portField.getText()));
125      } catch (java.lang.NumberFormatException ex) {
126        JmriJOptionPane.showMessageDialog(null, Bundle.getMessage(Bundle.UNABLE_PORT));
127      }
128    }
129  }
130  public class PortFieldFocusListener implements FocusListener {
131    @Override
132    public void focusLost(FocusEvent e) {
133      try {
134        getAdapter().setPort(Short.parseShort(portField.getText()));
135      } catch (java.lang.NumberFormatException ex) {
136        JmriJOptionPane.showMessageDialog(null, Bundle.getMessage(Bundle.UNABLE_PORT));
137      }
138    }
139
140    @Override
141    public void focusGained(FocusEvent e) {
142    }
143  }
144
145  @Override
146  public void loadDetails(JPanel details) {
147    _details = details;
148    _details.setLayout(gbLayout);
149    setInstance();
150
151    portField.setToolTipText(Bundle.getMessage(Bundle.PORT_TOOLTIP));
152    portField.setEnabled(true);
153    portField.setText(String.valueOf(getAdapter().getPort()));
154    portFieldLabel.setText(Bundle.getMessage(Bundle.PORT_LABEL));
155
156    if (getAdapter().getSystemConnectionMemo() != null) {
157      systemPrefixField.setText(getAdapter().getSystemConnectionMemo().getSystemPrefix());
158      connectionNameField.setText(getAdapter().getSystemConnectionMemo().getUserName());
159      NUMOPTIONS = NUMOPTIONS + 2;
160    }
161    systemPrefixField.addActionListener(new SystemPrefixFieldActionListener());
162    systemPrefixField.addFocusListener(new SystemPrefixFieldFocusListener());
163    connectionNameField.addActionListener(new ConnectionNameFieldActionListener());
164    connectionNameField.addFocusListener(new ConnectionNameFieldFocusListener());
165    portField.addActionListener(new PortFieldActionListener());
166    portField.addFocusListener(new PortFieldFocusListener());
167
168    int i = super.addStandardDetails(portController, false, 1);
169    cR.gridy = i;
170    cL.gridy = i;
171    gbLayout.setConstraints(portFieldLabel, cL);
172    gbLayout.setConstraints(portField, cR);
173    _details.add(portFieldLabel);
174    _details.add(portField);
175  }
176
177  @Override
178  protected void showAdvancedItems() {
179  }
180
181  @Override
182  public String getManufacturer() {
183    return IpocsConnectionTypeList.IPOCSMR;
184  }
185
186  @Override
187  public void setManufacturer(String manufacturer) {
188    portController.setManufacturer(manufacturer);
189  }
190
191  @Override
192  public String getConnectionName() {
193    return "IPOCSMR";
194  }
195
196  private boolean isDisabled = false;
197
198  @Override
199  public boolean getDisabled() {
200    return isDisabled;
201  }
202
203  @Override
204  public void setDisabled(boolean disable) {
205    this.isDisabled = disable;
206  }
207}