Skip to content

Commit efe131c

Browse files
author
gbr
committed
Rework preferences page.
1 parent a423d04 commit efe131c

File tree

20 files changed

+675
-460
lines changed

20 files changed

+675
-460
lines changed

net.certiv.tools.indentguide.feature/feature.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<feature
33
id="net.certiv.tools.indentguide.feature"
44
label="IndentGuide"
5-
version="1.8.0.qualifier"
5+
version="2.0.0.qualifier"
66
provider-name="Certiv Analytis">
77

88
<description>

net.certiv.tools.indentguide.feature/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<parent>
1010
<groupId>net.certiv</groupId>
1111
<artifactId>net.certiv.tools.indentguide.parent</artifactId>
12-
<version>1.8.0-SNAPSHOT</version>
12+
<version>2.0.0-SNAPSHOT</version>
1313
</parent>
1414

1515
<artifactId>net.certiv.tools.indentguide.feature</artifactId>

net.certiv.tools.indentguide.plugin/META-INF/MANIFEST.MF

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
22
Bundle-ManifestVersion: 2
33
Bundle-Name: IndentGuide
44
Bundle-SymbolicName: net.certiv.tools.indentguide;singleton:=true
5-
Bundle-Version: 1.8.0.qualifier
5+
Bundle-Version: 2.0.0.qualifier
66
Bundle-Activator: net.certiv.tools.indentguide.Activator
77
Bundle-Vendor: Certiv Analytics
88
Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.26.100,4.0.0)",

net.certiv.tools.indentguide.plugin/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<parent>
1010
<groupId>net.certiv</groupId>
1111
<artifactId>net.certiv.tools.indentguide.parent</artifactId>
12-
<version>1.8.0-SNAPSHOT</version>
12+
<version>2.0.0-SNAPSHOT</version>
1313
</parent>
1414

1515
<artifactId>net.certiv.tools.indentguide</artifactId>

net.certiv.tools.indentguide.plugin/src/net/certiv/tools/indentguide/Activator.java

+25-33
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,31 @@
1414
import org.eclipse.core.runtime.preferences.DefaultScope;
1515
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
1616
import org.eclipse.core.runtime.preferences.InstanceScope;
17-
import org.eclipse.e4.core.services.events.IEventBroker;
18-
import org.eclipse.e4.ui.css.swt.theme.IThemeEngine;
1917
import org.eclipse.jface.preference.PreferenceConverter;
2018
import org.eclipse.jface.resource.StringConverter;
19+
import org.eclipse.jface.util.IPropertyChangeListener;
2120
import org.eclipse.swt.graphics.Color;
2221
import org.eclipse.swt.graphics.RGB;
23-
import org.eclipse.ui.IWorkbench;
2422
import org.eclipse.ui.PlatformUI;
2523
import org.eclipse.ui.plugin.AbstractUIPlugin;
2624
import org.eclipse.ui.texteditor.AbstractTextEditor;
2725
import org.eclipse.ui.themes.ColorUtil;
2826
import org.osgi.framework.BundleContext;
29-
import org.osgi.service.event.EventHandler;
3027

3128
import net.certiv.tools.indentguide.preferences.Settings;
3229

33-
@SuppressWarnings("restriction")
3430
public class Activator extends AbstractUIPlugin {
3531

3632
public static final String PLUGIN_ID = "net.certiv.tools.indentguide"; //$NON-NLS-1$
3733
private static final String EditorsID = "org.eclipse.ui.editors"; //$NON-NLS-1$
34+
private static final String PREFIX = "Indent Guide: "; //$NON-NLS-1$
3835

3936
private static Activator plugin;
4037

41-
private final IEclipsePreferences[] scopes = new IEclipsePreferences[2];
42-
private final EventHandler themeChange = event -> {
38+
private final IEclipsePreferences[] editorScopes = new IEclipsePreferences[] {
39+
InstanceScope.INSTANCE.getNode(EditorsID), DefaultScope.INSTANCE.getNode(EditorsID) };
40+
41+
private final IPropertyChangeListener themeChange = event -> {
4342
disposeLineColor();
4443
log("Theme change '%s'", event);
4544
};
@@ -51,35 +50,24 @@ public Activator() {
5150
}
5251

5352
/** Returns the shared instance */
54-
public static Activator getDefault() { return plugin; }
53+
public static Activator getDefault() {
54+
return plugin;
55+
}
5556

5657
@Override
5758
public void start(BundleContext context) throws Exception {
5859
super.start(context);
5960
plugin = this;
6061

61-
log("Indent guide: startup");
62-
63-
scopes[0] = InstanceScope.INSTANCE.getNode(EditorsID);
64-
scopes[1] = DefaultScope.INSTANCE.getNode(EditorsID);
65-
66-
IWorkbench wb = PlatformUI.getWorkbench();
67-
IEventBroker broker = wb.getService(IEventBroker.class);
68-
if (broker != null) {
69-
broker.subscribe(IThemeEngine.Events.THEME_CHANGED, themeChange);
70-
}
62+
PlatformUI.getWorkbench().getThemeManager().addPropertyChangeListener(themeChange);
63+
log("Startup");
7164
}
7265

7366
@Override
7467
public void stop(BundleContext context) throws Exception {
75-
IWorkbench wb = PlatformUI.getWorkbench();
76-
IEventBroker broker = wb.getService(IEventBroker.class);
77-
if (broker != null) {
78-
broker.unsubscribe(themeChange);
79-
}
80-
68+
PlatformUI.getWorkbench().getThemeManager().removePropertyChangeListener(themeChange);
8169
disposeLineColor();
82-
scopes[0] = scopes[1] = null;
70+
editorScopes[0] = editorScopes[1] = null;
8371
plugin = null;
8472
super.stop(context);
8573
}
@@ -92,11 +80,20 @@ public Color getColor() {
9280
}
9381
String spec = getPreferenceStore().getString(key);
9482
color = new Color(PlatformUI.getWorkbench().getDisplay(), ColorUtil.getColorValue(spec));
95-
log(String.format("Line color set %s -> %s", key, spec));
83+
log("Line color set %s -> %s", key, spec);
9684
}
9785
return color;
9886
}
9987

88+
public void setColor(Color color) {
89+
disposeLineColor();
90+
this.color = color;
91+
}
92+
93+
public void setColor(RGB rgb) {
94+
setColor(new Color(PlatformUI.getWorkbench().getDisplay(), rgb));
95+
}
96+
10097
/**
10198
* Returns {@code true} if the current theme is 'dark', defined as where the foreground color is
10299
* relatively darker than the background color. (black -> '0'; white -> '255*3')
@@ -108,19 +105,14 @@ public boolean isDarkTheme() {
108105
}
109106

110107
private RGB getRawRGB(String key) {
111-
String value = Platform.getPreferencesService().get(key, null, scopes);
108+
String value = Platform.getPreferencesService().get(key, null, editorScopes);
112109
if (value == null) return PreferenceConverter.COLOR_DEFAULT_DEFAULT;
113110

114111
RGB rgb = StringConverter.asRGB(value, null);
115112
if (rgb == null) return PreferenceConverter.COLOR_DEFAULT_DEFAULT;
116113
return rgb;
117114
}
118115

119-
public void setColor(Color color) {
120-
disposeLineColor();
121-
this.color = color;
122-
}
123-
124116
private void disposeLineColor() {
125117
if (color != null) {
126118
color.dispose();
@@ -129,7 +121,7 @@ private void disposeLineColor() {
129121
}
130122

131123
public static void log(String fmt, Object... args) {
132-
plugin.getLog().log(new Status(IStatus.INFO, PLUGIN_ID, String.format(fmt, args)));
124+
plugin.getLog().log(new Status(IStatus.INFO, PLUGIN_ID, PREFIX + String.format(fmt, args)));
133125
}
134126

135127
public static void log(Throwable e) {

net.certiv.tools.indentguide.plugin/src/net/certiv/tools/indentguide/IndentGuidePainter.java

+15-10
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@
4040
public class IndentGuidePainter implements IPainter, PaintListener {
4141

4242
private static final Pattern COMMENT_LEAD = Pattern.compile("^ \\*([ \\t].*|/.*|)$"); // $NON-NLS-1$
43-
private static final String SPACE = " "; //$NON-NLS-1$
43+
44+
private static final char TAB = '\t'; // $NON-NLS-1$
45+
private static final char SPC = ' '; // $NON-NLS-1$
46+
private static final String SPACE = " "; // $NON-NLS-1$
4447

4548
private boolean advanced; // advanced graphics subsystem
4649
private boolean active; // painter state
@@ -114,7 +117,8 @@ public void paint(int reason) {
114117
if (widgetOffset >= 0 && redrawLength > 0) {
115118
widget.redrawRange(widgetOffset, redrawLength, true);
116119
}
117-
} catch (BadLocationException e) {}
120+
}
121+
catch (BadLocationException e) {}
118122
}
119123
}
120124

@@ -182,7 +186,7 @@ private void handleDrawRequest(GC gc, int x, int y, int w, int h) {
182186
*/
183187
private void drawLineRange(GC gc, int begLine, int endLine, int x, int w) {
184188
int tabWidth = widget.getTabs();
185-
int spaceWidth = gc.stringExtent(SPACE).x - 1;
189+
int spcWidth = Math.max(1, gc.stringExtent(SPACE).x - 1);
186190

187191
StyledTextContent content = widget.getContent();
188192

@@ -216,26 +220,26 @@ private void drawLineRange(GC gc, int begLine, int endLine, int x, int w) {
216220
}
217221
int count = countSpaces(text, tabWidth) + extend;
218222
for (int col = drawLeftEnd ? 0 : tabWidth; col < count; col += tabWidth) {
219-
draw(gc, offset, col, spaceWidth);
223+
draw(gc, offset, col, spcWidth);
220224
}
221225
}
222226
}
223227
}
224228

225-
private void draw(GC gc, int offset, int column, int spaceWidth) {
229+
private void draw(GC gc, int offset, int col, int spcWidth) {
226230
Point pos = widget.getLocationAtOffset(offset);
227-
pos.x += column * spaceWidth + lineShift;
231+
pos.x += col * spcWidth + lineShift;
228232
gc.drawLine(pos.x, pos.y, pos.x, pos.y + widget.getLineHeight(offset));
229233
}
230234

231235
private int countSpaces(String str, int tabs) {
232236
int count = 0;
233237
for (int i = 0; i < str.length(); i++) {
234238
switch (str.charAt(i)) {
235-
case ' ':
239+
case SPC:
236240
count++;
237241
break;
238-
case '\t':
242+
case TAB:
239243
int z = tabs - count % tabs;
240244
count += z;
241245
break;
@@ -252,10 +256,10 @@ private boolean assumeCommentBlock(String text, int tabs) {
252256
int index = 0;
253257
for (int i = 0; i < count; i++) {
254258
switch (text.charAt(index)) {
255-
case ' ':
259+
case SPC:
256260
index++;
257261
break;
258-
case '\t':
262+
case TAB:
259263
index++;
260264
int z = tabs - i % tabs;
261265
i += z;
@@ -320,6 +324,7 @@ private int getDocumentOffset(int widgetOffset) {
320324
ITextViewerExtension5 extension = (ITextViewerExtension5) viewer;
321325
return extension.widgetOffset2ModelOffset(widgetOffset);
322326
}
327+
323328
IRegion visible = viewer.getVisibleRegion();
324329
if (widgetOffset > visible.getLength()) return -1;
325330
return widgetOffset + visible.getOffset();

0 commit comments

Comments
 (0)