001package jmri.jmrit.roster.swing;
002
003import java.awt.Component;
004import java.awt.FlowLayout;
005import java.awt.event.ActionEvent;
006import java.awt.event.ActionListener;
007
008import javax.swing.AbstractAction;
009import javax.swing.BoxLayout;
010import javax.swing.JButton;
011import javax.swing.JLabel;
012import javax.swing.JPanel;
013
014import jmri.jmrit.roster.Roster;
015import jmri.jmrit.roster.RosterEntry;
016import jmri.util.JmriJFrame;
017import jmri.util.swing.JmriJOptionPane;
018
019/**
020 * Remove a locomotive from a roster grouping.
021 *
022 *
023 * <hr>
024 * This file is part of JMRI.
025 * <p>
026 * JMRI is free software; you can redistribute it and/or modify it under the
027 * terms of version 2 of the GNU General Public License as published by the Free
028 * Software Foundation. See the "COPYING" file for a copy of this license.
029 * <p>
030 * JMRI is distributed in the hope that it will be useful, but WITHOUT ANY
031 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
032 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
033 *
034 * @author Kevin Dickerson Copyright (C) 2009
035 */
036public class RemoveRosterEntryToGroupAction extends AbstractAction {
037
038
039    /**
040     * @param s   Name of this action, e.g. in menus
041     * @param who Component that action is associated with, used to ensure
042     *            proper position in of dialog boxes
043     */
044    public RemoveRosterEntryToGroupAction(String s, Component who) {
045        super(s);
046        _who = who;
047    }
048
049    Component _who;
050    String curRosterGroup;
051    JmriJFrame frame = null;
052//    JComboBox typeBox;
053    JLabel jLabel = new JLabel(Bundle.getMessage("SelectTheGroup"));
054    RosterEntrySelectorPanel rosterBox;
055    JButton okButton = new JButton(Bundle.getMessage("ButtonRemove"));
056    JButton cancelButton = new JButton(Bundle.getMessage("ButtonDone"));
057
058    @Override
059    public void actionPerformed(ActionEvent event) {
060        frame = new JmriJFrame(Bundle.getMessage("DeleteFromGroup"));
061        rosterBox = new RosterEntrySelectorPanel();
062        rosterBox.getRosterGroupComboBox().setAllEntriesEnabled(false);
063        frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
064        JPanel p;
065        p = new JPanel();
066        p.setLayout(new FlowLayout());
067        p.add(jLabel);
068        p.add(rosterBox);
069        frame.getContentPane().add(p);
070
071        p = new JPanel();
072        p.setLayout(new FlowLayout());
073        p.add(okButton);
074        okButton.addActionListener(new ActionListener() {
075            @Override
076            public void actionPerformed(ActionEvent e) {
077                okPressed();
078            }
079        });
080        p.add(cancelButton);
081        cancelButton.addActionListener(new ActionListener() {
082            @Override
083            public void actionPerformed(ActionEvent e) {
084                dispose();
085            }
086        });
087        frame.getContentPane().add(p);
088        frame.pack();
089        frame.setVisible(true);
090    }
091
092    /**
093     * Can provide some mechanism to prompt for user for one last chance to
094     * change his/her mind
095     * @param entry Which roster entry?
096     * @param group In which roster group?
097     * @return true if user says to continue
098     */
099    boolean userOK(String entry, String group) {
100        return (JmriJOptionPane.YES_OPTION
101                == JmriJOptionPane.showConfirmDialog(_who,
102                        Bundle.getMessage("DeleteEntryFromGroupDialog", entry, group),
103                        Bundle.getMessage("DeleteEntryFromGroupTitle", entry, group),
104                        JmriJOptionPane.YES_NO_OPTION));
105    }
106
107    public void okPressed() {
108        String group = rosterBox.getSelectedRosterGroup();
109        log.info("Selected {}", group);
110        if (group != null && !group.equals(Roster.ALLENTRIES)) {
111            if (rosterBox.getSelectedRosterEntries().length != 0) {
112                RosterEntry re = rosterBox.getSelectedRosterEntries()[0];
113                log.info("Preparing to remove {} from {}", re.getId(), group);
114                if (userOK(re.getId(), group)) {
115                    re.deleteAttribute(Roster.getRosterGroupProperty(group));
116                    re.updateFile();
117                    Roster.getDefault().writeRoster();
118                    rosterBox.getRosterEntryComboBox().update();
119                }
120            }
121        }
122        frame.pack();
123    }
124
125    public void dispose() {
126        frame.dispose();
127
128    }
129
130    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(RemoveRosterEntryToGroupAction.class);
131
132}