Version 5.7
Copyright © 2009, 2010 , 2011, 2012 Lars Vogel
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 |
Table of Contents
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); } }
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.
http://wiki.eclipse.org/E4 Eclipse E4 - Wiki