diff options
author | Harry Terkelsen | 2014-10-29 22:56:58 +0000 |
---|---|---|
committer | Markus Keller | 2014-12-07 23:31:12 +0000 |
commit | e80862cc16da4931b3162859fcc8cc41ba1da7a3 (patch) | |
tree | d406f88826adbcd6d2a43816d225dc5cb076e81b | |
parent | 64e811de299e3f52922f686dff8e1398e8c6e6c3 (diff) | |
download | eclipse.jdt.ui-e80862cc16da4931b3162859fcc8cc41ba1da7a3.tar.gz eclipse.jdt.ui-e80862cc16da4931b3162859fcc8cc41ba1da7a3.tar.xz eclipse.jdt.ui-e80862cc16da4931b3162859fcc8cc41ba1da7a3.zip |
Fixes Bug 449262 - Allow the use of third-party Java formattersI20141207-2000
Change-Id: Ide334544bf5aef532e17c2f42aac37d074a2dfa2
Signed-off-by: Harry Terkelsen <het@google.com>
6 files changed, 285 insertions, 14 deletions
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/CodeFormatterConfigurationBlock.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/CodeFormatterConfigurationBlock.java index e1d09d54b7..938c726a66 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/CodeFormatterConfigurationBlock.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/CodeFormatterConfigurationBlock.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2011 IBM Corporation and others. + * Copyright (c) 2000, 2014 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 @@ -8,6 +8,7 @@ * Contributors: * IBM Corporation - initial API and implementation * Aaron Luchko, aluchko@redhat.com - 105926 [Formatter] Exporting Unnamed profile fails silently + * Harry Terkelsen (het@google.com) - Bug 449262 - Allow the use of third-party Java formatters *******************************************************************************/ package org.eclipse.jdt.internal.ui.preferences.formatter; @@ -63,13 +64,19 @@ public class CodeFormatterConfigurationBlock extends ProfileConfigurationBlock { } public void update(Observable o, Object arg) { - final int value= ((Integer)arg).intValue(); - switch (value) { - case ProfileManager.PROFILE_CREATED_EVENT: - case ProfileManager.PROFILE_DELETED_EVENT: - case ProfileManager.SELECTION_CHANGED_EVENT: - case ProfileManager.SETTINGS_CHANGED_EVENT: - fJavaPreview.setWorkingValues(((ProfileManager)o).getSelected().getSettings()); + if (o instanceof ProfileManager) { + final int value= ((Integer) arg).intValue(); + switch (value) { + case ProfileManager.PROFILE_CREATED_EVENT: + case ProfileManager.PROFILE_DELETED_EVENT: + case ProfileManager.SELECTION_CHANGED_EVENT: + case ProfileManager.SETTINGS_CHANGED_EVENT: + fJavaPreview.setWorkingValues(((ProfileManager) o).getSelected().getSettings()); + fJavaPreview.update(); + } + } else if (o instanceof CustomCodeFormatterBlock) { + final String value = (String) arg; + fJavaPreview.setFormatterId(value); fJavaPreview.update(); } } @@ -80,10 +87,13 @@ public class CodeFormatterConfigurationBlock extends ProfileConfigurationBlock { /** * The JavaPreview. */ - private JavaPreview fJavaPreview; + private CompilationUnitPreview fJavaPreview; + + protected CustomCodeFormatterBlock fCustomCodeFormatterBlock; public CodeFormatterConfigurationBlock(IProject project, PreferencesAccess access) { super(project, access, DIALOGSTORE_LASTSAVELOADPATH); + fCustomCodeFormatterBlock = new CustomCodeFormatterBlock(project, access); } @Override @@ -103,8 +113,11 @@ public class CodeFormatterConfigurationBlock extends ProfileConfigurationBlock { @Override protected void configurePreview(Composite composite, int numColumns, ProfileManager profileManager) { + fCustomCodeFormatterBlock.createContents(composite, numColumns); + createLabel(composite, FormatterMessages.CodingStyleConfigurationBlock_preview_label_text, numColumns); CompilationUnitPreview result= new CompilationUnitPreview(profileManager.getSelected().getSettings(), composite); + result.setFormatterId(fCustomCodeFormatterBlock.getFormatterId()); result.setPreviewText(PREVIEW); fJavaPreview= result; @@ -115,7 +128,8 @@ public class CodeFormatterConfigurationBlock extends ProfileConfigurationBlock { gd.heightHint = 0; fJavaPreview.getControl().setLayoutData(gd); - new PreviewController(profileManager); + PreviewController previewController = new PreviewController(profileManager); + fCustomCodeFormatterBlock.addObserver(previewController); } @@ -123,4 +137,36 @@ public class CodeFormatterConfigurationBlock extends ProfileConfigurationBlock { protected ModifyDialog createModifyDialog(Shell shell, Profile profile, ProfileManager profileManager, ProfileStore profileStore, boolean newProfile) { return new FormatterModifyDialog(shell, profile, profileManager, profileStore, newProfile, FORMATTER_DIALOG_PREFERENCE_KEY, DIALOGSTORE_LASTSAVELOADPATH); } + + @Override + public void performApply() { + if (fCustomCodeFormatterBlock != null) { + fCustomCodeFormatterBlock.performOk(); + } + super.performApply(); + } + + @Override + public void performDefaults() { + if (fCustomCodeFormatterBlock != null) { + fCustomCodeFormatterBlock.performDefaults(); + } + super.performDefaults(); + } + + @Override + public boolean performOk() { + if (fCustomCodeFormatterBlock != null) { + fCustomCodeFormatterBlock.performOk(); + } + return super.performOk(); + } + + @Override + public void enableProjectSpecificSettings(boolean useProjectSpecificSettings) { + if (fCustomCodeFormatterBlock != null) { + fCustomCodeFormatterBlock.enableProjectSpecificSettings(useProjectSpecificSettings); + } + super.enableProjectSpecificSettings(useProjectSpecificSettings); + } } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/CompilationUnitPreview.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/CompilationUnitPreview.java index 931d5c97e9..1afc866900 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/CompilationUnitPreview.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/CompilationUnitPreview.java @@ -7,10 +7,12 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Harry Terkelsen (het@google.com) - Bug 449262 - Allow the use of third-party Java formatters *******************************************************************************/ package org.eclipse.jdt.internal.ui.preferences.formatter; +import java.util.HashMap; import java.util.Map; import org.eclipse.swt.widgets.Composite; @@ -24,6 +26,8 @@ import org.eclipse.jface.text.formatter.IContentFormatter; import org.eclipse.jface.text.formatter.IContentFormatterExtension; import org.eclipse.jface.text.formatter.IFormattingContext; +import org.eclipse.jdt.core.JavaCore; + import org.eclipse.jdt.internal.ui.IJavaStatusConstants; import org.eclipse.jdt.internal.ui.JavaPlugin; import org.eclipse.jdt.internal.ui.text.java.JavaFormattingContext; @@ -32,6 +36,7 @@ import org.eclipse.jdt.internal.ui.text.java.JavaFormattingContext; public class CompilationUnitPreview extends JavaPreview { private String fPreviewText; + private String fFormatterId; /** * @param workingValues @@ -56,7 +61,12 @@ public class CompilationUnitPreview extends JavaPreview { final IContentFormatter formatter = fViewerConfiguration.getContentFormatter(fSourceViewer); if (formatter instanceof IContentFormatterExtension) { final IContentFormatterExtension extension = (IContentFormatterExtension) formatter; - context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, fWorkingValues); + Map<String, String> prefs= fWorkingValues; + if (fFormatterId != null) { + prefs= new HashMap<String, String>(fWorkingValues); + prefs.put(JavaCore.JAVA_FORMATTER, fFormatterId); + } + context.setProperty(FormattingContextProperties.CONTEXT_PREFERENCES, prefs); context.setProperty(FormattingContextProperties.CONTEXT_DOCUMENT, Boolean.valueOf(true)); extension.format(fPreviewDocument, context); } else @@ -76,4 +86,8 @@ public class CompilationUnitPreview extends JavaPreview { fPreviewText= previewText; update(); } + + public void setFormatterId(String formatterId) { + fFormatterId= formatterId; + } } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/CustomCodeFormatterBlock.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/CustomCodeFormatterBlock.java new file mode 100644 index 0000000000..8cfe5419fb --- /dev/null +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/CustomCodeFormatterBlock.java @@ -0,0 +1,203 @@ +/******************************************************************************* + * Copyright (c) 2014 Google Inc. 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: + * Harry Terkelsen (het@google.com) - Bug 449262 - Allow the use of third-party Java formatters + * IBM Corporation - maintenance + *******************************************************************************/ +package org.eclipse.jdt.internal.ui.preferences.formatter; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.Observable; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.widgets.Combo; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; + +import org.eclipse.core.runtime.IConfigurationElement; +import org.eclipse.core.runtime.IExtension; +import org.eclipse.core.runtime.IExtensionPoint; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.preferences.IEclipsePreferences; +import org.eclipse.core.runtime.preferences.IScopeContext; + +import org.eclipse.core.resources.IProject; + +import org.eclipse.jface.layout.GridLayoutFactory; + +import org.eclipse.jdt.core.JavaCore; + +import org.eclipse.jdt.internal.corext.util.CollectionsUtil; + +import org.eclipse.jdt.internal.ui.preferences.PreferencesAccess; + +/** + * Shows a combo box to choose the formatter. If there are no contributed + * formatters, nothing is shown. + */ +public class CustomCodeFormatterBlock extends Observable { + + private static class FormatterContribution { + public String fId; + public String fName; + + public FormatterContribution(String id, String name) { + fId= id; + fName= name; + } + } + + private static final String ATTR_NAME = "name"; //$NON-NLS-1$ + private static final String ATTR_ID = "id"; //$NON-NLS-1$ + + private IEclipsePreferences fPrefs; + private String fDefaultFormatterId; + private FormatterContribution[] fFormatters; + + private Combo fFormatterCombo; + + public CustomCodeFormatterBlock(IProject project, PreferencesAccess access) { + final IScopeContext scope; + final IEclipsePreferences defaults; + if (project != null) { + scope= access.getProjectScope(project); + defaults= access.getInstanceScope().getNode(JavaCore.PLUGIN_ID); + } else { + scope= access.getInstanceScope(); + defaults= access.getDefaultScope().getNode(JavaCore.PLUGIN_ID); + } + fPrefs= scope.getNode(JavaCore.PLUGIN_ID); + fDefaultFormatterId= defaults.get(JavaCore.JAVA_FORMATTER, JavaCore.DEFAULT_JAVA_FORMATTER); + initializeFormatters(); + } + + public void performOk() { + if (fFormatterCombo == null) { + return; + } + String formatterId= fFormatters[fFormatterCombo.getSelectionIndex()].fId; + if (!formatterId.equals(fDefaultFormatterId)) { + fPrefs.put(JavaCore.JAVA_FORMATTER, formatterId); + } else { + // Simply reset to the default one. + performDefaults(); + } + } + + public void performDefaults() { + fPrefs.remove(JavaCore.JAVA_FORMATTER); + + if (fFormatterCombo == null) { + return; + } + int index= getFormatterIndex(fDefaultFormatterId); + fFormatterCombo.select(index); + handleFormatterChanged(); + } + + public void enableProjectSpecificSettings(boolean useProjectSpecificSettings) { + if (useProjectSpecificSettings) { + if (fDefaultFormatterId != null) { + fPrefs.put(JavaCore.JAVA_FORMATTER, fDefaultFormatterId); + } + } else { + initDefault(); + } + } + + public void createContents(Composite parent, int numColumns) { + if (fFormatters.length <= 1) { + return; // No selector is needed since there is only one formatter. + } + + Composite composite= new Composite(parent, SWT.NONE); + composite.setLayout(GridLayoutFactory.fillDefaults().margins(0, 10).numColumns(2).create()); + GridData gd= new GridData(GridData.FILL_HORIZONTAL); + gd.horizontalSpan= numColumns; + composite.setLayoutData(gd); + composite.setFont(parent.getFont()); + + Label label= new Label(composite, SWT.NONE); + label.setText(FormatterMessages.CustomCodeFormatterBlock_formatter_name); + + fFormatterCombo= new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY); + fFormatterCombo.setFont(composite.getFont()); + fFormatterCombo.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent e) { + handleFormatterChanged(); + } + }); + for (FormatterContribution formatter : fFormatters) { + fFormatterCombo.add(formatter.fName); + } + initDefault(); + } + + private void initDefault() { + if (fFormatterCombo == null) { + return; + } + String formatterID= fPrefs.get(JavaCore.JAVA_FORMATTER, fDefaultFormatterId); + fFormatterCombo.select(getFormatterIndex(formatterID)); + } + + private int getFormatterIndex(String formatterId) { + if (formatterId != null) { + for (int i= 0; i < fFormatters.length; i++) { + if (formatterId.equals(fFormatters[i].fId)) { + return i; + } + } + } + return 0; + } + + private void handleFormatterChanged() { + setChanged(); + String formatterId= getFormatterId(); + notifyObservers(formatterId); + } + + /** + * @return the currently selected formatter id + */ + public String getFormatterId() { + if (fFormatterCombo == null) { + return fPrefs.get(JavaCore.JAVA_FORMATTER, fDefaultFormatterId); + } + return fFormatters[fFormatterCombo.getSelectionIndex()].fId; + } + + private void initializeFormatters() { + ArrayList<FormatterContribution> formatters= new ArrayList<FormatterContribution>(); + IExtensionPoint point= Platform.getExtensionRegistry().getExtensionPoint(JavaCore.PLUGIN_ID, JavaCore.JAVA_FORMATTER_EXTENSION_POINT_ID); + if (point != null) { + IExtension[] exts= point.getExtensions(); + for (IExtension ext : exts) { + IConfigurationElement[] elements= ext.getConfigurationElements(); + for (int j = 0; j < elements.length; ++j) { + String name = elements[j].getAttribute(ATTR_NAME); + String id= elements[j].getAttribute(ATTR_ID); + formatters.add(new FormatterContribution(id, name)); + } + } + } + Collections.sort(formatters, new Comparator<FormatterContribution>() { + public int compare(FormatterContribution o1, FormatterContribution o2) { + return o1.fName.compareTo(o2.fName); + } + }); + fFormatters= CollectionsUtil.toArray(formatters, FormatterContribution.class); + } +} diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/FormatterMessages.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/FormatterMessages.java index 160ba95772..8110720270 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/FormatterMessages.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/FormatterMessages.java @@ -11,6 +11,7 @@ * Aaron Luchko, aluchko@redhat.com - 105926 [Formatter] Exporting Unnamed profile fails silently * Brock Janiczak <brockj@tpg.com.au> - [formatter] Add option: "add new line after label" - https://bugs.eclipse.org/bugs/show_bug.cgi?id=150741 * Ray V. (voidstar@gmail.com) - Contribution for bug 282988 + * Harry Terkelsen (het@google.com) - Bug 449262 - Allow the use of third-party Java formatters *******************************************************************************/ package org.eclipse.jdt.internal.ui.preferences.formatter; @@ -502,6 +503,9 @@ final class FormatterMessages extends NLS { public static String CodingStyleConfigurationBlock_delete_confirmation_title; public static String CodingStyleConfigurationBlock_delete_confirmation_question; + public static String CustomCodeFormatterBlock_formatter_name; + public static String CustomCodeFormatterBlock_default_formatter; + public static String CreateProfileDialog_status_message_profile_with_this_name_already_exists; public static String CreateProfileDialog_status_message_profile_name_is_empty; public static String CreateProfileDialog_dialog_title; diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/FormatterMessages.properties b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/FormatterMessages.properties index 69e3a56f21..1ca2f7cef5 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/FormatterMessages.properties +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/FormatterMessages.properties @@ -1,5 +1,5 @@ ############################################################################### -# Copyright (c) 2000, 2013 IBM Corporation and others. +# Copyright (c) 2000, 2014 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 @@ -11,6 +11,7 @@ # Aaron Luchko, aluchko@redhat.com - 105926 [Formatter] Exporting Unnamed profile fails silently # Brock Janiczak <brockj@tpg.com.au> - [formatter] Add option: "add new line after label" - https://bugs.eclipse.org/bugs/show_bug.cgi?id=150741 # Ray V. (voidstar@gmail.com) - Contribution for bug 282988 +# Harry Terkelsen (het@google.com) - Bug 449262 - Allow the use of third-party Java formatters ############################################################################### WhiteSpaceTabPage_assignments=Assignments @@ -566,6 +567,9 @@ CodingStyleConfigurationBlock_error_serializing_xml_message=Problems serializing CodingStyleConfigurationBlock_delete_confirmation_title=Confirm Remove CodingStyleConfigurationBlock_delete_confirmation_question=Are you sure you want to remove profile ''{0}''? +CustomCodeFormatterBlock_formatter_name=&Formatter implementation: +CustomCodeFormatterBlock_default_formatter=[built-in] + CreateProfileDialog_status_message_profile_with_this_name_already_exists=A profile with this name already exists. CreateProfileDialog_status_message_profile_name_is_empty=Profile name is empty CreateProfileDialog_dialog_title=New Profile diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/ProfileConfigurationBlock.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/ProfileConfigurationBlock.java index 482b538407..158cad36de 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/ProfileConfigurationBlock.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/preferences/formatter/ProfileConfigurationBlock.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2011 IBM Corporation and others. + * Copyright (c) 2000, 2014 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 @@ -421,7 +421,7 @@ public abstract class ProfileConfigurationBlock { fNewButton= createButton(fComposite, FormatterMessages.CodingStyleConfigurationBlock_new_button_desc, GridData.HORIZONTAL_ALIGN_BEGINNING); fLoadButton= createButton(fComposite, FormatterMessages.CodingStyleConfigurationBlock_load_button_desc, GridData.HORIZONTAL_ALIGN_END); fExportAllButton= createButton(fComposite, FormatterMessages.CodingStyleConfigurationBlock_export_all_button_desc, GridData.HORIZONTAL_ALIGN_BEGINNING); - createLabel(fComposite, "", 3); //$NON-NLS-1$ + createLabel(fComposite, "", 2); //$NON-NLS-1$ configurePreview(fComposite, numColumns, fProfileManager); |