View Javadoc

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