001package jmri.jmrit.beantable; 002 003import java.awt.Color; 004import java.awt.FlowLayout; 005import java.awt.event.ActionEvent; 006import java.awt.event.ActionListener; 007import java.awt.event.KeyAdapter; 008import java.awt.event.KeyEvent; 009import javax.swing.BoxLayout; 010import javax.swing.JButton; 011import javax.swing.JLabel; 012import javax.swing.JPanel; 013import javax.swing.JTextField; 014 015/** 016 * JPanel to create a new JMRI device (used to add IdTag). 017 * 018 * @author Bob Jacobsen Copyright (C) 2009 019 * @author Pete Cressman Copyright (C) 2010 020 */ 021public class AddNewDevicePanel extends jmri.util.swing.JmriPanel { 022 023 public AddNewDevicePanel(JTextField sys, JTextField userName, 024 String addButtonLabel, ActionListener okListener, ActionListener cancelListener) { 025 sysName = sys; 026 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 027 JPanel p; 028 p = new JPanel(); 029 p.setLayout(new FlowLayout()); 030 p.setLayout(new java.awt.GridBagLayout()); 031 java.awt.GridBagConstraints c = new java.awt.GridBagConstraints(); 032 c.gridwidth = 1; 033 c.gridheight = 1; 034 c.gridx = 0; 035 c.gridy = 0; 036 c.anchor = java.awt.GridBagConstraints.EAST; 037 p.add(sysNameLabel, c); 038 sysNameLabel.setLabelFor(sysName); 039 c.gridy = 1; 040 p.add(userNameLabel, c); 041 userNameLabel.setLabelFor(userName); 042 c.gridx = 1; 043 c.gridy = 0; 044 c.anchor = java.awt.GridBagConstraints.WEST; 045 c.weightx = 1.0; 046 c.fill = java.awt.GridBagConstraints.HORIZONTAL; // text field will expand 047 p.add(sysName, c); 048 c.gridy = 1; 049 p.add(userName, c); 050 add(p); 051 052 // button(s) at bottom of window 053 JPanel panelBottom = new JPanel(); 054 panelBottom.setLayout(new FlowLayout(FlowLayout.TRAILING)); 055 // only add a Cancel button when the OKbutton string is OK (so don't show on Picker Panels) 056 if (addButtonLabel.equals("ButtonOK")) { 057 panelBottom.add(cancel = new JButton(Bundle.getMessage("ButtonCancel"))); 058 cancel.addActionListener(cancelListener); 059 } 060 061 panelBottom.add(ok = new JButton(Bundle.getMessage(addButtonLabel))); 062 ok.addActionListener(okListener); 063 064 ok.addActionListener(new ActionListener() { 065 @Override 066 public void actionPerformed(ActionEvent a) { 067 reset(); 068 } 069 }); 070 071 add(panelBottom); 072 073 reset(); 074 sysName.addKeyListener(new KeyAdapter() { 075 @Override 076 public void keyReleased(KeyEvent a) { 077 if (sysName.getText().length() > 0) { 078 setOK(); 079 } 080 } 081 }); 082 } 083 084 void reset() { 085 ok.setEnabled(false); 086 ok.setToolTipText(Bundle.getMessage("ToolTipWillActivate")); 087 } 088 089 /** 090 * Activate the OK button without user key activity. 091 */ 092 public void setOK() { 093 ok.setEnabled(true); 094 ok.setToolTipText(null); 095 } 096 097 /** 098 * Lock the System Name JTextField. 099 */ 100 public void setSystemNameFieldIneditable() { 101 sysName.setEditable(false); 102 sysName.setBorder(null); 103 sysName.setDisabledTextColor(Color.black); 104 } 105 106 public void addLabels(String labelSystemName, String labelUserName) { 107 sysNameLabel.setText(labelSystemName); 108 userNameLabel.setText(labelUserName); 109 } 110 111 JButton ok; 112 JButton cancel; 113 JTextField sysName; 114 JLabel sysNameLabel = new JLabel(Bundle.getMessage("LabelSystemName")); 115 JLabel userNameLabel = new JLabel(Bundle.getMessage("LabelUserName")); 116 117}