1   /* ====================================================================
2    * The Apache Software License, Version 1.1
3    *
4    * Copyright (c) 2000 The Apache Software Foundation.  All rights
5    * reserved.
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions
9    * are met:
10   *
11   * 1. Redistributions of source code must retain the above copyright
12   *    notice, this list of conditions and the following disclaimer.
13   *
14   * 2. Redistributions in binary form must reproduce the above copyright
15   *    notice, this list of conditions and the following disclaimer in
16   *    the documentation and/or other materials provided with the
17   *    distribution.
18   *
19   * 3. The end-user documentation included with the redistribution,
20   *    if any, must include the following acknowledgment:
21   *       "This product includes software developed by the
22   *        Apache Software Foundation (http://www.apache.org/)."
23   *    Alternately, this acknowledgment may appear in the software itself,
24   *    if and wherever such third-party acknowledgments normally appear.
25   *
26   * 4. The names "Apache" and "Apache Software Foundation" must
27   *    not be used to endorse or promote products derived from this
28   *    software without prior written permission. For written
29   *    permission, please contact apache@apache.org.
30   *
31   * 5. Products derived from this software may not be called "Apache",
32   *    nor may "Apache" appear in their name, without prior written
33   *    permission of the Apache Software Foundation.
34   *
35   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46   * SUCH DAMAGE.
47   * ====================================================================
48   *
49   * This software consists of voluntary contributions made by many
50   * individuals on behalf of the Apache Software Foundation.  For more
51   * information on the Apache Software Foundation, please see
52   * <http://www.apache.org/>.
53   *
54   * Portions of this software are based upon public domain software
55   * originally written at the National Center for Supercomputing Applications,
56   * University of Illinois, Urbana-Champaign.
57   */
58  package com.bonevich.eclipse.logging.prefs;
59  
60  import java.util.HashMap;
61  import java.util.Map;
62  
63  import org.eclipse.jface.preference.BooleanFieldEditor;
64  import org.eclipse.jface.preference.FieldEditor;
65  import org.eclipse.jface.preference.PreferencePage;
66  import org.eclipse.jface.preference.RadioGroupFieldEditor;
67  import org.eclipse.jface.preference.StringFieldEditor;
68  import org.eclipse.swt.SWT;
69  import org.eclipse.swt.layout.GridData;
70  import org.eclipse.swt.layout.GridLayout;
71  import org.eclipse.swt.widgets.Composite;
72  import org.eclipse.swt.widgets.Control;
73  import org.eclipse.swt.widgets.Group;
74  import org.eclipse.swt.widgets.Label;
75  import org.eclipse.ui.IWorkbench;
76  import org.eclipse.ui.IWorkbenchPreferencePage;
77  
78  import com.bonevich.eclipse.logging.LoggingPlugin;
79  import com.bonevich.eclipse.logging.LoggingPluginMessages;
80  import com.bonevich.eclipse.logging.LoggingPluginResources;
81  
82  /***
83   * Preference page for the Boneclipse Logging Plugin.  Allows the user
84   * to specify which logging API to use and the default name for the logging
85   * field to be used.
86   * 
87   * @author jbonevic
88   */
89  public class LoggingPreferencePage 
90  	extends PreferencePage 
91  	implements IWorkbenchPreferencePage, LoggingPluginResources
92  {
93  	// field editor keys
94  	private static final String FIELDEDITOR_API    = "logging.impl";
95  	private static final String FIELDEDITOR_FIELD  = "logging.field";
96  	private static final String FIELDEDITOR_AUTOSAVE  = "logging.autosave";
97  	private static final String FIELDEDITOR_TRACE  = "logging.trace";
98  	private static final String FIELDEDITOR_DEBUG  = "logging.debug";
99  	private static final String FIELDEDITOR_INFO  = "logging.info";
100 	private static final String FIELDEDITOR_WARN  = "logging.warn";
101 	private static final String FIELDEDITOR_ERROR  = "logging.error";
102 	private static final String FIELDEDITOR_FATAL  = "logging.fatal";
103 
104 	private Map _fieldEditors = new HashMap();
105 	
106 	public LoggingPreferencePage()
107 	{
108 		super("Logging");
109 	}
110 
111 	public void init(IWorkbench workbench)
112 	{
113 		setPreferenceStore(LoggingPlugin.getPlugin().getPreferenceStore());
114 	}
115 
116 	protected Control createContents(Composite parent)
117 	{
118 		Composite top = createContainerWithDescription(parent);
119 		createLoggingImplementationRadioGroup(top);
120 		createFieldNameStringField(top);
121 		createAutoSaveCheckboxField(top);
122 		createUseConditionalCheckboxGroup(top);
123 		
124 		return top;
125 	}
126 
127 	private void createAutoSaveCheckboxField(Composite top)
128 	{
129 		Composite autoSaveCmp = new Composite(top, SWT.LEFT);
130 		autoSaveCmp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
131 		autoSaveCmp.setLayout(new GridLayout());
132 		
133 		BooleanFieldEditor autoSave = new BooleanFieldEditor(
134 				LoggingPlugin.PREFERENCE_AUTOSAVE,
135 				LoggingPluginMessages.bind(LOGGING_PREFS_AUTOSAVE_LABEL),
136 				autoSaveCmp
137 		);
138 		addFieldEditor(FIELDEDITOR_AUTOSAVE, autoSave);
139 	}
140 
141 	private void createUseConditionalCheckboxGroup(Composite top)
142 	{
143 		Group group1 = new Group(top, SWT.LEFT);
144 		group1.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
145 		GridLayout layout = new GridLayout();
146 		layout.marginWidth = 7;
147 		group1.setLayout(layout);
148 		group1.setText(LoggingPluginMessages.bind(LOGGING_PREFS_CONDITIONALS_LABEL));
149 
150 		Composite group = new Composite(group1, SWT.NONE);
151 		group.setLayoutData(new GridData(GridData.CENTER));
152 		group.setLayout(new GridLayout());
153 		
154 		BooleanFieldEditor useTraceConditionals = new BooleanFieldEditor(
155 				LoggingPlugin.PREFERENCE_TRACE_CONDITIONALS,
156 				LoggingPluginMessages.bind(LOGGING_PREFS_TRACE_CONDITIONALS_LABEL),
157 				group
158 		);
159 		BooleanFieldEditor useDebugConditionals = new BooleanFieldEditor(
160 				LoggingPlugin.PREFERENCE_DEBUG_CONDITIONALS,
161 				LoggingPluginMessages.bind(LOGGING_PREFS_DEBUG_CONDITIONALS_LABEL),
162 				group
163 		);
164 		BooleanFieldEditor useInfoConditionals = new BooleanFieldEditor(
165 				LoggingPlugin.PREFERENCE_INFO_CONDITIONALS,
166 				LoggingPluginMessages.bind(LOGGING_PREFS_INFO_CONDITIONALS_LABEL),
167 				group
168 		);
169 		BooleanFieldEditor useWarnConditionals = new BooleanFieldEditor(
170 				LoggingPlugin.PREFERENCE_WARN_CONDITIONALS,
171 				LoggingPluginMessages.bind(LOGGING_PREFS_WARN_CONDITIONALS_LABEL),
172 				group
173 		);
174 		BooleanFieldEditor useErrorConditionals = new BooleanFieldEditor(
175 				LoggingPlugin.PREFERENCE_ERROR_CONDITIONALS,
176 				LoggingPluginMessages.bind(LOGGING_PREFS_ERROR_CONDITIONALS_LABEL),
177 				group
178 		);
179 		BooleanFieldEditor useFatalConditionals = new BooleanFieldEditor(
180 				LoggingPlugin.PREFERENCE_FATAL_CONDITIONALS,
181 				LoggingPluginMessages.bind(LOGGING_PREFS_FATAL_CONDITIONALS_LABEL),
182 				group
183 		);
184 
185 		addFieldEditor(FIELDEDITOR_TRACE, useTraceConditionals);
186 		addFieldEditor(FIELDEDITOR_DEBUG, useDebugConditionals);
187 		addFieldEditor(FIELDEDITOR_INFO, useInfoConditionals);
188 		addFieldEditor(FIELDEDITOR_WARN, useWarnConditionals);
189 		addFieldEditor(FIELDEDITOR_ERROR, useErrorConditionals);
190 		addFieldEditor(FIELDEDITOR_FATAL, useFatalConditionals);
191 	}
192 
193 	private void createFieldNameStringField(Composite top)
194 	{
195 		Composite fieldNameComp = new Composite(top, SWT.LEFT);
196 		fieldNameComp.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
197 		fieldNameComp.setLayout(new GridLayout());
198 		
199 		StringFieldEditor fieldName = new StringFieldEditor(
200 				LoggingPlugin.PREFERENCE_FIELD,
201 				LoggingPluginMessages.bind(LOGGING_PREFS_FIELD_LABEL),
202 				15,
203 				StringFieldEditor.VALIDATE_ON_FOCUS_LOST,
204 				fieldNameComp
205 			);
206 		fieldName.setEmptyStringAllowed(false);
207 		addFieldEditor(FIELDEDITOR_FIELD, fieldName);
208 	}
209 
210 	private void createLoggingImplementationRadioGroup(Composite top)
211 	{
212 		RadioGroupFieldEditor loggingImpl = new RadioGroupFieldEditor(
213 				LoggingPlugin.PREFERENCE_API,
214 				LoggingPluginMessages.bind(LOGGING_PREFS_API_LABEL),
215 				1,
216 				new String[][] {
217 					{LoggingPluginMessages.bind(LOGGING_PREFS_API_COMMONS_LABEL), 
218 					 LoggingPlugin.LOGGING_PREFS_API_COMMONS
219 					},
220 					{LoggingPluginMessages.bind(LOGGING_PREFS_API_JDK_1_4_LABEL), 
221 					 LoggingPlugin.LOGGING_PREFS_API_JDK_1_4
222 					},
223 					{LoggingPluginMessages.bind(LOGGING_PREFS_API_LOG4J_LABEL), 
224 					 LoggingPlugin.LOGGING_PREFS_API_LOG4J
225 					},
226 					{LoggingPluginMessages.bind(LOGGING_PREFS_API_AVALON_LABEL), 
227 					 LoggingPlugin.LOGGING_PREFS_API_AVALON
228 					},
229 				},
230 				top,
231 				true
232 			);
233 		addFieldEditor(FIELDEDITOR_API, loggingImpl);
234 	}
235 
236 	private Composite createContainerWithDescription(Composite parent)
237 	{
238 		Composite top = new Composite(parent, SWT.LEFT);
239 		top.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
240 		top.setLayout(new GridLayout());
241 		
242 		Label description = new Label(top, SWT.NONE);
243 		description.setText(LoggingPluginMessages.bind(LOGGING_PREFS_GENERAL));
244 		return top;
245 	}
246 
247 	private void addFieldEditor(String name, FieldEditor editor)
248 	{
249 		_fieldEditors.put(name, editor);
250 		editor.setPreferencePage(this);
251 		editor.setPreferenceStore(getPreferenceStore());
252 		editor.load();
253 	}
254 	
255 	private FieldEditor getFieldEditor(String name)
256 	{
257 		return (FieldEditor) _fieldEditors.get(name);
258 	}
259 
260 	protected void performDefaults()
261 	{
262 		super.performDefaults();
263 
264 		loadDefault(FIELDEDITOR_API);
265 		loadDefault(FIELDEDITOR_FIELD);
266 		loadDefault(FIELDEDITOR_AUTOSAVE);
267 
268 		loadDefault(FIELDEDITOR_TRACE);
269 		loadDefault(FIELDEDITOR_DEBUG);
270 		loadDefault(FIELDEDITOR_INFO);
271 		loadDefault(FIELDEDITOR_WARN);
272 		loadDefault(FIELDEDITOR_ERROR);
273 		loadDefault(FIELDEDITOR_FATAL);
274 	}
275 	
276 	private void loadDefault(String field)
277 	{
278 		FieldEditor editor = getFieldEditor(field);
279 		editor.loadDefault();
280 	}
281 
282 	public boolean performOk()
283 	{
284 		storeValue(FIELDEDITOR_API);
285 		storeValue(FIELDEDITOR_FIELD);
286 		storeValue(FIELDEDITOR_AUTOSAVE);
287 
288 		storeValue(FIELDEDITOR_TRACE);
289 		storeValue(FIELDEDITOR_DEBUG);
290 		storeValue(FIELDEDITOR_INFO);
291 		storeValue(FIELDEDITOR_WARN);
292 		storeValue(FIELDEDITOR_ERROR);
293 		storeValue(FIELDEDITOR_FATAL);
294 		
295 		return super.performOk();
296 	}
297 	
298 	private void storeValue(String field)
299 	{
300 		FieldEditor editor = getFieldEditor(field);
301 		editor.store();
302 	}
303 
304 }
This page was automatically generated by Maven