001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.studio.apacheds.prefs;
021    
022    
023    import org.apache.directory.studio.apacheds.ApacheDsPlugin;
024    import org.apache.directory.studio.apacheds.ApacheDsPluginConstants;
025    import org.apache.directory.studio.apacheds.PreferenceStoreUtils;
026    import org.eclipse.jface.preference.ColorSelector;
027    import org.eclipse.jface.preference.PreferenceConverter;
028    import org.eclipse.jface.preference.PreferencePage;
029    import org.eclipse.swt.SWT;
030    import org.eclipse.swt.graphics.FontData;
031    import org.eclipse.swt.graphics.RGB;
032    import org.eclipse.swt.layout.GridData;
033    import org.eclipse.swt.layout.GridLayout;
034    import org.eclipse.swt.widgets.Button;
035    import org.eclipse.swt.widgets.Composite;
036    import org.eclipse.swt.widgets.Control;
037    import org.eclipse.swt.widgets.Group;
038    import org.eclipse.swt.widgets.Label;
039    import org.eclipse.ui.IWorkbench;
040    import org.eclipse.ui.IWorkbenchPreferencePage;
041    
042    
043    /**
044     * This class implements the Colors And Fonts preference page for the Apache DS plugin.
045     *
046     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
047     * @version $Rev$, $Date$
048     */
049    public class ColorsAndFontsPage extends PreferencePage implements IWorkbenchPreferencePage
050    {
051        // UI fields
052        private ColorSelector debugColorButton;
053        private Button debugBoldCheckbox;
054        private Button debugItalicCheckbox;
055        private ColorSelector infoColorButton;
056        private Button infoBoldCheckbox;
057        private Button infoItalicCheckbox;
058        private ColorSelector warnColorButton;
059        private Button warnBoldCheckbox;
060        private Button warnItalicCheckbox;
061        private ColorSelector errorColorButton;
062        private Button errorBoldCheckbox;
063        private Button errorItalicCheckbox;
064        private ColorSelector fatalColorButton;
065        private Button fatalBoldCheckbox;
066        private Button fatalItalicCheckbox;
067    
068    
069        /**
070         * Creates a new instance of ColorsAndFontsPage.
071         */
072        public ColorsAndFontsPage()
073        {
074            super( Messages.getString( "ColorsAndFontsPage.ColorsAndFonts" ) ); //$NON-NLS-1$
075            setPreferenceStore( ApacheDsPlugin.getDefault().getPreferenceStore() );
076        }
077    
078    
079        /* (non-Javadoc)
080         * @see org.eclipse.jface.preference.PreferencePage#createContents(org.eclipse.swt.widgets.Composite)
081         */
082        protected Control createContents( Composite parent )
083        {
084            // Composite
085            Composite composite = new Composite( parent, SWT.NONE );
086            composite.setLayout( new GridLayout() );
087            composite.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true ) );
088    
089            // Console Group
090            Group consoleGroup = new Group( composite, SWT.NONE );
091            consoleGroup.setText( Messages.getString( "ColorsAndFontsPage.Console" ) ); //$NON-NLS-1$
092            GridLayout consoleGroupLayout = new GridLayout( 4, false );
093            consoleGroup.setLayout( consoleGroupLayout );
094            consoleGroup.setLayoutData( new GridData( SWT.FILL, SWT.NONE, true, false ) );
095    
096            // Debug
097            Label debugLabel = new Label( consoleGroup, SWT.NONE );
098            debugLabel.setText( Messages.getString( "ColorsAndFontsPage.Debug" ) ); //$NON-NLS-1$
099            debugColorButton = new ColorSelector( consoleGroup );
100            debugColorButton.getButton().setLayoutData( new GridData( SWT.RIGHT, SWT.NONE, true, false ) );
101            debugBoldCheckbox = new Button( consoleGroup, SWT.CHECK );
102            debugBoldCheckbox.setText( Messages.getString( "ColorsAndFontsPage.Bold" ) ); //$NON-NLS-1$
103            debugItalicCheckbox = new Button( consoleGroup, SWT.CHECK );
104            debugItalicCheckbox.setText( Messages.getString( "ColorsAndFontsPage.Italic" ) ); //$NON-NLS-1$
105    
106            // Info
107            Label infoLabel = new Label( consoleGroup, SWT.NONE );
108            infoLabel.setText( Messages.getString( "ColorsAndFontsPage.Info" ) ); //$NON-NLS-1$
109            infoColorButton = new ColorSelector( consoleGroup );
110            infoColorButton.getButton().setLayoutData( new GridData( SWT.RIGHT, SWT.NONE, true, false ) );
111            infoBoldCheckbox = new Button( consoleGroup, SWT.CHECK );
112            infoBoldCheckbox.setText( Messages.getString( "ColorsAndFontsPage.Bold" ) ); //$NON-NLS-1$
113            infoItalicCheckbox = new Button( consoleGroup, SWT.CHECK );
114            infoItalicCheckbox.setText( Messages.getString( "ColorsAndFontsPage.Italic" ) ); //$NON-NLS-1$
115    
116            // Warn
117            Label warnLabel = new Label( consoleGroup, SWT.NONE );
118            warnLabel.setText( Messages.getString( "ColorsAndFontsPage.Warn" ) ); //$NON-NLS-1$
119            warnColorButton = new ColorSelector( consoleGroup );
120            warnColorButton.getButton().setLayoutData( new GridData( SWT.RIGHT, SWT.NONE, true, false ) );
121            warnBoldCheckbox = new Button( consoleGroup, SWT.CHECK );
122            warnBoldCheckbox.setText( Messages.getString( "ColorsAndFontsPage.Bold" ) ); //$NON-NLS-1$
123            warnItalicCheckbox = new Button( consoleGroup, SWT.CHECK );
124            warnItalicCheckbox.setText( Messages.getString( "ColorsAndFontsPage.Italic" ) ); //$NON-NLS-1$
125    
126            // Error
127            Label errorLabel = new Label( consoleGroup, SWT.NONE );
128            errorLabel.setText( Messages.getString( "ColorsAndFontsPage.Error" ) ); //$NON-NLS-1$
129            errorColorButton = new ColorSelector( consoleGroup );
130            errorColorButton.getButton().setLayoutData( new GridData( SWT.RIGHT, SWT.NONE, true, false ) );
131            errorBoldCheckbox = new Button( consoleGroup, SWT.CHECK );
132            errorBoldCheckbox.setText( Messages.getString( "ColorsAndFontsPage.Bold" ) ); //$NON-NLS-1$
133            errorItalicCheckbox = new Button( consoleGroup, SWT.CHECK );
134            errorItalicCheckbox.setText( Messages.getString( "ColorsAndFontsPage.Italic" ) ); //$NON-NLS-1$
135    
136            // Fatal
137            Label fatalLabel = new Label( consoleGroup, SWT.NONE );
138            fatalLabel.setText( Messages.getString( "ColorsAndFontsPage.Fatal" ) ); //$NON-NLS-1$
139            fatalColorButton = new ColorSelector( consoleGroup );
140            fatalColorButton.getButton().setLayoutData( new GridData( SWT.RIGHT, SWT.NONE, true, false ) );
141            fatalBoldCheckbox = new Button( consoleGroup, SWT.CHECK );
142            fatalBoldCheckbox.setText( Messages.getString( "ColorsAndFontsPage.Bold" ) ); //$NON-NLS-1$
143            fatalItalicCheckbox = new Button( consoleGroup, SWT.CHECK );
144            fatalItalicCheckbox.setText( Messages.getString( "ColorsAndFontsPage.Italic" ) ); //$NON-NLS-1$
145    
146            initFromPreferences();
147    
148            applyDialogFont( composite );
149            return composite;
150        }
151    
152    
153        /**
154         * Initializes the UI fields from the preferences values.
155         */
156        private void initFromPreferences()
157        {
158            // Debug
159            FontData[] fontDatas = PreferenceConverter.getFontDataArray( getPreferenceStore(),
160                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_DEBUG_FONT );
161            RGB rgb = PreferenceConverter.getColor( getPreferenceStore(),
162                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_DEBUG_COLOR );
163            setDebug( fontDatas, rgb );
164    
165            // Info
166            fontDatas = PreferenceConverter.getFontDataArray( getPreferenceStore(),
167                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_INFO_FONT );
168            rgb = PreferenceConverter.getColor( getPreferenceStore(),
169                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_INFO_COLOR );
170            setInfo( fontDatas, rgb );
171    
172            // Warn
173            fontDatas = PreferenceConverter.getFontDataArray( getPreferenceStore(),
174                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_WARN_FONT );
175            rgb = PreferenceConverter.getColor( getPreferenceStore(),
176                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_WARN_COLOR );
177            setWarn( fontDatas, rgb );
178    
179            // Error
180            fontDatas = PreferenceConverter.getFontDataArray( getPreferenceStore(),
181                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_ERROR_FONT );
182            rgb = PreferenceConverter.getColor( getPreferenceStore(),
183                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_ERROR_COLOR );
184            setError( fontDatas, rgb );
185    
186            // Fatal
187            fontDatas = PreferenceConverter.getFontDataArray( getPreferenceStore(),
188                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_FATAL_FONT );
189            rgb = PreferenceConverter.getColor( getPreferenceStore(),
190                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_FATAL_COLOR );
191            setFatal( fontDatas, rgb );
192        }
193    
194    
195        /* (non-Javadoc)
196         * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
197         */
198        protected void performDefaults()
199        {
200            // Debug
201            FontData[] fontDatas = PreferenceConverter.getDefaultFontDataArray( getPreferenceStore(),
202                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_DEBUG_FONT );
203            RGB rgb = PreferenceConverter.getDefaultColor( getPreferenceStore(),
204                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_DEBUG_COLOR );
205            setDebug( fontDatas, rgb );
206    
207            // Info
208            fontDatas = PreferenceConverter.getDefaultFontDataArray( getPreferenceStore(),
209                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_INFO_FONT );
210            rgb = PreferenceConverter.getDefaultColor( getPreferenceStore(),
211                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_INFO_COLOR );
212            setInfo( fontDatas, rgb );
213    
214            // Warn
215            fontDatas = PreferenceConverter.getDefaultFontDataArray( getPreferenceStore(),
216                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_WARN_FONT );
217            rgb = PreferenceConverter.getDefaultColor( getPreferenceStore(),
218                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_WARN_COLOR );
219            setWarn( fontDatas, rgb );
220    
221            // Error
222            fontDatas = PreferenceConverter.getDefaultFontDataArray( getPreferenceStore(),
223                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_ERROR_FONT );
224            rgb = PreferenceConverter.getDefaultColor( getPreferenceStore(),
225                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_ERROR_COLOR );
226            setError( fontDatas, rgb );
227    
228            // Fatal
229            fontDatas = PreferenceConverter.getDefaultFontDataArray( getPreferenceStore(),
230                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_FATAL_FONT );
231            rgb = PreferenceConverter.getDefaultColor( getPreferenceStore(),
232                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_FATAL_COLOR );
233            setFatal( fontDatas, rgb );
234    
235            super.performDefaults();
236        }
237    
238    
239        /* (non-Javadoc)
240         * @see org.eclipse.jface.preference.PreferencePage#performOk()
241         */
242        public boolean performOk()
243        {
244            // Debug
245            FontData[] fontDatas = PreferenceConverter.getFontDataArray( getPreferenceStore(),
246                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_DEBUG_FONT );
247            setFontData( fontDatas, debugBoldCheckbox, debugItalicCheckbox );
248            PreferenceConverter.setValue( getPreferenceStore(), ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_DEBUG_FONT,
249                fontDatas );
250            PreferenceConverter.setValue( getPreferenceStore(), ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_DEBUG_COLOR,
251                debugColorButton.getColorValue() );
252    
253            // Info
254            fontDatas = PreferenceConverter.getFontDataArray( getPreferenceStore(),
255                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_INFO_FONT );
256            setFontData( fontDatas, infoBoldCheckbox, infoItalicCheckbox );
257            PreferenceConverter.setValue( getPreferenceStore(), ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_INFO_FONT,
258                fontDatas );
259            PreferenceConverter.setValue( getPreferenceStore(), ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_INFO_COLOR,
260                infoColorButton.getColorValue() );
261    
262            // Warn
263            fontDatas = PreferenceConverter.getFontDataArray( getPreferenceStore(),
264                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_WARN_FONT );
265            setFontData( fontDatas, warnBoldCheckbox, warnItalicCheckbox );
266            PreferenceConverter.setValue( getPreferenceStore(), ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_WARN_FONT,
267                fontDatas );
268            PreferenceConverter.setValue( getPreferenceStore(), ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_WARN_COLOR,
269                warnColorButton.getColorValue() );
270    
271            // Error
272            fontDatas = PreferenceConverter.getFontDataArray( getPreferenceStore(),
273                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_ERROR_FONT );
274            setFontData( fontDatas, errorBoldCheckbox, errorItalicCheckbox );
275            PreferenceConverter.setValue( getPreferenceStore(), ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_ERROR_FONT,
276                fontDatas );
277            PreferenceConverter.setValue( getPreferenceStore(), ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_ERROR_COLOR,
278                errorColorButton.getColorValue() );
279    
280            // Fatal
281            fontDatas = PreferenceConverter.getFontDataArray( getPreferenceStore(),
282                ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_FATAL_FONT );
283            setFontData( fontDatas, fatalBoldCheckbox, fatalItalicCheckbox );
284            PreferenceConverter.setValue( getPreferenceStore(), ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_FATAL_FONT,
285                fontDatas );
286            PreferenceConverter.setValue( getPreferenceStore(), ApacheDsPluginConstants.PREFS_COLORS_AND_FONTS_FATAL_COLOR,
287                fatalColorButton.getColorValue() );
288    
289            return super.performOk();
290        }
291    
292    
293        /**
294         * Set the font, bold and italic values to the Debug UI fields.
295         *
296         * @param fontDatas
297         *      the array of {@link FontData}
298         * @param rgb
299         *      the color
300         */
301        private void setDebug( FontData[] fontDatas, RGB rgb )
302        {
303            boolean bold = PreferenceStoreUtils.isBold( fontDatas );
304            boolean italic = PreferenceStoreUtils.isItalic( fontDatas );
305            debugColorButton.setColorValue( rgb );
306            debugBoldCheckbox.setSelection( bold );
307            debugItalicCheckbox.setSelection( italic );
308        }
309    
310    
311        /**
312         * Set the font, bold and italic values to the Info UI fields.
313         *
314         * @param fontDatas
315         *      the array of {@link FontData}
316         * @param rgb
317         *      the color
318         */
319        private void setInfo( FontData[] fontDatas, RGB rgb )
320        {
321            boolean bold = PreferenceStoreUtils.isBold( fontDatas );
322            boolean italic = PreferenceStoreUtils.isItalic( fontDatas );
323            infoColorButton.setColorValue( rgb );
324            infoBoldCheckbox.setSelection( bold );
325            infoItalicCheckbox.setSelection( italic );
326        }
327    
328    
329        /**
330         * Set the font, bold and italic values to the Warn UI fields.
331         *
332         * @param fontDatas
333         *      the array of {@link FontData}
334         * @param rgb
335         *      the color
336         */
337        private void setWarn( FontData[] fontDatas, RGB rgb )
338        {
339            boolean bold = PreferenceStoreUtils.isBold( fontDatas );
340            boolean italic = PreferenceStoreUtils.isItalic( fontDatas );
341            warnColorButton.setColorValue( rgb );
342            warnBoldCheckbox.setSelection( bold );
343            warnItalicCheckbox.setSelection( italic );
344        }
345    
346    
347        /**
348         * Set the font, bold and italic values to the Info UI fields.
349         *
350         * @param fontDatas
351         *      the array of {@link FontData}
352         * @param rgb
353         *      the color
354         */
355        private void setError( FontData[] fontDatas, RGB rgb )
356        {
357            boolean bold = PreferenceStoreUtils.isBold( fontDatas );
358            boolean italic = PreferenceStoreUtils.isItalic( fontDatas );
359            errorColorButton.setColorValue( rgb );
360            errorBoldCheckbox.setSelection( bold );
361            errorItalicCheckbox.setSelection( italic );
362        }
363    
364    
365        /**
366         * Set the font, bold and italic values to the Fatal UI fields.
367         *
368         * @param fontDatas
369         *      the array of {@link FontData}
370         * @param rgb
371         *      the color
372         * @param bgRgb
373         *      the background color
374         */
375        private void setFatal( FontData[] fontDatas, RGB rgb )
376        {
377            boolean bold = PreferenceStoreUtils.isBold( fontDatas );
378            boolean italic = PreferenceStoreUtils.isItalic( fontDatas );
379            fatalColorButton.setColorValue( rgb );
380            fatalBoldCheckbox.setSelection( bold );
381            fatalItalicCheckbox.setSelection( italic );
382        }
383    
384    
385        /**
386         * Sets the bold and italic values to the array of {@link FontData}.
387         *
388         * @param fontDatas
389         *      the array of {@link FontData}
390         * @param boldButton
391         *      the bold button
392         * @param italicButton
393         *      the italic button
394         */
395        private void setFontData( FontData[] fontDatas, Button boldButton, Button italicButton )
396        {
397            for ( FontData fontData : fontDatas )
398            {
399                int style = SWT.NORMAL;
400                if ( boldButton.getSelection() )
401                {
402                    style |= SWT.BOLD;
403                }
404                if ( italicButton.getSelection() )
405                {
406                    style |= SWT.ITALIC;
407                }
408    
409                fontData.setStyle( style );
410            }
411        }
412    
413    
414        /* (non-Javadoc)
415         * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
416         */
417        public void init( IWorkbench workbench )
418        {
419            // Nothing to do
420        }
421    }