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.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( null, "Select XML Editor Font", 
78  									Font.decode( editorFontTextField.getText() ));
79  							
80  							if( font != null )
81  								editorFontTextField.setText( encodeFont( font ));
82  						}}));
83  			
84  			editorForm = new SimpleForm();
85  			editorForm.addSpace( 5 );
86  			editorForm.append( "Editor Font", builder.getPanel() );
87  			editorForm.appendSeparator();
88  			editorForm.appendCheckBox( XML_LINE_NUMBERS, "(show line-numbers in xml editors by default)", true );
89  			editorForm.appendCheckBox( GROOVY_LINE_NUMBERS, "(show line-numbers in groovy editors by default)", true );
90  			editorForm.appendSeparator();
91  			editorForm.appendCheckBox( NO_RESIZE_REQUEST_EDITOR, "(disables automatic resizing of request editors)", true );
92  			editorForm.appendCheckBox( START_WITH_REQUEST_TABS, "(defaults the request editor to the tabbed layout)", true );
93  			editorForm.appendSeparator();
94  
95  			autoValidateCheckBox = editorForm.appendCheckBox( AUTO_VALIDATE_REQUEST, "(always validate request messages before they are sent)", true );
96  			abortCheckBox = editorForm.appendCheckBox( ABORT_ON_INVALID_REQUEST, "(aborts invalid requests)", true );
97  			editorForm.appendCheckBox( AUTO_VALIDATE_RESPONSE, "(always validate response messages)", true );
98  			
99  			autoValidateCheckBox.addActionListener( new ActionListener() {
100 
101 				public void actionPerformed(ActionEvent e)
102 				{
103 					abortCheckBox.setEnabled( autoValidateCheckBox.isSelected() );
104 				}} );
105 		}
106 		
107 		return editorForm;
108 	}
109 
110 	public void getFormValues(Settings settings)
111 	{
112 		StringToStringMap values = new StringToStringMap();
113 		editorForm.getValues( values );
114 		storeValues(values, settings);
115 	}
116 	
117 	public void storeValues(StringToStringMap values, Settings settings)
118 	{
119 		if( editorFontTextField != null )
120 			settings.setString( UISettings.EDITOR_FONT, editorFontTextField.getText() );
121 		
122 		settings.setBoolean( UISettings.NO_RESIZE_REQUEST_EDITOR, values.getBoolean( NO_RESIZE_REQUEST_EDITOR ));
123 		settings.setBoolean( UISettings.START_WITH_REQUEST_TABS, values.getBoolean( START_WITH_REQUEST_TABS ));
124 		settings.setBoolean( UISettings.AUTO_VALIDATE_REQUEST, values.getBoolean( AUTO_VALIDATE_REQUEST ));
125 		settings.setBoolean( UISettings.ABORT_ON_INVALID_REQUEST, values.getBoolean( ABORT_ON_INVALID_REQUEST ));
126 		settings.setBoolean( UISettings.AUTO_VALIDATE_RESPONSE, values.getBoolean( AUTO_VALIDATE_RESPONSE ));
127 		settings.setBoolean( UISettings.SHOW_XML_LINE_NUMBERS, values.getBoolean( XML_LINE_NUMBERS ));
128 		settings.setBoolean( UISettings.SHOW_GROOVY_LINE_NUMBERS, values.getBoolean( GROOVY_LINE_NUMBERS ));
129 	}
130 	
131 	public static String encodeFont( Font font )
132 	{
133 		String editorFont = font.getFontName() + " ";
134 		if( font.isBold() )
135 			editorFont += "bold ";
136 		if( font.isItalic() )
137 			editorFont += "italic ";
138 		editorFont += font.getSize();
139 		
140 		return editorFont;
141 	}
142 
143 	public void setFormValues(Settings settings)
144 	{
145 		editorFontTextField.setText( encodeFont( UISupport.getEditorFont()) );
146 		editorForm.setValues( getValues(settings) );
147 		
148 		abortCheckBox.setEnabled( settings.getBoolean( UISettings.AUTO_VALIDATE_REQUEST ) );
149 	}
150 
151 	public StringToStringMap getValues(Settings settings)
152 	{
153 		StringToStringMap values = new StringToStringMap();
154 		values.put( NO_RESIZE_REQUEST_EDITOR, settings.getBoolean( UISettings.NO_RESIZE_REQUEST_EDITOR ));
155 		values.put( START_WITH_REQUEST_TABS, settings.getBoolean( UISettings.START_WITH_REQUEST_TABS ));
156 		values.put( AUTO_VALIDATE_REQUEST, settings.getBoolean( UISettings.AUTO_VALIDATE_REQUEST ));
157 		values.put( ABORT_ON_INVALID_REQUEST, settings.getBoolean( UISettings.ABORT_ON_INVALID_REQUEST ));
158 		values.put( AUTO_VALIDATE_RESPONSE, settings.getBoolean( UISettings.AUTO_VALIDATE_RESPONSE ));
159 		values.put( XML_LINE_NUMBERS, settings.getBoolean( UISettings.SHOW_XML_LINE_NUMBERS ));
160 		values.put( GROOVY_LINE_NUMBERS, settings.getBoolean( UISettings.SHOW_GROOVY_LINE_NUMBERS ));
161 		
162 		return values;
163 	}
164 }