Skip to content

Commit

Permalink
Fix groovy source image for resources outside classpath (source folders)
Browse files Browse the repository at this point in the history
GroovyImageDecorator moves image selection into GroovyImageProvider (for
PackageExplorer) and GroovyNavigatorLabelProvider (for ProjectExplorer).

See #388
  • Loading branch information
eric-milles committed Jul 9, 2018
1 parent 5237482 commit 3ae1193
Show file tree
Hide file tree
Showing 5 changed files with 251 additions and 28 deletions.
2 changes: 1 addition & 1 deletion ide/org.codehaus.groovy.eclipse.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -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
36 changes: 36 additions & 0 deletions ide/org.codehaus.groovy.eclipse.ui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,7 @@
id="org.codehaus.groovy.eclipse.ui.findOccurrences"
name="Find occurrences in current file">
</command>

<command
categoryId="org.codehaus.groovy.eclipse.refactoring.commands.refactoring"
description="Converts a file with a java extension to a file with a groovy extension"
Expand Down Expand Up @@ -935,6 +936,41 @@
</decorator>
</extension>

<extension point="org.eclipse.ui.navigator.navigatorContent">
<navigatorContent
activeByDefault="true"
contentProvider="org.eclipse.jdt.internal.ui.navigator.JavaNavigatorContentProvider"
id="org.codehaus.groovy.eclipse.ui.groovyContent"
labelProvider="org.codehaus.groovy.eclipse.ui.GroovyNavigatorLabelProvider"
name="Groovy Sources"
priority="high">
<triggerPoints>
<or>
<instanceof value="org.codehaus.jdt.groovy.model.GroovyCompilationUnit" />
<instanceof value="org.eclipse.core.resources.IFile" />
</or>
</triggerPoints>
<possibleChildren>
<or>
<instanceof value="org.codehaus.jdt.groovy.model.GroovyCompilationUnit" />
<instanceof value="org.eclipse.core.resources.IFile" />
</or>
</possibleChildren>
<override
policy="InvokeAlwaysRegardlessOfSuppressedExt"
suppressedExtensionId="org.eclipse.jdt.java.ui.javaContent">
</override>
</navigatorContent>
</extension>

<extension point="org.eclipse.ui.navigator.viewer">
<viewerContentBinding viewerId="org.eclipse.ui.navigator.ProjectExplorer">
<includes>
<contentExtension pattern="org.codehaus.groovy.eclipse.ui.groovyContent" />
</includes>
</viewerContentBinding>
</extension>

<extension point="org.eclipse.core.runtime.adapters">
<factory
adaptableType="org.codehaus.jdt.groovy.model.GrabDeclaration"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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;

import static org.eclipse.jdt.internal.ui.JavaPlugin.getImageDescriptorRegistry;

import org.codehaus.groovy.eclipse.ui.decorators.GroovyPluginImages;
import org.codehaus.jdt.groovy.model.GroovyCompilationUnit;
import org.eclipse.core.resources.IFile;
import org.eclipse.jdt.groovy.core.util.ContentTypeUtils;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.graphics.Image;

public class GroovyNavigatorLabelProvider implements ILabelProvider {

@Override
public void addListener(ILabelProviderListener listener) {
}

@Override
public void removeListener(ILabelProviderListener listener) {
}

@Override
public void dispose() {
}

@Override
public Image getImage(Object element) {
if (element instanceof GroovyCompilationUnit) {
return newGroovySourceImage();
}

boolean isGradle = false;
if (element instanceof IFile && ContentTypeUtils.isGroovyLikeFileName(((IFile) element).getName()) &&
!(isGradle = ContentTypeUtils.isGradleLikeFileName(((IFile) element).getName()))) {
return newGroovySourceImage();
}
if (isGradle) {
newGradleScriptImage();
}

return null;
}

@Override
public String getText(Object element) {
return null;
}

@Override
public boolean isLabelProperty(Object element, String property) {
return true;
}

private static Image newGradleScriptImage() {
return getImageDescriptorRegistry().get(
// Gradle files are a special case; image should be from Buildship plugin if it's present otherwise use Groovy's
GroovyPluginImages.DESC_GRADLE_FILE != null ? GroovyPluginImages.DESC_GRADLE_FILE : GroovyPluginImages.DESC_GROOVY_FILE);
}

private static Image newGroovySourceImage() {
return getImageDescriptorRegistry().get(GroovyPluginImages.DESC_GROOVY_FILE);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2017 the original author or authors.
* 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.
Expand Down Expand Up @@ -31,7 +31,6 @@
import org.eclipse.jdt.internal.ui.viewsupport.ImageImageDescriptor;
import org.eclipse.jdt.internal.ui.viewsupport.JavaElementImageProvider;
import org.eclipse.jdt.ui.JavaElementImageDescriptor;
import org.eclipse.jdt.ui.ProblemsLabelDecorator;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.BaseLabelProvider;
import org.eclipse.jface.viewers.ILabelDecorator;
Expand All @@ -41,10 +40,15 @@

public class GroovyImageDecorator extends BaseLabelProvider implements ILabelDecorator {

@Override
public void dispose() {
baseImageProvider.disconnect();
}

private boolean preventRecursion;
private ILabelDecorator defaultDecorator;
private ILabelDecorator problemsDecorator = new ProblemsLabelDecorator();
private Map<IProject, ScriptFolderSelector> scriptFolderSelectors = new HashMap<IProject, ScriptFolderSelector>(); // TODO: GroovyParser maintains this same cache
private GroovyImageProvider baseImageProvider = new GroovyImageProvider(); // replaces JavaElementImageProvider
private Map<IProject, ScriptFolderSelector> scriptFolderSelectors = new HashMap<>(); // TODO: GroovyParser maintains this same cache

private Image applyDefaultDecorator(Image image, Object element) {
preventRecursion = true;
Expand All @@ -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;
Expand All @@ -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);
}

Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 3ae1193

Please sign in to comment.