Bug 573710: Adapt to enhanced HandlerCollection

Change-Id: I78ff7506207911ce6831ee02aadebbedab4a9072
diff --git a/ltk/org.eclipse.statet.ltk.ui/META-INF/MANIFEST.MF b/ltk/org.eclipse.statet.ltk.ui/META-INF/MANIFEST.MF
index 2a9041e..c7764f6 100644
--- a/ltk/org.eclipse.statet.ltk.ui/META-INF/MANIFEST.MF
+++ b/ltk/org.eclipse.statet.ltk.ui/META-INF/MANIFEST.MF
@@ -32,6 +32,7 @@
  org.eclipse.search;resolution:=optional
 Import-Package: com.ibm.icu.text;version="67.1.0",
  org.eclipse.statet.ecommons.collections;version="4.4.0",
+ org.eclipse.statet.ecommons.commands.core;version="4.4.0",
  org.eclipse.statet.ecommons.databinding;version="4.4.0",
  org.eclipse.statet.ecommons.io,
  org.eclipse.statet.ecommons.preferences,
@@ -46,6 +47,7 @@
  org.eclipse.statet.ecommons.text.ui,
  org.eclipse.statet.ecommons.text.ui.presentation,
  org.eclipse.statet.ecommons.text.ui.settings,
+ org.eclipse.statet.ecommons.ui.workbench.texteditor,
  org.eclipse.statet.ecommons.workbench.search.ui,
  org.eclipse.statet.ecommons.workbench.ui,
  org.eclipse.statet.ecommons.workbench.ui.util,
diff --git a/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/EnhStyledText.java b/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/EnhStyledText.java
new file mode 100644
index 0000000..16aabd0
--- /dev/null
+++ b/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/EnhStyledText.java
@@ -0,0 +1,107 @@
+/*=============================================================================#
+ # Copyright (c) 2021 Stephan Wahlbrink and others.
+ # 
+ # This program and the accompanying materials are made available under the
+ # terms of the Eclipse Public License 2.0 which is available at
+ # https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
+ # which is available at https://www.apache.org/licenses/LICENSE-2.0.
+ # 
+ # SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
+ # 
+ # Contributors:
+ #     Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
+ #=============================================================================*/
+
+package org.eclipse.statet.ltk.ui.sourceediting;
+
+import org.eclipse.swt.custom.StyledText;
+import org.eclipse.swt.graphics.Color;
+import org.eclipse.swt.widgets.Composite;
+
+import org.eclipse.statet.jcommons.lang.NonNullByDefault;
+import org.eclipse.statet.jcommons.lang.Nullable;
+
+
+@NonNullByDefault
+public class EnhStyledText extends StyledText {
+	
+	
+	private @Nullable Color customBackgroundColor;
+	private @Nullable Color customForegroundColor;
+	
+	private boolean inSetEnabled;
+	
+	
+	public EnhStyledText(final Composite parent, final int style) {
+		super(parent, style);
+	}
+	
+	
+	@Override
+	public void setEnabled(final boolean enabled) {
+		this.inSetEnabled= true;
+		try {
+			if (enabled) {
+				super.setEnabled(true);
+				applyCustomBackgroundColor();
+				applyCustomForegroundColor();
+			}
+			else {
+				super.setBackground(null);
+				super.setForeground(null);
+				super.setEnabled(false);
+			}
+		}
+		finally {
+			this.inSetEnabled= false;
+		}
+	}
+	
+	@Override
+	public void setBackground(final @Nullable Color color) {
+		if (this.inSetEnabled) {
+			super.setBackground(color);
+		}
+		else {
+			this.customBackgroundColor= color;
+			if (isEnabled()) {
+				applyCustomBackgroundColor();
+			}
+		}
+	}
+	
+	private void applyCustomBackgroundColor() {
+		super.setBackground(this.customBackgroundColor);
+		if (this.customBackgroundColor == null) {
+			super.setBackground(getBackground());
+		}
+	}
+	
+	@Override
+	public void setForeground(final @Nullable Color color) {
+		if (this.inSetEnabled) {
+			super.setForeground(color);
+		}
+		else {
+			this.customForegroundColor= color;
+			if (isEnabled()) {
+				applyCustomForegroundColor();
+			}
+		}
+	}
+	
+	private void applyCustomForegroundColor() {
+		super.setForeground(this.customForegroundColor);
+		if (this.customForegroundColor == null) {
+			super.setForeground(getForeground());
+		}
+	}
+	
+	
+	public static EnhStyledText forSourceEditor(final Composite parent, final int styles) {
+		final var styledText= new EnhStyledText(parent, styles);
+		styledText.setLeftMargin(Math.max(styledText.getLeftMargin(), 2));
+		return styledText;
+	}
+	
+}
diff --git a/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/SnippetEditor.java b/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/SnippetEditor.java
index f2dfb00..8fed8ad 100644
--- a/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/SnippetEditor.java
+++ b/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/SnippetEditor.java
@@ -26,7 +26,6 @@
 import org.eclipse.jface.action.IMenuManager;
 import org.eclipse.jface.action.MenuManager;
 import org.eclipse.jface.action.Separator;
-import org.eclipse.jface.commands.ActionHandler;
 import org.eclipse.jface.text.Document;
 import org.eclipse.jface.text.DocumentEvent;
 import org.eclipse.jface.text.IDocumentListener;
@@ -36,7 +35,6 @@
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.custom.StyledText;
 import org.eclipse.swt.events.DisposeEvent;
-import org.eclipse.swt.graphics.Color;
 import org.eclipse.swt.layout.GridData;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Display;
@@ -48,15 +46,18 @@
 import org.eclipse.statet.jcommons.lang.NonNullByDefault;
 import org.eclipse.statet.jcommons.lang.Nullable;
 
+import org.eclipse.statet.ecommons.commands.core.BasicHandlerCollection;
+import org.eclipse.statet.ecommons.commands.core.HandlerCollection;
 import org.eclipse.statet.ecommons.preferences.ui.SettingsUpdater;
 import org.eclipse.statet.ecommons.text.ui.TextViewerAction;
 import org.eclipse.statet.ecommons.text.ui.TextViewerEditorColorUpdater;
 import org.eclipse.statet.ecommons.text.ui.TextViewerJFaceUpdater;
 import org.eclipse.statet.ecommons.ui.actions.ControlServicesUtil;
-import org.eclipse.statet.ecommons.ui.actions.HandlerCollection;
 import org.eclipse.statet.ecommons.ui.components.WidgetToolsButton;
 import org.eclipse.statet.ecommons.ui.util.LayoutUtils;
 import org.eclipse.statet.ecommons.ui.util.UIAccess;
+import org.eclipse.statet.ecommons.ui.workbench.ContextHandlers;
+import org.eclipse.statet.ecommons.ui.workbench.texteditor.ActionHandler;
 
 
 /**
@@ -69,39 +70,6 @@
 	public static final int DEFAULT_MULTI_LINE_STYLE= SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL | SWT.MULTI | SWT.LEFT_TO_RIGHT;
 	
 	
-	private static class ExtStyledText extends StyledText {
-		
-		
-		private @Nullable Color savedColor;
-		
-		
-		public ExtStyledText(final Composite parent, final int style) {
-			super(parent, style);
-		}
-		
-		
-		@Override
-		public void setBackground(final @Nullable Color color) {
-			this.savedColor= color;
-			if (isEnabled()) {
-				super.setBackground(color);
-			}
-		}
-		
-		@Override
-		public void setEnabled(final boolean enabled) {
-			super.setEnabled(enabled);
-			if (enabled) {
-				super.setBackground(this.savedColor);
-			}
-			else {
-				super.setBackground(getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND));
-			}
-		}
-		
-	}
-	
-	
 	private class Updater implements ISelectionChangedListener, IDocumentListener, Runnable {
 		
 		private boolean actionUpdateScheduled= false;
@@ -156,9 +124,8 @@
 	private Map<String, Action> globalActions;
 	private Updater updater;
 	
-	private final HandlerCollection handlers;
 	private final @Nullable IServiceLocator serviceLocator;
-	private ControlServicesUtil serviceUtil;
+	private final HandlerCollection handlers;
 	
 	
 	/**
@@ -170,8 +137,10 @@
 		this.configurator= configurator;
 		this.document= (initialContent != null) ? new Document(initialContent) : new Document();
 		this.configurator.getDocumentSetupParticipant().setup(this.document);
-		this.handlers= new HandlerCollection();
+		
 		this.serviceLocator= serviceParent;
+		this.handlers= (serviceParent != null) ?
+				new ContextHandlers(serviceParent) : new BasicHandlerCollection();
 		this.withToolButton= withToolButton;
 	}
 	
@@ -236,9 +205,7 @@
 			
 			@Override
 			protected StyledText createTextWidget(final Composite parent, final int styles) {
-				final StyledText styledText= new ExtStyledText(parent, styles);
-				styledText.setLeftMargin(Math.max(styledText.getLeftMargin(), 2));
-				return styledText;
+				return EnhStyledText.forSourceEditor(parent, styles);
 			}
 			
 		};
@@ -261,9 +228,11 @@
 	
 	
 	public void registerCommandHandler(final String commandId, final IHandler2 handler) {
-		this.handlers.add(commandId, handler);
-		if (this.serviceUtil != null) {
-			this.serviceUtil.activateHandler(commandId, handler);
+		if (this.handlers instanceof ContextHandlers) {
+			((ContextHandlers)this.handlers).addActivate(commandId, handler);
+		}
+		else {
+			this.handlers.add(commandId, handler);
 		}
 	}
 	
@@ -274,25 +243,28 @@
 	public void addAction(final Action action) {
 		this.globalActions.put(action.getId(), action);
 		final String commandId= action.getActionDefinitionId();
-		if (this.serviceUtil != null && commandId != null) {
-			this.serviceUtil.activateHandler(commandId, new ActionHandler(action));
+		if (this.handlers instanceof ContextHandlers && commandId != null) {
+			((ContextHandlers)this.handlers).addActivate(commandId, new ActionHandler(action));
 		}
 	}
 	
-	public Action getAction(final String id) {
+	public @Nullable Action getAction(final String id) {
 		return this.globalActions.get(id);
 	}
 	
 	protected void initActions() {
 		this.globalActions= new HashMap<>(10);
 		
-		if (this.serviceLocator != null) {
-			this.serviceUtil= new ControlServicesUtil(this.serviceLocator,
+		if (this.handlers instanceof ContextHandlers) {
+			final var contextHandlers= (ContextHandlers)this.handlers;
+			final var serviceUtil= new ControlServicesUtil(this.serviceLocator,
 					getClass().getName() + '#'+hashCode(), getSourceViewer().getControl() );
-			this.serviceUtil.addControl(getSourceViewer().getControl());
+			serviceUtil.addControl(getSourceViewer().getControl());
 			if (this.toolButton != null) {
-				this.serviceUtil.addControl(this.toolButton.getButton());
+				serviceUtil.addControl(this.toolButton.getButton());
 			}
+			contextHandlers.setDefaultActivationExpression(serviceUtil.getExpression());
+			contextHandlers.setDeactivateOnDisposal(serviceUtil.getRequireDeactivation());
 		}
 		
 		// default actions
diff --git a/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/SnippetEditor1.java b/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/SnippetEditor1.java
index c49ca3f..bbbb99d 100644
--- a/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/SnippetEditor1.java
+++ b/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/SnippetEditor1.java
@@ -25,8 +25,8 @@
 import org.eclipse.ui.menus.CommandContributionItemParameter;
 import org.eclipse.ui.services.IServiceLocator;
 
+import org.eclipse.statet.ecommons.commands.core.HandlerCollection;
 import org.eclipse.statet.ecommons.preferences.core.Preference.BooleanPref;
-import org.eclipse.statet.ecommons.ui.actions.HandlerCollection;
 import org.eclipse.statet.ecommons.ui.actions.HandlerContributionItem;
 
 import org.eclipse.statet.internal.ltk.ui.EditingMessages;
diff --git a/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/SourceEditor1OutlinePage.java b/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/SourceEditor1OutlinePage.java
index 3048d2c..4bcff23 100644
--- a/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/SourceEditor1OutlinePage.java
+++ b/ltk/org.eclipse.statet.ltk.ui/src/org/eclipse/statet/ltk/ui/sourceediting/SourceEditor1OutlinePage.java
@@ -50,8 +50,8 @@
 import org.eclipse.statet.jcommons.lang.NonNullByDefault;
 import org.eclipse.statet.jcommons.lang.Nullable;
 
+import org.eclipse.statet.ecommons.commands.core.HandlerCollection;
 import org.eclipse.statet.ecommons.ui.SharedUIResources;
-import org.eclipse.statet.ecommons.ui.actions.HandlerCollection;
 import org.eclipse.statet.ecommons.ui.util.UIAccess;
 import org.eclipse.statet.ecommons.ui.workbench.BasicEditorOutlinePage;
 import org.eclipse.statet.ecommons.ui.workbench.ContextHandlers;