diff options
| author | Mickael Istria | 2016-01-28 11:13:05 +0000 |
|---|---|---|
| committer | Dani Megert | 2016-03-02 11:26:12 +0000 |
| commit | 912f0f8bfe79b6c3e7bb8ae687d7399738b29d13 (patch) | |
| tree | 4582971b9a67df605584b41aee5e936b5465ce1f | |
| parent | 49431814203dc55bc92faafadd19684e1c4e2dab (diff) | |
| download | eclipse.platform.ui-912f0f8bfe79b6c3e7bb8ae687d7399738b29d13.tar.gz eclipse.platform.ui-912f0f8bfe79b6c3e7bb8ae687d7399738b29d13.tar.xz eclipse.platform.ui-912f0f8bfe79b6c3e7bb8ae687d7399738b29d13.zip | |
Bug 486665 - API breakage of IDE#getEditorDescription
This introduces a new flag on getEditorDescriptor methods to tell
whether interaction with user is allowed.
Change-Id: I663e0a5165ecb3cbd45911c3dfe36ce78e80c03c
Signed-off-by: Mickael Istria <mistria@redhat.com>
6 files changed, 277 insertions, 43 deletions
diff --git a/bundles/org.eclipse.ui.ide/extensions/org/eclipse/ui/actions/OpenWithMenu.java b/bundles/org.eclipse.ui.ide/extensions/org/eclipse/ui/actions/OpenWithMenu.java index 5118088e1fb..6c1255d564c 100644 --- a/bundles/org.eclipse.ui.ide/extensions/org/eclipse/ui/actions/OpenWithMenu.java +++ b/bundles/org.eclipse.ui.ide/extensions/org/eclipse/ui/actions/OpenWithMenu.java @@ -351,7 +351,7 @@ public class OpenWithMenu extends ContributionItem { if (menuItem.getSelection()) { IDE.setDefaultEditor(file, null); try { - openEditor(IDE.getEditorDescriptor(file), false); + openEditor(IDE.getEditorDescriptor(file, true, true), false); } catch (PartInitException e) { DialogUtil.openError(page.getWorkbenchWindow() .getShell(), IDEWorkbenchMessages.OpenWithMenu_dialogTitle, diff --git a/bundles/org.eclipse.ui.ide/plugin.xml b/bundles/org.eclipse.ui.ide/plugin.xml index 90a33631ddf..fb156645af7 100644 --- a/bundles/org.eclipse.ui.ide/plugin.xml +++ b/bundles/org.eclipse.ui.ide/plugin.xml @@ -2408,16 +2408,19 @@ <strategy class="org.eclipse.ui.internal.ide.SystemEditorOrTextEditorStrategy" id="org.eclipse.ui.ide.systemEditorThenTextEditor" + interactive="false" label="%systemEditorThenTextEditor"> </strategy> <strategy class="org.eclipse.ui.internal.ide.TextEditorStrategy" id="org.eclipse.ui.ide.textEditor" + interactive="false" label="%textEditor"> </strategy> <strategy class="org.eclipse.ui.internal.ide.AskUserViaPopupUnassociatedEditorStrategy" id="org.eclipse.ui.ide.askUserForUnassociatedEditor" + interactive="true" label="%askUserViaPopup"> </strategy> </extension> diff --git a/bundles/org.eclipse.ui.ide/schema/unassociatedEditorStrategy.exsd b/bundles/org.eclipse.ui.ide/schema/unassociatedEditorStrategy.exsd index 3e7795dc863..623a6c975af 100644 --- a/bundles/org.eclipse.ui.ide/schema/unassociatedEditorStrategy.exsd +++ b/bundles/org.eclipse.ui.ide/schema/unassociatedEditorStrategy.exsd @@ -26,21 +26,21 @@ The default strategy is to check for system editor and to open the Eclipse text <attribute name="point" type="string" use="required"> <annotation> <documentation> - + </documentation> </annotation> </attribute> <attribute name="id" type="string"> <annotation> <documentation> - + </documentation> </annotation> </attribute> <attribute name="name" type="string"> <annotation> <documentation> - + </documentation> <appInfo> <meta.attribute translatable="true"/> @@ -55,14 +55,14 @@ The default strategy is to check for system editor and to open the Eclipse text <attribute name="id" type="string" use="required"> <annotation> <documentation> - + A unique id that will be used to identify this unassociated editor strategy </documentation> </annotation> </attribute> <attribute name="class" type="string" use="required"> <annotation> <documentation> - + The fully qualified class name implementing the interface org.eclipse.ui.ide.IUnassociatedEditorStrategy </documentation> <appInfo> <meta.attribute kind="java" basedOn=":org.eclipse.ui.ide.IUnassociatedEditorStrategy"/> @@ -79,6 +79,13 @@ The default strategy is to check for system editor and to open the Eclipse text </appInfo> </annotation> </attribute> + <attribute name="interactive" type="boolean" use="required"> + <annotation> + <documentation> + Declare whether the strategy is interactive, requires user actions or control. In some context, interactive strategies will be ignored as operations have to happen without interaction. + </documentation> + </annotation> + </attribute> </complexType> </element> diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/IDE.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/IDE.java index 62c6f6e25c2..49363cd8365 100644 --- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/IDE.java +++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/ide/IDE.java @@ -9,6 +9,7 @@ * IBM Corporation - initial API and implementation * Jan-Ove Weichel <janove.weichel@vogella.com> - Bug 411578 * Andrey Loskutov <loskutov@gmx.de> - Bug 485201 + * Mickael Istria (Red Hat Inc.) - Bug 90292 (default editor) and family *******************************************************************************/ package org.eclipse.ui.ide; @@ -555,7 +556,7 @@ public final class IDE { // open the editor on the file IEditorDescriptor editorDesc; try { - editorDesc = getEditorDescriptor(input, determineContentType); + editorDesc = getEditorDescriptor(input, determineContentType, true); } catch (OperationCanceledException ex) { return null; } @@ -592,7 +593,7 @@ public final class IDE { // open the editor on the file IEditorDescriptor editorDesc; try { - editorDesc = getEditorDescriptor(input); + editorDesc = getEditorDescriptor(input, true, true); } catch (OperationCanceledException ex) { return null; } @@ -693,11 +694,11 @@ public final class IDE { * @return an editor descriptor, appropriate for opening the file * @throws PartInitException * if no editor can be found - * @throws OperationCanceledException - * in case descriptor lookup was canceled by the user + * @deprecated Since 3.12, use + * {@link IDE#getEditorDescriptor(IFile, boolean, boolean)} */ - public static IEditorDescriptor getEditorDescriptor(IFile file) - throws PartInitException, OperationCanceledException { + @Deprecated + public static IEditorDescriptor getEditorDescriptor(IFile file) throws PartInitException { return getEditorDescriptor(file, true); } @@ -734,12 +735,13 @@ public final class IDE { * @return an editor descriptor, appropriate for opening the file * @throws PartInitException * if no editor can be found - * @throws OperationCanceledException - * in case descriptor lookup was canceled by the user * @since 3.1 + * @deprecated Since 3.12, use + * {@link IDE#getEditorDescriptor(IFile, boolean, boolean)} + * instead. */ - public static IEditorDescriptor getEditorDescriptor(IFile file, - boolean determineContentType) throws PartInitException, OperationCanceledException { + @Deprecated + public static IEditorDescriptor getEditorDescriptor(IFile file, boolean determineContentType) throws PartInitException { if (file == null) { throw new IllegalArgumentException(); @@ -751,8 +753,62 @@ public final class IDE { } /** - * Returns an editor id appropriate for opening the given file - * store. + * Returns an editor descriptor appropriate for opening the given file + * resource. + * <p> + * The editor descriptor is determined using a multi-step process. This + * method will attempt to resolve the editor based on content-type bindings + * as well as traditional name/extension bindings if + * <code>determineContentType</code>is <code>true</code>. + * </p> + * <ol> + * <li>The <code>IResource</code> is consulted for a persistent property + * named <code>IDE.EDITOR_KEY</code> containing the preferred editor id to + * be used.</li> + * <li>The workbench editor registry is consulted to determine if an editor + * extension has been registered for the file type. If so, an instance of + * the editor extension is opened on the file. See + * <code>IEditorRegistry.getDefaultEditor(String)</code>.</li> + * <li>The preferred {@link IUnassociatedEditorStrategy} is consulted.</li> + * <li>The {@link SystemEditorOrTextEditorStrategy} is consulted, whose + * behavior is + * <ol> + * <li>The operating system is consulted to determine if an in-place + * component editor is available (e.g. OLE editor on Win32 platforms).</li> + * <li>The operating system is consulted to determine if an external editor + * is available.</li> + * <li>The workbench editor registry is consulted to determine if the + * default text editor is available.</li></li> + * </ol> + * </ol> + * </p> + * + * @param file + * the file + * @param determineContentType + * query the content type system for the content type of the file + * @param allowInteractive + * whether we allow user interactions + * @return an editor descriptor, appropriate for opening the file + * @throws PartInitException + * if no editor can be found + * @throws OperationCanceledException + * in case descriptor lookup was canceled by the user + * @since 3.12 + */ + public static IEditorDescriptor getEditorDescriptor(IFile file, boolean determineContentType, boolean allowInteractive) + throws PartInitException, OperationCanceledException { + + if (file == null) { + throw new IllegalArgumentException(); + } + + return getEditorDescriptor(file.getName(), PlatformUI.getWorkbench().getEditorRegistry(), + getDefaultEditor(file, determineContentType), allowInteractive); + } + + /** + * Returns an editor id appropriate for opening the given file store. * <p> * The editor descriptor is determined using a multi-step process. This * method will attempt to resolve the editor based on content-type bindings @@ -763,22 +819,29 @@ public final class IDE { * extension has been registered for the file type. If so, an instance of * the editor extension is opened on the file. See * <code>IEditorRegistry.getDefaultEditor(String)</code>.</li> + * <li>The preferred {@link IUnknownEditorStrategy} is consulted.</li> + * <li>The {@link SystemEditorOrTextEditorStrategy} is consulted, whose + * behavior is + * <ol> * <li>The operating system is consulted to determine if an in-place * component editor is available (e.g. OLE editor on Win32 platforms).</li> * <li>The operating system is consulted to determine if an external editor * is available.</li> * <li>The workbench editor registry is consulted to determine if the - * default text editor is available.</li> + * default text editor is available.</li></li> + * </ol> * </ol> * </p> * * @param fileStore * the file store + * @param allowInteractive + * Whether user interactions are allowed * @return the id of an editor, appropriate for opening the file * @throws PartInitException * if no editor can be found */ - private static String getEditorId(IFileStore fileStore) throws PartInitException { + private static String getEditorId(IFileStore fileStore, boolean allowInteractive) throws PartInitException { String name = fileStore.fetchInfo().getName(); if (name == null) { throw new IllegalArgumentException(); @@ -804,10 +867,11 @@ public final class IDE { IEditorRegistry editorReg = PlatformUI.getWorkbench().getEditorRegistry(); IEditorDescriptor defaultEditor = editorReg.getDefaultEditor(name, contentType); - defaultEditor = overrideDefaultEditorAssociation(new FileStoreEditorInput(fileStore), contentType, defaultEditor); - return getEditorDescriptor(name, editorReg, defaultEditor).getId(); + defaultEditor = overrideDefaultEditorAssociation(new FileStoreEditorInput(fileStore), contentType, + defaultEditor); + return getEditorDescriptor(name, editorReg, defaultEditor, allowInteractive).getId(); } - + /** * Applies the <code>org.eclipse.ui.ide.editorAssociationOverride</code> extensions to the given * input. @@ -945,12 +1009,14 @@ public final class IDE { * @return an editor descriptor, appropriate for opening the file * @throws PartInitException * if no editor can be found - * @throws OperationCanceledException - * in case descriptor lookup was canceled by the user * @since 3.1 + * @deprecated Since 3.12, use + * {@link IDE#getEditorDescriptor(String, boolean, boolean)} + * instead. */ + @Deprecated public static IEditorDescriptor getEditorDescriptor(String name) - throws PartInitException, OperationCanceledException { + throws PartInitException { return getEditorDescriptor(name, true); } @@ -984,12 +1050,14 @@ public final class IDE { * @return an editor descriptor, appropriate for opening the file * @throws PartInitException * if no editor can be found - * @throws OperationCanceledException - * in case descriptor lookup was canceled by the user * @since 3.1 + * @deprecated Since 3.12, use + * {@link IDE#getEditorDescriptor(String, boolean, boolean)} + * instead. */ - public static IEditorDescriptor getEditorDescriptor(String name, - boolean inferContentType) throws PartInitException, OperationCanceledException { + @Deprecated + public static IEditorDescriptor getEditorDescriptor(String name, boolean inferContentType) + throws PartInitException { if (name == null) { throw new IllegalArgumentException(); @@ -1006,8 +1074,67 @@ public final class IDE { } /** + * Returns an editor descriptor appropriate for opening a file resource with + * the given name. + * <p> + * The editor descriptor is determined using a multi-step process. This + * method will attempt to infer the content type of the file if + * <code>inferContentType</code> is <code>true</code>. + * </p> + * <ol> + * <li>The workbench editor registry is consulted to determine if an editor + * extension has been registered for the file type. If so, an instance of + * the editor extension is opened on the file. See + * <code>IEditorRegistry.getDefaultEditor(String)</code>.</li> + * <li>The preferred {@link IUnassociatedEditorStrategy} is consulted.</li> + * <li>The {@link SystemEditorOrTextEditorStrategy} is consulted, whose + * behavior is + * <ol> + * <li>The operating system is consulted to determine if an in-place + * component editor is available (e.g. OLE editor on Win32 platforms).</li> + * <li>The operating system is consulted to determine if an external editor + * is available.</li> + * <li>The workbench editor registry is consulted to determine if the + * default text editor is available.</li></li> + * </ol> + * </ol> + * </p> + * + * @param name + * the file name + * @param inferContentType + * attempt to infer the content type from the file name if this + * is <code>true</code> + * @param allowInteractive + * whether we allow user interactions. + * @return an editor descriptor, appropriate for opening the file + * @throws PartInitException + * if no editor can be found + * @throws OperationCanceledException + * in case descriptor lookup was canceled by the user + * @since 3.12 + */ + public static IEditorDescriptor getEditorDescriptor(String name, boolean inferContentType, boolean allowInteractive) + throws PartInitException, OperationCanceledException { + + if (name == null) { + throw new IllegalArgumentException(); + } + + IContentType contentType = inferContentType ? Platform + .getContentTypeManager().findContentTypeFor(name) : null; + IEditorRegistry editorReg = PlatformUI.getWorkbench() + .getEditorRegistry(); + + IEditorDescriptor defaultEditor = editorReg.getDefaultEditor(name, contentType); + defaultEditor = getEditorDescriptor(name, editorReg, defaultEditor, allowInteractive); + return overrideDefaultEditorAssociation(name, contentType, defaultEditor); + } + + /** * Get the editor descriptor for a given name using the editorDescriptor - * passed in as a default as a starting point. + * passed in as a default as a starting point. It may delegate computation + * to the active {@link IUnknownEditorStrategy}. * * @param name * The name of the element to open. @@ -1015,23 +1142,26 @@ public final class IDE { * The editor registry to do the lookups from. * @param defaultDescriptor * IEditorDescriptor or <code>null</code> + * @param allowInteractive + * Whether we ask selected {@link IUnknownEditorStrategy}, that + * can be interactive. * @return IEditorDescriptor * @throws PartInitException * if no valid editor can be found * @throws OperationCanceledException * in case descriptor lookup was canceled by the user * - * @since 3.1 + * @since 3.12 */ - private static IEditorDescriptor getEditorDescriptor(String name, - IEditorRegistry editorReg, IEditorDescriptor defaultDescriptor) - throws PartInitException, OperationCanceledException { + private static IEditorDescriptor getEditorDescriptor(String name, IEditorRegistry editorReg, + IEditorDescriptor defaultDescriptor, boolean allowInteractive) + throws PartInitException, OperationCanceledException { if (defaultDescriptor != null) { return defaultDescriptor; } - IUnassociatedEditorStrategy strategy = getUnknowEditorStrategy(); + IUnassociatedEditorStrategy strategy = getUnassociatedEditorStrategy(allowInteractive); IEditorDescriptor editorDesc; try { editorDesc = strategy.getEditorDescriptor(name, editorReg); @@ -1047,16 +1177,77 @@ public final class IDE { return editorDesc; } + + /** + * Get the editor descriptor for a given name using the editorDescriptor + * passed in as a default as a starting point. + * + * @param name + * The name of the element to open. + * @param editorReg + * The editor registry to do the lookups from. + * @param defaultDescriptor + * IEditorDescriptor or <code>null</code> + * @return IEditorDescriptor + * @throws PartInitException + * if no valid editor can be found + * + * @since 3.1 + * @deprecated Since 3.12, use {@link IDE#getEditorDescriptor(String, boolean, boolean)} instead + */ + @Deprecated + private static IEditorDescriptor getEditorDescriptor(String name, + IEditorRegistry editorReg, IEditorDescriptor defaultDescriptor) + throws PartInitException { + + if (defaultDescriptor != null) { + return defaultDescriptor; + } + + IEditorDescriptor editorDesc = defaultDescriptor; + + // next check the OS for in-place editor (OLE on Win32) + if (editorReg.isSystemInPlaceEditorAvailable(name)) { + editorDesc = editorReg + .findEditor(IEditorRegistry.SYSTEM_INPLACE_EDITOR_ID); + } + + // next check with the OS for an external editor + if (editorDesc == null + && editorReg.isSystemExternalEditorAvailable(name)) { + editorDesc = editorReg + .findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID); + } + + // next lookup the default text editor + if (editorDesc == null) { + editorDesc = editorReg + .findEditor(IDEWorkbenchPlugin.DEFAULT_TEXT_EDITOR_ID); + } + + // if no valid editor found, bail out + if (editorDesc == null) { + throw new PartInitException( + IDEWorkbenchMessages.IDE_noFileEditorFound); + } + + return editorDesc; + } /** - * @return The strategy to use in order to open unassociated file. Either as - * set by preference, or a {@link SystemEditorOrTextEditorStrategy} - * if none is explicitly configured. + * @param allowInteractive + * Whether interactive strategies are considered + * @return The strategy to use in order to open unknown file. Either as set + * by preference, or a {@link SystemEditorOrTextEditorStrategy} if + * none is explicitly configured. Never returns {@code null}. */ - private static IUnassociatedEditorStrategy getUnknowEditorStrategy() { + private static IUnassociatedEditorStrategy getUnassociatedEditorStrategy(boolean allowInteractive) { String preferedStrategy = IDEWorkbenchPlugin.getDefault().getPreferenceStore() .getString(UNASSOCIATED_EDITOR_STRATEGY_PREFERENCE_KEY); - IUnassociatedEditorStrategy res = UnassociatedEditorStrategyRegistry.getStrategy(preferedStrategy); + IUnassociatedEditorStrategy res = null; + if (allowInteractive || !UnassociatedEditorStrategyRegistry.isInteractive(preferedStrategy)) { + res = UnassociatedEditorStrategyRegistry.getStrategy(preferedStrategy); + } if (res == null) { res = new SystemEditorOrTextEditorStrategy(); } @@ -1195,7 +1386,7 @@ public final class IDE { IEditorInput input = getEditorInput(fileStore); String editorId; try { - editorId = getEditorId(fileStore); + editorId = getEditorId(fileStore, true); } catch (OperationCanceledException ex) { return null; } diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/SystemEditorOrTextEditorStrategy.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/SystemEditorOrTextEditorStrategy.java index 9db73a4060e..33c2952b50e 100644 --- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/SystemEditorOrTextEditorStrategy.java +++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/SystemEditorOrTextEditorStrategy.java @@ -15,8 +15,19 @@ import org.eclipse.ui.IEditorRegistry; import org.eclipse.ui.ide.IUnassociatedEditorStrategy; /** - * @since 3.12 + * Strategy for unassociated file types: + * <ol> + * <li>The operating system is consulted to determine if an in-place component + * editor is available (e.g. OLE editor on Win32 platforms).</li> + * <li>The operating system is consulted to determine if an external editor is + * available.</li> + * <li>The workbench editor registry is consulted to determine if the default + * text editor is available.</li></li> + * </ol> + * This is the default strategy, as it mimics the legacy behavior of IDE before + * {@link IUnassociatedEditorStrategy} got introduced. * + * @since 3.12 */ public final class SystemEditorOrTextEditorStrategy implements IUnassociatedEditorStrategy { static final String EXTENSION_ID = "org.eclipse.ui.ide.systemEditorThenTextEditor"; //$NON-NLS-1$ diff --git a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/UnassociatedEditorStrategyRegistry.java b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/UnassociatedEditorStrategyRegistry.java index d957e5003a2..64dd8a89d2e 100644 --- a/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/UnassociatedEditorStrategyRegistry.java +++ b/bundles/org.eclipse.ui.ide/src/org/eclipse/ui/internal/ide/UnassociatedEditorStrategyRegistry.java @@ -107,4 +107,26 @@ public class UnassociatedEditorStrategyRegistry { idsToLabel = res; } + /** + * @param strategyId + * @return Whether the specified strategy is interactive, or false is + * strategy is unknown + */ + public static boolean isInteractive(String strategyId) { + if (strategyId == null) { + return false; + } + IExtensionRegistry extRegistry = Platform.getExtensionRegistry(); + IConfigurationElement[] extensions = extRegistry.getConfigurationElementsFor(EXTENSION_POINT_ID); + if (extensions != null) { + for (IConfigurationElement extension : extensions) { + if (strategyId.equals(readAttribute(extension, "id"))) { //$NON-NLS-1$ + return Boolean.parseBoolean(readAttribute(extension, "interactive")); //$NON-NLS-1$ + } + } + } + IDEWorkbenchPlugin.log("No editor strategy found for " + strategyId); //$NON-NLS-1$ + return false; + } + } |
