Bug 8519 [Macros] - Removing dependencies on E3 APIs to get editor.
Change-Id: I982da1975a8f2ed04f9cb771ddc096685563bb79
Signed-off-by: Fabio Zadrozny <fabiofz@gmail.com>
diff --git a/bundles/org.eclipse.e4.ui.macros/src/org/eclipse/e4/ui/macros/internal/EditorUtils.java b/bundles/org.eclipse.e4.ui.macros/src/org/eclipse/e4/ui/macros/internal/EditorUtils.java
index 802dbb3..fa3664d 100644
--- a/bundles/org.eclipse.e4.ui.macros/src/org/eclipse/e4/ui/macros/internal/EditorUtils.java
+++ b/bundles/org.eclipse.e4.ui.macros/src/org/eclipse/e4/ui/macros/internal/EditorUtils.java
@@ -10,14 +10,12 @@
*******************************************************************************/
package org.eclipse.e4.ui.macros.internal;
+import org.eclipse.e4.core.contexts.IEclipseContext;
import org.eclipse.e4.core.macros.IMacroContext;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IEditorPart;
-import org.eclipse.ui.IWorkbench;
-import org.eclipse.ui.IWorkbenchPage;
-import org.eclipse.ui.IWorkbenchWindow;
-import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.ISources;
/**
* Utilities related to getting/storing the current editor from/to the macro
@@ -38,19 +36,20 @@
private final static String TARGET_EDITOR_PART = "TARGET_EDITOR_PART"; //$NON-NLS-1$
/**
- * Provides the styled text which is active from the current editor or null if
- * it is not available.
+ * Provides the {@link StyledText} from the passed editor or {@code null} if not
+ * available.
*
- * @return the StyledText related to the current editor or null if there is no
- * such widget available (i.e.: if the current editor is not a text
- * editor or if there is no open editor).
+ * @param editor
+ * the editor from where the {@link StyledText} should be gotten.
+ *
+ * @return the {@link StyledText} related to the current editor or null if it is
+ * not available (i.e.: if the editor passed is not a text editor).
*/
- public static StyledText getActiveEditorStyledText() {
- IEditorPart activeEditor = getActiveEditor();
- if (activeEditor == null) {
+ public static StyledText getActiveEditorStyledText(IEditorPart editor) {
+ if (editor == null) {
return null;
}
- Control control = activeEditor.getAdapter(Control.class);
+ Control control = editor.getAdapter(Control.class);
StyledText styledText = null;
if (control instanceof StyledText) {
styledText = (StyledText) control;
@@ -59,50 +58,38 @@
}
/**
- * Provides a way to get the editor part which is currently active or null if
- * there's no current editor part.
+ * Provides the {@link StyledText} which is currently active in the given
+ * eclipse context or {@code null} if not available.
*
- * @return the active editor part.
+ * @param eclipseContext
+ * the context to get the active editor from (from where the
+ * {@link StyledText} will be gotten).
+ * @return the {@link StyledText} from the editor which is currently active in
+ * the context or {@code null}.
*/
- public static IEditorPart getActiveEditor() {
- IWorkbenchWindow activeWorkbenchWindow = getActiveWorkbenchWindow();
- if (activeWorkbenchWindow == null) {
- return null;
+ public static StyledText getActiveEditorStyledText(IEclipseContext eclipseContext) {
+ Object active = eclipseContext.getActive(ISources.ACTIVE_EDITOR_NAME);
+ if (active instanceof IEditorPart) {
+ return EditorUtils.getActiveEditorStyledText((IEditorPart) active);
}
- IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage();
- if (activePage == null) {
- return null;
- }
- return activePage.getActiveEditor();
+ return null;
}
/**
- * Provides the current active workbench window or null if it is not available.
+ * Caches the current {@link StyledText} as being the one active in the passed
+ * macro context.
*
- * @return the current active workbench window or null if it is not available.
- */
- public static IWorkbenchWindow getActiveWorkbenchWindow() {
- IWorkbench workbench;
- try {
- workbench = PlatformUI.getWorkbench();
- } catch (IllegalStateException e) { // java.lang.IllegalStateException: Workbench has not been created yet.
- return null;
- }
- return workbench.getActiveWorkbenchWindow();
- }
-
- /**
- * Caches the current styled text as being the one active in the passed macro
- * context.
+ * @param activeEditor
+ * the editor from there the {@link StyledText} should be gotten.
*
* @param macroContext
* the macro context where it should be set.
*/
- public static void cacheTargetStyledText(IMacroContext macroContext) {
- if (macroContext != null) {
+ public static void cacheTargetStyledText(IEditorPart activeEditor, IMacroContext macroContext) {
+ if (macroContext != null && activeEditor != null) {
Object object = macroContext.get(TARGET_STYLED_TEXT);
if (object == null) {
- macroContext.set(TARGET_STYLED_TEXT, getActiveEditorStyledText());
+ macroContext.set(TARGET_STYLED_TEXT, getActiveEditorStyledText(activeEditor));
}
}
}
@@ -111,22 +98,25 @@
* Caches the current editor part as being the one active in the passed macro
* context.
*
+ * @param activeEditor
+ * the editor which should be cached as the target editor for the
+ * macro.
+ *
* @param macroContext
* the macro context where it should be set.
*/
- public static void cacheTargetEditorPart(IMacroContext macroContext) {
- if (macroContext != null) {
+ public static void cacheTargetEditorPart(IEditorPart activeEditor, IMacroContext macroContext) {
+ if (macroContext != null && activeEditor != null) {
Object object = macroContext.get(TARGET_EDITOR_PART);
if (object == null) {
- macroContext.set(TARGET_EDITOR_PART, getActiveEditor());
+ macroContext.set(TARGET_EDITOR_PART, activeEditor);
}
}
}
-
/**
- * Gets the styled text which was set as the current when the macro context was
- * created.
+ * Gets the {@link StyledText} which was set as the current when the macro
+ * context was created.
*
* @param macroContext
* the macro context.
diff --git a/bundles/org.eclipse.e4.ui.macros/src/org/eclipse/e4/ui/macros/internal/UserNotifications.java b/bundles/org.eclipse.e4.ui.macros/src/org/eclipse/e4/ui/macros/internal/UserNotifications.java
index 90c4d90..a60170b 100644
--- a/bundles/org.eclipse.e4.ui.macros/src/org/eclipse/e4/ui/macros/internal/UserNotifications.java
+++ b/bundles/org.eclipse.e4.ui.macros/src/org/eclipse/e4/ui/macros/internal/UserNotifications.java
@@ -25,8 +25,7 @@
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
-import org.eclipse.ui.IWorkbenchPage;
-import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.ISources;
/**
* Helper class to show notifications to the user.
@@ -44,6 +43,11 @@
@Inject
private Shell shell;
+ @Inject
+ @Named(ISources.ACTIVE_EDITOR_NAME)
+ @Optional
+ private IEditorPart activeEditor;
+
/**
* Sets a given message to be shown to the user.
*
@@ -99,15 +103,6 @@
* @return the available status line manager for the current editor.
*/
private IStatusLineManager getStatusLineManager() {
- IWorkbenchWindow activeWorkbenchWindow = EditorUtils.getActiveWorkbenchWindow();
- if (activeWorkbenchWindow == null) {
- return null;
- }
- IWorkbenchPage activePage = activeWorkbenchWindow.getActivePage();
- if (activePage == null) {
- return null;
- }
- IEditorPart activeEditor = activePage.getActiveEditor();
if (activeEditor == null) {
return null;
}
diff --git a/bundles/org.eclipse.e4.ui.macros/src/org/eclipse/e4/ui/macros/internal/keybindings/CommandManagerExecutionListener.java b/bundles/org.eclipse.e4.ui.macros/src/org/eclipse/e4/ui/macros/internal/keybindings/CommandManagerExecutionListener.java
index 13b92a9..8920079 100644
--- a/bundles/org.eclipse.e4.ui.macros/src/org/eclipse/e4/ui/macros/internal/keybindings/CommandManagerExecutionListener.java
+++ b/bundles/org.eclipse.e4.ui.macros/src/org/eclipse/e4/ui/macros/internal/keybindings/CommandManagerExecutionListener.java
@@ -146,8 +146,8 @@
if (acceptEvent(event)) {
if ("org.eclipse.ui.edit.findReplace".equals(commandId)) { //$NON-NLS-1$
// We can't deal with find/replace at this point. Let the user know.
- UserNotifications userNotifications = new UserNotifications();
- ContextInjectionFactory.inject(userNotifications, fEclipseContext);
+ UserNotifications userNotifications = ContextInjectionFactory.make(UserNotifications.class,
+ fEclipseContext);
try {
userNotifications.notifyFindReplace();
} finally {
@@ -206,7 +206,7 @@
@Override
public boolean acceptEvent(Event swtEvent) {
- if (EditorUtils.getActiveEditorStyledText() != EditorUtils
+ if (EditorUtils.getActiveEditorStyledText(fEclipseContext) != EditorUtils
.getTargetStyledText(fMacroService.getMacroRecordContext())) {
// Note: it previously checked swtEvent.widget, but sometimes the event was
// generated from the wrong control (i.e.: opening a new editor and doing
diff --git a/bundles/org.eclipse.e4.ui.macros/src/org/eclipse/e4/ui/macros/internal/keybindings/CommandManagerExecutionListenerInstaller.java b/bundles/org.eclipse.e4.ui.macros/src/org/eclipse/e4/ui/macros/internal/keybindings/CommandManagerExecutionListenerInstaller.java
index f655e57..57f4969 100644
--- a/bundles/org.eclipse.e4.ui.macros/src/org/eclipse/e4/ui/macros/internal/keybindings/CommandManagerExecutionListenerInstaller.java
+++ b/bundles/org.eclipse.e4.ui.macros/src/org/eclipse/e4/ui/macros/internal/keybindings/CommandManagerExecutionListenerInstaller.java
@@ -11,14 +11,18 @@
package org.eclipse.e4.ui.macros.internal.keybindings;
import javax.inject.Inject;
+import javax.inject.Named;
import org.eclipse.core.commands.CommandManager;
import org.eclipse.e4.core.commands.EHandlerService;
import org.eclipse.e4.core.contexts.IEclipseContext;
+import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.e4.core.macros.EMacroService;
import org.eclipse.e4.core.macros.IMacroPlaybackContext;
import org.eclipse.e4.core.macros.IMacroRecordContext;
import org.eclipse.e4.core.macros.IMacroStateListener;
import org.eclipse.e4.ui.macros.internal.EditorUtils;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.ISources;
/**
* A macro state listener that will install the execution listener when in a
@@ -35,6 +39,11 @@
@Inject
private IEclipseContext fEclipseContext;
+ @Inject
+ @Named(ISources.ACTIVE_EDITOR_NAME)
+ @Optional
+ private IEditorPart activeEditor;
+
private CommandManagerExecutionListener fCommandManagerExecutionListener;
/**
@@ -64,11 +73,11 @@
@Override
public void macroPlaybackContextCreated(IMacroPlaybackContext macroContext) {
- EditorUtils.cacheTargetStyledText(macroContext);
+ EditorUtils.cacheTargetStyledText(activeEditor, macroContext);
}
@Override
public void macroRecordContextCreated(IMacroRecordContext macroContext) {
- EditorUtils.cacheTargetStyledText(macroContext);
+ EditorUtils.cacheTargetStyledText(activeEditor, macroContext);
}
}
diff --git a/bundles/org.eclipse.ui.workbench.texteditor.macros/src/org/eclipse/ui/workbench/texteditor/macros/internal/MacroStyledTextInstaller.java b/bundles/org.eclipse.ui.workbench.texteditor.macros/src/org/eclipse/ui/workbench/texteditor/macros/internal/MacroStyledTextInstaller.java
index fffcc82..1b0070b 100644
--- a/bundles/org.eclipse.ui.workbench.texteditor.macros/src/org/eclipse/ui/workbench/texteditor/macros/internal/MacroStyledTextInstaller.java
+++ b/bundles/org.eclipse.ui.workbench.texteditor.macros/src/org/eclipse/ui/workbench/texteditor/macros/internal/MacroStyledTextInstaller.java
@@ -10,6 +10,9 @@
*******************************************************************************/
package org.eclipse.ui.workbench.texteditor.macros.internal;
+import javax.inject.Inject;
+import javax.inject.Named;
+import org.eclipse.e4.core.di.annotations.Optional;
import org.eclipse.e4.core.macros.EMacroService;
import org.eclipse.e4.core.macros.IMacroContext;
import org.eclipse.e4.core.macros.IMacroPlaybackContext;
@@ -24,6 +27,7 @@
import org.eclipse.swt.widgets.Control;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IMemento;
+import org.eclipse.ui.ISources;
import org.eclipse.ui.XMLMemento;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.ui.texteditor.ITextEditorActionConstants;
@@ -57,6 +61,11 @@
*/
private static final String QUICK_ASSIST_ENABLED = "quickAssistEnabled";//$NON-NLS-1$
+ @Inject
+ @Named(ISources.ACTIVE_EDITOR_NAME)
+ @Optional
+ private IEditorPart activeEditor;
+
/**
* Re-enables the content assist based on the state of the key
* {@link #CONTENT_ASSIST_ENABLED} in the passed memento.
@@ -191,14 +200,14 @@
@Override
public void macroPlaybackContextCreated(IMacroPlaybackContext context) {
- EditorUtils.cacheTargetEditorPart(context);
- EditorUtils.cacheTargetStyledText(context);
+ EditorUtils.cacheTargetEditorPart(activeEditor, context);
+ EditorUtils.cacheTargetStyledText(activeEditor, context);
}
@Override
public void macroRecordContextCreated(IMacroRecordContext context) {
- EditorUtils.cacheTargetEditorPart(context);
- EditorUtils.cacheTargetStyledText(context);
+ EditorUtils.cacheTargetEditorPart(activeEditor, context);
+ EditorUtils.cacheTargetStyledText(activeEditor, context);
}
/**
diff --git a/bundles/org.eclipse.ui.workbench.texteditor.macros/src/org/eclipse/ui/workbench/texteditor/macros/internal/NotifyMacroOnlyInCurrentEditor.java b/bundles/org.eclipse.ui.workbench.texteditor.macros/src/org/eclipse/ui/workbench/texteditor/macros/internal/NotifyMacroOnlyInCurrentEditor.java
index c117d00..5c2ef1a 100644
--- a/bundles/org.eclipse.ui.workbench.texteditor.macros/src/org/eclipse/ui/workbench/texteditor/macros/internal/NotifyMacroOnlyInCurrentEditor.java
+++ b/bundles/org.eclipse.ui.workbench.texteditor.macros/src/org/eclipse/ui/workbench/texteditor/macros/internal/NotifyMacroOnlyInCurrentEditor.java
@@ -136,8 +136,7 @@
*/
public UserNotifications getUserNotifications() {
if (fUserNotifications == null) {
- fUserNotifications = new UserNotifications();
- ContextInjectionFactory.inject(fUserNotifications, fEclipseContext);
+ fUserNotifications = ContextInjectionFactory.make(UserNotifications.class, fEclipseContext);
}
return fUserNotifications;
}
@@ -149,7 +148,7 @@
private void checkCurrentEditor() {
IMacroRecordContext macroRecordContext = this.fMacroService.getMacroRecordContext();
if (macroRecordContext != null) {
- StyledText currentStyledText = EditorUtils.getActiveEditorStyledText();
+ StyledText currentStyledText = EditorUtils.getActiveEditorStyledText(fEclipseContext);
StyledText targetStyledText = EditorUtils.getTargetStyledText(macroRecordContext);
if (targetStyledText != currentStyledText && currentStyledText != fLastEditor) {
getUserNotifications().setMessage(Messages.NotifyMacroOnlyInCurrentEditor_NotRecording);
@@ -169,10 +168,11 @@
* if there's no active editor available for the macro recording.
*/
public void checkEditorActiveForMacroRecording() throws CancelMacroRecordingException {
- StyledText currentStyledText = EditorUtils.getActiveEditorStyledText();
+ StyledText currentStyledText = EditorUtils.getActiveEditorStyledText(fEclipseContext);
if (currentStyledText == null) {
- getUserNotifications().setMessage(Messages.NotifyMacroOnlyInCurrentEditor_NotRecording);
- getUserNotifications().notifyNoEditorOnMacroRecordStartup();
+ UserNotifications userNotifications = getUserNotifications();
+ userNotifications.setMessage(Messages.NotifyMacroOnlyInCurrentEditor_NotRecording);
+ userNotifications.notifyNoEditorOnMacroRecordStartup();
throw new CancelMacroRecordingException();
}
}
@@ -184,7 +184,7 @@
* if there's no active editor available for the macro playback.
*/
public void checkEditorActiveForMacroPlayback() throws CancelMacroPlaybackException {
- StyledText currentStyledText = EditorUtils.getActiveEditorStyledText();
+ StyledText currentStyledText = EditorUtils.getActiveEditorStyledText(fEclipseContext);
if (currentStyledText == null) {
getUserNotifications().notifyNoEditorOnMacroPlaybackStartup();
throw new CancelMacroPlaybackException();