Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » Having multiple icon for the same entity(Having multiple icon for the same entity)
Having multiple icon for the same entity [message #528610] Wed, 21 April 2010 09:23 Go to next message
Alireza Missing name is currently offline Alireza Missing nameFriend
Messages: 105
Registered: July 2009
Senior Member


Hello,

I have a entity with a self relationship and the resulting diagram are something like an automata with different instances of the entity as its states. I want to be able to have different icon for each unique instance of the entity in resulting automata. Does anybody has any idea to do this?

Best regards,
Ali
Re: Having multiple icon for the same entity [message #528845 is a reply to message #528610] Thu, 22 April 2010 06:55 Go to previous messageGo to next message
Robert Wloch is currently offline Robert WlochFriend
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 #529139 is a reply to message #528845] Fri, 23 April 2010 07:59 Go to previous messageGo to next message
Alireza Missing name is currently offline Alireza Missing nameFriend
Messages: 105
Registered: July 2009
Senior Member


Hello Robert,

Thank you for your answer.
I did as you wrote but it does not work. The problem is about last part I think. I override the mentioned method in FooNameEditPart. My decision to load different images is based on an attribute of the entity (APPLICATION__NAME). I do not know how to change the handleNotificationEvent method. Below you can see my changes pertaining to FooNameEditPart .


@Override
protected void addSemanticListeners() {
addListenerFilter("SemanticModelIcon" + CompanyPackage.APPLICATION__NAME, this,
CompanyPackage.Literals.APPLICATION__NAME);
}

@Override
protected void removeSemanticListeners() {
removeListenerFilter("SemanticModelIcon" + CompanyPackage.APPLICATION__NAME);
}

@Override
protected void handleNotificationEvent(Notification event) {


}


Best,
Alireza
Re: Having multiple icon for the same entity [message #529177 is a reply to message #529139] Fri, 23 April 2010 09:57 Go to previous messageGo to next message
Robert Wloch is currently offline Robert WlochFriend
Messages: 109
Registered: July 2009
Senior Member
Hi Alireza,

your handle notification should look like that:
@Override
protected void handleNotificationEvent(Notification event) {
	Object feature = event.getFeature();
	if (CompanyPackage.Literals.APPLICATION__NAME.equals(feature)) {
		refreshLabel();
	}
	super.handleNotificationEvent(event);
}


I suppose, you tried that and it didn't work for you. The reason might be rather in the getImage() method of custom ItemProvider. To check if that code is executed, set breakpoints to the first line of handleNotificationEvent() and getImage().

From your snipped I believe your getImage() should contain something like the following:
@Override
public Object getImage(Object object) {
	Application entity = (Application) object;
	...
	if ("StateA".equals(entity.getName())) {
		imageKey = "stateA"; // stateA.gif
	} else if ("StateB".equals(entity.getName())) {
		imageKey = "stateB"; // stateB.gif
	} else if ("StateC".equals(entity.getName())) {
		imageKey = "stateC"; // stateC.gif
	}
	images.add(getResourceLocator().getImage(imagePath + "state")); // state.gif
	if (imageKey != null) {
		images.add(getResourceLocator().getImage(imagePath + imageKey));
	}
	return overlayImage(object, new ComposedImage(images));
}


Rob
Re: Having multiple icon for the same entity [message #529209 is a reply to message #529177] Fri, 23 April 2010 13:13 Go to previous messageGo to next message
Alireza Missing name is currently offline Alireza Missing nameFriend
Messages: 105
Registered: July 2009
Senior Member

Hello Robert,
The ApplicationItemProviderCustom is never executed but the is executed.


Here is my different part:



1- in ApplicationNameEditPart


@Override
protected void addSemanticListeners() {
addListenerFilter("SemanticModelIcon" + CompanyPackage.APPLICATION__NAME, this,
CompanyPackage.Literals.APPLICATION__NAME);
}

@Override
protected void removeSemanticListeners() {
removeListenerFilter("SemanticModelIcon" + CompanyPackage.APPLICATION__NAME);
}

@Override
protected void handleNotificationEvent(Notification event) {
Object feature = event.getFeature();
System.out.println("handleNotificationEvent");

if (CompanyPackage.Literals.APPLICATION__NAME.equals(feature)) {
refreshLabel();
}
super.handleNotificationEvent(event);
}


****************************************************

2- in CompanyItemProviderAdapterFactoryOverride

public class CompanyItemProviderAdapterFactoryOverride extends
CompanyItemProviderAdapterFactory {

@Override
public Adapter createEntityAdapter() {
if (applicationItemProvider == null) {
applicationItemProvider = new ApplicationItemProviderCustom(this);
}
return applicationItemProvider;
}


}


*************************************************

3- in ApplicationItemProviderCustom


public class ApplicationItemProviderCustom extends ApplicationItemProvider {

public ApplicationItemProviderCustom(AdapterFactory adapterFactory) {
super(adapterFactory);
// TODO Auto-generated constructor stub
}

@Override
public Object getImage(Object object) {
Application entity = (Application) object;
String imagePath = "full/obj16/";
String imageKey = null;
List<Object> images = new ArrayList<Object>();
if (entity.getName().contains("Firefox")) {
imageKey = "Firefox";
} else {
imageKey = "Application";
}
System.out.println("getImage");
if (imageKey != null) {
images.add(getResourceLocator().getImage(imagePath + imageKey));
}
//images.add(getResourceLocator().getImage(imagePath + "state"));
return overlayImage(object, new ComposedImage(images));
}


}

Re: Having multiple icon for the same entity [message #529285 is a reply to message #529209] Fri, 23 April 2010 17:01 Go to previous messageGo to next message
Robert Wloch is currently offline Robert WlochFriend
Messages: 109
Registered: July 2009
Senior Member
Hi Alireza,

Alireza wrote on Fri, 23 April 2010 09:13

Hello Robert,
The ApplicationItemProviderCustom is never executed but the is executed.


try setting a break point in your CompanyItemProviderAdapterFactoryOverride.createEntityAdapte r() method and it's constructor to check if your FactoryOverride is even called.

Btw, are you sure the factory method's name is "createEntityAdapter" in your case? It is creating an ApplicationItemProvider, so its name should be different? Make sure the following:
1) you override the right method (might be createApplicationAdapter() or similar -> look in the CompanyItemProviderAdapterFactory for the right name)
2) make sure you changed the class value in the itemProviderAdapterFactories extension declaration in you plugin.xml (see my first answer:
Quote:
Open your edit plugin's plugin.xml and change the "class" value of "org.eclipse.emf.edit.itemProviderAdapterFactories" extension point to your custom factory.

). If you miss that, your FactoryOverride will never be instanciated

Rest looks good to me.

Rob
Re: Having multiple icon for the same entity [message #529298 is a reply to message #529285] Fri, 23 April 2010 17:57 Go to previous messageGo to next message
Alireza Missing name is currently offline Alireza Missing nameFriend
Messages: 105
Registered: July 2009
Senior Member


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.

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>


What else do you think may cause this problem?

Best regards,
Alireza
Re: Having multiple icon for the same entity [message #529484 is a reply to message #529298] Mon, 26 April 2010 05:27 Go to previous messageGo to next message
Robert Wloch is currently offline Robert WlochFriend
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
Re: Having multiple icon for the same entity [message #529491 is a reply to message #529484] Mon, 26 April 2010 07:14 Go to previous messageGo to next message
Alireza Missing name is currently offline Alireza Missing nameFriend
Messages: 105
Registered: July 2009
Senior Member


Dear Robert,

Thank you for your answer.

Just one more question. In the getImage code, entity.getName() is null. Any idea why this happens?


@Override
public Object getImage(Object object) {
Application entity = (Application) object;
...
if ("StateA".equals(entity.getName())) {
imageKey = "stateA"; // stateA.gif
} else if ("StateB".equals(entity.getName())) {
imageKey = "stateB"; // stateB.gif
} else if ("StateC".equals(entity.getName())) {
imageKey = "stateC"; // stateC.gif
}
images.add(getResourceLocator().getImage(imagePath + "state")); // state.gif
if (imageKey != null) {
images.add(getResourceLocator().getImage(imagePath + imageKey));
}
return overlayImage(object, new ComposedImage(images));
}

Best,
Alireza
Re: Having multiple icon for the same entity [message #529983 is a reply to message #529491] Tue, 27 April 2010 21:57 Go to previous messageGo to next message
Robert Wloch is currently offline Robert WlochFriend
Messages: 109
Registered: July 2009
Senior Member
Hi Alireza,

there were still a few things missing and that's why it didn't work yet.
But last night I extracted the essential modifications that are required to realize that feature. To spare me the search the next time I need that I decided to write a small example and blog about that: Stateful Icon Provider for GMF Diagrams.

The example plugins are downloadable at the end of the blog post.

Especially the last part should be interesting for you, as it contains the things you still need.

Rob
Re: Having multiple icon for the same entity [message #530144 is a reply to message #529983] Wed, 28 April 2010 16:09 Go to previous messageGo to next message
Alireza Missing name is currently offline Alireza Missing nameFriend
Messages: 105
Registered: July 2009
Senior Member

Hello Robert,

Thank you for weblog. I started to use it, just one more thing: what is this Activator in labelImage method? I can not add required import for it, I do not know how what is its import line. I tried
//import org.eclipse.core.internal.preferences.Activator;
//import org.eclipse.core.internal.content.Activator;
//import org.eclipse.core.internal.runtime.Activator;
//import org.eclipse.core.internal.registry.osgi.Activator;

but they did not worked.

Best,
Alireza
Re: Having multiple icon for the same entity [message #530153 is a reply to message #530144] Wed, 28 April 2010 16:51 Go to previous messageGo to next message
Robert Wloch is currently offline Robert WlochFriend
Messages: 109
Registered: July 2009
Senior Member
Hi Alireza,

If you'd have a look into the zip containing the example from the blog you'd see what the Activator is... Wink

...it is the Activator class defined in the MANIFEST.MF's overview page of the diagram.contribution plugin. Normally, that class is automatically created by the New Plug-in Project Wizard that you use to create plugin projects in your workspace. However, these generated Activators usually don't have a
public static final String PLUGIN_ID = "my.very.own.diagram.contribution"; //$NON-NLS-1$

constant defined. So, you'd need to add that and insert the Plug-in ID string from the MANIFEST.MF file of the same plugin project to make the Activator work with the labelImage method from the blog.

In case you decided to change the generated GMF code in your generated diagram plugin then you should use that plugin's Activator. They're sometimes called *Plugin as they handle a plug-in's life cycle. For the example of the blog that would be
"StatefulElementsDiagramEditorPlugin" located in the "example.gmf.iconupdate.model.core.diagram.part" package.

Rob
Re: Having multiple icon for the same entity [message #530268 is a reply to message #530153] Thu, 29 April 2010 07:34 Go to previous messageGo to next message
Alireza Missing name is currently offline Alireza Missing nameFriend
Messages: 105
Registered: July 2009
Senior Member


Hello Robert,

I fixed the problem and I did as you wrote in the weblog, there is not error but it does not get into new getImage Sad

Any idea?

Best,
Alireza
Re: Having multiple icon for the same entity [message #530284 is a reply to message #530268] Thu, 29 April 2010 08:40 Go to previous messageGo to next message
Alireza Missing name is currently offline Alireza Missing nameFriend
Messages: 105
Registered: July 2009
Senior Member

BTW, I do not understand how the new contribution plugin is connected to generated diagram plugin? Which part make this connection and how overrride parts of diagram plugin in new plugin will be executed?

Thanks,
Best,
Ali
Re: Having multiple icon for the same entity [message #530334 is a reply to message #530284] Thu, 29 April 2010 12:13 Go to previous messageGo to next message
Robert Wloch is currently offline Robert WlochFriend
Messages: 109
Registered: July 2009
Senior Member
Hi Alireza,

from some of your question I suppose you're new to Eclipse plugin development, so: Welcome! If you want to learn about that take some time and study the example plugins from that zip file from my blog. The blog itself assumes some basic knowledge about Eclipse plugin development and hence only the essential steps for the feature realization are described. Maybe you want to attend one of the upcoming Eclipse democamp events, preferably one with a GMF topic. Smile

If you want to understand the whole stateful icon feature, please download the example zip and import its projects into your workspace. If you run those plugins in a runtime workspace, then you can see that it's actually working and compare with your own plugins to check what's different and wrong/missing.

The contribution plugin is connected to the GMF runtime via the declaration of the "org.eclipse.gmf.runtime.diagram.ui.editpartProviders" extension point. The GMF runtime will use the declared derived "StatefulElementsEditPartProviderOverride" instead of the generated one to create the EditParts. Using an overridden provider enables you to have GMF use you derived EditParts that can do custom things.

Rob
Re: Having multiple icon for the same entity [message #530336 is a reply to message #530334] Thu, 29 April 2010 12:28 Go to previous messageGo to next message
Alireza Missing name is currently offline Alireza Missing nameFriend
Messages: 105
Registered: July 2009
Senior Member

Hi Robert,

Ya, you are quite right, i am novice in this Smile Anyway, I created the workspace for your project, there is an error though in diagram plugin and it is about an import line :

import org.eclipse.gmf.runtime.diagram.ui.editpolicies.FlowLayoutEd itPolicy;


I could not find how to resolve it. Any idea?

Best,
Alireza
Re: Having multiple icon for the same entity [message #530385 is a reply to message #530336] Thu, 29 April 2010 14:39 Go to previous messageGo to next message
Robert Wloch is currently offline Robert WlochFriend
Messages: 109
Registered: July 2009
Senior Member
Hi Alireza,

well, don't bother finding that class: It will exist in the upcoming Helios version of GMF that I used to create the example.

You're experiencing your first useful example for the importance of separating generated from hand-written code.

To get the example working do the following:
1) delete the diagram plugin (with contents from disk)
2) in the model project right click the gmfmap file and regenerate the generator model file
3) right click the generator model file and generate the diagram plugin

Enjoy the errors to be gone. There maybe some errors in the contribution plugin's MANIFEST.MF, but if you remove the version stuff at the affected lines and save it, the errors will be gone.

Rob
Re: Having multiple icon for the same entity [message #530429 is a reply to message #530385] Thu, 29 April 2010 15:49 Go to previous messageGo to next message
Alireza Missing name is currently offline Alireza Missing nameFriend
Messages: 105
Registered: July 2009
Senior Member


Hi Robert,

Thank you, it works now. I hope I could find how to do the same in my case.

Best,
Alireza
Re: Having multiple icon for the same entity [message #530539 is a reply to message #530429] Fri, 30 April 2010 05:53 Go to previous message
Alireza Missing name is currently offline Alireza Missing nameFriend
Messages: 105
Registered: July 2009
Senior Member

Hello Robert,

You made it buddy Smile it works. Thank you so much Smile It was my stupid mistake yesterday. How about I send all my future questions to you Smile ? No, really, your explanation both here and in your weblog was perfect and even a person like my with few knowledge about plugin ... could handle it.
Thank again.

Best wishes,

Ali

Previous Topic:Logic Diagram Example not working properly
Next Topic:Where is FlowLayoutEditPolicy
Goto Forum:
  


Current Time: Tue Mar 19 11:13:50 GMT 2024

Powered by FUDForum. Page generated in 0.03308 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top