diff options
author | angelozerr | 2018-02-07 12:16:42 +0000 |
---|---|---|
committer | Noopur Gupta | 2018-03-21 16:56:50 +0000 |
commit | 1332a4312cbbf390f92cc692ec6eae2904899c43 (patch) | |
tree | 615bbdf458cd11535467a694b5866f5e6d261be3 | |
parent | 6cd5bd624cf4023ceb02ba3ab8de84a223245eab (diff) | |
download | eclipse.jdt.ui-1332a4312cbbf390f92cc692ec6eae2904899c43.tar.gz eclipse.jdt.ui-1332a4312cbbf390f92cc692ec6eae2904899c43.tar.xz eclipse.jdt.ui-1332a4312cbbf390f92cc692ec6eae2904899c43.zip |
Bug 530825 - [CodeMining] Update CodeMinings withI20180321-2000
IJavaReconcilingListener
Change-Id: Ic8b2607018befbb6e7e53e4f8c26d670816b4375
Signed-off-by: angelozerr <angelo.zerr@gmail.com>
4 files changed, 296 insertions, 1 deletions
diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaCodeMiningManager.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaCodeMiningManager.java new file mode 100644 index 0000000000..a4d393fd10 --- /dev/null +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaCodeMiningManager.java @@ -0,0 +1,114 @@ +/******************************************************************************* + * Copyright (c) 2018 Angelo ZERR. + * 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: + * Angelo Zerr <angelo.zerr@gmail.com> - [CodeMining] Update CodeMinings with IJavaReconcilingListener - Bug 530825 + *******************************************************************************/ +package org.eclipse.jdt.internal.ui.javaeditor; + +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.jface.util.IPropertyChangeListener; +import org.eclipse.jface.util.PropertyChangeEvent; + +/** + * @since 3.14 + */ +public class JavaCodeMiningManager implements IPropertyChangeListener { + + /** The editor */ + private JavaEditor fEditor; + + /** The source viewer */ + private JavaSourceViewer fSourceViewer; + + /** Java code mining reconciler */ + private JavaCodeMiningReconciler fReconciler; + + /** The preference store */ + private IPreferenceStore fPreferenceStore; + + /** + * Installs the Java code mining manager on the given editor infrastructure. + * + * @param editor the Java editor + * @param sourceViewer the source viewer + * @param preferenceStore the preference store + */ + public void install(JavaEditor editor, JavaSourceViewer sourceViewer, IPreferenceStore preferenceStore) { + fEditor= editor; + fSourceViewer= sourceViewer; + fPreferenceStore= preferenceStore; + + fPreferenceStore.addPropertyChangeListener(this); + + if (isJavaCodeMiningEnabled()) { + enable(); + } + } + + /** + * Enable Java code mining manager. + */ + private void enable() { + if (fEditor != null) { + fReconciler= new JavaCodeMiningReconciler(); + fReconciler.install(fEditor, fSourceViewer); + } + } + + /** + * Uninstall Java code mining manager. + */ + public void uninstall() { + disable(); + + if (fPreferenceStore != null) { + fPreferenceStore.removePropertyChangeListener(this); + fPreferenceStore= null; + } + + fEditor= null; + fSourceViewer= null; + } + + /** + * Disable Java code mining manager. + */ + private void disable() { + if (fReconciler != null) { + fReconciler.uninstall(); + fReconciler= null; + } + } + + /** + * @return <code>true</code> iff Java code mining is enabled in the preferences + */ + private boolean isJavaCodeMiningEnabled() { + return fEditor != null && fEditor.isJavaCodeMiningEnabled(); + } + + @Override + public void propertyChange(PropertyChangeEvent event) { + handlePropertyChangeEvent(event); + } + + private void handlePropertyChangeEvent(PropertyChangeEvent event) { + if (fPreferenceStore == null) { + return; // Uninstalled during event notification + } + + if (fEditor != null && fEditor.affectsJavaCodeMining(event)) { + if (isJavaCodeMiningEnabled()) { + enable(); + } else { + disable(); + } + } + } + +} diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaCodeMiningReconciler.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaCodeMiningReconciler.java new file mode 100644 index 0000000000..fa4f8aca76 --- /dev/null +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaCodeMiningReconciler.java @@ -0,0 +1,79 @@ +/******************************************************************************* + * Copyright (c) 2018 Angelo ZERR. + * 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: + * Angelo Zerr <angelo.zerr@gmail.com> - [CodeMining] Update CodeMinings with IJavaReconcilingListener - Bug 530825 + *******************************************************************************/ +package org.eclipse.jdt.internal.ui.javaeditor; + +import org.eclipse.core.runtime.IProgressMonitor; + +import org.eclipse.jface.text.source.ISourceViewer; +import org.eclipse.jface.text.source.ISourceViewerExtension5; + +import org.eclipse.jdt.core.dom.CompilationUnit; + +import org.eclipse.jdt.internal.ui.text.java.IJavaReconcilingListener; + +/** + * @since 3.14 + */ +public class JavaCodeMiningReconciler implements IJavaReconcilingListener { + + /** The Java editor this Java code mining reconciler is installed on */ + private JavaEditor fEditor; + + /** The source viewer this Java code mining reconciler is installed on */ + private ISourceViewer fSourceViewer; + + @Override + public void reconciled(CompilationUnit ast, boolean forced, IProgressMonitor progressMonitor) { + updateCodeMinings(); + } + + @Override + public void aboutToBeReconciled() { + // Do nothing + } + + /** + * Install this reconciler on the given editor. + * + * @param editor the editor + * @param sourceViewer the source viewer + */ + public void install(JavaEditor editor, ISourceViewer sourceViewer) { + fEditor= editor; + fSourceViewer= sourceViewer; + + if (fEditor instanceof CompilationUnitEditor) { + ((CompilationUnitEditor) fEditor).addReconcileListener(this); + updateCodeMinings(); + } + } + + /** + * Uninstall this reconciler from the editor. + */ + public void uninstall() { + if (fEditor instanceof CompilationUnitEditor) { + ((CompilationUnitEditor) fEditor).removeReconcileListener(this); + } + fEditor= null; + fSourceViewer= null; + } + + /** + * Update Java code mining in the Java editor. + */ + private void updateCodeMinings() { + if (fSourceViewer instanceof ISourceViewerExtension5) { + ((ISourceViewerExtension5) fSourceViewer).updateCodeMinings(); + } + } + +} diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaEditor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaEditor.java index 3faf272219..26350688cd 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaEditor.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/javaeditor/JavaEditor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2017 IBM Corporation and others. + * Copyright (c) 2000, 2018 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 @@ -10,6 +10,7 @@ * Tom Eicher <eclipse@tom.eicher.name> - [formatting] 'Format Element' in JavaDoc does also format method body - https://bugs.eclipse.org/bugs/show_bug.cgi?id=238746 * Tom Eicher (Avaloq Evolution AG) - block selection mode * Stefan Xenos (sxenos@gmail.com) - bug 306646, make editor margins follow the java formatter preference + * Angelo Zerr <angelo.zerr@gmail.com> - [CodeMining] Update CodeMinings with IJavaReconcilingListener - Bug 530825 *******************************************************************************/ package org.eclipse.jdt.internal.ui.javaeditor; @@ -100,6 +101,7 @@ import org.eclipse.jface.text.Position; import org.eclipse.jface.text.Region; import org.eclipse.jface.text.TextSelection; import org.eclipse.jface.text.TextUtilities; +import org.eclipse.jface.text.codemining.ICodeMiningProvider; import org.eclipse.jface.text.link.LinkedModeModel; import org.eclipse.jface.text.link.LinkedPosition; import org.eclipse.jface.text.reconciler.IReconciler; @@ -113,6 +115,7 @@ import org.eclipse.jface.text.source.ICharacterPairMatcher; import org.eclipse.jface.text.source.IOverviewRuler; import org.eclipse.jface.text.source.ISourceViewer; import org.eclipse.jface.text.source.ISourceViewerExtension2; +import org.eclipse.jface.text.source.ISourceViewerExtension5; import org.eclipse.jface.text.source.IVerticalRuler; import org.eclipse.jface.text.source.IVerticalRulerColumn; import org.eclipse.jface.text.source.LineChangeHover; @@ -1756,6 +1759,13 @@ public abstract class JavaEditor extends AbstractDecoratedTextEditor implements private List<IRegion> fPreviousSelections; /** + * Java code mining manager + * + * @since 3.14 + */ + private JavaCodeMiningManager fJavaCodeMiningManager; + + /** * Returns the most narrow java element including the given offset. * * @param offset the offset inside of the requested element @@ -2661,6 +2671,8 @@ public abstract class JavaEditor extends AbstractDecoratedTextEditor implements fBreadcrumb= null; } + uninstallJavaCodeMining(); + super.dispose(); fSelectionProvider= null; } @@ -2929,6 +2941,15 @@ public abstract class JavaEditor extends AbstractDecoratedTextEditor implements return; } + if (affectsJavaCodeMining(event)) { + if (isJavaCodeMiningEnabled()) { + installJavaCodeMining(); + } else { + uninstallJavaCodeMining(); + } + return; + } + if (JavaCore.COMPILER_SOURCE.equals(property)) { if (event.getNewValue() instanceof String) fBracketMatcher.setSourceVersion((String) event.getNewValue()); @@ -3104,6 +3125,10 @@ public abstract class JavaEditor extends AbstractDecoratedTextEditor implements if (fIsBreadcrumbVisible) showBreadcrumb(); + if (isJavaCodeMiningEnabled()) { + installJavaCodeMining(); + } + PlatformUI.getWorkbench().addWindowListener(fActivationListener); } @@ -4253,4 +4278,74 @@ public abstract class JavaEditor extends AbstractDecoratedTextEditor implements } } + /** + * Install Java code mining. + * + * @since 3.14 + */ + private void installJavaCodeMining() { + if (fJavaCodeMiningManager == null) { + fJavaCodeMiningManager= new JavaCodeMiningManager(); + fJavaCodeMiningManager.install(this, (JavaSourceViewer) getSourceViewer(), getPreferenceStore()); + } + } + + /** + * Uninstall Java code mining. + * + * @since 3.14 + */ + private void uninstallJavaCodeMining() { + if (fJavaCodeMiningManager != null) { + fJavaCodeMiningManager.uninstall(); + fJavaCodeMiningManager= null; + } + } + + /** + * @return <code>true</code> if Java code mining is enabled. + * + * @since 3.14 + */ + boolean isJavaCodeMiningEnabled() { + // check if there is a registered Java code mining. + ISourceViewer viewer= getViewer(); + if (viewer instanceof ISourceViewerExtension5) { + return ((ISourceViewerExtension5) viewer).hasCodeMiningProviders(); + } + return false; + } + + /** + * Determines whether the preference change encoded by the given event changes the Java code mining. + * + * @param event the event to be investigated + * @return <code>true</code> if event causes a change + * @since 3.14 + */ + protected boolean affectsJavaCodeMining(PropertyChangeEvent event) { + boolean isJavaCodeMiningPreference= isJavaCodeMiningPreference(event.getProperty()); + if (isJavaCodeMiningPreference) { + // It's a code mining preference, recompute the list of code mining providers. + installCodeMinigProviders(); + } + return isJavaCodeMiningPreference; + } + + /** + * Note that by convention, a Java code mining preference starts with + * {@link PreferenceConstants#EDITOR_JAVA_CODEMINING_PREFIX}. It provides the capability for + * external plug-ins to contribute with a custom {@link ICodeMiningProvider} and refresh the Java + * Editor mining when preferences changed. + * + * @param property the name of the preference property that changed + * + * @return <code>true</code> if the given property is a Java code mining preference. + * + * @since 3.14 + */ + private boolean isJavaCodeMiningPreference(String property) { + return property.startsWith(PreferenceConstants.EDITOR_JAVA_CODEMINING_PREFIX); + } + } diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/PreferenceConstants.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/PreferenceConstants.java index 69596e2270..b0c7af31c6 100644 --- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/PreferenceConstants.java +++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/ui/PreferenceConstants.java @@ -2511,6 +2511,13 @@ public class PreferenceConstants { public static final String EDITOR_SEMANTIC_HIGHLIGHTING_PREFIX="semanticHighlighting."; //$NON-NLS-1$ /** + * A named preference prefix for Java code mining preferences. + * + * @since 3.14 + */ + public static final String EDITOR_JAVA_CODEMINING_PREFIX="java.codemining."; //$NON-NLS-1$ + + /** * A named preference that controls if semantic highlighting is enabled. * <p> * Value is of type <code>Boolean</code>:<code>true</code> if enabled. |