Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » How to skip a model hierarchy by modifying the ItemProviders
How to skip a model hierarchy by modifying the ItemProviders [message #540299] Tue, 15 June 2010 14:51 Go to next message
Michael Jastram is currently offline Michael JastramFriend
Messages: 235
Registered: April 2010
Location: Düsseldorf, Germany
Senior Member
Howdy,

I actually got this to work - kind of - but I suspect that I didn't do it "right", as this doesn't work correctly all the time. Here is my issue:

Here is the relevant part of my model:

ReqIF (root)
   ReqIFContent (1)
      Specifications (0..*)

In the UI, I'd like to omit ReqIFContent. In short, this is what I would like:

ReqIF (root)
   Specifications (0..*)

To achieve this, I performed the following changes:

In ReqIFItemProvider:

        protected Collection<? extends EStructuralFeature> getChildrenFeatures(
			Object object) {
		if (childrenFeatures == null) {
			super.getChildrenFeatures(object);
			childrenFeatures
			.add(ReqIFPackage.Literals.REQ_IF_CONTENT__SPECIFICATIONS);
		}
		return childrenFeatures;
	}

By adding this code, Specifications are shown as children of the ReqIF. But I don't understand why this works in the first place: Is EMF smart enough to figure out that the provided Object has exactly one child of type ReqIFContent? And takes its children?!?

Next, I need to be able to add child elements to ReqIF. I managed this by overriding createCommand and collectNewChildDescriptors in ReqIFItemProvider:

	protected void collectNewChildDescriptors(
			Collection<Object> newChildDescriptors, Object object) {
		super.collectNewChildDescriptors(newChildDescriptors, object);
		newChildDescriptors.add(createChildParameter(
				ReqIFPackage.Literals.REQ_IF_CONTENT__SPECIFICATIONS,
				ReqIFFactory.eINSTANCE.createSpecification()));
	}

	public Command createCommand(Object object, EditingDomain domain,
			Class<? extends Command> commandClass,
			CommandParameter commandParameter) {
		if (commandClass == AddCommand.class
				&& commandParameter.feature == ReqIFPackage.Literals.REQ_IF_CONTENT__SPECIFICATIONS) {
			return createAddCommand(domain, ((ReqIF) object).getCoreContent(),
					commandParameter.getEStructuralFeature(), commandParameter
							.getCollection(), commandParameter.getIndex());
		} else {
			return super.createCommand(object, domain, commandClass,
					commandParameter);
		}
	}

This I understand and makes sense to me. Smile

My bigger problem is the "other side" - what has to happen in SpecificationItemProvider. First, I want ReqIF to be the parent of specifications: I added

	public Object getParent(Object object) {
		return ((Specification) object).getCoreContent().getDocumentRoot();
	}

However, this will result in a NullPointerException! I could track it down to the structural feature being null in BasicEObjectImpl (which makes sense, as ReqIF doesn't have the feature "Specifications").

Here things get ugly: I could fix this by modifying eGet in ReqIFImpl:

	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
		case ReqIFPackage.REQ_IF__CORE_CONTENT:
			return getCoreContent();
		case ReqIFPackage.REQ_IF__TOOL_EXTENSIONS:
			return getToolExtensions();
		case ReqIFPackage.REQ_IF_CONTENT__SPECIFICATIONS:
			return getCoreContent().getSpecifications();
		}
		return super.eGet(featureID, resolve, coreType);
	}

While this works, I know that this is not the right approach! The whole idea is to leave the model alone and to only change the providers. Does anybody know the "correct" approach?

Last, the Delete-Option on Specification is grayed out (no matter whether getParent() is overridden or not). I assume that this is related.

Thanks in advance for any insights!

- Michael
Re: How to skip a model hierarchy by modifying the ItemProviders [message #540398 is a reply to message #540299] Wed, 16 June 2010 01:50 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33140
Registered: July 2009
Senior Member
Michael,

Comments below.


Michael Jastram wrote:
> Howdy,
>
> I actually got this to work - kind of - but I suspect that I didn't do
> it "right", as this doesn't work correctly all the time. Here is my
> issue:
>
> Here is the relevant part of my model:
>
>
> ReqIF (root)
> ReqIFContent (1)
> Specifications (0..*)
>
> In the UI, I'd like to omit ReqIFContent. In short, this is what I
> would like:
>
>
> ReqIF (root)
> Specifications (0..*)
>
> To achieve this, I performed the following changes:
>
> In ReqIFItemProvider:
>
>
> protected Collection<? extends EStructuralFeature>
> getChildrenFeatures(
> Object object) {
> if (childrenFeatures == null) {
> super.getChildrenFeatures(object);
> childrenFeatures
> .add(ReqIFPackage.Literals.REQ_IF_CONTENT__SPECIFICATIONS);
These all need to be features of the object's which I think is a REQ_IF
so this doesn't seem good.
> }
> return childrenFeatures;
> }
>
> By adding this code, Specifications are shown as children of the
> ReqIF. But I don't understand why this works in the first place: Is
> EMF smart enough to figure out that the provided Object has exactly
> one child of type ReqIFContent? And takes its children?!?
No.
>
> Next, I need to be able to add child elements to ReqIF. I managed
> this by overriding createCommand and collectNewChildDescriptors in
> ReqIFItemProvider:
>
>
> protected void collectNewChildDescriptors(
> Collection<Object> newChildDescriptors, Object object) {
> super.collectNewChildDescriptors(newChildDescriptors, object);
> newChildDescriptors.add(createChildParameter(
> ReqIFPackage.Literals.REQ_IF_CONTENT__SPECIFICATIONS,
Again, I'm not sure I understand how this could work.
> ReqIFFactory.eINSTANCE.createSpecification()));
> }
>
> public Command createCommand(Object object, EditingDomain domain,
> Class<? extends Command> commandClass,
> CommandParameter commandParameter) {
> if (commandClass == AddCommand.class
> && commandParameter.feature ==
> ReqIFPackage.Literals.REQ_IF_CONTENT__SPECIFICATIONS) {
> return createAddCommand(domain, ((ReqIF)
> object).getCoreContent(),
What if this feature's value is null?
> commandParameter.getEStructuralFeature(),
> commandParameter
> .getCollection(),
> commandParameter.getIndex());
> } else {
> return super.createCommand(object, domain, commandClass,
> commandParameter);
> }
> }
>
> This I understand and makes sense to me. :)
Yes.
>
> My bigger problem is the "other side" - what has to happen in
> SpecificationItemProvider. First, I want ReqIF to be the parent of
> specifications: I added
>
>
> public Object getParent(Object object) {
> return ((Specification)
> object).getCoreContent().getDocumentRoot();
Hmmm. Is it really a document root?
> }
>
> However, this will result in a NullPointerException! I could track it
> down to the structural feature being null in BasicEObjectImpl (which
> makes sense, as ReqIF doesn't have the feature "Specifications").
>
> Here things get ugly: I could fix this by modifying eGet in ReqIFImpl:
>
>
> public Object eGet(int featureID, boolean resolve, boolean
> coreType) {
> switch (featureID) {
> case ReqIFPackage.REQ_IF__CORE_CONTENT:
> return getCoreContent();
> case ReqIFPackage.REQ_IF__TOOL_EXTENSIONS:
> return getToolExtensions();
> case ReqIFPackage.REQ_IF_CONTENT__SPECIFICATIONS:
> return getCoreContent().getSpecifications();
This doesn't make sense.
> }
> return super.eGet(featureID, resolve, coreType);
> }
>
> While this works, I know that this is not the right approach!
Nope.
> The whole idea is to leave the model alone and to only change the
> providers.
Yes.
> Does anybody know the "correct" approach?
I'm trying to remember if there was a section in the book about this. I
know there was a section for introducing a node not in the actual tree.
If the newsgroup search was better (search just the EMF newsgroup) you
could find previous answers to this more easily.
>
> Last, the Delete-Option on Specification is grayed out (no matter
> whether getParent() is overridden or not). I assume that this is
> related.
>
> Thanks in advance for any insights!
There are so many things to explain it would take an hour, and I'm
traveling and tired... I think you should be specializing getChildren
and getParent, and then specialize the various comments to do the right
things. E.g., AddCommand would add to the intermediate object and would
create it if necessary...
>
> - Michael


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: How to skip a model hierarchy by modifying the ItemProviders [message #540437 is a reply to message #540398] Wed, 16 June 2010 07:39 Go to previous message
Michael Jastram is currently offline Michael JastramFriend
Messages: 235
Registered: April 2010
Location: Düsseldorf, Germany
Senior Member
Hi Ed,

Thanks for taking the time to respond, in spite of...

> I'm traveling and tired...

> I'm trying to remember if there was a section in the book about this.

In Chapter 19, you introduce non-modeled nodes. This is similar to what I am doing, but not exactly the same. I definitely took some inspiration from that chapter.

> If the newsgroup search was better (search just the EMF newsgroup) you
> could find previous answers to this more easily.

Yeah, I was trying a newgroup and Google search - it didn't produce much useful information.

Anyway, thanks for your comments, they certainly give me some pointers, especially in terms of what and what not to do. I suspect some of the unexpected behavior may stem from some other changes I did to the generated code. So I may actually start over with newly generated code.

Best,

- Michael
Previous Topic:java.lang.ClassCastException: java.lang.String cannot be cast to org.eclipse.emf.ecore.InternalEObje
Next Topic:inner class
Goto Forum:
  


Current Time: Tue Apr 23 10:20:33 GMT 2024

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

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

Back to the top