KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > common > dialogs > preferences > AttributesPreferencePage


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.browser.common.dialogs.preferences;
22
23
24 import org.apache.directory.ldapstudio.browser.common.BrowserCommonActivator;
25 import org.apache.directory.ldapstudio.browser.common.BrowserCommonConstants;
26 import org.apache.directory.ldapstudio.browser.common.widgets.BaseWidgetUtils;
27 import org.eclipse.jface.preference.ColorSelector;
28 import org.eclipse.jface.preference.PreferenceConverter;
29 import org.eclipse.jface.preference.PreferencePage;
30 import org.eclipse.swt.SWT;
31 import org.eclipse.swt.graphics.FontData;
32 import org.eclipse.swt.graphics.RGB;
33 import org.eclipse.swt.layout.GridData;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.widgets.Button;
36 import org.eclipse.swt.widgets.Composite;
37 import org.eclipse.swt.widgets.Control;
38 import org.eclipse.swt.widgets.Group;
39 import org.eclipse.swt.widgets.Label;
40 import org.eclipse.ui.IWorkbench;
41 import org.eclipse.ui.IWorkbenchPreferencePage;
42
43
44 public class AttributesPreferencePage extends PreferencePage implements IWorkbenchPreferencePage
45 {
46
47     private final String JavaDoc[] ATTRIBUTE_TYPES = new String JavaDoc[]
48         { "Objectclass attribute:", "Must attributes:", "May attributes:", "Operational attributes:" };
49
50     private final String JavaDoc[] ATTRIBUTE_FONT_CONSTANTS = new String JavaDoc[]
51         { BrowserCommonConstants.PREFERENCE_OBJECTCLASS_FONT, BrowserCommonConstants.PREFERENCE_MUSTATTRIBUTE_FONT,
52         BrowserCommonConstants.PREFERENCE_MAYATTRIBUTE_FONT, BrowserCommonConstants.PREFERENCE_OPERATIONALATTRIBUTE_FONT };
53
54     private final String JavaDoc[] ATTRIBUTE_COLOR_CONSTANTS = new String JavaDoc[]
55         { BrowserCommonConstants.PREFERENCE_OBJECTCLASS_COLOR, BrowserCommonConstants.PREFERENCE_MUSTATTRIBUTE_COLOR,
56         BrowserCommonConstants.PREFERENCE_MAYATTRIBUTE_COLOR, BrowserCommonConstants.PREFERENCE_OPERATIONALATTRIBUTE_COLOR };
57
58     private Label[] attributeTypeLabels = new Label[ATTRIBUTE_TYPES.length];
59
60     private ColorSelector[] attributeColorSelectors = new ColorSelector[ATTRIBUTE_TYPES.length];
61
62     private Button[] attributeBoldButtons = new Button[ATTRIBUTE_TYPES.length];
63
64     private Button[] attributeItalicButtons = new Button[ATTRIBUTE_TYPES.length];
65
66     private Button showRawValuesButton;
67
68
69     public AttributesPreferencePage()
70     {
71         super( "Attributes" );
72         super.setPreferenceStore( BrowserCommonActivator.getDefault().getPreferenceStore() );
73         super.setDescription( "General settings for attributes:" );
74     }
75
76
77     public void init( IWorkbench workbench )
78     {
79     }
80
81
82     protected Control createContents( Composite parent )
83     {
84
85         Composite composite = new Composite( parent, SWT.NONE );
86         GridLayout layout = new GridLayout( 1, false );
87         layout.marginWidth = 0;
88         layout.marginHeight = 0;
89         layout.marginLeft = 0;
90         layout.marginRight = 0;
91         layout.marginTop = 0;
92         layout.marginBottom = 0;
93         composite.setLayout( layout );
94         composite.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
95
96         BaseWidgetUtils.createSpacer( composite, 1 );
97         BaseWidgetUtils.createSpacer( composite, 1 );
98         Group colorsAndFontsGroup = BaseWidgetUtils.createGroup( composite, "Attribute Colors and Fonts", 1 );
99         colorsAndFontsGroup.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
100         Composite colorsAndFontsComposite = BaseWidgetUtils.createColumnContainer( colorsAndFontsGroup, 4, 1 );
101         for ( int i = 0; i < ATTRIBUTE_TYPES.length; i++ )
102         {
103             final int index = i;
104
105             attributeTypeLabels[i] = BaseWidgetUtils.createLabel( colorsAndFontsComposite, ATTRIBUTE_TYPES[i], 1 );
106             attributeTypeLabels[i].setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) );
107             attributeColorSelectors[i] = new ColorSelector( colorsAndFontsComposite );
108             attributeBoldButtons[i] = BaseWidgetUtils.createCheckbox( colorsAndFontsComposite, "Bold", 1 );
109             attributeItalicButtons[i] = BaseWidgetUtils.createCheckbox( colorsAndFontsComposite, "Italic", 1 );
110
111             FontData[] fontDatas = PreferenceConverter.getFontDataArray( BrowserCommonActivator.getDefault()
112                 .getPreferenceStore(), ATTRIBUTE_FONT_CONSTANTS[i] );
113             RGB rgb = PreferenceConverter.getColor( BrowserCommonActivator.getDefault().getPreferenceStore(),
114                 ATTRIBUTE_COLOR_CONSTANTS[i] );
115             setColorsAndFonts( index, fontDatas, rgb );
116         }
117
118         BaseWidgetUtils.createSpacer( composite, 1 );
119         BaseWidgetUtils.createSpacer( composite, 1 );
120         showRawValuesButton = BaseWidgetUtils.createCheckbox( composite, "Show raw values", 1 );
121         showRawValuesButton.setSelection( getPreferenceStore().getBoolean(
122             BrowserCommonConstants.PREFERENCE_SHOW_RAW_VALUES ) );
123
124         applyDialogFont( composite );
125         return composite;
126     }
127
128
129     private void setColorsAndFonts( int index, FontData[] fontDatas, RGB rgb )
130     {
131         boolean bold = isBold( fontDatas );
132         boolean italic = isItalic( fontDatas );
133         attributeColorSelectors[index].setColorValue( rgb );
134         attributeBoldButtons[index].setSelection( bold );
135         attributeItalicButtons[index].setSelection( italic );
136     }
137
138
139     private void setFontData( FontData[] fontDatas, Button boldButton, Button italicButton )
140     {
141         for ( int j = 0; j < fontDatas.length; j++ )
142         {
143             int style = SWT.NORMAL;
144             if ( boldButton.getSelection() )
145                 style |= SWT.BOLD;
146             if ( italicButton.getSelection() )
147                 style |= SWT.ITALIC;
148             fontDatas[j].setStyle( style );
149         }
150     }
151
152
153     private boolean isBold( FontData[] fontDatas )
154     {
155         boolean bold = false;
156         for ( int j = 0; j < fontDatas.length; j++ )
157         {
158             if ( ( fontDatas[j].getStyle() & SWT.BOLD ) != SWT.NORMAL )
159                 bold = true;
160         }
161         return bold;
162     }
163
164
165     private boolean isItalic( FontData[] fontDatas )
166     {
167         boolean italic = false;
168         for ( int j = 0; j < fontDatas.length; j++ )
169         {
170             if ( ( fontDatas[j].getStyle() & SWT.ITALIC ) != SWT.NORMAL )
171                 italic = true;
172         }
173         return italic;
174     }
175
176
177     public boolean performOk()
178     {
179
180         for ( int i = 0; i < ATTRIBUTE_TYPES.length; i++ )
181         {
182             FontData[] fontDatas = PreferenceConverter.getFontDataArray( BrowserCommonActivator.getDefault()
183                 .getPreferenceStore(), ATTRIBUTE_FONT_CONSTANTS[i] );
184             setFontData( fontDatas, this.attributeBoldButtons[i], this.attributeItalicButtons[i] );
185             RGB rgb = attributeColorSelectors[i].getColorValue();
186             PreferenceConverter.setValue( BrowserCommonActivator.getDefault().getPreferenceStore(),
187                 ATTRIBUTE_FONT_CONSTANTS[i], fontDatas );
188             PreferenceConverter.setValue( BrowserCommonActivator.getDefault().getPreferenceStore(),
189                 ATTRIBUTE_COLOR_CONSTANTS[i], rgb );
190         }
191
192         getPreferenceStore().setValue( BrowserCommonConstants.PREFERENCE_SHOW_RAW_VALUES,
193             this.showRawValuesButton.getSelection() );
194
195         return true;
196     }
197
198
199     protected void performDefaults()
200     {
201
202         for ( int i = 0; i < ATTRIBUTE_TYPES.length; i++ )
203         {
204             FontData[] fontDatas = PreferenceConverter.getDefaultFontDataArray( BrowserCommonActivator.getDefault()
205                 .getPreferenceStore(), ATTRIBUTE_FONT_CONSTANTS[i] );
206             RGB rgb = PreferenceConverter.getDefaultColor( BrowserCommonActivator.getDefault().getPreferenceStore(),
207                 ATTRIBUTE_COLOR_CONSTANTS[i] );
208             setColorsAndFonts( i, fontDatas, rgb );
209         }
210
211         showRawValuesButton.setSelection( getPreferenceStore().getDefaultBoolean(
212             BrowserCommonConstants.PREFERENCE_SHOW_RAW_VALUES ) );
213
214         super.performDefaults();
215     }
216
217 }
218
Popular Tags