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        boolean allEntriesEnabled = rosterBox.getRosterGroupComboBox().isAllEntriesEnabled();
063        rosterBox.getRosterGroupComboBox().setAllEntriesEnabled(false);
064        frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
065        JPanel p;
066        p = new JPanel();
067        p.setLayout(new FlowLayout());
068        p.add(jLabel);
069        p.add(rosterBox);
070        frame.getContentPane().add(p);
071
072        p = new JPanel();
073        p.setLayout(new FlowLayout());
074        p.add(okButton);
075        okButton.addActionListener(new ActionListener() {
076            @Override
077            public void actionPerformed(ActionEvent e) {
078                okPressed();
079            }
080        });
081        p.add(cancelButton);
082        cancelButton.addActionListener(new ActionListener() {
083            @Override
084            public void actionPerformed(ActionEvent e) {
085                dispose();
086            }
087        });
088        
089        rosterBox.getRosterGroupComboBox().setAllEntriesEnabled(allEntriesEnabled);
090        
091        frame.getContentPane().add(p);
092        frame.pack();
093        frame.setVisible(true);
094    }
095
096    /**
097     * Can provide some mechanism to prompt for user for one last chance to
098     * change his/her mind
099     * @param entry Which roster entry?
100     * @param group In which roster group?
101     * @return true if user says to continue
102     */
103    boolean userOK(String entry, String group) {
104        return (JmriJOptionPane.YES_OPTION
105                == JmriJOptionPane.showConfirmDialog(_who,
106                        Bundle.getMessage("DeleteEntryFromGroupDialog", entry, group),
107                        Bundle.getMessage("DeleteEntryFromGroupTitle", entry, group),
108                        JmriJOptionPane.YES_NO_OPTION));
109    }
110
111    public void okPressed() {
112        String group = rosterBox.getSelectedRosterGroup();
113        log.info("Selected {}", group);
114        if (group != null && !group.equals(Roster.ALLENTRIES)) {
115            if (rosterBox.getSelectedRosterEntries().length != 0) {
116                RosterEntry re = rosterBox.getSelectedRosterEntries()[0];
117                log.info("Preparing to remove {} from {}", re.getId(), group);
118                if (userOK(re.getId(), group)) {
119                    re.deleteAttribute(Roster.getRosterGroupProperty(group));
120                    re.updateFile();
121                    Roster.getDefault().writeRoster();
122                    rosterBox.getRosterEntryComboBox().update();
123                }
124            }
125        }
126        frame.pack();
127    }
128
129    public void dispose() {
130        frame.dispose();
131
132    }
133
134    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(RemoveRosterEntryToGroupAction.class);
135
136}