View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2009 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.x.impl.swing;
14  
15  import java.awt.event.ActionEvent;
16  import java.io.File;
17  
18  import javax.swing.AbstractAction;
19  import javax.swing.JButton;
20  import javax.swing.JFileChooser;
21  import javax.swing.JPanel;
22  import javax.swing.JTextField;
23  import javax.swing.text.Document;
24  
25  import org.apache.log4j.Logger;
26  
27  import com.eviware.soapui.settings.ProjectSettings;
28  import com.eviware.soapui.support.DocumentListenerAdapter;
29  import com.eviware.soapui.support.UISupport;
30  import com.eviware.soapui.support.components.JUndoableTextField;
31  import com.eviware.x.form.XFormTextField;
32  import com.eviware.x.form.XForm.FieldType;
33  import com.jgoodies.forms.builder.ButtonBarBuilder;
34  import com.l2fprod.common.swing.JDirectoryChooser;
35  
36  public class FileFormField extends AbstractSwingXFormField<JPanel> implements XFormTextField
37  {
38  	private final static Logger log = Logger.getLogger( FileFormField.class );
39  
40  	private JTextField textField;
41  	private final FieldType type;
42  	private JButton selectDirectoryButton;
43  	private String projectRoot;
44  
45  	private boolean updating;
46  	private String oldValue;
47  	private String currentDirectory;
48  
49  	public FileFormField( String tooltip, FieldType type )
50  	{
51  		super( new JPanel() );
52  		this.type = type;
53  
54  		ButtonBarBuilder builder = new ButtonBarBuilder( getComponent() );
55  		textField = new JUndoableTextField( 30 );
56  		textField.setToolTipText( tooltip );
57  		builder.addGriddedGrowing( textField );
58  		builder.addRelatedGap();
59  		selectDirectoryButton = new JButton( new SelectDirectoryAction() );
60  		builder.addFixed( selectDirectoryButton );
61  
62  		textField.getDocument().addDocumentListener( new DocumentListenerAdapter()
63  		{
64  
65  			@Override
66  			public void update( Document document )
67  			{
68  				String text = textField.getText();
69  
70  				if( !updating )
71  					fireValueChanged( text, oldValue );
72  
73  				oldValue = text;
74  			}
75  		} );
76  	}
77  
78  	public void setValue( String value )
79  	{
80  		updating = true;
81  		oldValue = null;
82  		updateValue( value );
83  		updating = false;
84  	}
85  
86  	private void updateValue( String value )
87  	{
88  		if( value != null && projectRoot != null && value.startsWith( projectRoot ) )
89  		{
90  			if( value.equals( projectRoot ) )
91  				value = "";
92  			else if( value.length() > projectRoot.length() + 1 )
93  				value = value.substring( projectRoot.length() + 1 );
94  		}
95  
96  		textField.setText( value );
97  	}
98  
99  	public String getValue()
100 	{
101 		String text = textField.getText().trim();
102 
103 		if( projectRoot != null && text.length() > 0 )
104 		{
105 			String tempName = projectRoot + File.separatorChar + text;
106 			if( new File( tempName ).exists() )
107 			{
108 				text = tempName;
109 			}
110 		}
111 
112 		return text;
113 	}
114 
115 	public void setEnabled( boolean enabled )
116 	{
117 		textField.setEnabled( enabled );
118 		selectDirectoryButton.setEnabled( enabled );
119 	}
120 
121 	@Override
122 	public boolean isEnabled()
123 	{
124 		return textField.isEnabled();
125 	}
126 
127 	public void setCurrentDirectory( String currentDirectory )
128 	{
129 		this.currentDirectory = currentDirectory;
130 	}
131 
132 	public class SelectDirectoryAction extends AbstractAction
133 	{
134 		private JFileChooser fileChooser;
135 
136 		public SelectDirectoryAction()
137 		{
138 			super( "Browse..." );
139 		}
140 
141 		public void actionPerformed( ActionEvent e )
142 		{
143 			if( fileChooser == null )
144 			{
145 				if( type == FieldType.FOLDER || type == FieldType.PROJECT_FOLDER )
146 					fileChooser = new JDirectoryChooser();
147 				else
148 					fileChooser = new JFileChooser();
149 			}
150 
151 			String value = FileFormField.this.getValue();
152 			if( value.length() > 0 )
153 			{
154 				fileChooser.setSelectedFile( new File( value ) );
155 			}
156 			else if( currentDirectory != null )
157 			{
158 				fileChooser.setCurrentDirectory( new File( currentDirectory ) );
159 			}
160 			else if( projectRoot != null )
161 			{
162 				fileChooser.setCurrentDirectory( new File( projectRoot ) );
163 			}
164 
165 			int returnVal = fileChooser.showOpenDialog( UISupport.getMainFrame() );
166 			if( returnVal == JFileChooser.APPROVE_OPTION )
167 			{
168 				updateValue( fileChooser.getSelectedFile().getAbsolutePath() );
169 			}
170 		}
171 	}
172 
173 	public void setProperty( String name, Object value )
174 	{
175 		super.setProperty( name, value );
176 
177 		if( name.equals( ProjectSettings.PROJECT_ROOT ) && type == FieldType.PROJECT_FOLDER )
178 		{
179 			projectRoot = ( String )value;
180 			log.debug( "Set projectRoot to [" + projectRoot + "]" );
181 		}
182 		else if( name.equals( CURRENT_DIRECTORY ) )
183 		{
184 			currentDirectory = ( String )value;
185 			log.debug( "Set projectRoot to [" + projectRoot + "]" );
186 		}
187 	}
188 
189 	public void setWidth( int columns )
190 	{
191 		textField.setColumns( columns );
192 	}
193 
194 	public String getCurrentDirectory()
195 	{
196 		return currentDirectory;
197 	}
198 }