Xtext Save Action [message #1059783] |
Tue, 21 May 2013 11:06  |
Eclipse User |
|
|
|
Hi all,
I write a code in the Xtext save method.I have problem:
1:Auto Complision is not active in the editor.
2.I can not Ctrl+Z
3-My cursor is in the beginning of the editor every time.
4-I can not trim mymodel.getDisplayName().if I trim it ,changes not do.
@Override
public void afterSave(XtextEditor editor) {
// TODO Auto-generated method stub
IXtextDocument myDocument = editor.getDocument();
myDocument.modify(new IUnitOfWork<Void, XtextResource>() {
@Override
public java.lang.Void exec(XtextResource res) throws Exception {
ContentModel mymodel =(ContentModel) res.getContents().get(0);
mymodel.setName(mymodel.getDisplayName().toString().intern());
System.out.println(mymodel.getDisplayName().toString());
return null;
}
});
Best Regrads,
[Updated on: Tue, 21 May 2013 11:06] by Moderator
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: Xtext Save Action [message #1060763 is a reply to message #1060755] |
Tue, 28 May 2013 07:46   |
Eclipse User |
|
|
|
Thank you for help .I try it.My problem may be solved
I written code in the public class MyXtextDocumentProvider extends XtextDocumentProvider{
@Override
protected void doSaveDocument(IProgressMonitor monitor, Object element,
IDocument document, boolean overwrite) throws CoreException {
super.doSaveDocument(monitor, element, document, overwrite);
}
protected boolean createNewEntity(Entity sibling, String name) {
Entity newEntity = DomainmodelFactory.eINSTANCE.createEntity();
newEntity.setName(name);
return addTypeAsSibling(sibling, newEntity);
}
protected boolean createNewDatatype(Entity sibling, String name) {
DataType newDatatype = DomainmodelFactory.eINSTANCE.createDataType();
newDatatype.setName(name);
return addTypeAsSibling(sibling, newDatatype);
}
protected boolean addTypeAsSibling(Entity sibling, Type newType) {
EObject container = sibling.eContainer();
EList<AbstractElement> elements = null;
if (container instanceof PackageDeclaration) {
elements = ((PackageDeclaration) container).getElements();
} else if (container instanceof DomainModel) {
elements = ((DomainModel) container).getElements();
} else {
return false;
}
int index = elements.indexOf(sibling) + 1;
elements.add(index, newType);
return true;
}
}
|
|
|
|
Re: Xtext Save Action [message #1060803 is a reply to message #1060775] |
Tue, 28 May 2013 09:13   |
Eclipse User |
|
|
|
No .not solved my problem.I will try XtextDocumentProvider.doSaveDocument.
but I want to use org.eclipse.xtext.ui.editor.model.edit.ISemanticModification in the XtextDocumentProvider.I do not use :(How can I solved my problem.
I want to try below code in the XtextDocumentProvider with Save action:
@Fix(DomainmodelLinkingDiagnosticMessageProvider.MISSING_REFERENCE_TYPE)
public void createReferenceType(final Issue issue, IssueResolutionAcceptor acceptor) {
final String linkText = issue.getData()[0];
acceptor.accept(issue, "Create entity '" + linkText + "'", "Create entity '" + linkText + "'", null,
new ISemanticModification() {
public void apply(final EObject element, IModificationContext context) {
createNewEntity((Entity) element.eContainer().eContainer(), linkText);
}
});
createLinkingIssueResolutions(issue, acceptor);
}
protected boolean createNewEntity(Entity sibling, String name) {
Entity newEntity = DomainmodelFactory.eINSTANCE.createEntity();
newEntity.setName(name);
return addTypeAsSibling(sibling, newEntity);
}
protected boolean createNewDatatype(Entity sibling, String name) {
DataType newDatatype = DomainmodelFactory.eINSTANCE.createDataType();
newDatatype.setName(name);
return addTypeAsSibling(sibling, newDatatype);
}
protected boolean addTypeAsSibling(Entity sibling, Type newType) {
EObject container = sibling.eContainer();
EList<AbstractElement> elements = null;
if (container instanceof PackageDeclaration) {
elements = ((PackageDeclaration) container).getElements();
} else if (container instanceof DomainModel) {
elements = ((DomainModel) container).getElements();
} else {
return false;
}
int index = elements.indexOf(sibling) + 1;
elements.add(index, newType);
return true;
}
|
|
|
|
|
|
Re: Xtext Save Action [message #1060819 is a reply to message #1060812] |
Tue, 28 May 2013 10:02   |
Eclipse User |
|
|
|
Hi Christian,
not works it .I must do When User create Contenttype,textfield created in it .Hense Save action may not solved my problem.
public class MyXtextDocumentProvider extends XtextDocumentProvider{
@Override
protected void doSaveDocument(IProgressMonitor monitor, Object element,
IDocument document, boolean overwrite) throws CoreException {
super.doSaveDocument(monitor, element, document, overwrite);
//boolean createNewEntity(Field sibling, String name) {
TextField newEntity = MyDslFactory.eINSTANCE.createTextField();
newEntity.setName("text");
System.out.println("....");
//}
}
I want to Which class detect which elements.hence When User create Contenttype ,I will do change after contenttype is created
[Updated on: Tue, 28 May 2013 10:31] by Moderator
|
|
|
|
|
Re: Xtext Save Action [message #1060879 is a reply to message #1060840] |
Tue, 28 May 2013 15:56   |
Eclipse User |
|
|
|
Hi Christian ,
I write code now,but not fineshed.
public class MyXtextDocumentProvider extends XtextDocumentProvider{
@Override
protected void doSaveDocument(IProgressMonitor monitor, Object element,
IDocument document, boolean overwrite) throws CoreException {
super.doSaveDocument(monitor, element, document, overwrite);
((IXtextDocument)document).modify(new IUnitOfWork<Void, XtextResource>() {
@Override
public java.lang.Void exec(XtextResource state) throws Exception {
org.xtext.example.mydsl.myDsl.ContentType newElement = org.xtext.example.mydsl.myDsl.MyDslFactory.eINSTANCE
.createContentType();
TextField text = MyDslFactory.eINSTANCE.createTextField();
text.setName("text");
text.setDisplayName("text");
newElement.getOwnedField().add(text);
TextField created = MyDslFactory.eINSTANCE.createTextField();
created.setName("created");
created.setDisplayName("created");
newElement.getOwnedField().add(created);
TextField createdBy = MyDslFactory.eINSTANCE.createTextField();
createdBy.setName("createdBy");
createdBy.setDisplayName("created By");
newElement.getOwnedField().add(createdBy);
return null;
}
} );
my problem is added elements index and When user create a contenttype,such as Text text is created automatically.
|
|
|
Re: Xtext Save Action [message #1060932 is a reply to message #1060840] |
Wed, 29 May 2013 04:27   |
Eclipse User |
|
|
|
Hi Christian,
I do not solved my problem.
my code is below:
@Override
protected void doSaveDocument(IProgressMonitor monitor, Object element,
IDocument document, boolean overwrite) throws CoreException {
super.doSaveDocument(monitor, element, document, overwrite);
((IXtextDocument)document).modify(new IUnitOfWork<Void, XtextResource>() {
@Override
public java.lang.Void exec(XtextResource state) throws Exception {
ContentType newElement = MyDslFactory.eINSTANCE.createContentType();
state.getContents().set(0 ,MyDslFactory.eINSTANCE.createTextField() );
MyDslPackage.eINSTANCE.eClass();
MyDslFactory factory = MyDslFactory.eINSTANCE;
TextField newEntity = MyDslFactory.eINSTANCE.createTextField();
newEntity.setName("text");
newEntity.setDisplayName("text");
newElement.getOwnedField().add(newEntity);
return null;
}
} );
}
I receive an error :
Save Failed
These errors need to be fixed before the model can be serialized.
TextField: Feature TextField.displayName must be set. Constraint: (name displayName) Quantities: displayName:0, name:0
TextField: Feature TextField.name must be set. Constraint: (name guID) Quantities: guID:1, name:0
Best regards,
|
|
|
|
|
|
|
Re: Xtext Save Action [message #1061016 is a reply to message #1060932] |
Wed, 29 May 2013 10:11   |
Eclipse User |
|
|
|
Hi Christian,
I understand you,but This code is not work,same as EMF changes.Solved my problem with this method (below).is there a class for this problem.One User create an element,such as contenttype,created own default fields automatically.I could try another methor or class.
junior developer wrote on Wed, 29 May 2013 04:27Hi Christian,
I do not solved my problem.
my code is below:
@Override
protected void doSaveDocument(IProgressMonitor monitor, Object element,
IDocument document, boolean overwrite) throws CoreException {
super.doSaveDocument(monitor, element, document, overwrite);
((IXtextDocument)document).modify(new IUnitOfWork<Void, XtextResource>() {
@Override
public java.lang.Void exec(XtextResource state) throws Exception {
ContentType newElement = MyDslFactory.eINSTANCE.createContentType();
state.getContents().set(0 ,MyDslFactory.eINSTANCE.createTextField() );
MyDslPackage.eINSTANCE.eClass();
MyDslFactory factory = MyDslFactory.eINSTANCE;
TextField newEntity = MyDslFactory.eINSTANCE.createTextField();
newEntity.setName("text");
newEntity.setDisplayName("text");
newElement.getOwnedField().add(newEntity);
return null;
}
} );
}
I receive an error :
Save Failed
These errors need to be fixed before the model can be serialized.
TextField: Feature TextField.displayName must be set. Constraint: (name displayName) Quantities: displayName:0, name:0
TextField: Feature TextField.name must be set. Constraint: (name guID) Quantities: guID:1, name:0
Best regards,
|
|
|
|
|
|
|
|
|
|
|