001package jmri.jmrix.pi;
002
003import java.awt.GraphicsEnvironment;
004import java.util.Date;
005
006import jmri.util.swing.JmriJOptionPane;
007
008/**
009 * Handle configuring a Raspberry Pi layout connection.
010 * <p>
011 * This uses the {@link RaspberryPiAdapter} class to do the actual connection.
012 *
013 * @author Paul Bender Copyright (C) 2015
014 *
015 * @see RaspberryPiAdapter
016 */
017public class RaspberryPiConnectionConfig extends jmri.jmrix.AbstractConnectionConfig {
018
019    private boolean disabled = false;
020    private RaspberryPiAdapter adapter = null;
021    private Date GPIOMessageShown = null;
022
023    /**
024     * Ctor for an object being created during load process; Swing init is
025     * deferred.
026     *
027     * @param p the pre-existing adapter
028     */
029    public RaspberryPiConnectionConfig(RaspberryPiAdapter p) {
030        super();
031        adapter = p;
032    }
033
034    /**
035     * Ctor for a connection configuration with no preexisting adapter.
036     * {@link #setInstance()} will fill the adapter member.
037     */
038    public RaspberryPiConnectionConfig() {
039        super();
040        adapter = new RaspberryPiAdapter();
041    }
042
043    protected boolean init = false;
044
045    /**
046     * {@inheritDoc}
047     */
048    @Override
049    protected void checkInitDone() {
050        log.debug("init called for {}", name());
051        if (init) {
052            return;
053        }
054        addNameEntryCheckers(adapter);
055        init = true;
056    }
057
058    @Override
059    public void updateAdapter() {
060        if (adapter.getSystemConnectionMemo() != null && !adapter.getSystemConnectionMemo().setSystemPrefix(systemPrefixField.getText())) {
061            systemPrefixField.setText(adapter.getSystemConnectionMemo().getSystemPrefix());
062            connectionNameField.setText(adapter.getSystemConnectionMemo().getUserName());
063        }
064    }
065
066    @Override
067    protected void showAdvancedItems() {
068    }
069
070    /**
071     * {@inheritDoc}
072     */
073    @Override
074    public void loadDetails(final javax.swing.JPanel details) {
075        _details = details;
076        setInstance();
077        if (!init) {
078            if (adapter.getSystemConnectionMemo() != null) {
079                systemPrefixField.setText(adapter.getSystemConnectionMemo().getSystemPrefix());
080                connectionNameField.setText(adapter.getSystemConnectionMemo().getUserName());
081                NUMOPTIONS = NUMOPTIONS + 2;
082            }
083            addStandardDetails(adapter, false, NUMOPTIONS);
084            init = false;
085            checkInitDone();
086        }
087    }
088
089    /**
090     * {@inheritDoc}
091     */
092    @Override
093    protected void setInstance() {
094        if (adapter == null) {
095            adapter = new RaspberryPiAdapter();
096        }
097        if (adapter.getGPIOController() == null) {
098            // don't show more than once every 30 seconds
099            if (!GraphicsEnvironment.isHeadless()
100                    && (this.GPIOMessageShown == null || ((new Date().getTime() - this.GPIOMessageShown.getTime()) / 1000 % 60) > 30)) {
101                JmriJOptionPane.showMessageDialog(this._details,
102                        Bundle.getMessage("NoGpioControllerMessage"),
103                        Bundle.getMessage("NoGpioControllerTitle"),
104                        JmriJOptionPane.ERROR_MESSAGE);
105                this.GPIOMessageShown = new Date();
106            }
107        }
108    }
109
110    @Override
111    public RaspberryPiAdapter getAdapter() {
112        return adapter;
113    }
114
115    @Override
116    public String getInfo() {
117        return "GPIO";
118    }
119
120    String manuf = RaspberryPiConnectionTypeList.PI;
121
122    @Override
123    public String getManufacturer() {
124        return manuf;
125    }
126
127    @Override
128    public void setManufacturer(String manufacturer) {
129        manuf = manufacturer;
130    }
131
132    @Override
133    public String name() {
134        return getConnectionName();
135    }
136
137    @Override
138    public String getConnectionName() {
139        return "Raspberry Pi GPIO"; // NOI18N
140    }
141
142    @Override
143    public boolean getDisabled() {
144        return disabled;
145    }
146
147    @Override
148    public void setDisabled(boolean disable) {
149        this.disabled = disable;
150    }
151
152    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(RaspberryPiConnectionConfig.class);
153
154}