From 3ae1193d03a6e49aeb26a7620edaf6b99a1d9e6d Mon Sep 17 00:00:00 2001 From: Eric Milles Date: Sun, 8 Jul 2018 21:27:14 -0500 Subject: [PATCH] Fix groovy source image for resources outside classpath (source folders) GroovyImageDecorator moves image selection into GroovyImageProvider (for PackageExplorer) and GroovyNavigatorLabelProvider (for ProjectExplorer). See #388 --- .../META-INF/MANIFEST.MF | 2 +- ide/org.codehaus.groovy.eclipse.ui/plugin.xml | 36 ++++++ .../ui/GroovyNavigatorLabelProvider.java | 79 ++++++++++++ .../ui/decorators/GroovyImageDecorator.java | 42 +++--- .../ui/decorators/GroovyImageProvider.java | 120 ++++++++++++++++++ 5 files changed, 251 insertions(+), 28 deletions(-) create mode 100644 ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/ui/GroovyNavigatorLabelProvider.java create mode 100644 ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/ui/decorators/GroovyImageProvider.java diff --git a/ide/org.codehaus.groovy.eclipse.ui/META-INF/MANIFEST.MF b/ide/org.codehaus.groovy.eclipse.ui/META-INF/MANIFEST.MF index 7d2df53aac..b1b7121991 100644 --- a/ide/org.codehaus.groovy.eclipse.ui/META-INF/MANIFEST.MF +++ b/ide/org.codehaus.groovy.eclipse.ui/META-INF/MANIFEST.MF @@ -47,6 +47,6 @@ Require-Bundle: org.codehaus.groovy;bundle-version="2.1.9", org.eclipse.ui.ide, org.eclipse.ui.views, org.eclipse.ui.workbench.texteditor -Bundle-ActivationPolicy: lazy;exclude:="org.codehaus.groovy.eclipse.ui.decorators" +Bundle-ActivationPolicy: lazy;exclude:="org.codehaus.groovy.eclipse.ui,org.codehaus.groovy.eclipse.ui.decorators" Bundle-Activator: org.codehaus.groovy.eclipse.GroovyPlugin Bundle-RequiredExecutionEnvironment: JavaSE-1.8 diff --git a/ide/org.codehaus.groovy.eclipse.ui/plugin.xml b/ide/org.codehaus.groovy.eclipse.ui/plugin.xml index b8d2827f18..75eafb21b3 100644 --- a/ide/org.codehaus.groovy.eclipse.ui/plugin.xml +++ b/ide/org.codehaus.groovy.eclipse.ui/plugin.xml @@ -778,6 +778,7 @@ id="org.codehaus.groovy.eclipse.ui.findOccurrences" name="Find occurrences in current file"> + + + + + + + + + + + + + + + + + + + + + + + + + + + + scriptFolderSelectors = new HashMap(); // TODO: GroovyParser maintains this same cache + private GroovyImageProvider baseImageProvider = new GroovyImageProvider(); // replaces JavaElementImageProvider + private Map scriptFolderSelectors = new HashMap<>(); // TODO: GroovyParser maintains this same cache private Image applyDefaultDecorator(Image image, Object element) { preventRecursion = true; @@ -58,22 +62,14 @@ private Image applyDefaultDecorator(Image image, Object element) { } } + @Override public Image decorateImage(Image image, Object element) { if (preventRecursion) { return null; } - if (element instanceof GroovyCompilationUnit && image.getBounds().width <= 16) { - // JavaElementImageProvider has overridden the icon tied to the GroovyEditor; need to replace image - image = getJavaElementImage(((GroovyCompilationUnit) element).getResource(), getImageSize(image)); - - // the Java ProblemsDecorator is not registered in the official decorator list, so call it directly - image = problemsDecorator.decorateImage(image, element); - - // add non-problem decor, like type indicator and version control status - image = applyDefaultDecorator(image, element); - - return image; + if (element instanceof GroovyCompilationUnit) { + return decorateImage(new ImageImageDescriptor(image), ((GroovyCompilationUnit) element).getResource(), getImageSize(image)); } boolean isGradle = false; @@ -84,8 +80,8 @@ public Image decorateImage(Image image, Object element) { // Gradle files are a special case; image should be from Buildship plugin if it's present otherwise use Groovy's if (isGradle) { - ImageDescriptor base = (GroovyPluginImages.DESC_GRADLE_FILE != null ? - GroovyPluginImages.DESC_GRADLE_FILE : GroovyPluginImages.DESC_GROOVY_FILE); + ImageDescriptor base = (GroovyPluginImages.DESC_GRADLE_FILE != null + ? GroovyPluginImages.DESC_GRADLE_FILE : GroovyPluginImages.DESC_GROOVY_FILE); return applyDefaultDecorator(getImage(base, 0, getImageSize(image)), element); } @@ -113,16 +109,6 @@ private Image decorateImage(ImageDescriptor base, IResource rsrc, Point size) { return getImage(base, flags, size); } - private Image getJavaElementImage(IResource rsrc, Point size) { - boolean isGradle = (GroovyPluginImages.DESC_GRADLE_FILE != null && ContentTypeUtils.isGradleLikeFileName(rsrc.getName())); - ImageDescriptor base = isGradle ? GroovyPluginImages.DESC_GRADLE_FILE : GroovyPluginImages.DESC_GROOVY_FILE; - if (!isGradle) { - return decorateImage(base, rsrc, size); - } else { - return getImage(base, 0, size); - } - } - private Image getImage(ImageDescriptor base, int flags, Point size) { return JavaPlugin.getImageDescriptorRegistry().get(new JavaElementImageDescriptor(base, flags, size)); } @@ -157,10 +143,12 @@ private boolean isRuntimeCompiled(IResource resource) throws CoreException { //-------------------------------------------------------------------------- + @Override public boolean isLabelProperty(Object element, String property) { return false; } + @Override public String decorateText(String text, Object element) { return null; } diff --git a/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/ui/decorators/GroovyImageProvider.java b/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/ui/decorators/GroovyImageProvider.java new file mode 100644 index 0000000000..0adead8b20 --- /dev/null +++ b/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/ui/decorators/GroovyImageProvider.java @@ -0,0 +1,120 @@ +/* + * Copyright 2009-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.codehaus.groovy.eclipse.ui.decorators; + +import org.codehaus.jdt.groovy.model.GroovyCompilationUnit; +import org.eclipse.core.resources.IFile; +import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.groovy.core.util.ContentTypeUtils; +import org.eclipse.jdt.groovy.core.util.ReflectionUtils; +import org.eclipse.jdt.internal.ui.packageview.PackageExplorerLabelProvider; +import org.eclipse.jdt.internal.ui.packageview.PackageExplorerPart; +import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider; +import org.eclipse.jdt.internal.ui.viewsupport.JavaUILabelProvider; +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.ui.IPartListener2; +import org.eclipse.ui.IWindowListener; +import org.eclipse.ui.IWorkbenchPart; +import org.eclipse.ui.IWorkbenchPartReference; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.PlatformUI; + +public class GroovyImageProvider extends JavaElementImageProvider { + + private IWindowListener listener = new IWindowListener() { + @Override + public void windowOpened(IWorkbenchWindow window) { + window.getActivePage().addPartListener(new IPartListener2() { + @Override + public void partOpened(IWorkbenchPartReference partRef) { + IWorkbenchPart part = partRef.getPart(false); + if (part instanceof PackageExplorerPart) { + enhance((PackageExplorerPart) part); + } + } + + @Override + public void partClosed(IWorkbenchPartReference partRef) { + IWorkbenchPart part = partRef.getPart(false); + if (part instanceof PackageExplorerPart) { + } + } + + @Override + public void partActivated(IWorkbenchPartReference partRef) {} + @Override + public void partDeactivated(IWorkbenchPartReference partRef) {} + @Override + public void partHidden(IWorkbenchPartReference partRef) {} + @Override + public void partVisible(IWorkbenchPartReference partRef) {} + @Override + public void partBroughtToTop(IWorkbenchPartReference partRef) {} + @Override + public void partInputChanged(IWorkbenchPartReference partRef) {} + }); + } + @Override + public void windowClosed(IWorkbenchWindow window) {} + @Override + public void windowActivated(IWorkbenchWindow window) {} + @Override + public void windowDeactivated(IWorkbenchWindow window) {} + }; + + //-------------------------------------------------------------------------- + + public GroovyImageProvider() { + PlatformUI.getWorkbench().addWindowListener(listener); + + listener.windowOpened(PlatformUI.getWorkbench().getActiveWorkbenchWindow()); + + PackageExplorerPart packageExplorer = PackageExplorerPart.getFromActivePerspective(); + if (packageExplorer != null) { + enhance(packageExplorer); + } + } + + public void disconnect() { + PlatformUI.getWorkbench().removeWindowListener(listener); + } + + protected void enhance(PackageExplorerPart packageExplorer) { + PackageExplorerLabelProvider fLabelProvider = ReflectionUtils.getPrivateField(PackageExplorerPart.class, "fLabelProvider", packageExplorer); + if (fLabelProvider != null) { + JavaElementImageProvider fImageLabelProvider = ReflectionUtils.getPrivateField(JavaUILabelProvider.class, "fImageLabelProvider", fLabelProvider); + if (fImageLabelProvider != null && fImageLabelProvider != this) { fImageLabelProvider.dispose(); + ReflectionUtils.setPrivateField(JavaUILabelProvider.class, "fImageLabelProvider", fLabelProvider, this); + } + } + } + + @Override + public ImageDescriptor getCUResourceImageDescriptor(IFile file, int flags) { + if (ContentTypeUtils.isGroovyLikeFileName(file.getName())) { + return GroovyPluginImages.DESC_GROOVY_FILE; + } + return super.getCUResourceImageDescriptor(file, flags); + } + + @Override + public ImageDescriptor getJavaImageDescriptor(IJavaElement element, int flags) { + if (element instanceof GroovyCompilationUnit) { + return GroovyPluginImages.DESC_GROOVY_FILE; + } + return super.getJavaImageDescriptor(element, flags); + } +}