Home » Modeling » EMF » Create own PropertyDescriptor
|
Re: Create own PropertyDescriptor [message #1237689 is a reply to message #1237435] |
Thu, 30 January 2014 10:30 |
Christophe Bouhier Messages: 937 Registered: July 2009 |
Senior Member |
|
|
On 29-01-14 19:17, Phil H wrote:
> Hi,
>
> I want to create my own dialog instead the standard property descriptor. This dialog should include two buttons for adding and deleting elements. The elements are the following:
>
>
> class FM{
> unsettable attr EString name = "FM";
>
> ref Constraint[*] internalConstraints;
> }
>
> class Constraint{
> unsettable attr ConstraintType constraintType;
>
> ref Feat[1] source;
> ref Feat[1] target;
> }
>
>
> I want to be able, to assign/delete objects of Constraints to FM during the dialog is open.
>
> I've already created this dialog, but I've no clue how to add or delete objects to each other without breaking the emf rules.
>
> My strategy so far was to create the according objects and then use the AddCommand/RemoveCommand to save them (fm and constraint are objects in this case):
>
>
> EditingDomain editingDomain = org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain.getEditingDomainFor(fm);
> Command cmdRequiredConstraint = AddCommand.create(editingDomain, fm, PldPackage.Literals.F_M__INTERNAL_CONSTRAINTS, constraint);
> editingDomain.getCommandStack().execute(cmdRequiredConstraint);
>
This looks OK.
>
> As you can see in the following images, the constraints are assigned to FM, but it doesn't seems to be persisted in the file..Can anyone help me with this?
>
You are almost there. the final step is to save the 'container' holding
the fm object. I would expect the fm object to stored in an EMF
Resource. If so, calling the resource.save([options...]) will persist
your change. I would really recommend to study a generator Editor for
your model. It contains *all* the concepts dealing with persistence of
EMF. ( save/saveAs methods).
>
|
|
| |
Re: Create own PropertyDescriptor [message #1237740 is a reply to message #1237719] |
Thu, 30 January 2014 13:19 |
Christophe Bouhier Messages: 937 Registered: July 2009 |
Senior Member |
|
|
On 30-01-14 13:11, Phil H wrote:
> Thx for your help so far, but I still have some questions
>
> Quote:
>> I would really recommend to study a generator Editor for your model. It contains *all* the concepts dealing with persistence of EMF. ( save/saveAs methods).
>
>
> What do you mean by generator Editor? Are there any articles/links describing this? Or do you mean I should take a look at a generated class inside my EMF/GMF project?
>
I mean the generated editor, which you seem to be using judging from the
warning you get. I think I understand your use case slightly better.
What happens is that the resource is already in use and loaded in your
..xyz editor. Then if you load the resource explicitly and save it, the
editor will detect this and show the warning.
So I think you should obtain your editing domain from the editor which
implements: IEditingDomainProvider and execute the AddCommand on the
stack of that Editingdomain. I am not very familiar with the
getEditingDomain() code in AdapterFactory, but you could debug this and
see what happens. (Is it the same editingdomain as the one used by the
editor).
> Quote:
>> You are almost there. the final step is to save the 'container' holding the fm object. I would expect the fm object to stored in an EMF Resource. If so, calling the resource.save([options...]) will persist your change.
>
>
> So I added the following lines of code after my AddCommand:
>
>
> ...
> editingDomain.getCommandStack().execute(cmdRequiredConstraint);
>
> Resource res = editingDomain.getResourceSet().getResources().get(0);
> try {
> res.save(null);
> }
> catch (IOException e) {
> e.printStackTrace();
> }
>
>
> But then I get the following warning, which I don't really understand:
>
>
>
> When I'm adding another constraint, I get this error, which wasn't there before:
>
>
> java.lang.NullPointerException
> at org.eclipse.emf.edit.command.AddCommand.create(AddCommand.java:93)
> at org.eclipse.emf.edit.command.AddCommand.create(AddCommand.java:66)
> at property.sheet.internalConstraint.ConstraintDialog$1.widgetSelected(ConstraintDialog.java:165)
> at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
> at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
> at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1057)
> at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4170)
> at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3759)
> at org.eclipse.jface.window.Window.runEventLoop(Window.java:826)
> at org.eclipse.jface.window.Window.open(Window.java:802)
>
>
> Am I persisting the resource wrong?
>
|
|
|
Re: Create own PropertyDescriptor [message #1237744 is a reply to message #1237740] |
Thu, 30 January 2014 13:21 |
Christophe Bouhier Messages: 937 Registered: July 2009 |
Senior Member |
|
|
One more thing, what I normally do is close the dialog without
persisting. The editor will be dirty (As notified by the command stack)
and then a regular editor save will persist your added objects. (But
this all have to be on the same editing domain).
On 30-01-14 14:19, Christophe Bouhier wrote:
> On 30-01-14 13:11, Phil H wrote:
>> Thx for your help so far, but I still have some questions
>>
>> Quote:
>>> I would really recommend to study a generator Editor for your model.
>>> It contains *all* the concepts dealing with persistence of EMF. (
>>> save/saveAs methods).
>>
>>
>> What do you mean by generator Editor? Are there any articles/links
>> describing this? Or do you mean I should take a look at a generated
>> class inside my EMF/GMF project?
>>
> I mean the generated editor, which you seem to be using judging from the
> warning you get. I think I understand your use case slightly better.
> What happens is that the resource is already in use and loaded in your
> .xyz editor. Then if you load the resource explicitly and save it, the
> editor will detect this and show the warning.
>
> So I think you should obtain your editing domain from the editor which
> implements: IEditingDomainProvider and execute the AddCommand on the
> stack of that Editingdomain. I am not very familiar with the
> getEditingDomain() code in AdapterFactory, but you could debug this and
> see what happens. (Is it the same editingdomain as the one used by the
> editor).
>
>
>
>> Quote:
>>> You are almost there. the final step is to save the 'container'
>>> holding the fm object. I would expect the fm object to stored in an
>>> EMF Resource. If so, calling the resource.save([options...]) will
>>> persist your change.
>>
>>
>> So I added the following lines of code after my AddCommand:
>>
>>
>> ...
>> editingDomain.getCommandStack().execute(cmdRequiredConstraint);
>>
>> Resource res = editingDomain.getResourceSet().getResources().get(0);
>> try {
>> res.save(null);
>> }
>> catch (IOException e) {
>> e.printStackTrace();
>> }
>>
>>
>> But then I get the following warning, which I don't really understand:
>>
>>
>>
>> When I'm adding another constraint, I get this error, which wasn't
>> there before:
>>
>>
>> java.lang.NullPointerException
>> at org.eclipse.emf.edit.command.AddCommand.create(AddCommand.java:93)
>> at org.eclipse.emf.edit.command.AddCommand.create(AddCommand.java:66)
>> at
>> property.sheet.internalConstraint.ConstraintDialog$1.widgetSelected(ConstraintDialog.java:165)
>>
>> at
>> org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
>> at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
>> at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1057)
>> at
>> org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4170)
>> at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3759)
>> at org.eclipse.jface.window.Window.runEventLoop(Window.java:826)
>> at org.eclipse.jface.window.Window.open(Window.java:802)
>>
>>
>> Am I persisting the resource wrong?
>>
>
|
|
| | | |
Re: Create own PropertyDescriptor [message #1238078 is a reply to message #1238069] |
Fri, 31 January 2014 10:41 |
Christophe Bouhier Messages: 937 Registered: July 2009 |
Senior Member |
|
|
On 31-01-14 11:12, Phil H wrote:
> That's the problem, I'm stucked with this for a while now..
>
> Maybe you or someone else can tell me, what it mean when objects are shown inside the property window, like in the following image:
>
Hi,
Maybe it's worth studying a bit how this works.
In a generated editor you will see this:
public IPropertySheetPage getPropertySheetPage() {
PropertySheetPage propertySheetPage =
new ExtendedPropertySheetPage(editingDomain) {
@Override
public void setSelectionToViewer(List<?> selection) {
ServicesEditor.this.setSelectionToViewer(selection);
ServicesEditor.this.setFocus();
}
@Override
public void setActionBars(IActionBars actionBars) {
super.setActionBars(actionBars);
getActionBarContributor().shareGlobalActions(this, actionBars);
}
};
propertySheetPage.setPropertySourceProvider(new
AdapterFactoryContentProvider(adapterFactory));
propertySheetPages.add(propertySheetPage);
return propertySheetPage;
}
so the propertysource is an EMF AdapterFactoryContentProvider
This can adapt an object to an IItemPropertySource for an object.
Eventually when changing a value the code in ItemProperyDescriptor is
called:
public void setPropertyValue(Object object, Object value)
this uses the EditingDomain obtained from the factory and a SetCommand
to modify the object.
>
>
> Using then the save mechanism, I would expect that they are persisted in the file, but this is not the case..
they are persisted! (You get a warning the file changed). but it seems a
different *process* is writing the file, hence the warning. this is the
part I have no insight in.. perhaps you can share how the dialog saves
the code.
>
> So it seems that the reference exist inside some EMF cache, but for some reasons can't be persisted in the file.
>
|
|
|
Re: Create own PropertyDescriptor [message #1238093 is a reply to message #1238078] |
Fri, 31 January 2014 11:24 |
Phil H Messages: 267 Registered: November 2012 |
Senior Member |
|
|
Thx for your help till now!
Just to clarify:
they are persisted! (You get a warning the file changed). but it seems a different *process* is writing the file, hence the warning. this is the part I have no insight in.. perhaps you can share how the dialog saves the code.
What do you mean with the last sentence? What code should be saved?
The warning only appeared, because I added the resource.save stuff, but this causes only problems and the regular save mechanism sufficient..So I removed it and now I don't get any warning or exceptions..
I can add properties and close the dialog..When I reopen the dialog, the properties are still there, which is what I want:
When I close the editor then, it ask me for saving (so it recognizes that there has been changes in the model), which I answer with yes..but when I reopen the editor, the properties are not existing anymore:
So in my eyes it seems that they are cached somewhere during runtime, but not persisted for some reasons
|
|
|
Re: Create own PropertyDescriptor [message #1238111 is a reply to message #1238093] |
Fri, 31 January 2014 12:22 |
Christophe Bouhier Messages: 937 Registered: July 2009 |
Senior Member |
|
|
On 31-01-14 12:24, Phil H wrote:
> Thx for your help till now!
>
> Just to clarify:
>
>
> they are persisted! (You get a warning the file changed). but it seems a different *process* is writing the file, hence the warning. this is the part I have no insight in.. perhaps you can share how the dialog saves the code.
>
>
> What do you mean with the last sentence? What code should be saved?
>
> The warning only appeared, because I added the resource.save stuff, but this causes only problems and the regular save mechanism sufficient..So I removed it and now I don't get any warning or exceptions..
>
> I can add properties and close the dialog..When I reopen the dialog, the properties are still there, which is what I want:
>
>
>
> When I close the editor then, it ask me for saving (so it recognizes that there has been changes in the model), which I answer with yes..but when I reopen the editor, the properties are not existing anymore:
>
and you are sure the save is succesfull? Do you see the change appear in
the resource? (If you open the file holding your model with a text
editor, would you see the XML or XMI holding with your change in it?
>
>
> So in my eyes it seems that they are cached somewhere during runtime, but not persisted for some reasons
yes, look at the content of the file with a text editor. you can also
debug the save method of the editor.. If the Command is wrong for some
reason which would lead to an invalid model, this could break the save.
>
|
|
| | | | | | | |
Re: Create own PropertyDescriptor [message #1240348 is a reply to message #1240106] |
Thu, 06 February 2014 09:15 |
Christophe Bouhier Messages: 937 Registered: July 2009 |
Senior Member |
|
|
On 05-02-14 19:06, Phil H wrote:
> Oh, you're maybe right. It's a non-containment reference, as shown here:
Sorry, I am not familiar with xcore. In the Ecore you can see it in the
properties.
>
>
> class FM{
> unsettable attr EString name = "FM";
>
> ref Constraint[*] internalConstraints;
> }
>
> class Constraint{
> unsettable attr ConstraintType constraintType;
>
> ref Feat[1] source;
> ref Feat[1] target;
> }
>
>
> But for what reason is emf generating then F_M__INTERNAL_CONSTRAINTS ? This is the only reference between this two classes..
Regardless if it's contained or not contained, a feature is generated
(also in the Literal format), as you specify this in your model. So to
answer your question: Because you told EMF to do so :P
The Resource can contain *any* EObject so the definition of a specific
feature is not needed to hold objects in Resource.
>
> So constraint has only the reference to FM and no containment reference.
Judging from the diagram, it's the other way around FM -> Contstraint.
..Which is then the resurce which holds my object?
You need to put it in the Resource opened by your editor. You can use an
ADD command for this also.
constraint = ...
myResource = ResourceSet.get(URI TO YOUR RESOURCE);
Command addToResource = new
AddCommand(editingService.getEditingDomain(),
myResource.getContents(),constraint);
a_command_stack.execute(addToResource)
have you tried this?
In the following there is snippet from my metamodel:
>
>
|
|
| | |
Goto Forum:
Current Time: Mon Sep 16 05:56:37 GMT 2024
Powered by FUDForum. Page generated in 0.04637 seconds
|