11package com .aobuchow .themes .spectrum .preferences ;
22
3- import java .net .MalformedURLException ;
4- import java .net .URL ;
5-
6- import org .eclipse .core .runtime .Status ;
73import org .eclipse .e4 .core .services .events .IEventBroker ;
84import org .eclipse .jface .preference .PreferencePage ;
95import org .eclipse .swt .SWT ;
106import org .eclipse .swt .custom .StyledText ;
7+ import org .eclipse .swt .events .SelectionListener ;
118import org .eclipse .swt .graphics .Color ;
12- import org .eclipse .swt .graphics .Rectangle ;
13- import org .eclipse .swt .layout .GridData ;
14- import org .eclipse .swt .layout .GridLayout ;
159import org .eclipse .swt .widgets .Composite ;
1610import org .eclipse .swt .widgets .Control ;
17- import org .eclipse .swt .widgets .Group ;
1811import org .eclipse .swt .widgets .Scale ;
19- import org .eclipse .swt .widgets .Text ;
2012import org .eclipse .ui .IWorkbench ;
2113import org .eclipse .ui .IWorkbenchPreferencePage ;
2214import org .eclipse .ui .PlatformUI ;
23- import org .eclipse .ui .browser .IWebBrowser ;
2415import org .eclipse .ui .internal .themes .WorkbenchThemeManager ;
2516import org .osgi .service .event .EventHandler ;
2617
2920
3021public class PreferencesPage extends PreferencePage implements IWorkbenchPreferencePage {
3122
32- private IWebBrowser browser ;
33- private URL issuesURL ;
34- private URL repoURL ;
35- private StyledText colorScheme ;
3623 private Color prevAccentColor ;
3724 private Color prevBaseColor ;
3825 private IEventBroker eventBroker ;
3926 private ColorManager colorManager ;
4027 private Color prevBackgroundColor ;
41- private Scale hueScale ;
42- private Scale saturationScale ;
43- private Scale luminanceScale ;
28+
29+ private SpectrumPreferencesControl preferenceControl ;
4430
4531 @ Override
4632 public void init (IWorkbench workbench ) {
4733 colorManager = Activator .getDefault ().getColorManager ();
48- browser = BrowserUtils .newBrowser ();
4934 eventBroker = workbench .getService (IEventBroker .class );
5035 eventBroker .subscribe (WorkbenchThemeManager .Events .THEME_REGISTRY_RESTYLED , themeRegistryRestyledHandler );
5136 eventBroker .subscribe (WorkbenchThemeManager .Events .THEME_REGISTRY_MODIFIED , themeRegistryRestyledHandler );
@@ -54,51 +39,33 @@ public void init(IWorkbench workbench) {
5439 prevBackgroundColor = colorManager .getBackgroundColor ();
5540 prevBaseColor = colorManager .getBaseColor ();
5641 prevAccentColor = colorManager .getAccentColor ();
57-
58- try {
59- issuesURL = new URL ("https://github.com/AObuchow/Eclipse-Spectrum-Theme/issues" ); //$NON-NLS-1$
60- repoURL = new URL ("https://github.com/AObuchow/Eclipse-Spectrum-Theme" ); //$NON-NLS-1$
61- } catch (MalformedURLException e ) {
62- Activator .getDefault ().getLog ().log (new Status (ERROR , getClass (), e .getMessage ()));
63- }
6442 }
6543
6644 @ Override
6745 protected Control createContents (Composite parent ) {
68- Composite composite = new Composite (parent , SWT .NONE );
69- GridLayout layout = new GridLayout (1 , true );
70- layout .marginWidth = 0 ;
71- layout .marginHeight = 0 ;
72- composite .setLayout (layout );
7346
74- Group customizeGroup = new Group (composite , SWT .SHADOW_ETCHED_IN );
75- customizeGroup .setText (Messages .SpectrumPreferencePage_CustomizationGroup );
76- customizeGroup .setLayoutData (new GridData (SWT .FILL , SWT .FILL , true , true ));
77- customizeGroup .setLayout (layout );
47+ preferenceControl = new SpectrumPreferencesControl (parent , SWT .NONE );
48+
49+ addHSLCustomization (preferenceControl );
50+
51+ preferenceControl .getRepoLink ().addSelectionListener (
52+ SelectionListener .widgetSelectedAdapter (e -> openURL ("repo" , Messages .SpectrumPreferencePage_GithubURL )));
7853
79- addHSLCustomization (customizeGroup );
54+ preferenceControl .getIssuesLink ().addSelectionListener (SelectionListener
55+ .widgetSelectedAdapter (e -> openURL ("issues" , Messages .SpectrumPreferencePage_GithubURL_Issues )));
8056
81- colorScheme = new StyledText (customizeGroup , SWT .LEAD | SWT .BORDER | SWT .MULTI | SWT .READ_ONLY );
82- colorScheme .setLayoutData (new GridData (SWT .FILL , SWT .FILL , true , true ));
8357 String currentColorScheme = Activator .getDefault ().getColorManager ().getCurrentColorSchemeCSS ();
84- colorScheme .setText (currentColorScheme );
85- Activator .getDefault ().getColorManager ().setStyledTextColoring (colorScheme );
58+ final StyledText cssText = preferenceControl .getCssText ();
59+ cssText .setText (currentColorScheme );
60+ Activator .getDefault ().getColorManager ().setStyledTextColoring (cssText );
8661
87- Group communityGroup = new Group (composite , SWT .SHADOW_ETCHED_IN );
88- communityGroup .setText (Messages .SpectrumPreferencePage_CommunityGroup );
89- communityGroup .setLayoutData (new GridData (SWT .FILL , SWT .FILL , true , true ));
90- communityGroup .setLayout (layout );
91- BrowserUtils .createLinkURL (communityGroup , Messages .SpectrumPreferencePage_RepositoryLink , repoURL , browser );
92- BrowserUtils .createLinkURL (communityGroup , Messages .SpectrumPreferencePage_BugReportLink , issuesURL , browser );
62+ refresh ();
9363
94- return composite ;
64+ return preferenceControl ;
9565 }
9666
97- private void addHSLCustomization (Group customizeGroup ) {
98- Text hueLabel = new Text (customizeGroup , SWT .BOLD );
99- hueLabel .setText (Messages .PreferencesPage_LabelHue );
100- hueScale = newScale (customizeGroup , 0 , 360 ,
101- (int ) new ColorHSL (colorManager .getBackgroundColor ()).getHue ());
67+ private void addHSLCustomization (SpectrumPreferencesControl control ) {
68+ final Scale hueScale = control .getHueScale ();
10269 hueScale .addListener (SWT .Selection , event -> {
10370 int selectionValue = hueScale .getSelection ();
10471 ColorHSL newBackgroundColor = new ColorHSL (colorManager .getBackgroundColor ()).setHue (selectionValue );
@@ -111,24 +78,17 @@ private void addHSLCustomization(Group customizeGroup) {
11178 colorManager .saveColors ();
11279 });
11380
114- Text saturationLabel = new Text (customizeGroup , SWT .BOLD );
115- saturationLabel .setLayoutData (new GridData (SWT .BEGINNING , SWT .BEGINNING , false , true ));
116- saturationLabel .setText (Messages .PreferencesPage_LabelSaturation );
117- saturationScale = newScale (customizeGroup , 0 , 100 ,
118- (int ) (new ColorHSL (colorManager .getBackgroundColor ()).getSaturation () * 100 ));
81+ final Scale saturationScale = control .getSaturationScale ();
11982 saturationScale .addListener (SWT .Selection , event -> {
12083 float selectionValue = ((float ) saturationScale .getSelection ()) / 100 ;
12184 ColorHSL newBackgroundColor = new ColorHSL (colorManager .getBackgroundColor ()).setSaturation (selectionValue );
12285 colorManager .setBackgroundColor (newBackgroundColor .getColor ());
12386 colorManager .saveColors ();
12487 });
12588
126- Text luminanceLabel = new Text (customizeGroup , SWT .BOLD );
127- luminanceLabel .setText (Messages .PreferencesPage_LabelLuminance );
128- luminanceScale = newScale (customizeGroup , 0 , 100 ,
129- (int ) (new ColorHSL (colorManager .getBackgroundColor ()).getLuminance () * 100 ));
130- luminanceScale .addListener (SWT .Selection , event -> {
131- float selectionValue = ((float ) luminanceScale .getSelection ()) / 100 ;
89+ final Scale brightnessScale = control .getBrightnessScale ();
90+ brightnessScale .addListener (SWT .Selection , event -> {
91+ float selectionValue = ((float ) brightnessScale .getSelection ()) / 100 ;
13292 ColorHSL newBackgroundColor = new ColorHSL (colorManager .getBackgroundColor ()).setLuminance (selectionValue );
13393 colorManager .setBackgroundColor (newBackgroundColor .getColor ());
13494 colorManager .saveColors ();
@@ -140,14 +100,14 @@ public boolean performOk() {
140100 prevBackgroundColor = colorManager .getBackgroundColor ();
141101 prevBaseColor = colorManager .getBaseColor ();
142102 prevAccentColor = colorManager .getAccentColor ();
143- updateScales ();
103+ refresh ();
144104 return super .performOk ();
145105 }
146106
147107 @ Override
148108 protected void performDefaults () {
149109 colorManager .resetColors ();
150- updateScales ();
110+ refresh ();
151111 super .performDefaults ();
152112 }
153113
@@ -160,36 +120,43 @@ public boolean performCancel() {
160120 prevBackgroundColor = colorManager .getBackgroundColor ();
161121 prevBaseColor = colorManager .getBaseColor ();
162122 prevAccentColor = colorManager .getAccentColor ();
163- updateScales ();
123+ refresh ();
164124 return super .performCancel ();
165125 }
166126
167127 private EventHandler themeRegistryRestyledHandler = event -> {
168128 // Update the relevant UI when the theme's colors are modified
169- if (!colorScheme .isDisposed ()) {
129+ if (!getControl () .isDisposed ()) {
170130 String currentColorScheme = Activator .getDefault ().getColorManager ().getCurrentColorSchemeCSS ();
171- colorScheme .setText (currentColorScheme );
172- Activator .getDefault ().getColorManager ().setStyledTextColoring (colorScheme );
131+ final StyledText cssText = preferenceControl .getCssText ();
132+ cssText .setText (currentColorScheme );
133+ Activator .getDefault ().getColorManager ().setStyledTextColoring (cssText );
173134 }
174135 };
175136
176- private void updateScales () {
177- hueScale .setSelection ((int ) new ColorHSL (colorManager .getBackgroundColor ()).getHue ());
178- saturationScale .setSelection ((int ) (new ColorHSL (colorManager .getBackgroundColor ()).getSaturation () * 100 ));
179- luminanceScale .setSelection ((int ) (new ColorHSL (colorManager .getBackgroundColor ()).getLuminance () * 100 ));
137+ private void refresh () {
138+ refreshScales ();
139+ refreshCssText ();
140+ }
141+
142+ private void refreshScales () {
143+ preferenceControl .getHueScale ().setSelection ((int ) new ColorHSL (colorManager .getBackgroundColor ()).getHue ());
144+ preferenceControl .getSaturationScale ()
145+ .setSelection ((int ) (new ColorHSL (colorManager .getBackgroundColor ()).getSaturation () * 100 ));
146+ preferenceControl .getBrightnessScale ()
147+ .setSelection ((int ) (new ColorHSL (colorManager .getBackgroundColor ()).getLuminance () * 100 ));
148+ }
149+
150+ private void refreshCssText () {
151+ String currentColorScheme = Activator .getDefault ().getColorManager ().getCurrentColorSchemeCSS ();
152+ final StyledText cssText = preferenceControl .getCssText ();
153+ if (!cssText .getText ().equals (currentColorScheme )) {
154+ cssText .setText (currentColorScheme );
155+ }
180156 }
181157
182- private static Scale newScale (Composite parent , int min , int max , int selection ) {
183- Scale scale = new Scale (parent , SWT .BORDER );
184- scale .setLayoutData (new GridData (SWT .FILL , SWT .FILL , true , false ));
185- Rectangle clientArea = parent .getClientArea ();
186- scale .setBounds (clientArea .x , clientArea .y , clientArea .width , clientArea .height );
187- scale .setMaximum (max );
188- scale .setMinimum (min );
189- scale .setSelection (selection );
190- scale .setPageIncrement (5 );
191- scale .setIncrement (5 );
192- return scale ;
158+ private void openURL (String category , String url ) {
159+ BrowserUtils .openUrl (category , url );
193160 }
194161
195162}
0 commit comments