1 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.widgets.Button; 35 import org.eclipse.swt.widgets.Composite; 36 import org.eclipse.swt.widgets.Control; 37 import org.eclipse.swt.widgets.Group; 38 import org.eclipse.swt.widgets.Label; 39 import org.eclipse.ui.IWorkbench; 40 import org.eclipse.ui.IWorkbenchPreferencePage; 41 42 43 public class MainPreferencePage extends PreferencePage implements IWorkbenchPreferencePage 44 { 45 46 private final String [] ERROR_TYPES = new String [] 47 { "Warnings:", "Errors:" }; 48 49 private final String [] ERROR_FONT_CONSTANTS = new String [] 50 { BrowserCommonConstants.PREFERENCE_WARNING_FONT, BrowserCommonConstants.PREFERENCE_ERROR_FONT }; 51 52 private final String [] ERROR_COLOR_CONSTANTS = new String [] 53 { BrowserCommonConstants.PREFERENCE_WARNING_COLOR, BrowserCommonConstants.PREFERENCE_ERROR_COLOR }; 54 55 private Label[] errorTypeLabels = new Label[ERROR_TYPES.length]; 56 57 private ColorSelector[] errorColorSelectors = new ColorSelector[ERROR_TYPES.length]; 58 59 private Button[] errorBoldButtons = new Button[ERROR_TYPES.length]; 60 61 private Button[] errorItalicButtons = new Button[ERROR_TYPES.length]; 62 63 private Label quickfilterTypeLabel; 64 65 private ColorSelector quickfilterForegroundColorSelector;; 66 67 private ColorSelector quickfilterBackgroundColorSelector;; 68 69 private Button quickfilterBoldButton; 70 71 private Button quickfilterItalicButton; 72 73 74 public MainPreferencePage() 75 { 76 super( "LDAP" ); 77 super.setPreferenceStore( BrowserCommonActivator.getDefault().getPreferenceStore() ); 78 super.setDescription( "General settings for the LDAP plugin:" ); 79 } 80 81 82 public void init( IWorkbench workbench ) 83 { 84 } 85 86 87 protected Control createContents( Composite parent ) 88 { 89 90 Composite composite = BaseWidgetUtils.createColumnContainer( parent, 1, 1 ); 91 92 BaseWidgetUtils.createSpacer( composite, 1 ); 93 BaseWidgetUtils.createSpacer( composite, 1 ); 94 Group errorGroup = BaseWidgetUtils.createGroup( composite, "Warning and Error Colors and Fonts", 1 ); 95 errorGroup.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) ); 96 Composite errorComposite = BaseWidgetUtils.createColumnContainer( errorGroup, 4, 1 ); 97 for ( int i = 0; i < ERROR_TYPES.length; i++ ) 98 { 99 final int index = i; 100 101 errorTypeLabels[i] = BaseWidgetUtils.createLabel( errorComposite, ERROR_TYPES[i], 1 ); 102 errorTypeLabels[i].setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) ); 103 errorColorSelectors[i] = new ColorSelector( errorComposite ); 104 errorBoldButtons[i] = BaseWidgetUtils.createCheckbox( errorComposite, "Bold", 1 ); 105 errorItalicButtons[i] = BaseWidgetUtils.createCheckbox( errorComposite, "Italic", 1 ); 106 107 FontData[] fontDatas = PreferenceConverter.getFontDataArray( BrowserCommonActivator.getDefault() 108 .getPreferenceStore(), ERROR_FONT_CONSTANTS[i] ); 109 RGB rgb = PreferenceConverter.getColor( BrowserCommonActivator.getDefault().getPreferenceStore(), 110 ERROR_COLOR_CONSTANTS[i] ); 111 setErrors( index, fontDatas, rgb ); 112 } 113 114 BaseWidgetUtils.createSpacer( composite, 1 ); 115 BaseWidgetUtils.createSpacer( composite, 1 ); 116 Group otherGroup = BaseWidgetUtils.createGroup( composite, "Quick Filter Colors and Fonts", 1 ); 117 otherGroup.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) ); 118 Composite otherComposite = BaseWidgetUtils.createColumnContainer( otherGroup, 4, 1 ); 119 quickfilterTypeLabel = BaseWidgetUtils.createLabel( otherComposite, "Quick Filter", 1 ); 120 quickfilterTypeLabel.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) ); 121 quickfilterForegroundColorSelector = new ColorSelector( otherComposite ); 122 quickfilterBoldButton = BaseWidgetUtils.createCheckbox( otherComposite, "Bold", 1 ); 123 quickfilterItalicButton = BaseWidgetUtils.createCheckbox( otherComposite, "Italic", 1 ); 124 Label quickfilterBgLabel = BaseWidgetUtils.createLabel( otherComposite, "Quick Filter Background", 1 ); 125 quickfilterBgLabel.setLayoutData( new GridData( GridData.FILL_HORIZONTAL ) ); 126 quickfilterBackgroundColorSelector = new ColorSelector( otherComposite ); 127 FontData[] qfFontDatas = PreferenceConverter.getFontDataArray( BrowserCommonActivator.getDefault() 128 .getPreferenceStore(), BrowserCommonConstants.PREFERENCE_QUICKFILTER_FONT ); 129 RGB qfBgRgb = PreferenceConverter.getColor( BrowserCommonActivator.getDefault().getPreferenceStore(), 130 BrowserCommonConstants.PREFERENCE_QUICKFILTER_BACKGROUND_COLOR ); 131 RGB qfFgRgb = PreferenceConverter.getColor( BrowserCommonActivator.getDefault().getPreferenceStore(), 132 BrowserCommonConstants.PREFERENCE_QUICKFILTER_FOREGROUND_COLOR ); 133 setQuickfilter( qfFontDatas, qfFgRgb, qfBgRgb ); 134 135 return composite; 136 } 137 138 139 private void setErrors( int index, FontData[] fontDatas, RGB rgb ) 140 { 141 boolean bold = isBold( fontDatas ); 142 boolean italic = isItalic( fontDatas ); 143 errorColorSelectors[index].setColorValue( rgb ); 144 errorBoldButtons[index].setSelection( bold ); 145 errorItalicButtons[index].setSelection( italic ); 146 } 147 148 149 private void setQuickfilter( FontData[] fontDatas, RGB fgRgb, RGB bgRgb ) 150 { 151 boolean bold = isBold( fontDatas ); 152 boolean italic = isItalic( fontDatas ); 153 quickfilterBackgroundColorSelector.setColorValue( bgRgb ); 154 quickfilterForegroundColorSelector.setColorValue( fgRgb ); 155 quickfilterBoldButton.setSelection( bold ); 156 quickfilterItalicButton.setSelection( italic ); 157 } 158 159 160 protected void performDefaults() 161 { 162 for ( int i = 0; i < ERROR_TYPES.length; i++ ) 163 { 164 FontData[] fontDatas = PreferenceConverter.getDefaultFontDataArray( BrowserCommonActivator.getDefault() 165 .getPreferenceStore(), ERROR_FONT_CONSTANTS[i] ); 166 RGB rgb = PreferenceConverter.getDefaultColor( BrowserCommonActivator.getDefault().getPreferenceStore(), 167 ERROR_COLOR_CONSTANTS[i] ); 168 setErrors( i, fontDatas, rgb ); 169 } 170 171 FontData[] qfFontDatas = PreferenceConverter.getDefaultFontDataArray( BrowserCommonActivator.getDefault() 172 .getPreferenceStore(), BrowserCommonConstants.PREFERENCE_QUICKFILTER_FONT ); 173 RGB qfBgRgb = PreferenceConverter.getDefaultColor( BrowserCommonActivator.getDefault().getPreferenceStore(), 174 BrowserCommonConstants.PREFERENCE_QUICKFILTER_BACKGROUND_COLOR ); 175 RGB qfFgRgb = PreferenceConverter.getDefaultColor( BrowserCommonActivator.getDefault().getPreferenceStore(), 176 BrowserCommonConstants.PREFERENCE_QUICKFILTER_FOREGROUND_COLOR ); 177 setQuickfilter( qfFontDatas, qfFgRgb, qfBgRgb ); 178 179 super.performDefaults(); 180 } 181 182 183 public boolean performOk() 184 { 185 for ( int i = 0; i < ERROR_TYPES.length; i++ ) 186 { 187 FontData[] fontDatas = PreferenceConverter.getFontDataArray( BrowserCommonActivator.getDefault() 188 .getPreferenceStore(), ERROR_FONT_CONSTANTS[i] ); 189 setFontData( fontDatas, this.errorBoldButtons[i], this.errorBoldButtons[i] ); 190 RGB rgb = errorColorSelectors[i].getColorValue(); 191 PreferenceConverter.setValue( BrowserCommonActivator.getDefault().getPreferenceStore(), ERROR_FONT_CONSTANTS[i], 192 fontDatas ); 193 PreferenceConverter.setValue( BrowserCommonActivator.getDefault().getPreferenceStore(), ERROR_COLOR_CONSTANTS[i], 194 rgb ); 195 } 196 197 FontData[] qfFontDatas = PreferenceConverter.getFontDataArray( BrowserCommonActivator.getDefault() 198 .getPreferenceStore(), BrowserCommonConstants.PREFERENCE_QUICKFILTER_FONT ); 199 setFontData( qfFontDatas, this.quickfilterBoldButton, this.quickfilterItalicButton ); 200 RGB qfBgRgb = quickfilterBackgroundColorSelector.getColorValue(); 201 RGB qfFgRgb = quickfilterForegroundColorSelector.getColorValue(); 202 PreferenceConverter.setValue( BrowserCommonActivator.getDefault().getPreferenceStore(), 203 BrowserCommonConstants.PREFERENCE_QUICKFILTER_FONT, qfFontDatas ); 204 PreferenceConverter.setValue( BrowserCommonActivator.getDefault().getPreferenceStore(), 205 BrowserCommonConstants.PREFERENCE_QUICKFILTER_BACKGROUND_COLOR, qfBgRgb ); 206 PreferenceConverter.setValue( BrowserCommonActivator.getDefault().getPreferenceStore(), 207 BrowserCommonConstants.PREFERENCE_QUICKFILTER_FOREGROUND_COLOR, qfFgRgb ); 208 209 return true; 210 } 211 212 213 private void setFontData( FontData[] fontDatas, Button boldButton, Button italicButton ) 214 { 215 for ( int j = 0; j < fontDatas.length; j++ ) 216 { 217 int style = SWT.NORMAL; 218 if ( boldButton.getSelection() ) 219 style |= SWT.BOLD; 220 if ( italicButton.getSelection() ) 221 style |= SWT.ITALIC; 222 fontDatas[j].setStyle( style ); 223 } 224 } 225 226 227 private boolean isBold( FontData[] fontDatas ) 228 { 229 boolean bold = false; 230 for ( int j = 0; j < fontDatas.length; j++ ) 231 { 232 if ( ( fontDatas[j].getStyle() & SWT.BOLD ) != SWT.NORMAL ) 233 bold = true; 234 } 235 return bold; 236 } 237 238 239 private boolean isItalic( FontData[] fontDatas ) 240 { 241 boolean italic = false; 242 for ( int j = 0; j < fontDatas.length; j++ ) 243 { 244 if ( ( fontDatas[j].getStyle() & SWT.ITALIC ) != SWT.NORMAL ) 245 italic = true; 246 } 247 return italic; 248 } 249 250 } 251 | Popular Tags |