Search in sources :

Example 1 with RosterToEcos

use of jmri.jmrix.ecos.utilities.RosterToEcos in project JMRI by JMRI.

the class EcosLocoAddressManager method propertyChange.

/**
     * The PropertyChangeListener interface in this class is intended to keep
     * track of roster entries and sync them up with the Ecos.
     */
@Override
public void propertyChange(java.beans.PropertyChangeEvent e) {
    //If we are adding the loco to the roster from the ecos, we don't want to be adding it back to the ecos!
    if (getLocoToRoster()) {
        return;
    }
    if (e.getPropertyName().equals("add")) {
        _re = (RosterEntry) e.getNewValue();
    } else if (e.getPropertyName().equals("saved")) {
        if (_re != null) {
            if (_re.getAttribute(rosterAttribute) != null) {
                _re = null;
                return;
            }
            //if the ecosobject attribute exists this would then indicate that it has already been created on the ecos
            if (p.getAddLocoToEcos() == EcosPreferences.ASK) {
                final JDialog dialog = new JDialog();
                dialog.setTitle("Add Loco to the ECoS?");
                //test.setSize(300,130);
                dialog.setLocation(300, 200);
                dialog.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
                JPanel container = new JPanel();
                container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
                container.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
                JLabel question = new JLabel("Do you also want to add " + _re.getId() + " to the " + adaptermemo.getUserName() + "?");
                question.setAlignmentX(Component.CENTER_ALIGNMENT);
                container.add(question);
                final JCheckBox remember = new JCheckBox("Remember this setting for next time?");
                remember.setFont(remember.getFont().deriveFont(10f));
                remember.setAlignmentX(Component.CENTER_ALIGNMENT);
                //user preferences do not have the save option, but once complete the following line can be removed
                //Need to get the method to save connection configuration.
                remember.setVisible(true);
                JButton yesButton = new JButton("Yes");
                JButton noButton = new JButton("No");
                JPanel button = new JPanel();
                button.setAlignmentX(Component.CENTER_ALIGNMENT);
                button.add(yesButton);
                button.add(noButton);
                container.add(button);
                noButton.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (remember.isSelected()) {
                            p.setAddLocoToEcos(0x01);
                        }
                        _re = null;
                        dialog.dispose();
                    }
                });
                yesButton.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (remember.isSelected()) {
                            p.setAddLocoToEcos(0x02);
                        }
                        RosterToEcos rosterToEcos = new RosterToEcos();
                        rosterToEcos.createEcosLoco(_re, adaptermemo);
                        _re = null;
                        dialog.dispose();
                    }
                });
                container.add(remember);
                container.setAlignmentX(Component.CENTER_ALIGNMENT);
                container.setAlignmentY(Component.CENTER_ALIGNMENT);
                dialog.getContentPane().add(container);
                dialog.pack();
                dialog.setModal(true);
                dialog.setVisible(true);
            }
            if (p.getAddLocoToEcos() == 0x02) {
                RosterToEcos rosterToEcos = new RosterToEcos();
                rosterToEcos.createEcosLoco(_re, adaptermemo);
                _re = null;
            }
        }
    } else if (e.getPropertyName().equals("remove")) {
        _re = (RosterEntry) e.getNewValue();
        if (_re.getAttribute(rosterAttribute) != null) {
            if (p.getRemoveLocoFromEcos() == EcosPreferences.YES) {
                RemoveObjectFromEcos removeObjectFromEcos = new RemoveObjectFromEcos();
                removeObjectFromEcos.removeObjectFromEcos(_re.getAttribute(p.getRosterAttribute()), tc);
                deleteEcosLoco(provideByEcosObject(_re.getAttribute(p.getRosterAttribute())));
            } else if (p.getRemoveLocoFromEcos() == EcosPreferences.ASK) {
                final JDialog dialog = new JDialog();
                dialog.setTitle("Remove Loco From ECoS?");
                //test.setSize(300,130);
                dialog.setLocation(300, 200);
                dialog.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE);
                JPanel container = new JPanel();
                container.setLayout(new BoxLayout(container, BoxLayout.Y_AXIS));
                container.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
                JLabel question = new JLabel("Do you also want to remove this loco from the " + adaptermemo.getUserName());
                question.setAlignmentX(Component.CENTER_ALIGNMENT);
                container.add(question);
                final JCheckBox remember = new JCheckBox("Remember this setting for next time?");
                remember.setFont(remember.getFont().deriveFont(10f));
                remember.setAlignmentX(Component.CENTER_ALIGNMENT);
                //user preferences do not have the save option, but once complete the following line can be removed
                //Need to get the method to save connection configuration.
                remember.setVisible(true);
                JButton yesButton = new JButton("Yes");
                JButton noButton = new JButton("No");
                JPanel button = new JPanel();
                button.setAlignmentX(Component.CENTER_ALIGNMENT);
                button.add(yesButton);
                button.add(noButton);
                container.add(button);
                noButton.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (remember.isSelected()) {
                            p.setRemoveLocoFromEcos(0x01);
                        }
                        provideByEcosObject(_re.getAttribute(p.getRosterAttribute())).setRosterId(null);
                        dialog.dispose();
                    }
                });
                yesButton.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        if (remember.isSelected()) {
                            p.setRemoveLocoFromEcos(0x02);
                        }
                        RemoveObjectFromEcos removeObjectFromEcos = new RemoveObjectFromEcos();
                        removeObjectFromEcos.removeObjectFromEcos(_re.getAttribute(p.getRosterAttribute()), tc);
                        deleteEcosLoco(provideByEcosObject(_re.getAttribute(p.getRosterAttribute())));
                        dialog.dispose();
                    }
                });
                container.add(remember);
                container.setAlignmentX(Component.CENTER_ALIGNMENT);
                container.setAlignmentY(Component.CENTER_ALIGNMENT);
                dialog.getContentPane().add(container);
                dialog.pack();
                dialog.setModal(true);
                dialog.setVisible(true);
            }
        }
        _re = null;
    } else if (e.getPropertyName().equals("throttleAssigned")) {
        jmri.DccLocoAddress la = (jmri.DccLocoAddress) e.getNewValue();
        EcosLocoAddress ela = getByDccAddress(la.getNumber());
        EcosMessage m = new EcosMessage("get(" + ela.getEcosObject() + ", speed)");
        tc.sendEcosMessage(m, this);
        m = new EcosMessage("get(" + ela.getEcosObject() + ", dir)");
        tc.sendEcosMessage(m, this);
    }
}
Also used : JPanel(javax.swing.JPanel) ActionEvent(java.awt.event.ActionEvent) BoxLayout(javax.swing.BoxLayout) JButton(javax.swing.JButton) JLabel(javax.swing.JLabel) RosterToEcos(jmri.jmrix.ecos.utilities.RosterToEcos) JCheckBox(javax.swing.JCheckBox) ActionListener(java.awt.event.ActionListener) JDialog(javax.swing.JDialog) RemoveObjectFromEcos(jmri.jmrix.ecos.utilities.RemoveObjectFromEcos)

Aggregations

ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 BoxLayout (javax.swing.BoxLayout)1 JButton (javax.swing.JButton)1 JCheckBox (javax.swing.JCheckBox)1 JDialog (javax.swing.JDialog)1 JLabel (javax.swing.JLabel)1 JPanel (javax.swing.JPanel)1 RemoveObjectFromEcos (jmri.jmrix.ecos.utilities.RemoveObjectFromEcos)1 RosterToEcos (jmri.jmrix.ecos.utilities.RosterToEcos)1