001package jmri.swing; 002 003import java.util.List; 004 005import javax.annotation.CheckForNull; 006import javax.annotation.Nonnull; 007import javax.swing.ButtonGroup; 008import javax.swing.JMenu; 009import javax.swing.JMenuItem; 010import javax.swing.JRadioButtonMenuItem; 011 012import jmri.InstanceManager; 013import jmri.PowerManager; 014 015/** 016 * Create a menu for selecting the Power Manager to use 017 * 018 * @author Bob Jacobsen Copyright 2010 019 * @since 2.9.5 020 */ 021public abstract class PowerManagerMenu extends JMenu { 022 023 JMenuItem allConnsItem = new JRadioButtonMenuItem(Bundle.getMessage("AllConnections")); 024 private final List<JMenuItem> menuItems = new java.util.ArrayList<>(); 025 026 protected abstract void choiceChanged(); 027 028 /** 029 * Create a PowerManager menu. 030 */ 031 public PowerManagerMenu() { 032 this(false, null); 033 } 034 035 /** 036 * Create a PowerManager menu. 037 * @param includeAllConns true to include all connections 038 * @param defaultPwrMgr specify a PowerManager to be selected on Menu startup. 039 */ 040 public PowerManagerMenu(boolean includeAllConns, PowerManager defaultPwrMgr) { 041 super(); 042 043 ButtonGroup group = new ButtonGroup(); 044 045 // label this menu 046 setText(Bundle.getMessage("MenuConnection")) ; 047 048 if (includeAllConns) { 049 add(allConnsItem); 050 group.add(allConnsItem); 051 menuItems.add(allConnsItem); 052 allConnsItem.addActionListener((java.awt.event.ActionEvent e) -> 053 choiceChanged()); 054 } 055 056 // now add an item for each available manager 057 List<PowerManager> managers = InstanceManager.getList(PowerManager.class); 058 for (PowerManager mgr : managers) { 059 if (mgr != null) { 060 JMenuItem item = new JRadioButtonMenuItem(getManagerNameIncludeIfDefault(mgr)); 061 item.setActionCommand(mgr.getUserName()); 062 add(item); 063 group.add(item); 064 menuItems.add(item); 065 item.addActionListener((java.awt.event.ActionEvent e) -> 066 choiceChanged()); 067 } 068 } 069 070 PowerManagerMenu.this.setManager(defaultPwrMgr); 071 } 072 073 public void setManager(@CheckForNull PowerManager suppliedMgr) { 074 if (InstanceManager.getNullableDefault(jmri.PowerManager.class) == null) { 075 return; 076 } 077 String searchMgr = ( suppliedMgr != null ? suppliedMgr.getUserName() : ""); 078 for (JMenuItem item : menuItems) { 079 if (searchMgr.equals(item.getActionCommand())) { 080 item.setSelected(true); 081 return; 082 } 083 } 084 allConnsItem.setSelected(true); 085 } 086 087 /** 088 * Get the selected PowerManager. 089 * @return null if All Connections option selected. 090 */ 091 @CheckForNull 092 public PowerManager getManager() { 093 094 String name=""; 095 // find active name 096 for (JMenuItem item : menuItems) { 097 if (item.isSelected()) { 098 name = item.getActionCommand(); 099 break; 100 } 101 } 102 // find PowerManager and return 103 List<PowerManager> managers = InstanceManager.getList(PowerManager.class); 104 if (managers.size()==1){ 105 return managers.get(0); 106 } 107 for (PowerManager mgr : managers) { 108 if (name.equals(mgr.getUserName())) { 109 return mgr; 110 } 111 } 112 return null; 113 } 114 115 @Nonnull 116 public static String getManagerNameIncludeIfDefault(@Nonnull PowerManager mgr){ 117 String mgrName = mgr.getUserName(); 118 if ( mgr == InstanceManager.getDefault(PowerManager.class) ) { 119 mgrName = Bundle.getMessage("DefaultConnection",mgr.getUserName()); 120 } 121 return mgrName; 122 } 123 124}