View Javadoc

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