Changes

Jump to: navigation, search

Teams Winter 2011/team1/RCP/Define and use editors

2,742 bytes added, 13:34, 9 March 2011
3.4.2 Add a "double click" listener to the StudentsView class
=== 3. Define and Use Editors ===
====3.1 Editor area====
The first step to add editor area to the application is to make the visibility of the editor area in <code>Perspective.java </code> to <code> true </code>.
}
</source>
====3.2 Add input editor====
IEditorInput serves as the model for the editor and is supposed to be a light-weight representation of the model. For example the Eclipse IDE uses this concept to identify files without handling with the complete file. Based on the equals method the system will determine if the corresponding editor is already open or not therefore you should overwrite the method equals and hashcode in an implementation of IEditor.
====3.3 Adding the editor====
Now we should add the <code>''editor''</code> to our extensions.<br/>
Go to <code>''plugin.xml''</code> and select the tab extensions. Add the extension <code>''org.eclipse.ui.editors''</code>.<br/>
====3.4 Open editor====
The way we want to interact with the editor is, whenever we double click on a field, the editor opens in its area.<br/>
We need to take two steps for this:<br/> =====3.4.1 Open editor command=====We add a command to the <code>''commands''</code> extension as it is explained in [[Teams_Winter_2011/team1/RCP/Define_and_use_commands |part 2 ]] of this tutorial.<br/>[[Image: rcp_editor4.png | 700px]]<br/> Here is how the <code>''callEditor''</code> class looks like: <source lang=java>public class callEditor extends AbstractHandler {  @Override public Object execute(ExecutionEvent event) throws ExecutionException { //System.out.println("called"); // Get the view IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event); IWorkbenchPage page = window.getActivePage(); StudentsView studentsView = (StudentsView) page.findView(StudentsView.ID); // Get the selection ISelection selection = studentsView.getSite().getSelectionProvider() .getSelection(); if (selection != null && selection instanceof IStructuredSelection) { Object obj = ((IStructuredSelection) selection).getFirstElement(); // If we had a selection lets open the editor if (obj != null) { Student student = (Student) obj; MyStudentEditorInput input = new MyStudentEditorInput( student.getId()); try { page.openEditor(input, MyStudentEditor.ID);  } catch (PartInitException e) { throw new RuntimeException(e); } } } return null; } }</source>=====3.4.2 Add a "double click" listener to the <code>StudentsView</code> class=====For this, we need to add <code>''hookDoubleClick()''</code> method to the </code>StudentsView.java </code>. <br/><source lang=java> private void hookDoubleClickCommand() { viewer.addDoubleClickListener(new IDoubleClickListener() { public void doubleClick(DoubleClickEvent event ) { IHandlerService handlerService = (IHandlerService) getSite() .getService(IHandlerService.class); try { handlerService.executeCommand( "cs.ecl.rcp.simplercp.openEditor", null); } catch (Exception ex) { throw new RuntimeException( "cs.ecl.rcp.simplercp.openEditor not found"); } } });  }</source>We will call this method from <code>''createPartControl()''</code> method.The view makes his viewer available as selection provider via the following line:<br/><code>''getSite().setSelectionProvider(viewer);''</code> <br/>This make is possible for the command which opens the editor to get the selection of the view.<br/>All workbench parts have a site, which can be accessed via the method <code>getSite()</code>.<source lang=java> public void createPartControl(Composite parent) { ............ // add double click listener for editing fields in a text editor viewer.setInput(ModelProvider.INSTANCE.getStudents()); getSite().setSelectionProvider(viewer); hookDoubleClickCommand(); }</source>  <br/>[[Teams_Winter_2011/team1| Index Page]] &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [[Teams_Winter_2011/team1/RCP/Define_and_use_commands Define_and_use_JFace| <codeNext>StudentsView</code>]] class
1
edit

Navigation menu