Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Xtext Save Action
Re: Xtext Save Action [message #1060775 is a reply to message #1060763] Tue, 28 May 2013 12:09 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
Hi,

is it solved or not? you code seem to be incomplete


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext Save Action [message #1060803 is a reply to message #1060775] Tue, 28 May 2013 13:13 Go to previous messageGo to next message
junior developer is currently offline junior developerFriend
Messages: 354
Registered: January 2013
Senior Member
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 #1060807 is a reply to message #1060803] Tue, 28 May 2013 13:21 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
Hi this is a quick fix. What about solving one problem at a time :(

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext Save Action [message #1060809 is a reply to message #1060807] Tue, 28 May 2013 13:30 Go to previous messageGo to next message
junior developer is currently offline junior developerFriend
Messages: 354
Registered: January 2013
Senior Member
Hi Christian,

yes ,it is a quick fix.it create entity,So I try it in the XtextDocumentProvider Automatically.Can I explain Sad
Re: Xtext Save Action [message #1060812 is a reply to message #1060809] Tue, 28 May 2013 13:37 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
I don't think this will work

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext Save Action [message #1060819 is a reply to message #1060812] Tue, 28 May 2013 14:02 Go to previous messageGo to next message
junior developer is currently offline junior developerFriend
Messages: 354
Registered: January 2013
Senior Member
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 14:31]

Report message to a moderator

Re: Xtext Save Action [message #1060836 is a reply to message #1060819] Tue, 28 May 2013 14:58 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
Please share the complete class

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext Save Action [message #1060840 is a reply to message #1060836] Tue, 28 May 2013 15:33 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
Cant you simply

@Override
protected void doSaveDocument(IProgressMonitor monitor, Object element,
IDocument document, boolean overwrite) throws CoreException {
((IXtextDocument)document).modify(....)// ; stuff you should already have
super.doSaveDocument(monitor, element, document, overwrite);

}


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext Save Action [message #1060879 is a reply to message #1060840] Tue, 28 May 2013 19:56 Go to previous messageGo to next message
junior developer is currently offline junior developerFriend
Messages: 354
Registered: January 2013
Senior Member
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 08:27 Go to previous messageGo to next message
junior developer is currently offline junior developerFriend
Messages: 354
Registered: January 2013
Senior Member
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 #1060935 is a reply to message #1060932] Wed, 29 May 2013 08:37 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
Hi,

the error message should be clear !?!


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext Save Action [message #1060963 is a reply to message #1060935] Wed, 29 May 2013 10:37 Go to previous messageGo to next message
junior developer is currently offline junior developerFriend
Messages: 354
Registered: January 2013
Senior Member
Hi Christian,

Error is in the dialog window.load screenshot.
  • Attachment: saveerror.png
    (Size: 29.11KB, Downloaded 111 times)
Re: Xtext Save Action [message #1060973 is a reply to message #1060963] Wed, 29 May 2013 11:41 Go to previous messageGo to next message
junior developer is currently offline junior developerFriend
Messages: 354
Registered: January 2013
Senior Member
Hi Christian,

My problem is solved with setDocumentContent method.I confused Sad

@Override
protected boolean setDocumentContent(IDocument document,
IEditorInput editorInput, String encoding) throws CoreException {
// TODO Auto-generated method stub
return super.setDocumentContent(document, editorInput, encoding);
}
Re: Xtext Save Action [message #1060980 is a reply to message #1060973] Wed, 29 May 2013 12:05 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
Sorry I cannot help you with that

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext Save Action [message #1061016 is a reply to message #1060932] Wed, 29 May 2013 14:11 Go to previous messageGo to next message
junior developer is currently offline junior developerFriend
Messages: 354
Registered: January 2013
Senior Member
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:27
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 #1061021 is a reply to message #1061016] Wed, 29 May 2013 14:28 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
Hi,

why call super.doSaveDocument(monitor, element, document, overwrite);
first and the modify it, id do it the other way round.


state.getContents().set(0 ,MyDslFactory.eINSTANCE.createTextField() );
this textfield has no name and description .
why do you want to change the root at alle.

id do something like

MyRootClass root = (MyRootClass )state.getContents().get(0);
//work with the root,


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext Save Action [message #1061029 is a reply to message #1061021] Wed, 29 May 2013 15:00 Go to previous messageGo to next message
junior developer is currently offline junior developerFriend
Messages: 354
Registered: January 2013
Senior Member
Hi Christian,

You right.I will try .

but my operation is happened when I save my document.actually,My operation must works When I create Contenttype .it is possible.

I want to ask question that is different topic
in addition.I wrote code in the save action (
NatureAddingEditorCallback) :I solved another problem with NatureAddingEditorCallback class .

now,I use XtextDocumentProvider.dosaveDocument method.

What is the different XtextDocumentProvider.dosaveDocument
and NatureAddingEditorCallback.aftersave(XtextEditor editor)

Best Regards
Re: Xtext Save Action [message #1061030 is a reply to message #1061029] Wed, 29 May 2013 15:12 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
This is not possible

--
Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext at itemis dot de


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext Save Action [message #1061193 is a reply to message #1061030] Thu, 30 May 2013 13:24 Go to previous messageGo to next message
junior developer is currently offline junior developerFriend
Messages: 354
Registered: January 2013
Senior Member
Hi Christian ,

I try this method for my problem.but do not works .really I do not understand my error.I debug it ,if condition is error.but I do not find Sad

private final class addAutomaticField implements IAutoEditStrategy {
		@Inject
		Provider<IGrammarAccess> iGrammar;
		private String contents;
		private Set<String> kwds;
		@Inject DocumentUtil u;
		
		
		
		@Override
		public void customizeDocumentCommand(IDocument document,
				DocumentCommand command) {
			
			
			List<String> keywords = Arrays.asList("ContentType"
					 );

			StringBuilder sb = new StringBuilder();
			for (String s : keywords) {
				sb.append(s);
				sb.append("|");

			}

			
			String keywordsString = sb.deleteCharAt(sb.length() - 1).toString();

			String pattern = String.format("\\b(%s)\\b((\\w|\\s|\\.)+)[{]",
					keywordsString);

			Pattern compile = Pattern.compile(pattern);

			String alldocument = document.get();
			//System.out.println(alldocument);
			java.util.regex.Matcher matcher = compile.matcher(alldocument);
			final List<String> contentUnitNames = new ArrayList<String>();
			while (matcher.find()) {
	          String kelime = matcher.group(2).trim();
	            
				 contentUnitNames.add(kelime);
				 System.out.println(contentUnitNames);
				

			}
			
			
			try {  
				
				
				int i=0;
				
				while(i<=contentUnitNames.size()-1){	
					String s=	contentUnitNames.get(i).toString();
					System.out.println(s);
					int index =contentUnitNames.get(i).length();
					System.out.println(index);
					int offsetindex=12+index;
if (command.text.equals("{") && document.get(command.offset-offsetindex,offsetindex).equals("ContentType "+s)) {
//if (command.text.equals("{") && document.get(command.offset-16,16).equals("ContentType cccc")) {
					//System.out.println(contentUnitNames.get(0).length());
					contents = document.get();
				
					String afterCommandString = contents.substring(command.offset, document.getLength());
					contents = document.get(0, command.offset);
					contents = contents + " guID = "+ GUIDGenerator.generateUniqueId() +  "Text text {";
					contents = contents + afterCommandString;
					document.set(contents);
			
					
					contents = null;
					
				} else {
					if (contents != null) {
						document.set(contents);
						contents = null;
					}	
				}
			i++;
			
				}//forun
				
                  
			}catch (BadLocationException e) {
				e.printStackTrace();
				
			}
			
			
			}
	
	
	}




Best Regards
Re: Xtext Save Action [message #1061198 is a reply to message #1061193] Thu, 30 May 2013 13:40 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14716
Registered: July 2009
Senior Member
Sorry i cannot follow you.
you do 10 things at a time.
why dont you try one approach to the end ?!?
i cannot support this way sorry


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Xtext Save Action [message #1061208 is a reply to message #1061198] Thu, 30 May 2013 14:20 Go to previous messageGo to next message
junior developer is currently offline junior developerFriend
Messages: 354
Registered: January 2013
Senior Member
Hi Christian,

other method happens when the user save editor.so ,I try AutoeditStrategy. but if condition is not works Sad I do not understrand my error
Why do not you support this way?


[Updated on: Thu, 30 May 2013 14:22]

Report message to a moderator

Re: Xtext Save Action [message #1061211 is a reply to message #1061208] Thu, 30 May 2013 14:34 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33218
Registered: July 2009
Senior Member
This is one of the longest threads I've ever seen. It's really hard for
anyone to help you down this meandering path...

On 30/05/2013 4:20 PM, junior developer wrote:
> Hi Christian,
>
> other method happens when the user save editor.so ,I try
> AutoeditStrategy.
> Why do not you support this way?
>
>
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Xtext Save Action [message #1061222 is a reply to message #1061211] Thu, 30 May 2013 15:15 Go to previous message
junior developer is currently offline junior developerFriend
Messages: 354
Registered: January 2013
Senior Member
Hi Ed ,
Thnak you.I create a new topic .
http://www.eclipse.org/forums/index.php/m/1061221/#msg_1061221

Best regards
Previous Topic:Create Automatically elements when one element is created
Next Topic:SelectionListener registration
Goto Forum:
  


Current Time: Tue Sep 24 20:28:46 GMT 2024

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

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

Back to the top