View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.support.components;
14  
15  import java.awt.BorderLayout;
16  import java.awt.Dimension;
17  import java.awt.event.ActionEvent;
18  import java.awt.event.ActionListener;
19  import java.io.File;
20  
21  import javax.swing.Box;
22  import javax.swing.BoxLayout;
23  import javax.swing.DefaultListModel;
24  import javax.swing.JButton;
25  import javax.swing.JList;
26  import javax.swing.JPanel;
27  import javax.swing.JScrollPane;
28  import javax.swing.event.ListSelectionEvent;
29  import javax.swing.event.ListSelectionListener;
30  
31  import com.eviware.soapui.support.UISupport;
32  
33  public class FileListFormComponent extends JPanel implements JFormComponent, ActionListener
34  {
35  	private DefaultListModel listModel;
36  	private JButton addButton;
37  	private JButton removeButton;
38  	private JList list;
39  
40  	public FileListFormComponent( String tooltip )
41  	{
42  		listModel = new DefaultListModel();
43  		list = new JList( listModel );
44  		list.setToolTipText( tooltip );
45  		JScrollPane scrollPane = new JScrollPane( list );
46  		scrollPane.setPreferredSize( new Dimension( 300, 70 ) );
47  		add( scrollPane, BorderLayout.CENTER );
48  		Box box = new Box( BoxLayout.Y_AXIS );
49  		addButton = new JButton( "Add.." );
50  		addButton.addActionListener( this );
51  		box.add( addButton );
52  		box.add( Box.createVerticalStrut( 5 ) );
53  		removeButton = new JButton( "Remove.." );
54  		removeButton.addActionListener( this );
55  		box.add( removeButton );
56  		box.add( Box.createVerticalGlue() );
57  
58  		add( box, BorderLayout.EAST );
59  
60  		list.addListSelectionListener( new ListSelectionListener()
61  		{
62  
63  			public void valueChanged( ListSelectionEvent e )
64  			{
65  				removeButton.setEnabled( list.getSelectedIndex() != -1 );
66  			}
67  		} );
68  
69  		removeButton.setEnabled( list.getSelectedIndex() != -1 );
70  	}
71  
72  	public void setValue( String value )
73  	{
74  		listModel.clear();
75  		String[] files = value.split( ";" );
76  		for( String file : files )
77  			if( file.trim().length() > 0 )
78  				listModel.addElement( file );
79  	}
80  
81  	public String getValue()
82  	{
83  		Object[] values = listModel.toArray();
84  		StringBuffer buf = new StringBuffer();
85  		for( int c = 0; c < values.length; c++ )
86  		{
87  			if( c > 0 )
88  				buf.append( ';' );
89  
90  			buf.append( values[c] );
91  		}
92  
93  		return buf.toString();
94  	}
95  
96  	public void actionPerformed( ActionEvent arg0 )
97  	{
98  		if( arg0.getSource() == addButton )
99  		{
100 			File file = UISupport.getFileDialogs().open( this, "Add file", null, null, null );
101 			if( file != null )
102 			{
103 				listModel.addElement( file.getAbsolutePath() );
104 			}
105 		}
106 		else if( arg0.getSource() == removeButton && list.getSelectedIndex() != -1 )
107 		{
108 			Object elm = listModel.getElementAt( list.getSelectedIndex() );
109 			if( UISupport.confirm( "Remove [" + elm.toString() + "] from list", "Remove" ) )
110 			{
111 				listModel.remove( list.getSelectedIndex() );
112 			}
113 		}
114 	}
115 }