001package jmri.jmrit.roster.swing; 002 003import java.beans.PropertyChangeEvent; 004import java.util.ArrayList; 005import java.util.Collections; 006import java.util.Locale; 007import javax.swing.JComboBox; 008import jmri.jmrit.roster.Roster; 009import jmri.jmrit.roster.rostergroup.RosterGroupSelector; 010 011/** 012 * A JComboBox of Roster Groups. 013 * 014 * @author Randall Wood Copyright (C) 2011, 2014 015 * @see jmri.jmrit.roster.Roster 016 */ 017public class RosterGroupComboBox extends JComboBox<String> implements RosterGroupSelector { 018 019 private Roster _roster; 020 private boolean allEntriesEnabled = true; 021 022 /** 023 * Create a RosterGroupComboBox with an arbitrary Roster instead of the 024 * default Roster instance. 025 * 026 * @param roster the Roster to show the groups of 027 */ 028 // needed for unit tests 029 public RosterGroupComboBox(Roster roster) { 030 this(roster, roster.getDefaultRosterGroup()); 031 } 032 033 /** 034 * Create a RosterGroupComboBox with an arbitrary selection. 035 * 036 * @param selection the initial roster group selection 037 */ 038 public RosterGroupComboBox(String selection) { 039 this(Roster.getDefault(), selection); 040 } 041 042 /** 043 * Create a RosterGroupComboBox with arbitrary selection and Roster. 044 * 045 * @param roster the Roster to show the groups of 046 * @param selection the initial roster group selection 047 */ 048 public RosterGroupComboBox(Roster roster, String selection) { 049 super(); 050 _roster = roster; 051 update(selection); 052 roster.addPropertyChangeListener((PropertyChangeEvent pce) -> { 053 if (pce.getPropertyName().equals("RosterGroupAdded")) { 054 update(); 055 } else if (pce.getPropertyName().equals("RosterGroupRemoved") 056 || pce.getPropertyName().equals("RosterGroupRenamed")) { 057 if (getSelectedItem().equals(pce.getOldValue())) { 058 update((String) pce.getNewValue()); 059 } else { 060 update(); 061 } 062 } 063 }); 064 } 065 066 /** 067 * Create a RosterGroupComboBox with the default Roster instance and the 068 * default roster group. 069 */ 070 public RosterGroupComboBox() { 071 this(Roster.getDefault(), Roster.getDefault().getDefaultRosterGroup()); 072 } 073 074 /** 075 * Update the combo box and reselect the current selection. 076 */ 077 public final void update() { 078 update(this.getSelectedItem()); 079 } 080 081 /** 082 * Update the combo box and select given String. 083 * @param selection the selection to update to 084 */ 085 public final void update(String selection) { 086 removeAllItems(); 087 ArrayList<String> l = _roster.getRosterGroupList(); 088 Collections.sort(l); 089 l.forEach((g) -> { 090 addItem(g); 091 }); 092 if (allEntriesEnabled) { 093 insertItemAt(Roster.allEntries(Locale.getDefault()), 0); 094 if (selection == null) { 095 selection = Roster.ALLENTRIES; 096 } 097 this.setToolTipText(null); 098 } else { 099 if (this.getItemCount() == 0) { 100 this.addItem(Bundle.getMessage("RosterGroupComboBoxNoGroups")); 101 this.setToolTipText(Bundle.getMessage("RosterGroupComboBoxNoGroupsToolTip")); 102 } else { 103 this.setToolTipText(null); 104 } 105 } 106 setSelectedItem(selection); 107 if (this.getItemCount() == 1) { 108 this.setSelectedIndex(0); 109 this.setEnabled(false); 110 } else { 111 this.setEnabled(true); 112 } 113 } 114 115 @Override 116 public String getSelectedRosterGroup() { 117 if (getSelectedItem() == null) { 118 return null; 119 } else if (getSelectedItem().equals(Roster.ALLENTRIES) 120 || getSelectedItem().equals(Roster.allEntries(Locale.getDefault()))) { 121 return null; 122 } else { 123 return getSelectedItem(); 124 } 125 } 126 127 /** 128 * @return the allEntriesEnabled 129 */ 130 public boolean isAllEntriesEnabled() { 131 return allEntriesEnabled; 132 } 133 134 /** 135 * @param allEntriesEnabled the allEntriesEnabled to set 136 */ 137 public void setAllEntriesEnabled(boolean allEntriesEnabled) { 138 this.allEntriesEnabled = allEntriesEnabled; 139 this.update(); 140 } 141 142 @Override 143 public String getSelectedItem() { 144 145 Object item = super.getSelectedItem(); 146 return item != null ? item.toString() : null; 147 } 148}