Просмотр исходного кода

Extracted scope handling to own class ContextScopeHandler. Removed parentId of embeddedEditorScope context. Defining editor context on startup (#2036)

Robert Rudi 7 лет назад
Родитель
Сommit
2485520df8

+ 1 - 2
plugins/org.yakindu.sct.ui.editor/plugin.xml

@@ -929,8 +929,7 @@
     <context
           description="Embedded Xtext Editor Scope"
           id="org.eclipse.xtext.ui.embeddedTextEditorScope"
-          name="Embedded Xtext Editor Scope"
-          parentId="org.eclipse.ui.contexts.dialogAndWindow">
+          name="Embedded Xtext Editor Scope">
     </context>
  </extension>
  <extension

+ 57 - 0
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/definitionsection/ContextScopeHandler.java

@@ -0,0 +1,57 @@
+/**
+ * Copyright (c) 2018 committers of YAKINDU and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ * Contributors:
+ * 	committers of YAKINDU - initial API and implementation
+ * 
+ */
+package org.yakindu.sct.ui.editor.definitionsection;
+
+import org.eclipse.core.commands.contexts.Context;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.contexts.IContextActivation;
+import org.eclipse.ui.contexts.IContextService;
+
+/**
+ * 
+ * @author robert rudi - Initial contribution and API
+ *
+ */
+public abstract class ContextScopeHandler {
+
+	protected static final String SCOPE_DESCRIPTION = "Editor scope";
+	protected static final String DIALOG_AND_WINDOW_SCOPE = "org.eclipse.ui.contexts.dialogAndWindow";
+	protected static final String EMBEDDED_TEXT_EDITOR_SCOPE = "org.eclipse.xtext.ui.embeddedTextEditorScope";
+	protected static final String TEXT_EDITOR_SCOPE = "org.eclipse.ui.textEditorScope";
+
+	protected static final IContextService contextService = PlatformUI.getWorkbench().getService(IContextService.class);
+
+	/**
+	 * Sets a new parent context for the given child context with optional scope
+	 * description
+	 */
+	public static String defineContext(String childCtxId, String parentCtxId) {
+		Context childCtx = getContext(childCtxId);
+		if (childCtx != null) {
+			childCtx.define(childCtx.getId(), SCOPE_DESCRIPTION, parentCtxId);
+			return childCtxId;
+		}
+		return null;
+	}
+
+	private static Context getContext(String contextId) {
+		return contextService.getContext(contextId);
+	}
+
+	public static IContextActivation activateContext(String context) {
+		return contextService.activateContext(context);
+	}
+
+	public static void deactivateContext(IContextActivation context) {
+		contextService.deactivateContext(context);
+	}
+
+}

+ 4 - 0
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/definitionsection/StatechartDefinitionSection.java

@@ -10,6 +10,9 @@
  */
 package org.yakindu.sct.ui.editor.definitionsection;
 
+import static org.yakindu.sct.ui.editor.definitionsection.ContextScopeHandler.EMBEDDED_TEXT_EDITOR_SCOPE;
+import static org.yakindu.sct.ui.editor.definitionsection.ContextScopeHandler.TEXT_EDITOR_SCOPE;
+
 import java.util.List;
 
 import org.eclipse.core.databinding.observable.value.IObservableValue;
@@ -227,6 +230,7 @@ public class StatechartDefinitionSection extends Composite
 	}
 
 	protected EmbeddedEditor createSpecificationEditor() {
+		ContextScopeHandler.defineContext(EMBEDDED_TEXT_EDITOR_SCOPE, TEXT_EDITOR_SCOPE);
 		EmbeddedEditor embeddedEditor = createEmbeddedEditor();
 		embeddedEditor.createPartialEditor();
 		GridDataFactory.fillDefaults().grab(true, true).span(2, 1).applyTo(embeddedEditor.getViewer().getControl());

+ 11 - 19
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/definitionsection/StyledTextSelectionListener.java

@@ -10,7 +10,10 @@
  */
 package org.yakindu.sct.ui.editor.definitionsection;
 
-import org.eclipse.core.commands.contexts.Context;
+import static org.yakindu.sct.ui.editor.definitionsection.ContextScopeHandler.DIALOG_AND_WINDOW_SCOPE;
+import static org.yakindu.sct.ui.editor.definitionsection.ContextScopeHandler.EMBEDDED_TEXT_EDITOR_SCOPE;
+import static org.yakindu.sct.ui.editor.definitionsection.ContextScopeHandler.TEXT_EDITOR_SCOPE;
+
 import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.viewers.ISelectionProvider;
 import org.eclipse.jface.viewers.StructuredSelection;
@@ -21,7 +24,6 @@ import org.eclipse.ui.IEditorSite;
 import org.eclipse.ui.IWorkbenchPartSite;
 import org.eclipse.ui.actions.ActionFactory;
 import org.eclipse.ui.contexts.IContextActivation;
-import org.eclipse.ui.contexts.IContextService;
 import org.yakindu.base.xtext.utils.jface.viewers.StyledTextXtextAdapter;
 import org.yakindu.sct.ui.editor.editor.StatechartDiagramActionbarContributor;
 
@@ -40,10 +42,6 @@ import org.yakindu.sct.ui.editor.editor.StatechartDiagramActionbarContributor;
  */
 public class StyledTextSelectionListener extends StyledTextXtextAdapter.ChangeSelectionProviderOnFocusGain {
 
-	protected static final String CHILD_CONTEXT_SCOPE_DESCRIPTION = "Embedded text editor scope";
-	protected static final String DIALOG_AND_WINDOW_SCOPE = "org.eclipse.ui.contexts.dialogAndWindow";
-	protected static final String EMBEDDED_TEXT_EDITOR_SCOPE = "org.eclipse.xtext.ui.embeddedTextEditorScope";
-	protected static final String TEXT_EDITOR_SCOPE = "org.eclipse.ui.textEditorScope";
 	private IAction copyAction;
 	private IAction cutAction;
 	private IAction pasteAction;
@@ -51,12 +49,10 @@ public class StyledTextSelectionListener extends StyledTextXtextAdapter.ChangeSe
 	private IAction printAction;
 	private StyledTextActionHandler actionHandler;
 	private IContextActivation embeddedEditorCtx;
-	private IContextService contextService;
 
 	public StyledTextSelectionListener(IWorkbenchPartSite site, StyledText widget,
 			ISelectionProvider selectionProviderOnFocusGain) {
 		super(site, selectionProviderOnFocusGain, widget);
-		contextService = site.getService(IContextService.class);
 		site.setSelectionProvider(selectionProviderOnFocusGain);
 		widget.addFocusListener(this);
 		widget.addDisposeListener(this);
@@ -77,18 +73,14 @@ public class StyledTextSelectionListener extends StyledTextXtextAdapter.ChangeSe
 		super.focusGained(e);
 	}
 
-	protected void redefineParentContext(String childContext, String parentContext) {
-		if (embeddedEditorCtx != null) { // deactivates the context on focus
-											// lost
-			contextService.deactivateContext(embeddedEditorCtx);
+	protected void redefineParentContext(String childCtx, String parentCtx) {
+		if (embeddedEditorCtx != null) { // deactivates the context on focus lost
+			ContextScopeHandler.deactivateContext(embeddedEditorCtx);
 			embeddedEditorCtx = null;
-		} else { // redefines the parent of the child context, to avoid
-					// keybinding conflicts
-			final Context childCtx = contextService.getContext(childContext);
-			if (childCtx != null) {
-				childCtx.define(childContext, CHILD_CONTEXT_SCOPE_DESCRIPTION, parentContext);
-				embeddedEditorCtx = contextService.activateContext(childContext);
-			}
+		} else { // redefines child context, to avoid keybinding conflicts
+			ContextScopeHandler.defineContext(childCtx, parentCtx);
+			embeddedEditorCtx = ContextScopeHandler.activateContext(childCtx);
+
 		}
 	}