001package jmri.jmrit.roster.swing;
002
003import java.awt.Component;
004import java.awt.event.ActionEvent;
005import java.awt.event.ActionListener;
006
007import javax.swing.AbstractAction;
008import javax.swing.JComboBox;
009
010import jmri.jmrit.roster.Roster;
011import jmri.jmrit.roster.RosterEntry;
012import jmri.util.swing.JmriJOptionPane;
013
014/**
015 * Associate a Roster Entry to a Roster Group
016 *
017 *
018 * <hr>
019 * This file is part of JMRI.
020 * <p>
021 * JMRI is free software; you can redistribute it and/or modify it under the
022 * terms of version 2 of the GNU General Public License as published by the Free
023 * Software Foundation. See the "COPYING" file for a copy of this license.
024 * <p>
025 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
026 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
027 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
028 *
029 * @author Kevin Dickerson Copyright (C) 2009
030 */
031public class RosterEntryToGroupAction extends AbstractAction {
032
033    /**
034     * @param s Name of this action, e.g. in menus
035     * @param who Component that action is associated with, used to ensure
036     * proper position in of dialog boxes
037     */
038    public RosterEntryToGroupAction(String s, Component who) {
039        super(s);
040        _who = who;
041    }
042
043    Component _who;
044    JComboBox<String> rosterEntry = new JComboBox<String>();
045    RosterGroupComboBox selections;
046    Roster roster;
047    String lastGroupSelect = null;
048
049    @Override
050    public void actionPerformed(ActionEvent event) {
051
052        roster = Roster.getDefault();
053
054        selections = new RosterGroupComboBox();
055        selections.setAllEntriesEnabled(false);
056        if (lastGroupSelect != null) {
057            selections.setSelectedItem(lastGroupSelect);
058        }
059
060        rosterEntryUpdate();
061        selections.addActionListener(new ActionListener() {
062            @Override
063            public void actionPerformed(ActionEvent e) {
064                rosterEntryUpdate();
065            }
066        });
067        int retval = JmriJOptionPane.showOptionDialog(_who,
068                Bundle.getMessage("AddEntryToGroupDialog"), Bundle.getMessage("AddEntryToGroupTitle"),
069                JmriJOptionPane.DEFAULT_OPTION, JmriJOptionPane.INFORMATION_MESSAGE, null,
070                new Object[]{Bundle.getMessage("ButtonDone"), Bundle.getMessage("ButtonOK"), selections, rosterEntry}, null);
071        log.debug("Dialog value {} selected {}:{}, {}:{}", retval, selections.getSelectedIndex(), selections.getSelectedItem(), rosterEntry.getSelectedIndex(), rosterEntry.getSelectedItem());
072        if (retval != 1) { // not array position 1, ButtonOK
073            return;
074        }
075
076        String selEntry = (String) rosterEntry.getSelectedItem();
077        lastGroupSelect = selections.getSelectedItem();
078        RosterEntry re = roster.entryFromTitle(selEntry);
079        String selGroup = Roster.getRosterGroupProperty(selections.getSelectedItem());
080        re.putAttribute(selGroup, "yes");
081        Roster.getDefault().writeRoster();
082        re.updateFile();
083        actionPerformed(event);
084    }
085
086    void rosterEntryUpdate() {
087        if (rosterEntry != null) {
088            rosterEntry.removeAllItems();
089        }
090        String group = Roster.ROSTER_GROUP_PREFIX + selections.getSelectedItem();
091        roster.getAllEntries().stream().filter((r) -> (r.getAttribute(group) == null)).forEachOrdered((r) -> {
092            rosterEntry.addItem(r.titleString());
093        });
094    }
095
096    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(RosterEntryToGroupAction.class);
097}