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 boolean allEntriesEnabled = selections.isAllEntriesEnabled(); 056 selections.setAllEntriesEnabled(false); 057 if (lastGroupSelect != null) { 058 selections.setSelectedItem(lastGroupSelect); 059 } 060 061 rosterEntryUpdate(); 062 selections.addActionListener(new ActionListener() { 063 @Override 064 public void actionPerformed(ActionEvent e) { 065 rosterEntryUpdate(); 066 } 067 }); 068 int retval = JmriJOptionPane.showOptionDialog(_who, 069 Bundle.getMessage("AddEntryToGroupDialog"), Bundle.getMessage("AddEntryToGroupTitle"), 070 JmriJOptionPane.DEFAULT_OPTION, JmriJOptionPane.INFORMATION_MESSAGE, null, 071 new Object[]{Bundle.getMessage("ButtonDone"), Bundle.getMessage("ButtonOK"), selections, rosterEntry}, null); 072 log.debug("Dialog value {} selected {}:{}, {}:{}", retval, selections.getSelectedIndex(), selections.getSelectedItem(), rosterEntry.getSelectedIndex(), rosterEntry.getSelectedItem()); 073 if (retval != 1) { // not array position 1, ButtonOK 074 return; 075 } 076 077 String selEntry = (String) rosterEntry.getSelectedItem(); 078 lastGroupSelect = selections.getSelectedItem(); 079 RosterEntry re = roster.entryFromTitle(selEntry); 080 String selGroup = Roster.getRosterGroupProperty(selections.getSelectedItem()); 081 re.putAttribute(selGroup, "yes"); 082 Roster.getDefault().writeRoster(); 083 re.updateFile(); 084 actionPerformed(event); 085 086 selections.setAllEntriesEnabled(allEntriesEnabled); 087 } 088 089 void rosterEntryUpdate() { 090 if (rosterEntry != null) { 091 rosterEntry.removeAllItems(); 092 } 093 String group = Roster.ROSTER_GROUP_PREFIX + selections.getSelectedItem(); 094 roster.getAllEntries().stream().filter((r) -> (r.getAttribute(group) == null)).forEachOrdered((r) -> { 095 rosterEntry.addItem(r.titleString()); 096 }); 097 } 098 099 private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(RosterEntryToGroupAction.class); 100}