View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 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.soapui.actions;
    14
    15  import java.lang.reflect.Field;
    16  import java.util.ArrayList;
    17  import java.util.List;
    18
    19  import com.eviware.soapui.SoapUI;
    20  import com.eviware.soapui.model.settings.Settings;
    21  import com.eviware.soapui.settings.Setting;
    22  import com.eviware.soapui.support.components.DirectoryFormComponent;
    23  import com.eviware.soapui.support.components.FileFormComponent;
    24  import com.eviware.soapui.support.components.FileListFormComponent;
    25  import com.eviware.soapui.support.components.SimpleForm;
    26  import com.eviware.soapui.support.components.StringListFormComponent;
    27  import com.eviware.soapui.support.types.StringToStringMap;
    28
    29  /***
    30   * Support class for annotation-based settings
    31   *
    32   * @author ole.matzura
    33   */
    34
    35  public class AnnotatedSettingsPrefs implements Prefs
    36  {
    37  	private SimpleForm simpleForm;
    38  	private Class settingsClass;
    39  	private final String title;
    40
    41  	public AnnotatedSettingsPrefs( Class settingsClass, String title )
    42  	{
    43  		this.settingsClass = settingsClass;
    44  		this.title = title;
    45  	}
    46
    47  	public SimpleForm getForm()
    48  	{
    49  		if( simpleForm == null )
    50  		{
    51  			simpleForm = new SimpleForm();
    52  			simpleForm.addSpace( 5 );
    53
    54  			buildForm( simpleForm );
    55
    56  			simpleForm.addSpace( 5 );
    57  		}
    58
    59  		return simpleForm;
    60  	}
    61
    62     public List<Setting> getSettings()
    63     {
    64        ArrayList<Setting> settings = new ArrayList<Setting>();
    65        for( Field field : settingsClass.getFields() )
    66        {
    67           Setting annotation = field.getAnnotation( Setting.class );
    68           if( annotation != null )
    69           {
    70              settings.add( annotation );
    71           }
    72        }
    73        return settings;
    74     }
    75
    76  	private void buildForm( SimpleForm form )
    77  	{
    78        List<Setting> settings = getSettings();
    79  		for( Setting annotation : settings )
    80  		{
    81  			switch( annotation.type() )
    82  			{
    83  				case BOOLEAN :
    84  				{
    85  					form.appendCheckBox( annotation.name(), annotation.description(), false );
    86  					break;
    87  				}
    88  				case FILE :
    89  				{
    90  					form.append( annotation.name(), new FileFormComponent(  annotation.description() ));
    91  					break;
    92  				}
    93  				case FILELIST :
    94  				{
    95  					form.append( annotation.name(), new FileListFormComponent(  annotation.description() ));
    96  					break;
    97  				}
    98  				case STRINGLIST :
    99  				{
    100 					form.append( annotation.name(), new StringListFormComponent(  annotation.description() ));
    101 					break;
    102 				}
    103 				case FOLDER :
    104 				{
    105 					form.append( annotation.name(), new DirectoryFormComponent(  annotation.description() ));
    106 					break;
    107 				}
    108 				case ENUMERATION :
    109 				{
    110 					form.appendComboBox( annotation.name(), annotation.values(), annotation.description() );
    111 					break;
    112 				}
    113 				case PASSWORD :
    114 				{
    115 					form.appendPasswordField( annotation.name(), annotation.description() );
    116 					break;
    117 				}
    118 				default :
    119 				{
    120 					form.appendTextField( annotation.name(), annotation.description() );
    121 					break;
    122 				}
    123 			}
    124 		}
    125 	}
    126
    127 	public StringToStringMap getValues(Settings settings)
    128 	{
    129 		StringToStringMap result = new StringToStringMap();
    130
    131 		for( Field field : settingsClass.getFields() )
    132 		{
    133 			Setting annotation = field.getAnnotation( Setting.class );
    134 			if( annotation != null )
    135 			{
    136 				try
    137 				{
    138 					result.put( annotation.name(), settings.getString( field.get(null).toString(), annotation.defaultValue() ));
    139 				}
    140 				catch (Exception e)
    141 				{
    142 					SoapUI.logError( e );
    143 				}
    144 			}
    145 		}
    146
    147 		return result;
    148 	}
    149
    150 	public void setFormValues(Settings settings)
    151 	{
    152 		getForm().setValues( getValues( settings ) );
    153 	}
    154
    155 	public void getFormValues(Settings settings)
    156 	{
    157 		StringToStringMap values = new StringToStringMap();
    158       getForm().getValues( values );
    159       storeValues( values, settings );
    160 	}
    161
    162 	public void storeValues(StringToStringMap values, Settings settings)
    163 	{
    164 		for( Field field : settingsClass.getFields() )
    165 		{
    166 			Setting annotation = field.getAnnotation( Setting.class );
    167 			if( annotation != null )
    168 			{
    169 				try
    170 				{
    171 					settings.setString( field.get(null).toString(), values.get( annotation.name() ));
    172 				}
    173 				catch (IllegalArgumentException e)
    174 				{
    175 					SoapUI.logError( e );
    176 				}
    177 				catch (IllegalAccessException e)
    178 				{
    179 					SoapUI.logError( e );
    180 				}
    181 			}
    182 		}
    183 	}
    184
    185 	public String getTitle()
    186 	{
    187 		return title;
    188 	}
    189
    190
    191 }