| author | szarnekow | 2009-03-26 14:02:48 (EDT) |
|---|---|---|
| committer | sefftinge | 2009-03-26 14:02:48 (EDT) |
| commit | c2450309c490284a42b5a6c58523bb3680a6ca28 (patch) (side-by-side diff) | |
| tree | a960d6eae6ef1809bc3d7217465c92ebff0d8dac | |
| parent | 91d3d2dbbdd4d54998a520f9c80b149d5809fa2e (diff) | |
| download | org.eclipse.xtext-c2450309c490284a42b5a6c58523bb3680a6ca28.zip org.eclipse.xtext-c2450309c490284a42b5a6c58523bb3680a6ca28.tar.gz org.eclipse.xtext-c2450309c490284a42b5a6c58523bb3680a6ca28.tar.bz2 | |
Simplified DefaultLabelProvider.image(..), decoupled from Activator singleton
4 files changed, 176 insertions, 8 deletions
diff --git a/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/service/UIPluginModule.java b/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/service/UIPluginModule.java new file mode 100644 index 0000000..1fe2d1a --- a/dev/null +++ b/plugins/org.eclipse.xtext.ui.common/src/org/eclipse/xtext/ui/common/service/UIPluginModule.java @@ -0,0 +1,36 @@ +/******************************************************************************* + * Copyright (c) 2008 itemis AG (http://www.itemis.eu) 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 + *******************************************************************************/ +package org.eclipse.xtext.ui.common.service; + +import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.eclipse.xtext.service.AbstractGenericModule; +import org.eclipse.xtext.ui.core.IImageHelper; +import org.eclipse.xtext.ui.core.PluginImageHelper; + +import com.google.inject.Binder; + +/** + * @author Sebastian Zarnekow + */ +public class UIPluginModule extends AbstractGenericModule { + + private AbstractUIPlugin plugin; + + public UIPluginModule(AbstractUIPlugin plugin) { + this.plugin = plugin; + } + + public void configure(Binder binder) { + super.configure(binder); + binder.bind(AbstractUIPlugin.class).toInstance(plugin); + } + + public Class<? extends IImageHelper> bindIImageHelper() { + return PluginImageHelper.class; + } +} diff --git a/plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/DefaultLabelProvider.java b/plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/DefaultLabelProvider.java index eebbd2f..a535648 100644 --- a/plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/DefaultLabelProvider.java +++ b/plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/DefaultLabelProvider.java @@ -1,3 +1,10 @@ +/******************************************************************************* + * Copyright (c) 2008 itemis AG (http://www.itemis.eu) 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 + *******************************************************************************/ package org.eclipse.xtext.ui.core; import java.util.Collections; @@ -12,9 +19,12 @@ import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.swt.graphics.Image; import org.eclipse.xtext.util.PolymorphicDispatcher; +import com.google.inject.Inject; + /** * @author Sven Efftinge - Initial contribution and API * @author Peter Friese - Implementation + * @author Sebastian Zarnekow */ public class DefaultLabelProvider extends LabelProvider { @@ -28,16 +38,19 @@ public class DefaultLabelProvider extends LabelProvider { } }); - private final PolymorphicDispatcher<Image> getImage = new PolymorphicDispatcher<Image>("image",1,1,Collections.singletonList(this), - new PolymorphicDispatcher.ErrorHandler<Image>(){ + private final PolymorphicDispatcher<String> getImage = new PolymorphicDispatcher<String>("image",1,1,Collections.singletonList(this), + new PolymorphicDispatcher.ErrorHandler<String>(){ - private final PolymorphicDispatcher<Image> recoverImage = new PolymorphicDispatcher<Image>("error_image",2,2,Collections.singletonList(DefaultLabelProvider.this)); + private final PolymorphicDispatcher<String> recoverImage = new PolymorphicDispatcher<String>("error_image",2,2,Collections.singletonList(DefaultLabelProvider.this)); - public Image handle(Object[] params, Throwable e) { + public String handle(Object[] params, Throwable e) { return recoverImage.invoke(params[0],e); } }); + @Inject + private IImageHelper imageHelper; + @Override public final String getText(Object element) { return getText.invoke(element); @@ -45,7 +58,8 @@ public class DefaultLabelProvider extends LabelProvider { @Override public final Image getImage(Object element) { - return getImage.invoke(element); + String imageName = getImage.invoke(element); + return imageHelper.getImage(imageName); } public String error_text(Object object, Exception e) { @@ -73,13 +87,13 @@ public class DefaultLabelProvider extends LabelProvider { return "<unknown>"; } - public Image error_image(Object object, Exception e) { + public String error_image(Object object, Exception e) { throw new WrappedException(e); } - public Image error_image(Object object, NullPointerException e) { + public String error_image(Object object, NullPointerException e) { return image(object); } - public Image image(Object obj) { + public String image(Object obj) { return null; } @@ -102,4 +116,5 @@ public class DefaultLabelProvider extends LabelProvider { } return result; } + } diff --git a/plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/IImageHelper.java b/plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/IImageHelper.java new file mode 100644 index 0000000..f3b41f4 --- a/dev/null +++ b/plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/IImageHelper.java @@ -0,0 +1,29 @@ +/******************************************************************************* + * Copyright (c) 2008 itemis AG (http://www.itemis.eu) 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 + *******************************************************************************/ +package org.eclipse.xtext.ui.core; + +import org.eclipse.swt.graphics.Image; + +import com.google.inject.ImplementedBy; + +/** + * @author Sebastian Zarnekow + */ +@ImplementedBy(IImageHelper.NullImageHelper.class) +public interface IImageHelper { + + Image getImage(String name); + + class NullImageHelper implements IImageHelper { + + public Image getImage(String name) { + return null; + } + + } +} diff --git a/plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/PluginImageHelper.java b/plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/PluginImageHelper.java new file mode 100644 index 0000000..e0942df --- a/dev/null +++ b/plugins/org.eclipse.xtext.ui.core/src/org/eclipse/xtext/ui/core/PluginImageHelper.java @@ -0,0 +1,88 @@ +/******************************************************************************* + * Copyright (c) 2008 itemis AG (http://www.itemis.eu) 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 + *******************************************************************************/ +package org.eclipse.xtext.ui.core; + +import java.net.URL; + +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.swt.graphics.Image; +import org.eclipse.ui.plugin.AbstractUIPlugin; + +import com.google.inject.Inject; +import com.google.inject.name.Named; + +/** + * @author Sebastian Zarnekow + */ +public class PluginImageHelper implements IImageHelper { + + @Inject + private AbstractUIPlugin plugin; + + @Inject(optional=true) + @Named("org.eclipse.xtext.ui.core.PluginImageHelper.pathSuffix") + private String pathSuffix = "icons/"; + + @Inject(optional=true) + @Named("org.eclipse.xtext.ui.core.PluginImageHelper.defaultImage") + private String defaultImage = "default.gif"; + + @Inject(optional=true) + @Named("org.eclipse.xtext.ui.core.PluginImageHelper.notFound") + private String notFound = "notFound.gif"; + + public Image getImage(String imgname) { + if (imgname == null) + imgname = defaultImage; + if (imgname != null) { + Image result = null; + URL imgUrl = getPlugin().getBundle().getEntry(getPathSuffix() + imgname); + if (imgUrl != null) { + ImageDescriptor id = null; + result = getPlugin().getImageRegistry().get(imgUrl.toExternalForm()); + if (result == null) { + id = ImageDescriptor.createFromURL(imgUrl); + if (id != null) { + result = id.createImage(); + getPlugin().getImageRegistry().put(imgUrl.toExternalForm(), result); + } + } + return result; + } else { + if (!imgname.equals(notFound)) { + return getImage(notFound); + } + } + } + return null; + } + + public void setPathSuffix(String pathSuffix) { + this.pathSuffix = pathSuffix; + } + + public String getPathSuffix() { + return pathSuffix; + } + + public void setPlugin(AbstractUIPlugin plugin) { + this.plugin = plugin; + } + + public AbstractUIPlugin getPlugin() { + return plugin; + } + + public void setNotFound(String notFound) { + this.notFound = notFound; + } + + public String getNotFound() { + return notFound; + } +} |

