diff options
| author | Andrey Loskutov | 2015-02-23 00:04:59 +0000 |
|---|---|---|
| committer | Andrey Loskutov | 2015-03-26 05:38:37 +0000 |
| commit | 5f5655a94e574f0e4496f89a1506be82edd0ef2c (patch) | |
| tree | 6e13b368eb5544e142cfcb16253c3b4b6dc3d322 | |
| parent | ca71705c47022a63a08da06a48194a0d935336b3 (diff) | |
| download | eclipse.platform.ui-5f5655a94e574f0e4496f89a1506be82edd0ef2c.tar.gz eclipse.platform.ui-5f5655a94e574f0e4496f89a1506be82edd0ef2c.tar.xz eclipse.platform.ui-5f5655a94e574f0e4496f89a1506be82edd0ef2c.zip | |
Bug 378485 - "Open With > Other..." should allow to store as default
Change-Id: I176b8156e0fb3d11098eca2c9560af9f2fd6160e
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
4 files changed, 191 insertions, 55 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 f2368804ab5..456616605c8 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2012 IBM Corporation and others. + * Copyright (c) 2000, 2015 IBM Corporation 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 @@ -9,6 +9,7 @@ * IBM Corporation - initial API and implementation * Benjamin Muskalla - Bug 29633 [EditorMgmt] "Open" menu should * have Open With-->Other + * Andrey Loskutov <loskutov@gmx.de> - Bug 378485 *******************************************************************************/ package org.eclipse.ui.actions; @@ -16,6 +17,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; +import java.util.List; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IResource; @@ -42,6 +44,7 @@ import org.eclipse.ui.internal.WorkbenchPage; import org.eclipse.ui.internal.ide.DialogUtil; import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; +import org.eclipse.ui.internal.util.Util; import org.eclipse.ui.part.FileEditorInput; import com.ibm.icu.text.Collator; @@ -60,12 +63,12 @@ import com.ibm.icu.text.Collator; * @noextend This class is not intended to be subclassed by clients. */ public class OpenWithMenu extends ContributionItem { + private IWorkbenchPage page; - private IAdaptable file; + private IAdaptable adaptable; - private IEditorRegistry registry = PlatformUI.getWorkbench() - .getEditorRegistry(); + private IEditorRegistry registry; /** * The id of this action. @@ -77,16 +80,16 @@ public class OpenWithMenu extends ContributionItem { */ private static final int MATCH_BOTH = IWorkbenchPage.MATCH_INPUT | IWorkbenchPage.MATCH_ID; - /* - * Compares the labels from two IEditorDescriptor objects - */ - private static final Comparator comparer = new Comparator() { + /** + * Compares the labels from two IEditorDescriptor objects + */ + private static final Comparator<IEditorDescriptor> comparer = new Comparator<IEditorDescriptor>() { private Collator collator = Collator.getInstance(); @Override - public int compare(Object arg0, Object arg1) { - String s1 = ((IEditorDescriptor) arg0).getLabel(); - String s2 = ((IEditorDescriptor) arg1).getLabel(); + public int compare(IEditorDescriptor arg0, IEditorDescriptor arg1) { + String s1 = arg0.getLabel(); + String s2 = arg1.getLabel(); return collator.compare(s1, s2); } }; @@ -114,7 +117,8 @@ public class OpenWithMenu extends ContributionItem { public OpenWithMenu(IWorkbenchPage page, IAdaptable file) { super(ID); this.page = page; - this.file = file; + this.adaptable = file; + registry = PlatformUI.getWorkbench().getEditorRegistry(); } /** @@ -138,18 +142,14 @@ public class OpenWithMenu extends ContributionItem { private ImageDescriptor getImageDescriptor(IEditorDescriptor editorDesc) { ImageDescriptor imageDesc = null; if (editorDesc == null) { - imageDesc = registry - .getImageDescriptor(getFileResource().getName()); + imageDesc = registry.getImageDescriptor(getFileResource().getName()); //TODO: is this case valid, and if so, what are the implications for content-type editor bindings? } else { imageDesc = editorDesc.getImageDescriptor(); } if (imageDesc == null) { - if (editorDesc.getId().equals( - IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID)) { - imageDesc = registry - .getSystemExternalEditorImageDescriptor(getFileResource() - .getName()); + if (editorDesc.getId().equals(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID)) { + imageDesc = registry.getSystemExternalEditorImageDescriptor(getFileResource().getName()); } } return imageDesc; @@ -166,8 +166,7 @@ public class OpenWithMenu extends ContributionItem { final IEditorDescriptor preferredEditor) { // XXX: Would be better to use bold here, but SWT does not support it. final MenuItem menuItem = new MenuItem(menu, SWT.RADIO); - boolean isPreferred = preferredEditor != null - && descriptor.getId().equals(preferredEditor.getId()); + boolean isPreferred = preferredEditor != null && descriptor.getId().equals(preferredEditor.getId()); menuItem.setSelection(isPreferred); menuItem.setText(descriptor.getLabel()); Image image = getImage(descriptor); @@ -207,13 +206,10 @@ public class OpenWithMenu extends ContributionItem { public void handleEvent(Event event) { switch (event.type) { case SWT.Selection: - EditorSelectionDialog dialog = new EditorSelectionDialog( - menu.getShell()); - dialog - .setMessage(NLS - .bind( - IDEWorkbenchMessages.OpenWithMenu_OtherDialogDescription, - fileResource.getName())); + EditorSelectionDialog dialog = new EditorSelectionDialog(menu.getShell()); + String fileName = fileResource.getName(); + dialog.setFileName(fileName); + dialog.setMessage(NLS.bind(IDEWorkbenchMessages.OpenWithMenu_OtherDialogDescription, fileName)); if (dialog.open() == Window.OK) { IEditorDescriptor editor = dialog.getSelectedEditor(); if (editor != null) { @@ -232,7 +228,7 @@ public class OpenWithMenu extends ContributionItem { */ @Override public void fill(Menu menu, int index) { - final IFile file= getFileResource(); + final IFile file = getFileResource(); if (file == null) { return; } @@ -240,13 +236,14 @@ public class OpenWithMenu extends ContributionItem { IContentType contentType= IDE.getContentType(file); FileEditorInput editorInput= new FileEditorInput(file); - IEditorDescriptor defaultEditor = registry - .findEditor(IDEWorkbenchPlugin.DEFAULT_TEXT_EDITOR_ID); // may be null + IEditorDescriptor defaultEditor = registry.findEditor(IDEWorkbenchPlugin.DEFAULT_TEXT_EDITOR_ID); // may + // be + // null final IEditorDescriptor preferredEditor= IDE.getDefaultEditor(file); // may be null - IEditorDescriptor[] editors= registry.getEditors(file.getName(), contentType); + IEditorDescriptor[] editors = registry.getEditors(file.getName(), contentType); - editors= IDE.overrideEditorAssociations(editorInput, contentType, editors); + editors = IDE.overrideEditorAssociations(editorInput, contentType, editors); Collections.sort(Arrays.asList(editors), comparer); @@ -254,14 +251,13 @@ public class OpenWithMenu extends ContributionItem { //Check that we don't add it twice. This is possible //if the same editor goes to two mappings. - ArrayList alreadyMapped = new ArrayList(); + List<IEditorDescriptor> alreadyMapped = new ArrayList<>(); for (int i = 0; i < editors.length; i++) { IEditorDescriptor editor= editors[i]; if (!alreadyMapped.contains(editor)) { createMenuItem(menu, editor, preferredEditor); - if (defaultEditor != null - && editor.getId().equals(defaultEditor.getId())) { + if (defaultEditor != null && editor.getId().equals(defaultEditor.getId())) { defaultFound = true; } alreadyMapped.add(editor); @@ -279,13 +275,11 @@ public class OpenWithMenu extends ContributionItem { } // Add system editor (should never be null) - IEditorDescriptor descriptor = registry - .findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID); + IEditorDescriptor descriptor = registry.findEditor(IEditorRegistry.SYSTEM_EXTERNAL_EDITOR_ID); createMenuItem(menu, descriptor, preferredEditor); // Add system in-place editor (can be null) - descriptor = registry - .findEditor(IEditorRegistry.SYSTEM_INPLACE_EDITOR_ID); + descriptor = registry.findEditor(IEditorRegistry.SYSTEM_INPLACE_EDITOR_ID); if (descriptor != null) { createMenuItem(menu, descriptor, preferredEditor); } @@ -295,19 +289,18 @@ public class OpenWithMenu extends ContributionItem { createOtherMenuItem(menu); } - /** * Converts the IAdaptable file to IFile or null. */ private IFile getFileResource() { - if (this.file instanceof IFile) { - return (IFile) this.file; + IFile file = Util.getAdapter(adaptable, IFile.class); + if (file != null) { + return file; } - IResource resource = this.file.getAdapter(IResource.class); + IResource resource = Util.getAdapter(adaptable, IResource.class); if (resource instanceof IFile) { return (IFile) resource; } - return null; } diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/dialogs/EditorSelectionDialog.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/dialogs/EditorSelectionDialog.java index 77897e974ab..b9de73dfb1d 100644 --- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/dialogs/EditorSelectionDialog.java +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/dialogs/EditorSelectionDialog.java @@ -10,7 +10,7 @@ * Benjamin Muskalla - Bug 29633 [EditorMgmt] "Open" menu should * have Open With-->Other * Helena Halperin - Bug 298747 [EditorMgmt] Bidi Incorrect file type direction in mirrored "Editor Selection" dialog - * Andrey Loskutov <loskutov@gmx.de> - Bug 460555 + * Andrey Loskutov <loskutov@gmx.de> - Bug 378485, 460555 *******************************************************************************/ package org.eclipse.ui.dialogs; @@ -41,6 +41,7 @@ import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; @@ -51,12 +52,14 @@ import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; import org.eclipse.swt.widgets.Tree; import org.eclipse.ui.IEditorDescriptor; +import org.eclipse.ui.IFileEditorMapping; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.internal.IWorkbenchHelpContextIds; import org.eclipse.ui.internal.WorkbenchMessages; import org.eclipse.ui.internal.WorkbenchPlugin; import org.eclipse.ui.internal.registry.EditorDescriptor; import org.eclipse.ui.internal.registry.EditorRegistry; +import org.eclipse.ui.internal.registry.FileEditorMapping; import org.eclipse.ui.progress.IProgressService; import org.eclipse.ui.statushandlers.StatusManager; @@ -153,6 +156,14 @@ public class EditorSelectionDialog extends Dialog { private TreeViewer editorTableViewer; + private String fileName; + + private Button fileTypeButton; + + private Button fileNameButton; + + private Button rememberEditorButton; + private static final String[] Executable_Filters; private static final int TABLE_WIDTH = 200; @@ -234,20 +245,22 @@ public class EditorSelectionDialog extends Dialog { textLabel.setLayoutData(data); textLabel.setFont(font); - internalButton = new Button(contents, SWT.RADIO | SWT.LEFT); + Composite group = new Composite(contents, SWT.SHADOW_NONE); + data = new GridData(); + data.grabExcessHorizontalSpace = true; + data.horizontalAlignment = SWT.FILL; + data.horizontalSpan = 2; + group.setLayout(new RowLayout(SWT.HORIZONTAL)); + group.setLayoutData(data); + + internalButton = new Button(group, SWT.RADIO | SWT.LEFT); internalButton.setText(WorkbenchMessages.EditorSelection_internal); internalButton.addListener(SWT.Selection, listener); - data = new GridData(); - data.horizontalSpan = 1; - internalButton.setLayoutData(data); internalButton.setFont(font); - externalButton = new Button(contents, SWT.RADIO | SWT.LEFT); + externalButton = new Button(group, SWT.RADIO | SWT.LEFT); externalButton.setText(WorkbenchMessages.EditorSelection_external); externalButton.addListener(SWT.Selection, listener); - data = new GridData(); - data.horizontalSpan = 1; - externalButton.setLayoutData(data); externalButton.setFont(font); editorTable = new FilteredTree(contents, SWT.SINGLE | SWT.BORDER, new PatternFilter(), true); @@ -289,9 +302,57 @@ public class EditorSelectionDialog extends Dialog { int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); data.widthHint = Math.max(widthHint, browseExternalEditorsButton .computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x); + data.horizontalSpan = 2; browseExternalEditorsButton.setLayoutData(data); browseExternalEditorsButton.setFont(font); + if (fileName != null) { + + rememberEditorButton = new Button(contents, SWT.CHECK | SWT.LEFT); + rememberEditorButton.setText(WorkbenchMessages.EditorSelection_rememberEditor); + rememberEditorButton.addListener(SWT.Selection, listener); + data = new GridData(); + data.horizontalSpan = 2; + rememberEditorButton.setLayoutData(data); + rememberEditorButton.setFont(font); + + group = new Composite(contents, SWT.SHADOW_NONE); + group.setLayout(new GridLayout(2, false)); + data = new GridData(); + data.grabExcessHorizontalSpace = true; + data.horizontalAlignment = SWT.FILL; + data.horizontalSpan = 2; + data.horizontalIndent = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN); + group.setLayoutData(data); + + String fileType = getFileType(); + if (!fileType.isEmpty()) { + fileTypeButton = new Button(group, SWT.RADIO | SWT.LEFT); + fileTypeButton.setText("*." + fileType); //$NON-NLS-1$ + fileTypeButton.addListener(SWT.Selection, listener); + data = new GridData(); + data.horizontalSpan = 1; + data.grabExcessHorizontalSpace = true; + fileTypeButton.setLayoutData(data); + fileTypeButton.setFont(font); + fileTypeButton.setSelection(true); + fileTypeButton.setEnabled(false); + } + + fileNameButton = new Button(group, SWT.RADIO | SWT.LEFT); + fileNameButton.setText(fileName); + fileNameButton.addListener(SWT.Selection, listener); + data = new GridData(); + data.horizontalSpan = 1; + data.grabExcessHorizontalSpace = true; + fileNameButton.setLayoutData(data); + fileNameButton.setFont(font); + fileNameButton.setEnabled(false); + if (fileType.isEmpty()) { + fileNameButton.setSelection(true); + } + } + restoreWidgetValues(); // Place buttons to the appropriate state // Run async to restore selection on *visible* dialog - otherwise three won't scroll @@ -306,7 +367,18 @@ public class EditorSelectionDialog extends Dialog { updateEnableState(); } }); - return contents; + return contents; + } + + private String getFileType() { + if (fileName == null) { + return ""; //$NON-NLS-1$ + } + int lastDot = fileName.lastIndexOf('.'); + if (lastDot == -1 || lastDot >= fileName.length() - 2) { + return ""; //$NON-NLS-1$ + } + return fileName.substring(lastDot + 1, fileName.length()); } protected void fillEditorTable() { @@ -511,6 +583,56 @@ public class EditorSelectionDialog extends Dialog { settings .put(STORE_ID_INTERNAL_EXTERNAL, !internalButton.getSelection()); settings.put(STORE_ID_DESCR, selectedEditor.getId()); + String editorId = selectedEditor.getId(); + settings.put(STORE_ID_DESCR, editorId); + if (rememberEditorButton == null || !rememberEditorButton.getSelection()) { + return; + } + EditorRegistry reg = (EditorRegistry) WorkbenchPlugin.getDefault().getEditorRegistry(); + boolean useFileName = fileNameButton.getSelection(); + updateFileMappings(reg, useFileName); + if (useFileName) { + reg.setDefaultEditor(fileName, editorId); + } else { + reg.setDefaultEditor("*." + getFileType(), editorId); //$NON-NLS-1$ + } + } + + /** + * Make sure EditorRegistry has editor mapping for the file name/type + */ + private void updateFileMappings(EditorRegistry reg, boolean useFileName) { + IFileEditorMapping[] mappings = reg.getFileEditorMappings(); + boolean hasMapping = false; + String fileType = getFileType(); + for (IFileEditorMapping mapping : mappings) { + if (useFileName) { + if (fileName.equals(mapping.getLabel())) { + hasMapping = true; + break; + } + } else { + if (fileType.equals(mapping.getExtension())) { + hasMapping = true; + break; + } + + } + } + if (hasMapping) { + return; + } + FileEditorMapping mapping; + if (useFileName) { + mapping = new FileEditorMapping(fileName, null); + } else { + mapping = new FileEditorMapping(null, fileType); + } + List<IFileEditorMapping> newMappings = new ArrayList<IFileEditorMapping>(); + newMappings.addAll(Arrays.asList(mappings)); + newMappings.add(mapping); + FileEditorMapping[] array = newMappings.toArray(new FileEditorMapping[newMappings.size()]); + reg.setFileEditorMappings(array); } /** @@ -524,6 +646,18 @@ public class EditorSelectionDialog extends Dialog { } /** + * Set the file name which can be used to store the selected editor + * preference + * + * @param fileName + * the file name + * @since 3.107 + */ + public void setFileName(String fileName) { + this.fileName = fileName; + } + + /** * Set the editors which will not appear in the dialog. * * @param editors @@ -540,6 +674,13 @@ public class EditorSelectionDialog extends Dialog { boolean enableExternal = externalButton.getSelection(); browseExternalEditorsButton.setEnabled(enableExternal); updateOkButton(); + if (rememberEditorButton != null) { + boolean selection = rememberEditorButton.getSelection(); + fileNameButton.setEnabled(selection); + if (!getFileType().isEmpty()) { + fileTypeButton.setEnabled(selection); + } + } } @Override diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WorkbenchMessages.java b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WorkbenchMessages.java index b5485bab717..518376de9a8 100644 --- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WorkbenchMessages.java +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/WorkbenchMessages.java @@ -521,6 +521,7 @@ public class WorkbenchMessages extends NLS { public static String EditorSelection_chooseAnEditor; public static String EditorSelection_internal; public static String EditorSelection_external; + public static String EditorSelection_rememberEditor; public static String EditorSelection_browse; public static String EditorSelection_title; diff --git a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/messages.properties b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/messages.properties index 5a29378c9a0..47741b2c440 100644 --- a/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/messages.properties +++ b/bundles/org.eclipse.ui.workbench/Eclipse UI/org/eclipse/ui/internal/messages.properties @@ -470,6 +470,7 @@ Choose_the_editor_for_file = Choose the editor for files of type ({0}) EditorSelection_chooseAnEditor = Choose an editor: EditorSelection_internal = &Internal editors EditorSelection_external = &External programs +EditorSelection_rememberEditor = &Always use this editor for: EditorSelection_browse = &Browse... EditorSelection_title = Editor Selection |
