Skip to content

Commit c0eff4d

Browse files
ingomohrAObuchow
authored andcommitted
Update layout of preference page; make links open in their own browsers
1 parent e096588 commit c0eff4d

File tree

6 files changed

+211
-117
lines changed

6 files changed

+211
-117
lines changed

com.aobuchow.themes.spectrum.preferences/META-INF/MANIFEST.MF

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ Require-Bundle: org.eclipse.ui,
1111
org.eclipse.e4.core.services,
1212
org.eclipse.e4.ui.model.workbench,
1313
org.eclipse.e4.core.contexts,
14-
org.eclipse.e4.ui.css.swt.theme
14+
org.eclipse.e4.ui.css.swt.theme,
15+
org.eclipse.ui.forms;bundle-version="3.9.100"
1516
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
1617
Automatic-Module-Name: com.aobuchow.themes.spectrum.preferences
1718
Bundle-ActivationPolicy: lazy
Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,25 @@
11
package com.aobuchow.themes.spectrum.preferences;
22

3+
import java.net.MalformedURLException;
34
import java.net.URL;
45

6+
import org.eclipse.core.runtime.IStatus;
57
import org.eclipse.core.runtime.Status;
6-
import org.eclipse.swt.SWT;
7-
import org.eclipse.swt.widgets.Group;
8-
import org.eclipse.swt.widgets.Link;
98
import org.eclipse.ui.PartInitException;
109
import org.eclipse.ui.PlatformUI;
1110
import org.eclipse.ui.browser.IWebBrowser;
1211

13-
public interface BrowserUtils {
12+
public final class BrowserUtils {
1413

15-
public static IWebBrowser newBrowser() {
16-
IWebBrowser browser = null;
14+
public static void openUrl(String category, String url) {
1715
try {
18-
browser = PlatformUI.getWorkbench().getBrowserSupport().createBrowser("spectrum.browser"); //$NON-NLS-1$
19-
} catch (PartInitException e) {
20-
// Internal browser shouldn't cause an exception
21-
Activator.getDefault().getLog().log(new Status(org.eclipse.jface.dialogs.IMessageProvider.ERROR, BrowserUtils.class, e.getMessage()));
16+
IWebBrowser browser = PlatformUI.getWorkbench().getBrowserSupport()
17+
.createBrowser("spectrum.browser." + category);
18+
browser.openURL(new URL(url));
19+
} catch (PartInitException | MalformedURLException e) {
20+
final Status status = new Status(IStatus.ERROR, BrowserUtils.class, "Cannot open URL '" + url + "'", e);
21+
Activator.getDefault().getLog().log(status);
2222
}
23-
24-
try {
25-
browser = PlatformUI.getWorkbench().getBrowserSupport().getExternalBrowser();
26-
} catch (PartInitException e) {
27-
Activator.getDefault().getLog().log(new Status(org.eclipse.jface.dialogs.IMessageProvider.INFORMATION, BrowserUtils.class, e.getMessage()));
28-
}
29-
30-
return browser;
31-
}
32-
33-
public static Link createLinkURL(Group parent, String text, URL url, IWebBrowser webBrowser) {
34-
Link link = new Link(parent, SWT.BORDER | SWT.BOLD);
35-
link.setText(text);
36-
link.addListener(SWT.Selection, event -> {
37-
try {
38-
webBrowser.openURL(url);
39-
} catch (PartInitException e) {
40-
Activator.getDefault().getLog().log(new Status(org.eclipse.jface.dialogs.IMessageProvider.ERROR, BrowserUtils.class, e.getMessage()));
41-
}
42-
});
43-
return link;
4423
}
4524

4625
}

com.aobuchow.themes.spectrum.preferences/src/com/aobuchow/themes/spectrum/preferences/Messages.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ public class Messages extends NLS {
1111
public static String SpectrumPreferencePage_CommunityGroup;
1212
public static String SpectrumPreferencePage_CustomizationGroup;
1313
public static String SpectrumPreferencePage_RepositoryLink;
14+
public static String SpectrumPreferencePage_GithubURL_Issues;
15+
public static String SpectrumPreferencePage_GithubURL;
16+
1417
static {
1518
// initialize resource bundle
1619
NLS.initializeMessages(BUNDLE_NAME, Messages.class);
Lines changed: 50 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
11
package com.aobuchow.themes.spectrum.preferences;
22

3-
import java.net.MalformedURLException;
4-
import java.net.URL;
5-
6-
import org.eclipse.core.runtime.Status;
73
import org.eclipse.e4.core.services.events.IEventBroker;
84
import org.eclipse.jface.preference.PreferencePage;
95
import org.eclipse.swt.SWT;
106
import org.eclipse.swt.custom.StyledText;
7+
import org.eclipse.swt.events.SelectionListener;
118
import 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;
159
import org.eclipse.swt.widgets.Composite;
1610
import org.eclipse.swt.widgets.Control;
17-
import org.eclipse.swt.widgets.Group;
1811
import org.eclipse.swt.widgets.Scale;
19-
import org.eclipse.swt.widgets.Text;
2012
import org.eclipse.ui.IWorkbench;
2113
import org.eclipse.ui.IWorkbenchPreferencePage;
2214
import org.eclipse.ui.PlatformUI;
23-
import org.eclipse.ui.browser.IWebBrowser;
2415
import org.eclipse.ui.internal.themes.WorkbenchThemeManager;
2516
import org.osgi.service.event.EventHandler;
2617

@@ -29,23 +20,17 @@
2920

3021
public 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

Comments
 (0)