/* * Created on 12-Sep-2005 by Ryan McNally */ package com.ryanm.config.swing.imp; import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Arrays; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JList; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; import javax.swing.border.TitledBorder; import com.ryanm.config.Configurator; import com.ryanm.config.swing.Widget; /** * Widget for controlling a list of strings * * @author ryanm */ public class StringListWidget extends Widget { private final JList list = new JList(); /** * Holds the controls used to add entries to the list */ private final JPanel entryPanel = new JPanel(); private JComponent entryComponent = null; /** * Causes the current list selection to be removed */ private final JButton remove = new JButton( "Remove selection" ); /** * Constructs a new StringListWidget * * @param conf * The configurator * @param name * The name of the variable */ public StringListWidget( Configurator conf, String name ) { super( conf, name ); setBorder( new TitledBorder( name ) ); entryPanel.setBorder( new TitledBorder( "Add element" ) ); entryPanel.setLayout( new BorderLayout() ); setLayout( new BorderLayout() ); add( entryPanel, BorderLayout.NORTH ); add( new JScrollPane( list ), BorderLayout.CENTER ); JPanel spoof = new JPanel( new BorderLayout() ); spoof.add( remove, BorderLayout.CENTER ); add( spoof, BorderLayout.SOUTH ); setRange( conf.getRange( name ) ); remove.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { final int[] selectedIndices = list.getSelectedIndices(); if( selectedIndices.length > 0 ) { synchronized( lock ) { String[] contents = ( String[] ) StringListWidget.this.conf .getValue( StringListWidget.this.name ); Arrays.sort( contents ); String[] newContents = new String[ contents.length - selectedIndices.length ]; int index = 0; for( int i = 0; i < contents.length; i++ ) { if( Arrays.binarySearch( selectedIndices, i ) < 0 ) { newContents[ index++ ] = contents[ i ]; } } applyChange( StringListWidget.this.conf, StringListWidget.this.name, newContents ); } list.setSelectedIndices( new int[ 0 ] ); } } } ); refreshValue(); setEnabled( conf.isGUIEnabled( name ) ); } /** * * */ public StringListWidget() { } @Override public void updateValue() { String[] array = ( String[] ) conf.getValue( name ); Arrays.sort( array ); list.setListData( array ); } @Override public void updateRange( Object range ) { range = conf.getRange( name ); String[] possibles; if( range instanceof String ) { possibles = new String[] { ( String ) range }; } else if( range instanceof String[] ) { possibles = ( String[] ) range; } else if( range == null ) { possibles = null; } else { throw new IllegalArgumentException( "Non-string range object " + range + " found for variable " + conf.getPath() + "/" + name + "\n" ); } entryPanel.removeAll(); if( possibles == null ) { final JTextField tf = new JTextField(); tf.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { final String s = tf.getText(); if( s.length() > 0 ) { synchronized( lock ) { String[] contents = ( String[] ) conf.getValue( name ); String[] newContents = new String[ contents.length + 1 ]; System.arraycopy( contents, 0, newContents, 0, contents.length ); newContents[ newContents.length - 1 ] = s; Arrays.sort( newContents ); applyChange( conf, name, newContents ); } } tf.setText( null ); } } ); entryComponent = tf; entryPanel.add( tf, BorderLayout.CENTER ); } else { Arrays.sort( possibles ); final JComboBox combo = new JComboBox( possibles ); combo.setSelectedItem( null ); combo.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent ae ) { final String s = ( String ) combo.getSelectedItem(); if( s != null ) { String[] contents = ( String[] ) conf.getValue( name ); String[] newContents = new String[ contents.length + 1 ]; System.arraycopy( contents, 0, newContents, 0, contents.length ); newContents[ newContents.length - 1 ] = s; Arrays.sort( newContents ); conf.setValue( name, newContents ); applyChange( conf, name, newContents ); } combo.setSelectedItem( null ); } } ); entryComponent = combo; entryPanel.add( combo, BorderLayout.CENTER ); } } @Override public void setEnabled( boolean b ) { list.setEnabled( b ); entryComponent.setEnabled( b ); remove.setEnabled( b ); } @Override public Widget newWidget( Configurator conf, String name, Class runtimeType ) { return new StringListWidget( conf, name ); } @Override public Class getType() { return String[].class; } }