|
Re: Having multiple icon for the same entity [message #528845 is a reply to message #528610] |
Thu, 22 April 2010 06:55 |
Robert Wloch Messages: 109 Registered: July 2009 |
Senior Member |
|
|
Alireza,
you're lucky that I had to do this recently. You need to change two things: The EntityItemProvider in your model.edit plugin and the EntityNameEditPart in your diagram plugin.
For the first you have two options: Change the generated one or create a custom ItemProvider extending the generated one. I prefer the custom class as it leaves the generated code alone. So I'll show you how to do that.
Let's assume the generated ItemProvider to be foo.provider.EntityItemProvider and it is created using the foo.provider.FooItemProviderAdapterFactory. (You'll need to exchange "foo" to fit your model) Let's further assume you have not changed the paths in your foo.genmodel so the above files are in the "src" folder of you edit plugin.
Now create a new source folder "src-custom" in your edit plugin and inside it create the package "foo.provider". Inside that package create FooItemProviderAdapterFactoryOverride extending the generated one as well as EntityItemProviderCustom extending the generated one, too.
Open your edit plugin's plugin.xml and change the "class" value of "org.eclipse.emf.edit.itemProviderAdapterFactories" extension point to your custom factory. In your custom factory override the createEntityAdapter() method:
@Override
public Adapter createEntityAdapter() {
if (entityItemProvider == null) {
entityItemProvider = new EntityItemProviderCustom(this);
}
return entityItemProvider;
}
To finish step one, you need to override getImage() in you custom item provider. Let's say you have a base image "state.gif" and overlay images "stateA.gif", "stateB.gif", and "stateC.gif". If your states are represented by an Enum "State" in your ecore model and stored in an attribute "state" in your Entity then this this would work:
@Override
public Object getImage(Object object) {
Entity entity = (Entity) object;
String imagePath = "full/obj16/";
String baseKey = "state";
String imageKey = null;
List<Object> images = new ArrayList<Object>();
if (State.A.equals(entity.getState())) {
imageKey = baseKey + "A";
} else if (State.B.equals(entity.getState())) {
imageKey = baseKey + "B";
} else if (State.C.equals(entity.getState())) {
imageKey = baseKey + "C";
}
if (imageKey != null) {
images.add(getResourceLocator().getImage(imagePath + imageKey));
}
images.add(getResourceLocator().getImage(imagePath + "state"));
return overlayImage(object, new ComposedImage(images));
}
The first part is complete. By default GMF only listens to features that yould alter the text of labels and triggers a label refresh() if they do. In your case you'll need to tell your EntityNameEditPart to refresh itself upon state changes, too. You've two options again: Change the generated code directly or create a custom edit part. Again, I prefer the latter, but as it's essentially similar to what I did above I'll leave it up to you to figure that out. So here's what your diagram.foo.edit.parts.EntityNameEditPart[Override] would have to look like:
@Override
protected void addSemanticListeners() {
addListenerFilter("SemanticModelIcon" + FooPackage.ENTITY__STATE, this,
FooPackage.Literals.ENTITY__STATE);
}
@Override
protected void removeSemanticListeners() {
removeListenerFilter("SemanticModelIcon" + FooPackage.ENTITY__STATE);
}
@Override
protected void handleNotificationEvent(Notification event) {
Object feature = event.getFeature();
if (FooPackage.Literals.ENTITY__STATE.equals(feature)) {
refreshLabel();
}
super.handleNotificationEvent(event);
}
This works for me very well.
Rgrds
Rob
|
|
|
|
|
|
|
|
Re: Having multiple icon for the same entity [message #529484 is a reply to message #529298] |
Mon, 26 April 2010 05:27 |
Robert Wloch Messages: 109 Registered: July 2009 |
Senior Member |
|
|
Hi Alireza,
comments below
Alireza wrote on Fri, 23 April 2010 13:57 |
Hi Robert,
About createApplicationAdapter, you are right, I fixed it. About plugin.xml it is the same way as you mentioned in your first reply. Still it can not get into CompanyItemProviderAdapterFactoryOverride constructor and its createApplicationAdapter's method.
|
Did you create a default constructor? If so you need to change it so it overrides the super class's constructor.
Alireza wrote on Fri, 23 April 2010 13:57 |
here is my plugin.xml file :
<plugin>
<extension point="org.eclipse.emf.edit.itemProviderAdapterFactories">
<factory
uri="http://www.example.com/company"
class=" company.provider.custom.CompanyItemProviderAdapterFactoryOve rride "
supportedTypes=
"org.eclipse.emf.edit.provider.IEditingDomainItemProvider
org.eclipse.emf.edit.provider.IStructuredItemContentProvider
org.eclipse.emf.edit.provider.ITreeItemContentProvider
org.eclipse.emf.edit.provider.IItemLabelProvider
org.eclipse.emf.edit.provider.IItemPropertySource"/>
</extension>
</plugin>
|
That seems to be okay.
Alireza wrote on Fri, 23 April 2010 13:57 |
What else do you think may cause this problem?
|
As the issue is the overriden edit plugin factory not being called I think Ed Merks from the EMF forum may have further ideas.
Alireza wrote on Fri, 23 April 2010 13:57 |
Best regards,
Alireza
|
Sorry, Alireza, but I'm out of ideas for the moment. Of course, you can change the generated ItemProviderFactory and change the comment to "@generated NOT", although it mixes generated with manual code. This way, you can get it running, too, and check, if your icon refresh mechanism actually works.
Rob
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02131 seconds