Skip to content

Commit

Permalink
Refactor painter logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
gbr committed Mar 2, 2023
1 parent ca98961 commit 80492fa
Show file tree
Hide file tree
Showing 32 changed files with 1,249 additions and 680 deletions.
8 changes: 8 additions & 0 deletions .mvn/extensions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<extensions>
<extension>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-build</artifactId>
<version>3.0.1</version>
</extension>
</extensions>
2 changes: 1 addition & 1 deletion net.certiv.tools.indentguide.feature/feature.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<feature
id="net.certiv.tools.indentguide.feature"
label="IndentGuide"
version="2.0.0.qualifier"
version="2.1.0.qualifier"
provider-name="Certiv Analytis">

<description>
Expand Down
2 changes: 1 addition & 1 deletion net.certiv.tools.indentguide.feature/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>net.certiv</groupId>
<artifactId>net.certiv.tools.indentguide.parent</artifactId>
<version>2.0.0-SNAPSHOT</version>
<version>2.1.0-SNAPSHOT</version>
</parent>

<artifactId>net.certiv.tools.indentguide.feature</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion net.certiv.tools.indentguide.plugin/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: IndentGuide
Bundle-SymbolicName: net.certiv.tools.indentguide;singleton:=true
Bundle-Version: 2.0.0.qualifier
Bundle-Version: 2.1.0.qualifier
Bundle-Activator: net.certiv.tools.indentguide.Activator
Bundle-Vendor: Certiv Analytics
Require-Bundle: org.eclipse.core.runtime;bundle-version="[3.26.100,4.0.0)",
Expand Down
4 changes: 3 additions & 1 deletion net.certiv.tools.indentguide.plugin/build.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
source.. = src/
source.. = src/main/java
output.. = target/classes/
bin.includes = plugin.xml,\
.,\
META-INF/,\
OSGI-INF/,\
icons/
src.excludes = src/test/java/,\
src/test/resources/
1 change: 1 addition & 0 deletions net.certiv.tools.indentguide.plugin/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
class="net.certiv.tools.indentguide.preferences.Initializer">
</initializer>
</extension>

<extension
point="org.eclipse.ui.startup">
<startup
Expand Down
2 changes: 1 addition & 1 deletion net.certiv.tools.indentguide.plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>net.certiv</groupId>
<artifactId>net.certiv.tools.indentguide.parent</artifactId>
<version>2.0.0-SNAPSHOT</version>
<version>2.1.0-SNAPSHOT</version>
</parent>

<artifactId>net.certiv.tools.indentguide</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/******************************************************************************
* Copyright (c) 2006-2023 The IndentGuide Authors.
* All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the MIT License. A copy of the MIT License is included this
* distribution and is available at https://opensource.org/licenses/MIT.
*****************************************************************************/
package net.certiv.tools.indentguide;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import net.certiv.tools.indentguide.util.MsgBuilder;

public class Activator extends AbstractUIPlugin {

public static final String PLUGIN_ID = "net.certiv.tools.indentguide"; //$NON-NLS-1$
private static final String PREFIX = "Indent Guide: "; //$NON-NLS-1$

private static Activator plugin;

public Activator() {
super();
}

/** Returns the shared instance */
public static Activator getDefault() {
return plugin;
}

@Override
public void start(BundleContext context) throws Exception {
super.start(context);
plugin = this;
log("Startup");
}

@Override
public void stop(BundleContext context) throws Exception {
plugin = null;
super.stop(context);
}

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

public static void log(MsgBuilder mb) {
plugin.getLog().log(new Status(IStatus.INFO, PLUGIN_ID, PREFIX + mb.toString()));
}

public static void log(Throwable e) {
plugin.getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, e.getMessage()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
*****************************************************************************/
package net.certiv.tools.indentguide;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Set;

Expand All @@ -24,7 +23,6 @@
import org.eclipse.jface.text.ITextViewerExtension2;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IStartup;
Expand All @@ -40,38 +38,65 @@
import org.eclipse.ui.texteditor.AbstractTextEditor;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.IDocumentProviderExtension4;
import org.eclipse.ui.themes.IThemeManager;

import net.certiv.tools.indentguide.adaptors.PartAdaptor;
import net.certiv.tools.indentguide.adaptors.WindowAdaptor;
import net.certiv.tools.indentguide.painter.GuidePainter;
import net.certiv.tools.indentguide.preferences.Pref;
import net.certiv.tools.indentguide.util.MsgBuilder;
import net.certiv.tools.indentguide.util.Utils;
import net.certiv.tools.indentguide.util.Utils.Delta;

public class Starter implements IStartup {

private static final Class<?>[] CLS_TYPE = null;
private static final Object[] ARG_TYPE = null;

private static final String UNKNOWN = "Unknown"; // $NON-NLS-1$

private static final String ACTIVE_EDITOR = "getActiveEditor"; // $NON-NLS-1$
private static final String SOURCE_VIEWER = "getSourceViewer"; // $NON-NLS-1$

private IPreferenceStore store;
// excluded content types
private Set<String> excludedTypeIds;
private Set<String> excludedTypeIds; // excluded content types

// row=window; col=page/editor; val=painter
private HashMap<IWorkbenchPart, HashMap<ISourceViewer, IndentGuidePainter>> paintMap = new HashMap<>();
private HashMap<IWorkbenchPart, HashMap<ISourceViewer, GuidePainter>> paintMap = new HashMap<>();

private final IPropertyChangeListener propsListener = evt -> {
String prop = evt.getProperty();
Object old = evt.getOldValue();
Object cur = evt.getNewValue();

if (prop.equals(IThemeManager.CHANGE_CURRENT_THEME)) {
Activator.log("theme change '%s' [%s] => [%s]", prop, old, cur);
loadPaintPrefs();

} else if (prop.startsWith(Pref.KEY)) {
if (prop.equals(Pref.CONTENT_TYPES)) {
updateContentTypes();

Delta<String> delta = Utils.delta(Utils.undelimit((String) old), Utils.undelimit((String) cur));
MsgBuilder mb = new MsgBuilder("content type change [%s]", prop);
if (!delta.added.isEmpty()) mb.nl().indent("added [%s]", delta.added);
if (!delta.rmved.isEmpty()) mb.nl().indent("removed [%s]", delta.rmved);
Activator.log(mb.toString());

} else {
Activator.log("property change '%s' [%s] => [%s]", prop, old, cur);
}

loadPaintPrefs();
}
};

@Override
public void earlyStartup() {
UIJob job = new UIJob("Indent Guide Startup") {

@Override
public IStatus runInUIThread(IProgressMonitor monitor) {
PlatformUI.getWorkbench().getThemeManager().addPropertyChangeListener(propsListener);
store = Activator.getDefault().getPreferenceStore();
store.addPropertyChangeListener(new StoreWatcher());
store.addPropertyChangeListener(propsListener);
updateContentTypes();

IWorkbench workbench = PlatformUI.getWorkbench();
Expand Down Expand Up @@ -110,33 +135,34 @@ private void initWorkbenchWindow(IWorkbenchWindow window) {

private void installPainter(IEditorPart part, IWorkbenchPart window) {
if (!store.getBoolean(Pref.ENABLED)) return;
Activator.log("inspecting editor [%s]", name(part));

if (part instanceof AbstractTextEditor) {
AbstractTextEditor editor = (AbstractTextEditor) part;
Activator.log("inspecting editor [%s]", name(part));

if (!validType(editor)) return;

Class<?> cls = editor.getClass();
while (!cls.equals(AbstractTextEditor.class)) {
cls = cls.getSuperclass();
}

try {
Method method = cls.getDeclaredMethod(SOURCE_VIEWER, CLS_TYPE);
method.setAccessible(true);
ISourceViewer viewer = (ISourceViewer) method.invoke(editor, ARG_TYPE);
ISourceViewer viewer = Utils.invoke(cls, SOURCE_VIEWER);

if (viewer instanceof ITextViewerExtension2) {
HashMap<ISourceViewer, IndentGuidePainter> painters = paintMap.get(window);
HashMap<ISourceViewer, GuidePainter> painters = paintMap.get(window);
if (painters == null) painters = new HashMap<>();
if (!painters.containsKey(viewer)) {
IndentGuidePainter painter = new IndentGuidePainter(viewer);
GuidePainter painter = new GuidePainter(viewer);
painters.put(viewer, painter);

((ITextViewerExtension2) viewer).addPainter(painter);
Activator.log("painter installed");
}
paintMap.put(window, painters);
}
} catch (Exception e) {
} catch (Throwable e) {
Activator.log(e);
}
}
Expand All @@ -148,11 +174,9 @@ private IEditorPart activeEditor(MultiPageEditorPart mpe) {
}

try {
Method method = mpe.getClass().getDeclaredMethod(ACTIVE_EDITOR, CLS_TYPE);
method.setAccessible(true);
return (IEditorPart) method.invoke(mpe, ARG_TYPE);
return Utils.invoke(mpe, ACTIVE_EDITOR);

} catch (Exception e) {
} catch (Throwable e) {
Activator.log(e);
return null;
}
Expand Down Expand Up @@ -193,6 +217,15 @@ private String name(IWorkbenchPart part) {
return part.getClass().getName();
}

private void loadPaintPrefs() {
for (HashMap<ISourceViewer, GuidePainter> map : paintMap.values()) {
for (GuidePainter painter : map.values()) {
painter.loadPrefs();
painter.redrawAll();
}
}
}

private class WindowWatcher extends WindowAdaptor {

@Override
Expand Down Expand Up @@ -221,9 +254,9 @@ public void partOpened(IWorkbenchPartReference ref) {
public void partClosed(IWorkbenchPartReference ref) {
IWorkbenchPart part = ref.getPart(false);
if (part instanceof MultiPageEditorPart || part instanceof AbstractTextEditor) {
HashMap<ISourceViewer, IndentGuidePainter> painters = paintMap.remove(part);
HashMap<ISourceViewer, GuidePainter> painters = paintMap.remove(part);
if (painters != null) {
for (IndentGuidePainter painter : painters.values()) {
for (GuidePainter painter : painters.values()) {
painter.deactivate(true);
}
painters.clear();
Expand All @@ -245,26 +278,4 @@ public void pageChanged(PageChangedEvent event) {
}
}
}

private class StoreWatcher implements IPropertyChangeListener {

@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getProperty().startsWith(Pref.KEY)) {
String property = evt.getProperty();
Object old = evt.getOldValue();
Object now = evt.getNewValue();
if (property.equals(Pref.CONTENT_TYPES)) {
updateContentTypes();

Delta<String> delta = Utils.delta(Utils.undelimit((String) old), Utils.undelimit((String) now));
if (!delta.rmved.isEmpty()) Activator.log("property change '%s' removed %s", property, delta.rmved);
if (!delta.added.isEmpty()) Activator.log("property change '%s' added %s", property, delta.added);

} else {
Activator.log("property change '%s' [%s] => [%s]", property, old, now);
}
}
}
}
}
Loading

0 comments on commit 80492fa

Please sign in to comment.