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
46 public FileFormField( String tooltip, FieldType type )
47 {
48 super( new JPanel() );
49 this.type = type;
50
51 ButtonBarBuilder builder = new ButtonBarBuilder( getComponent() );
52 textField = new JTextField( 30 );
53 textField.setToolTipText( tooltip );
54 builder.addGriddedGrowing( textField );
55 builder.addRelatedGap();
56 selectDirectoryButton = new JButton( new SelectDirectoryAction());
57 builder.addFixed( selectDirectoryButton );
58
59 textField.getDocument().addDocumentListener( new DocumentListenerAdapter() {
60
61 @Override
62 public void update( Document document )
63 {
64 String text = textField.getText();
65
66 if( !updating )
67 fireValueChanged( text, oldValue );
68
69 oldValue = text;
70 }} );
71 }
72
73 public void setValue(String value)
74 {
75 updating = true;
76 oldValue = null;
77 updateValue( value );
78 updating = false;
79 }
80
81 private void updateValue( String value )
82 {
83 if( value != null && projectRoot != null && value.startsWith( projectRoot ) )
84 {
85 value = value.substring( projectRoot.length()+1 );
86 }
87
88 textField.setText( value );
89 }
90
91 public String getValue()
92 {
93 String text = textField.getText().trim();
94
95 if( projectRoot != null && text.length() > 0 )
96 {
97 String tempName = projectRoot + File.separatorChar + text;
98 if( new File( tempName ).exists() )
99 {
100 text = tempName;
101 }
102 }
103
104 return text;
105 }
106
107 public void setEnabled(boolean enabled)
108 {
109 textField.setEnabled( enabled );
110 selectDirectoryButton.setEnabled( enabled );
111 }
112
113 public class SelectDirectoryAction extends AbstractAction
114 {
115 private JFileChooser fileChooser;
116
117 public SelectDirectoryAction()
118 {
119 super( "Browse..." );
120 }
121
122 public void actionPerformed(ActionEvent e)
123 {
124 if( fileChooser == null )
125 {
126 fileChooser = new JFileChooser();
127 if( type == FieldType.FOLDER || type == FieldType.PROJECT_FOLDER )
128 fileChooser.setFileSelectionMode( JFileChooser.DIRECTORIES_ONLY );
129 }
130
131 String value = FileFormField.this.getValue();
132 if( value.length() > 0 )
133 {
134 fileChooser.setSelectedFile( new File( value ));
135 }
136 else if( projectRoot != null )
137 {
138 fileChooser.setCurrentDirectory( new File( projectRoot ));
139 }
140
141 int returnVal = fileChooser.showOpenDialog( UISupport.getMainFrame() );
142 if( returnVal == JFileChooser.APPROVE_OPTION )
143 {
144 updateValue( fileChooser.getSelectedFile().getAbsolutePath());
145 }
146 }
147 }
148
149 public void setProperty(String name, Object value)
150 {
151 super.setProperty(name, value);
152
153 if( name.equals( ProjectSettings.PROJECT_ROOT ))
154 {
155 projectRoot = (String) value;
156 log.debug( "Set projectRoot to [" + projectRoot + "]" );
157 }
158 }
159
160 public void setWidth(int columns)
161 {
162 textField.setColumns( columns );
163 }
164 }