Skip to main content



      Home
Home » Modeling » TMF (Xtext) » How to add a code segment into the current editor via popup menu?
How to add a code segment into the current editor via popup menu? [message #1688568] Fri, 20 March 2015 22:32 Go to next message
Eclipse UserFriend
Hi,

I have add a popup menu to the XTextEditor and bind a handler to the menu.
the Handler code like this:

public class GenFieldGroupHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {
		
		IEditorPart editor=HandlerUtil.getActiveEditor(event);
		ISelection selection=HandlerUtil.getCurrentSelection(event);
		IEditorInput input=HandlerUtil.getActiveEditorInput(event);
		if (editor instanceof XtextEditor & selection instanceof TextSelection)
		 {
			XtextEditor textEditor=(XtextEditor)editor;
			TextSelection textSelection = (TextSelection) selection;		
			
			
			System.out.println("Gen Field Group Handler is executed.");
			 
		 }
		 
		return null;
	}

	

}


My question is how to add a code segment into the editor at cursor position.
could you give me a example or a suggestion.

Thanks!
Re: How to add a code segment into the current editor via popup menu? [message #1689428 is a reply to message #1688568] Mon, 23 March 2015 11:09 Go to previous messageGo to next message
Eclipse UserFriend
From the xtwxteditor you should get an ixtextdocument on which you can call Module
Re: How to add a code segment into the current editor via popup menu? [message #1689626 is a reply to message #1689428] Mon, 23 March 2015 20:39 Go to previous messageGo to next message
Eclipse UserFriend
Thanks,Currently, I implement it like this.
XtextEditor textEditor=(XtextEditor)editor;
			TextSelection textSelection = (TextSelection) selection;		
			
			 IDocumentProvider dp = textEditor.getDocumentProvider();
			 IDocument doc = dp.getDocument(editor.getEditorInput());
			 int offset = textSelection.getOffset();
			 int startLine=textSelection.getStartLine();
			 int endLine=textSelection.getEndLine();
			 
			 try {
				String pasteText="replacement text\n";
				doc.replace(offset, 0, pasteText);
			} catch (BadLocationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

Re: How to add a code segment into the current editor via popup menu? [message #1689706 is a reply to message #1689626] Tue, 24 March 2015 02:22 Go to previous message
Eclipse UserFriend
yes, but this way you can change the file in a way that it gets invalid
Previous Topic:Serializing an unordered group?
Next Topic:Maven "exec-maven-plugin" --> Could not load class: org.eclipse.core.runtime.OperationC
Goto Forum:
  


Current Time: Mon Nov 03 13:35:04 EST 2025

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

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

Back to the top