001package jmri.jmrit.roster.swing;
002
003import java.awt.FlowLayout;
004import java.awt.event.ActionEvent;
005import java.awt.event.ActionListener;
006import java.beans.PropertyChangeEvent;
007import java.beans.PropertyChangeListener;
008import javax.swing.JPanel;
009import jmri.jmrit.roster.RosterEntry;
010import jmri.jmrit.roster.RosterEntrySelector;
011import jmri.jmrit.roster.rostergroup.RosterGroupSelector;
012
013/**
014 *
015 * @author Randall Wood
016 */
017public class RosterEntrySelectorPanel extends JPanel implements RosterEntrySelector, RosterGroupSelector {
018
019    private RosterEntryComboBox entryCombo;
020    private RosterGroupComboBox groupCombo;
021
022    public RosterEntrySelectorPanel() {
023        this(null, null);
024    }
025
026    public RosterEntrySelectorPanel(RosterEntry re, String rg) {
027        super();
028        this.setLayout(new FlowLayout());
029        this.entryCombo = new RosterEntryComboBox(rg);
030        if (re != null) {
031            this.entryCombo.setSelectedItem(re.titleString());
032        }
033        this.groupCombo = new RosterGroupComboBox(rg);
034        this.add(groupCombo);
035        this.add(entryCombo);
036
037        this.groupCombo.addActionListener(new ActionListener() {
038
039            @Override
040            public void actionPerformed(ActionEvent ae) {
041                entryCombo.update(getSelectedRosterGroup());
042            }
043
044        });
045
046        this.entryCombo.addPropertyChangeListener(new PropertyChangeListener() {
047
048            @Override
049            public void propertyChange(PropertyChangeEvent pce) {
050                if (pce.getPropertyName().equals(RosterEntrySelector.SELECTED_ROSTER_ENTRIES)) {
051                    fireSelectedRosterEntriesPropertyChange(pce.getOldValue(), pce.getNewValue());
052                }
053            }
054
055        });
056    }
057
058    protected void fireSelectedRosterEntriesPropertyChange(Object oldValue, Object newValue) {
059        this.firePropertyChange(RosterEntrySelector.SELECTED_ROSTER_ENTRIES, oldValue, newValue);
060    }
061
062    @Override
063    public RosterEntry[] getSelectedRosterEntries() {
064        return entryCombo.getSelectedRosterEntries();
065    }
066
067    public void setSelectedRosterEntry(RosterEntry re) {
068        entryCombo.setSelectedItem(re);
069    }
070
071    public void setSelectedRosterEntryAndGroup(RosterEntry re, String rg) {
072        this.setSelectedRosterEntry(re);
073        this.setSelectedRosterGroup(rg);
074    }
075
076    @Override
077    public String getSelectedRosterGroup() {
078        return groupCombo.getSelectedRosterGroup();
079    }
080
081    public void setSelectedRosterGroup(String rg) {
082        groupCombo.update(rg);
083    }
084
085    public RosterEntryComboBox getRosterEntryComboBox() {
086        return entryCombo;
087    }
088
089    public RosterGroupComboBox getRosterGroupComboBox() {
090        return groupCombo;
091    }
092
093    @Override
094    public void setEnabled(boolean enabled) {
095        this.entryCombo.setEnabled(enabled);
096        this.groupCombo.setEnabled(enabled);
097    }
098
099    @Override
100    public boolean isEnabled() {
101        return this.entryCombo.isEnabled();
102    }
103
104    public String getNonSelectedItem() {
105        return this.entryCombo.getNonSelectedItem();
106    }
107
108    public void setNonSelectedItem(String itemText) {
109        this.entryCombo.setNonSelectedItem(itemText);
110    }
111}