001package jmri.jmrit.display.controlPanelEditor; 002 003import java.awt.FlowLayout; 004import java.awt.Window; 005import java.awt.event.ActionEvent; 006 007import javax.swing.BorderFactory; 008import javax.swing.Box; 009import javax.swing.BoxLayout; 010import javax.swing.JButton; 011import javax.swing.JFrame; 012import javax.swing.JLabel; 013import javax.swing.JPanel; 014import javax.swing.SwingConstants; 015import jmri.NamedBean; 016import jmri.jmrit.picker.PickListModel; 017import jmri.jmrit.picker.PickSinglePanel; 018 019/** 020 * 021 * @author Pete Cressman Copyright: Copyright (c) 2019 022 * 023 * @param <T> type of NamedBean in PickList 024 */ 025public class OpenPickListButton<T extends NamedBean> { 026 027 private final JPanel _buttonPanel; 028 private JFrame _pickFrame; 029 private JButton _openPicklistButton; 030 String[] _blurbLines; 031 PickListModel<T> _model; 032 Window _parent; 033 034 OpenPickListButton(String[] blurbLines, PickListModel<T> model, Window parent, String buttonCaption) { 035 _model = model; 036 _blurbLines = blurbLines; 037 _buttonPanel = makePickListPanel(buttonCaption); 038 _parent = parent; 039 } 040 041 private JPanel makePickListPanel(String buttonCaption) { 042 JPanel buttonPanel = new JPanel(); 043 buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.Y_AXIS)); 044 JPanel panel = new JPanel(); 045 panel.setLayout(new FlowLayout()); 046 047 _openPicklistButton = new JButton(buttonCaption); 048 _openPicklistButton.addActionListener((ActionEvent a) -> { 049 if (_pickFrame == null) { 050 openPickList(); 051 } else { 052 closePickList(); 053 } 054 }); 055 panel.add(_openPicklistButton); 056 057 buttonPanel.add(panel); 058 return buttonPanel; 059 } 060 061 public JPanel getButtonPanel() { 062 return _buttonPanel; 063 } 064 065 void openPickList() { 066 _pickFrame = new JFrame(); 067 _pickFrame.setTitle(_model.getName()); 068 JPanel content = new JPanel(); 069 content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS)); 070 javax.swing.border.Border padding = BorderFactory.createEmptyBorder(10, 5, 3, 5); 071 content.setBorder(padding); 072 073 JPanel blurb = new JPanel(); 074 blurb.setLayout(new BoxLayout(blurb, BoxLayout.Y_AXIS)); 075 for (String text : _blurbLines) { 076 blurb.add(new JLabel(text, SwingConstants.CENTER)); 077 } 078 blurb.add(Box.createVerticalStrut(5)); 079 JPanel panel = new JPanel(); 080 panel.add(blurb); 081 content.add(panel); 082 PickSinglePanel<T> pickPanel = new PickSinglePanel<>(_model); 083 content.add(pickPanel); 084 content.setToolTipText(Bundle.getMessage("ToolTipPickLists")); 085 pickPanel.setToolTipText(Bundle.getMessage("ToolTipPickLists")); 086 pickPanel.getTable().setToolTipText(Bundle.getMessage("ToolTipPickLists")); 087 088 _pickFrame.setContentPane(content); 089 _pickFrame.addWindowListener(new java.awt.event.WindowAdapter() { 090 @Override 091 public void windowClosing(java.awt.event.WindowEvent e) { 092 closePickList(); 093 } 094 }); 095 jmri.InstanceManager.getDefault(jmri.util.PlaceWindow.class). nextTo(_parent, null, _pickFrame); 096 _pickFrame.toFront(); 097 _pickFrame.setVisible(true); 098 _pickFrame.pack(); 099 _openPicklistButton.setText(Bundle.getMessage("ClosePicklist")); 100 } 101 102 public void closePickList() { 103 if (_pickFrame != null) { 104 _pickFrame.dispose(); 105 _pickFrame = null; 106 _openPicklistButton.setText(Bundle.getMessage("OpenPicklist", _model.getName())); 107 } 108 } 109 110}