浏览代码

Add guards for focus lost with context menu on gtk (#2029)

* Add guards for focus lost with context menu on gtk

* Fix d61b269
Rene Beckmann 7 年之前
父节点
当前提交
eafbf93ea2

+ 45 - 13
plugins/org.yakindu.base.xtext.utils.jface/src/org/yakindu/base/xtext/utils/jface/viewers/StyledTextXtextAdapter.java

@@ -1,9 +1,9 @@
-/** 
- * Copyright (c) 2015 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 
+/**
+ * Copyright (c) 2015 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
  *
@@ -64,11 +64,11 @@ import com.google.inject.Injector;
 import com.google.inject.Provider;
 
 /**
- * 
+ *
  * @author andreas.muelder@itemis.de
  * @author alexander.nyssen@itemis.de
  * @author patrick.koenemann@itemis.de
- * 
+ *
  */
 @SuppressWarnings("restriction")
 public class StyledTextXtextAdapter {
@@ -151,6 +151,7 @@ public class StyledTextXtextAdapter {
 			// when using the StyledText.VerifyKey event (3005), we get the
 			// event early enough!
 			styledText.addListener(3005, new Listener() {
+				@Override
 				public void handleEvent(Event event) {
 					if (event.character == SWT.CR && !completionProposalAdapter.isProposalPopupOpen()) {
 						Event selectionEvent = new Event();
@@ -185,7 +186,7 @@ public class StyledTextXtextAdapter {
 			XtextStyledTextSelectionProvider xtextStyledTextSelectionProvider = new XtextStyledTextSelectionProvider(
 					this.styledText, getFakeResourceContext().getFakeResource());
 			ChangeSelectionProviderOnFocusGain listener = new ChangeSelectionProviderOnFocusGain(site,
-					xtextStyledTextSelectionProvider);
+					xtextStyledTextSelectionProvider, styledText);
 			getStyledText().addFocusListener(listener);
 			getStyledText().addDisposeListener(listener);
 		} catch (NullPointerException ex) {
@@ -204,6 +205,7 @@ public class StyledTextXtextAdapter {
 		result.setDescriptionText("Content Assist Available (CTRL + Space)");
 		result.setMarginWidth(2);
 		styledText.addDisposeListener(new DisposeListener() {
+			@Override
 			public void widgetDisposed(DisposeEvent e) {
 				if (getDecoration() != null) {
 					getDecoration().dispose();
@@ -238,9 +240,10 @@ public class StyledTextXtextAdapter {
 	}
 
 	/**
-	 * Creates decoration support for the sourceViewer. code is entirely copied from
-	 * {@link XtextEditor} and its super class {@link AbstractDecoratedTextEditor}.
-	 * 
+	 * Creates decoration support for the sourceViewer. code is entirely copied
+	 * from {@link XtextEditor} and its super class
+	 * {@link AbstractDecoratedTextEditor}.
+	 *
 	 */
 	protected void configureSourceViewerDecorationSupport(SourceViewerDecorationSupport support) {
 		MarkerAnnotationPreferences annotationPreferences = new MarkerAnnotationPreferences();
@@ -310,6 +313,7 @@ public class StyledTextXtextAdapter {
 	public IParseResult getXtextParseResult() {
 		return getXtextDocument().readOnly(new IUnitOfWork<IParseResult, XtextResource>() {
 
+			@Override
 			public IParseResult exec(XtextResource state) throws Exception {
 				return state.getParseResult();
 			}
@@ -386,24 +390,52 @@ public class StyledTextXtextAdapter {
 		protected ISelectionProvider selectionProviderOnFocusGain;
 		protected ISelectionProvider selectionProviderOnFocusLost;
 		protected IWorkbenchPartSite site;
+		private boolean ignoreNextFocusLost = false;
+		private StyledText text;
 
 		public ChangeSelectionProviderOnFocusGain(IWorkbenchPartSite site,
-				ISelectionProvider selectionProviderOnFocusGain) {
+				ISelectionProvider selectionProviderOnFocusGain, StyledText text) {
 			this.selectionProviderOnFocusGain = selectionProviderOnFocusGain;
 			this.site = site;
+			this.text = text;
 		}
 
+		@Override
 		public void focusLost(FocusEvent e) {
+			if (SWT.getPlatform().equals("gtk")) {
+				if (isIgnoreNextFocusLost()) {
+					setIgnoreNextFocusLost(false);
+					return;
+				}
+				if (text.getMenu().isVisible()) {
+					setIgnoreNextFocusLost(true);
+					return;
+				}
+			}
+
 			if (this.selectionProviderOnFocusLost != null) {
 				this.site.setSelectionProvider(this.selectionProviderOnFocusLost);
 			}
+
+			text.setSelection(0);
+		}
+
+		protected void setIgnoreNextFocusLost(boolean b) {
+			this.ignoreNextFocusLost = b;
+
+		}
+
+		protected boolean isIgnoreNextFocusLost() {
+			return ignoreNextFocusLost;
 		}
 
+		@Override
 		public void focusGained(FocusEvent e) {
 			this.selectionProviderOnFocusLost = this.site.getSelectionProvider();
 			this.site.setSelectionProvider(this.selectionProviderOnFocusGain);
 		}
 
+		@Override
 		public void widgetDisposed(DisposeEvent e) {
 			if (this.selectionProviderOnFocusLost != null) {
 				this.site.setSelectionProvider(this.selectionProviderOnFocusLost);

+ 18 - 12
plugins/org.yakindu.base.xtext.utils.jface/src/org/yakindu/base/xtext/utils/jface/viewers/XtextStyledTextCellEditor.java

@@ -4,10 +4,10 @@
  * 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:
  * 	itemis AG - initial API and implementation
- * 
+ *
  */
 package org.yakindu.base.xtext.utils.jface.viewers;
 
@@ -43,12 +43,12 @@ import com.google.inject.Injector;
  * This class integrates Xtext features into a {@link CellEditor} and can be
  * used e.g. in jFace {@link StructuredViewer}s or in GMF EditParts via
  * DirectEditManager.
- * 
+ *
  * The current implementation supports, code completion, syntax highlighting and
  * validation
- * 
+ *
  * @see XtextStyledTextProvider
- * 
+ *
  * @author andreas.muelder@itemis.de
  * @author alexander.nyssen@itemis.de
  * @author patrick.koenemann@itemis.de
@@ -80,6 +80,7 @@ public class XtextStyledTextCellEditor extends StyledTextCellEditor {
 	protected Control createControl(Composite parent) {
 		StyledText styledText = (StyledText) super.createControl(parent);
 		styledText.addFocusListener(new FocusAdapter() {
+			@Override
 			public void focusLost(FocusEvent e) {
 				XtextStyledTextCellEditor.this.focusLost();
 			}
@@ -97,6 +98,7 @@ public class XtextStyledTextCellEditor extends StyledTextCellEditor {
 		// This listener notifies the modification, when text is selected via
 		// proposal. A ModifyEvent is not thrown by the StyledText in this case.
 		getXtextAdapter().getXtextSourceviewer().addTextListener(new ITextListener() {
+			@Override
 			public void textChanged(TextEvent event) {
 				editOccured(null);
 			}
@@ -108,6 +110,7 @@ public class XtextStyledTextCellEditor extends StyledTextCellEditor {
 			// when using the StyledText.VerifyKey event (3005), we get the
 			// event early enough!
 			styledText.addListener(3005, new Listener() {
+				@Override
 				public void handleEvent(Event event) {
 					if (event.character == SWT.CR && !getCompletionProposalAdapter().isProposalPopupOpen()) {
 						focusLost();
@@ -116,6 +119,7 @@ public class XtextStyledTextCellEditor extends StyledTextCellEditor {
 			});
 		}
 		styledText.addListener(3005, new Listener() {
+			@Override
 			public void handleEvent(Event event) {
 				if (event.character == '\u001b' // ESC
 						&& !getCompletionProposalAdapter().isProposalPopupOpen()) {
@@ -142,6 +146,7 @@ public class XtextStyledTextCellEditor extends StyledTextCellEditor {
 		return new FilteringMenuManager();
 	}
 
+	@Override
 	protected void keyReleaseOccured(KeyEvent keyEvent) {
 		if (keyEvent.character == '\u001b') { // ESC
 			return;
@@ -198,8 +203,7 @@ public class XtextStyledTextCellEditor extends StyledTextCellEditor {
 				setIgnoreNextFocusLost(false);
 				return;
 			}
-
-			if (getCompletionProposalAdapter().isProposalPopupOpen()) {
+			if (getCompletionProposalAdapter().isProposalPopupOpen() || text.getMenu().isVisible()) {
 				setIgnoreNextFocusLost(true);
 				return;
 			}
@@ -228,7 +232,7 @@ public class XtextStyledTextCellEditor extends StyledTextCellEditor {
 	public void setVisibleRegion(int start, int length) {
 		getXtextAdapter().setVisibleRegion(start, length);
 	}
-	
+
 	public StyledTextXtextAdapter getXtextAdapter() {
 		return this.xtextAdapter;
 	}
@@ -241,14 +245,16 @@ public class XtextStyledTextCellEditor extends StyledTextCellEditor {
 		return this.contextFakeResourceProvider;
 	}
 
-	protected CompletionProposalAdapter createCompletionProposalAdapter(StyledText styledText, final IContentAssistant contentAssistant) {
-		return new CompletionProposalAdapter(styledText, contentAssistant, KeyStroke.getInstance(
-				SWT.CTRL, SWT.SPACE), null);
+	protected CompletionProposalAdapter createCompletionProposalAdapter(StyledText styledText,
+			final IContentAssistant contentAssistant) {
+		return new CompletionProposalAdapter(styledText, contentAssistant, KeyStroke.getInstance(SWT.CTRL, SWT.SPACE),
+				null);
 	}
 
 	protected StyledTextXtextAdapter createXtextAdapter() {
 		return new StyledTextXtextAdapter(this.getInjector(),
-				getContextFakeResourceProvider() == null ? IXtextFakeContextResourcesProvider.NULL_CONTEXT_PROVIDER
+				getContextFakeResourceProvider() == null
+						? IXtextFakeContextResourcesProvider.NULL_CONTEXT_PROVIDER
 						: getContextFakeResourceProvider());
 	}
 

+ 19 - 15
plugins/org.yakindu.sct.refactoring/src/org/yakindu/sct/refactoring/refactor/AbstractRefactoring.java

@@ -6,7 +6,7 @@
  * http://www.eclipse.org/legal/epl-v10.html
  * Contributors:
  * 	committers of YAKINDU - initial API and implementation
- * 
+ *
  */
 package org.yakindu.sct.refactoring.refactor;
 
@@ -35,9 +35,9 @@ import org.yakindu.sct.refactoring.utils.RefactoringHelper;
  * Base abstract class for all refactorings providing some default
  * implementations. Concrete subclasses are responsible for implementing the
  * refactoring logic which is applied on the refactoring's context objects.
- * 
+ *
  * @author thomas kutz - Initial contribution and API
- * 
+ *
  */
 public abstract class AbstractRefactoring<T extends Object> implements IRefactoring<T> {
 
@@ -61,6 +61,7 @@ public abstract class AbstractRefactoring<T extends Object> implements IRefactor
 	 */
 	protected RefactoringHelper helper = new RefactoringHelper();
 
+	@Override
 	public boolean isExecutable() {
 		return getContextObjects() != null && getContextObjects().size() > 0;
 	}
@@ -68,7 +69,7 @@ public abstract class AbstractRefactoring<T extends Object> implements IRefactor
 	/**
 	 * Returns the resource which is affected by this refactoring. Default
 	 * implementation returns the resource of the first context object.
-	 * 
+	 *
 	 * @return resource
 	 */
 	protected Resource getResource() {
@@ -83,6 +84,7 @@ public abstract class AbstractRefactoring<T extends Object> implements IRefactor
 	 * Wraps an {@link AbstractTransactionalCommand} around the refactoring
 	 * logic.
 	 */
+	@Override
 	public void execute() {
 		if (!isExecutable()) {
 			return;
@@ -120,10 +122,11 @@ public abstract class AbstractRefactoring<T extends Object> implements IRefactor
 	protected boolean internalDoUndo() {
 		return true;
 	}
-	
-    /**
-     * Combines the internal command execution call with the parsing and linking of the specification elements.
-     */
+
+	/**
+	 * Combines the internal command execution call with the parsing and linking
+	 * of the specification elements.
+	 */
 	protected void executeRefactoring() {
 		internalExecute();
 		parseAndLinkSpecificationElements();
@@ -134,7 +137,8 @@ public abstract class AbstractRefactoring<T extends Object> implements IRefactor
 	protected abstract void internalExecute();
 
 	/**
-	 * Parses and links the specification elements of {@link AbstractRefactoring#getResource()}
+	 * Parses and links the specification elements of
+	 * {@link AbstractRefactoring#getResource()}
 	 */
 	protected void parseAndLinkSpecificationElements() {
 		Resource resource = getResource();
@@ -143,10 +147,10 @@ public abstract class AbstractRefactoring<T extends Object> implements IRefactor
 			((AbstractSCTResource) resource).linkSpecificationElements();
 		}
 	}
-	
+
 	/**
 	 * Getter for the editing domain of the resource used in this refactoring.
-	 * 
+	 *
 	 * @return editing domain
 	 */
 	protected TransactionalEditingDomain getEditingDomain() {
@@ -155,7 +159,7 @@ public abstract class AbstractRefactoring<T extends Object> implements IRefactor
 
 	/**
 	 * Returns a list of files affected by this refactoring.
-	 * 
+	 *
 	 * @return list of affected files
 	 */
 	protected List<?> getAffectedFiles() {
@@ -164,7 +168,7 @@ public abstract class AbstractRefactoring<T extends Object> implements IRefactor
 
 	/**
 	 * Returns the label of this refactoring's command.
-	 * 
+	 *
 	 * @return command label
 	 */
 	protected String getCommandLabel() {
@@ -174,7 +178,7 @@ public abstract class AbstractRefactoring<T extends Object> implements IRefactor
 	/**
 	 * Executes the specified command and handles enabling and disabling of the
 	 * resource's serializer.
-	 * 
+	 *
 	 * @param command
 	 *            the command to execute
 	 * @param resource
@@ -206,7 +210,7 @@ public abstract class AbstractRefactoring<T extends Object> implements IRefactor
 			}
 		}
 	}
-	
+
 	protected String asIdentifier(String string) {
 		return string.replaceAll("[^a-z&&[^A-Z&&[^0-9]]]", "_");
 	}

+ 8 - 6
plugins/org.yakindu.sct.ui.editor/src/org/yakindu/sct/ui/editor/definitionsection/StyledTextSelectionListener.java

@@ -6,7 +6,7 @@
  * http://www.eclipse.org/legal/epl-v10.html
  * Contributors:
  *     committers of YAKINDU - initial API and implementation
- * 
+ *
  */
 package org.yakindu.sct.ui.editor.definitionsection;
 
@@ -30,12 +30,12 @@ import org.yakindu.sct.ui.editor.editor.StatechartDiagramActionbarContributor;
  * be able to release selection of previous selection provider. This in needed
  * to be able to perform keyboard shortcuts on the text in the statechart
  * definition section without interacting with the elements of the diagram.
- * 
+ *
  * This class does also hook the default clipboard actions Copy, Cut, Paste and
  * Select All for text widgets to a <code>StyledText</code> widget, which gets a
  * focus. The hook is for retargeting global actions, which are mapped to
  * diagram actions by default.
- * 
+ *
  * @author robert rudi - Initial contribution and API
  */
 public class StyledTextSelectionListener extends StyledTextXtextAdapter.ChangeSelectionProviderOnFocusGain {
@@ -55,7 +55,7 @@ public class StyledTextSelectionListener extends StyledTextXtextAdapter.ChangeSe
 
 	public StyledTextSelectionListener(IWorkbenchPartSite site, StyledText widget,
 			ISelectionProvider selectionProviderOnFocusGain) {
-		super(site, selectionProviderOnFocusGain);
+		super(site, selectionProviderOnFocusGain, widget);
 		contextService = site.getService(IContextService.class);
 		site.setSelectionProvider(selectionProviderOnFocusGain);
 		widget.addFocusListener(this);
@@ -78,10 +78,12 @@ public class StyledTextSelectionListener extends StyledTextXtextAdapter.ChangeSe
 	}
 
 	protected void redefineParentContext(String childContext, String parentContext) {
-		if (embeddedEditorCtx != null) { // deactivates the context on focus lost
+		if (embeddedEditorCtx != null) { // deactivates the context on focus
+											// lost
 			contextService.deactivateContext(embeddedEditorCtx);
 			embeddedEditorCtx = null;
-		} else { // redefines the parent of the child context, to avoid keybinding conflicts
+		} 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);