/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package eclserver; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; /** * * @author rbalsewich */ public class BESList extends JPanel implements ListSelectionListener { private JList list; private DefaultListModel listModel; private static final String addString = "+"; private static final String removeString = "-"; private JButton removeButton; private JTextField serverName; public BESList() { super(new BorderLayout()); listModel = new DefaultListModel(); listModel.addElement("TBD"); // listModel.addElement("server2:8080"); // listModel.addElement("server3:8080"); //Create the list and put it in a scroll pane. list = new JList(listModel); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.setSelectedIndex(0); list.addListSelectionListener(this); list.setVisibleRowCount(5); JScrollPane listScrollPane = new JScrollPane(list); JButton addButton = new JButton(addString); AddListener addListener = new AddListener(addButton); addButton.setActionCommand(addString); addButton.addActionListener(addListener); addButton.setEnabled(false); removeButton = new JButton(removeString); removeButton.setActionCommand(removeString); removeButton.addActionListener(new RemoveListener()); serverName = new JTextField(10); serverName.addActionListener(addListener); serverName.getDocument().addDocumentListener(addListener); String name = listModel.getElementAt( list.getSelectedIndex()).toString(); //Create a panel that uses BoxLayout. JPanel buttonPane = new JPanel(); buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS)); buttonPane.add(removeButton); buttonPane.add(Box.createHorizontalStrut(5)); buttonPane.add(new JSeparator(SwingConstants.VERTICAL)); buttonPane.add(Box.createHorizontalStrut(5)); buttonPane.add(serverName); buttonPane.add(addButton); buttonPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); add(listScrollPane, BorderLayout.CENTER); add(buttonPane, BorderLayout.PAGE_END); } class RemoveListener implements ActionListener { public void actionPerformed(ActionEvent e) { //This method can be called only if //there's a valid selection //so go ahead and remove whatever's selected. int index = list.getSelectedIndex(); listModel.remove(index); int size = listModel.getSize(); if (size == 0) { //Nobody's left, disable remove. removeButton.setEnabled(false); } else { //Select an index. if (index == listModel.getSize()) { //removed item in last position index--; } list.setSelectedIndex(index); list.ensureIndexIsVisible(index); } } } //This listener is shared by the text field and the add button. class AddListener implements ActionListener, DocumentListener { private boolean alreadyEnabled = false; private JButton button; public AddListener(JButton button) { this.button = button; } //Required by ActionListener. public void actionPerformed(ActionEvent e) { String name = serverName.getText(); //User didn't type in a unique name... if (name.equals("") || alreadyInList(name)) { Toolkit.getDefaultToolkit().beep(); serverName.requestFocusInWindow(); serverName.selectAll(); return; } int index = list.getSelectedIndex(); //get selected index if (index == -1) { //no selection, so insert at beginning index = 0; } else { //add after the selected item index++; } listModel.insertElementAt(serverName.getText(), index); //If we just wanted to add to the end, we'd do this: //listModel.addElement(serverName.getText()); //Reset the text field. serverName.requestFocusInWindow(); serverName.setText(""); //Select the new item and make it visible. list.setSelectedIndex(index); list.ensureIndexIsVisible(index); } //This method tests for string equality. protected boolean alreadyInList(String name) { return listModel.contains(name.trim()); } //Required by DocumentListener. public void insertUpdate(DocumentEvent e) { enableButton(); } //Required by DocumentListener. public void removeUpdate(DocumentEvent e) { handleEmptyTextField(e); } //Required by DocumentListener. public void changedUpdate(DocumentEvent e) { if (!handleEmptyTextField(e)) { enableButton(); } } private void enableButton() { if (!alreadyEnabled) { button.setEnabled(true); } } private boolean handleEmptyTextField(DocumentEvent e) { if (e.getDocument().getLength() <= 0) { button.setEnabled(false); alreadyEnabled = false; return true; } return false; } } //This method is required by ListSelectionListener. public void valueChanged(ListSelectionEvent e) { if (e.getValueIsAdjusting() == false) { if (list.getSelectedIndex() == -1) { //No selection, disable fire button. removeButton.setEnabled(false); } else { //Selection, enable the fire button. removeButton.setEnabled(true); } } } }