1
2
3
4
5
6
7
8
9
10
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 public void setCurrentDirectory( String currentDirectory )
118 {
119 this.currentDirectory = currentDirectory;
120 }
121
122 public class SelectDirectoryAction extends AbstractAction
123 {
124 private JFileChooser fileChooser;
125
126 public SelectDirectoryAction()
127 {
128 super( "Browse..." );
129 }
130
131 public void actionPerformed(ActionEvent e)
132 {
133 if( fileChooser == null )
134 {
135 fileChooser = new JFileChooser();
136 if( type == FieldType.FOLDER || type == FieldType.PROJECT_FOLDER )
137 fileChooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
138 }
139
140 String value = FileFormField.this.getValue();
141 if( value.length() > 0 )
142 {
143 fileChooser.setSelectedFile( new File( value ));
144 }
145 else if( currentDirectory != null )
146 {
147 fileChooser.setCurrentDirectory( new File( currentDirectory ));
148 }
149 else if( projectRoot != null )
150 {
151 fileChooser.setCurrentDirectory( new File( projectRoot ));
152 }
153
154 int returnVal = fileChooser.showOpenDialog( UISupport.getMainFrame() );
155 if( returnVal == JFileChooser.APPROVE_OPTION )
156 {
157 updateValue( fileChooser.getSelectedFile().getAbsolutePath());
158 }
159 }
160 }
161
162 public void setProperty(String name, Object value)
163 {
164 super.setProperty(name, value);
165
166 if( name.equals( ProjectSettings.PROJECT_ROOT ) && type == FieldType.PROJECT_FOLDER)
167 {
168 projectRoot = (String) value;
169 log.debug( "Set projectRoot to [" + projectRoot + "]" );
170 }
171 else if( name.equals( CURRENT_DIRECTORY ) )
172 {
173 currentDirectory = (String) value;
174 log.debug( "Set projectRoot to [" + projectRoot + "]" );
175 }
176 }
177
178 public void setWidth(int columns)
179 {
180 textField.setColumns( columns );
181 }
182
183 public String getCurrentDirectory()
184 {
185 return currentDirectory;
186 }
187 }