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.awt.Font;
    16  import java.awt.event.ActionEvent;
    17  import java.awt.event.ActionListener;
    18
    19  import javax.swing.AbstractAction;
    20  import javax.swing.JButton;
    21  import javax.swing.JCheckBox;
    22  import javax.swing.JTextField;
    23
    24  import com.eviware.soapui.model.settings.Settings;
    25  import com.eviware.soapui.settings.UISettings;
    26  import com.eviware.soapui.support.components.SimpleForm;
    27  import com.eviware.soapui.support.types.StringToStringMap;
    28  import com.jgoodies.forms.builder.ButtonBarBuilder;
    29  import com.l2fprod.common.swing.JFontChooser;
    30
    31  /***
    32   * Preferences class for UISettings
    33   *
    34   * @author ole.matzura
    35   */
    36
    37  public class EditorPrefs implements Prefs
    38  {
    39  	public static final String NO_RESIZE_REQUEST_EDITOR = "Disable auto-resize";
    40  	public static final String START_WITH_REQUEST_TABS = "Tabbed request view";
    41  	public static final String AUTO_VALIDATE_REQUEST = "Validate Requests";
    42  	public static final String ABORT_ON_INVALID_REQUEST = "Abort on invalid";
    43  	public static final String AUTO_VALIDATE_RESPONSE = "Validate Responses";
    44  	public static final String XML_LINE_NUMBERS = "XML Line Numbers";
    45  	public static final String GROOVY_LINE_NUMBERS = "Groovy Line Numbers";
    46
    47  	private JTextField editorFontTextField;
    48  	private SimpleForm editorForm;
    49  	private final String title;
    50  	private JCheckBox abortCheckBox;
    51  	private JCheckBox autoValidateCheckBox;
    52
    53  	public EditorPrefs(String title)
    54  	{
    55  		this.title = title;
    56  	}
    57
    58  	public String getTitle()
    59  	{
    60  		return title;
    61  	}
    62
    63  	public SimpleForm getForm()
    64  	{
    65  		if( editorForm == null )
    66  		{
    67  			ButtonBarBuilder builder = new ButtonBarBuilder();
    68  			editorFontTextField = new JTextField( 20 );
    69  			editorFontTextField.setEnabled( false );
    70  			builder.addFixed( editorFontTextField );
    71  			builder.addRelatedGap();
    72  			builder.addFixed( new JButton( new AbstractAction("Select Font..")
    73  					{
    74  						public void actionPerformed(ActionEvent e)
    75  						{
    76  							Font font = JFontChooser.showDialog( null, "Select XML Editor Font",
    77  									Font.decode( editorFontTextField.getText() ));
    78
    79  							if( font != null )
    80  								editorFontTextField.setText( font.getFontName() + "-plain-" + font.getSize() );
    81  						}}));
    82
    83  			editorForm = new SimpleForm();
    84  			editorForm.addSpace( 5 );
    85  			editorForm.append( "Editor Font", builder.getPanel() );
    86  			editorForm.appendSeparator();
    87  			editorForm.appendCheckBox( XML_LINE_NUMBERS, "(show line-numbers in xml editors by default)", true );
    88  			editorForm.appendCheckBox( GROOVY_LINE_NUMBERS, "(show line-numbers in groovy editors by default)", true );
    89  			editorForm.appendSeparator();
    90  			editorForm.appendCheckBox( NO_RESIZE_REQUEST_EDITOR, "(disables automatic resizing of request editors)", true );
    91  			editorForm.appendCheckBox( START_WITH_REQUEST_TABS, "(defaults the request editor to the tabbed layout)", true );
    92  			editorForm.appendSeparator();
    93
    94  			autoValidateCheckBox = editorForm.appendCheckBox( AUTO_VALIDATE_REQUEST, "(always validate request messages before they are sent)", true );
    95  			abortCheckBox = editorForm.appendCheckBox( ABORT_ON_INVALID_REQUEST, "(aborts invalid requests)", true );
    96  			editorForm.appendCheckBox( AUTO_VALIDATE_RESPONSE, "(always validate response messages)", true );
    97
    98  			autoValidateCheckBox.addActionListener( new ActionListener() {
    99
    100 				public void actionPerformed(ActionEvent e)
    101 				{
    102 					abortCheckBox.setEnabled( autoValidateCheckBox.isSelected() );
    103 				}} );
    104 		}
    105
    106 		return editorForm;
    107 	}
    108
    109 	public void getFormValues(Settings settings)
    110 	{
    111 		StringToStringMap values = new StringToStringMap();
    112 		editorForm.getValues( values );
    113 		storeValues(values, settings);
    114 	}
    115
    116 	public void storeValues(StringToStringMap values, Settings settings)
    117 	{
    118 		if( editorFontTextField != null )
    119 			settings.setString( UISettings.EDITOR_FONT, editorFontTextField.getText() );
    120
    121 		settings.setBoolean( UISettings.NO_RESIZE_REQUEST_EDITOR, values.getBoolean( NO_RESIZE_REQUEST_EDITOR ));
    122 		settings.setBoolean( UISettings.START_WITH_REQUEST_TABS, values.getBoolean( START_WITH_REQUEST_TABS ));
    123 		settings.setBoolean( UISettings.AUTO_VALIDATE_REQUEST, values.getBoolean( AUTO_VALIDATE_REQUEST ));
    124 		settings.setBoolean( UISettings.ABORT_ON_INVALID_REQUEST, values.getBoolean( ABORT_ON_INVALID_REQUEST ));
    125 		settings.setBoolean( UISettings.AUTO_VALIDATE_RESPONSE, values.getBoolean( AUTO_VALIDATE_RESPONSE ));
    126 		settings.setBoolean( UISettings.SHOW_XML_LINE_NUMBERS, values.getBoolean( XML_LINE_NUMBERS ));
    127 		settings.setBoolean( UISettings.SHOW_GROOVY_LINE_NUMBERS, values.getBoolean( GROOVY_LINE_NUMBERS ));
    128 	}
    129
    130 	public void setFormValues(Settings settings)
    131 	{
    132 		String editorFont = settings.getString( UISettings.EDITOR_FONT, UISettings.DEFAULT_EDITOR_FONT );
    133 		if( editorFont == null || editorFont.length() == 0 )
    134 			editorFont = UISettings.DEFAULT_EDITOR_FONT;
    135
    136 		editorFontTextField.setText( editorFont );
    137 		editorForm.setValues( getValues(settings) );
    138
    139 		abortCheckBox.setEnabled( settings.getBoolean( UISettings.AUTO_VALIDATE_REQUEST ) );
    140 	}
    141
    142 	public StringToStringMap getValues(Settings settings)
    143 	{
    144 		StringToStringMap values = new StringToStringMap();
    145 		values.put( NO_RESIZE_REQUEST_EDITOR, settings.getBoolean( UISettings.NO_RESIZE_REQUEST_EDITOR ));
    146 		values.put( START_WITH_REQUEST_TABS, settings.getBoolean( UISettings.START_WITH_REQUEST_TABS ));
    147 		values.put( AUTO_VALIDATE_REQUEST, settings.getBoolean( UISettings.AUTO_VALIDATE_REQUEST ));
    148 		values.put( ABORT_ON_INVALID_REQUEST, settings.getBoolean( UISettings.ABORT_ON_INVALID_REQUEST ));
    149 		values.put( AUTO_VALIDATE_RESPONSE, settings.getBoolean( UISettings.AUTO_VALIDATE_RESPONSE ));
    150 		values.put( XML_LINE_NUMBERS, settings.getBoolean( UISettings.SHOW_XML_LINE_NUMBERS ));
    151 		values.put( GROOVY_LINE_NUMBERS, settings.getBoolean( UISettings.SHOW_GROOVY_LINE_NUMBERS ));
    152
    153 		return values;
    154 	}
    155 }