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