Back to top

vogella training Training Books

Eclipse 4 Editor - Implementing the Edit and Save Paradigm

Based on Eclipse 4.2

Lars Vogel

Version 5.7

08.05.2012

Revision History
Revision 0.1 14.02.2009 Lars
Vogel
created
Revision 0.2 - 5.7 16.02.2009 - 08.05.2012 Lars
Vogel
bug fixes and enhancements

Eclipse 4 Editor Tutorial

This tutorial gives an overview how to implement an editor like behavior in Eclipse 4.


Table of Contents

1. Eclipse Editor
2. Thank you
3. Questions and Discussion
4. Links and Literature
4.1. Source Code

1. Eclipse Editor

Eclipse 4 has support for parts which behave like Eclipse 3.x editors.

A part has the flag MDirtyable which indicates that it can be marked as dirty. Dirty indicates that the part contains data which has been changed but not yet saved.

You can query this information in your model and react to it.

This indication can be used so that Eclipse can then call the method marked with @Persists. In this method you can save the data.

You can also implement the model element InputPart. This supports the MInputPart by which an Input can be provided to the InputPart. In its @PostConstruct method the InputPart can access this and perform the required action.

To open an new InputPart you can use the following OpenEditor class. This editor assumes that you have defined an MyInput object which allows to receive the InputURI, the label for the InputPart and the Tooltip for the part from it.

public class OpenEditor {
  
  @Execute
  public void open(MyInput input
      MApplication application, 
      EModelService modelService, 
      EPartService partService) {
    // Assuming that all editors should open in the the stack with the
    // ID "org.eclipse.e4.primaryDataStack"
    
    MPartStack stack = (MPartStack) modelService.find("org.eclipse.e4.primaryDataStack", application);
    
    MInputPart part = MBasicFactory.INSTANCE.createInputPart();
    // Pointing to the contributing class
    part.setContributionURI("bundleclass://de.vogella.rcp.e4.todo/de.vogella.rcp.e4.parts.Part1");
    part.setInputURI(input.getInputURI());
    part.setIconURI("platform:/plugin/de.vogella.rcp.e4.todo/icons/sample.gif");
    part.setLabel(input.getName());
    part.setTooltip(input.getTooltip());
    part.setCloseable(true);
    stack.getChildren().add(part);
    partService.showPart(part, PartState.ACTIVATE);
  }
} 

A handler for saving all dirty parts could be implemented like this. A similar implementation can be found in the org.eclipse.e4.ui.internal.workbench.handlers.SaveAllHandler class.

public class SaveAllHandler {

  @CanExecute
  boolean canExecute(@Optional MWindow window) {
    if (window != null) {
      IEclipseContext context = window.getContext();
      if (context != null) {
        EPartService partService = context.get(EPartService.class);
        if (partService != null) {
          return !partService.getDirtyParts().isEmpty();
        }
      }
    }
    return false;
  }

  @Execute
  void execute(EPartService partService) {
    partService.saveAll(false);
  }

} 

Saving the current part can be implemented similar to org.eclipse.e4.ui.internal.workbench.handlers.SaveHandler .

public class SaveHandler {

  @CanExecute
  boolean canExecute(@Named(IServiceConstants.ACTIVE_PART) MDirtyable dirtyable) {
    return dirtyable == null ? false : dirtyable.isDirty();
  }

  @Execute
  void execute(EPartService partService, @Named(IServiceConstants.ACTIVE_PART) MPart part) {
    partService.savePart(part, false);
  }

} 

2. Thank you

Please help me to support this article:

Flattr this

3. Questions and Discussion

Before posting questions, please see the vogella FAQ. If you have questions or find an error in this article please use the www.vogella.com Google Group. I have created a short list how to create good questions which might also help you.

4. Links and Literature

4.1. Source Code

Source Code of Examples

http://wiki.eclipse.org/E4 Eclipse E4 - Wiki

Eclipse RCP

Eclipse EMF

Dependency Injection