View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2007 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  			public void valueChanged( ListSelectionEvent e )
63  			{
64  				removeButton.setEnabled( list.getSelectedIndex() != -1 );
65  			}} );
66  		
67  		removeButton.setEnabled( list.getSelectedIndex() != -1 );
68  	}
69  	
70  	public void setValue(String value)
71  	{
72  		listModel.clear();
73  		String[] files = value.split( ";" );
74  		for( String file : files )
75  			if( file.trim().length() > 0)
76  				listModel.addElement( file );
77  	}
78  
79  	public String getValue()
80  	{
81  		Object[] values = listModel.toArray();
82  		StringBuffer buf = new StringBuffer();
83  		for( int c = 0; c < values.length; c++ )
84  		{
85  			if( c > 0 )
86  				buf.append( ';' );
87  			
88  			buf.append( values[c] );
89  		}
90  		
91  		return buf.toString();
92  	}
93  
94  	public void actionPerformed( ActionEvent arg0 )
95  	{
96  		if( arg0.getSource() == addButton )
97  		{
98  			File file = UISupport.getFileDialogs().open(this, "Add file", null, null, null );
99           if( file != null )
100 		   {
101 		     	listModel.addElement( file.getAbsolutePath() );
102 		   }
103 		}
104 		else if( arg0.getSource() == removeButton && list.getSelectedIndex() != -1 )
105 		{
106 			Object elm = listModel.getElementAt( list.getSelectedIndex() );
107 			if( UISupport.confirm( "Remove [" + elm.toString() + "] from list", "Remove" ))
108 			{
109 				listModel.remove( list.getSelectedIndex() );
110 			}
111 		}
112 	}
113 }