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