Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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] Sat, 21 March 2015 02:32 Go to next message
Bob Tao is currently offline Bob TaoFriend
Messages: 23
Registered: March 2015
Junior Member
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 15:09 Go to previous messageGo to next message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 14667
Registered: July 2009
Senior Member
From the xtwxteditor you should get an ixtextdocument on which you can call Module

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to add a code segment into the current editor via popup menu? [message #1689626 is a reply to message #1689428] Tue, 24 March 2015 00:39 Go to previous messageGo to next message
Bob Tao is currently offline Bob TaoFriend
Messages: 23
Registered: March 2015
Junior Member
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 06:22 Go to previous message
Christian Dietrich is currently online Christian DietrichFriend
Messages: 14667
Registered: July 2009
Senior Member
yes, but this way you can change the file in a way that it gets invalid

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
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: Fri Apr 26 17:02:56 GMT 2024

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

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

Back to the top