1
|
|
2
|
package eu.etaxonomy.taxeditor.workbench;
|
3
|
|
4
|
import java.util.Collections;
|
5
|
|
6
|
import javax.inject.Named;
|
7
|
|
8
|
import org.eclipse.core.commands.Command;
|
9
|
import org.eclipse.core.commands.ParameterizedCommand;
|
10
|
import org.eclipse.core.runtime.NullProgressMonitor;
|
11
|
import org.eclipse.e4.core.commands.ECommandService;
|
12
|
import org.eclipse.e4.core.commands.EHandlerService;
|
13
|
import org.eclipse.e4.core.di.annotations.CanExecute;
|
14
|
import org.eclipse.e4.core.di.annotations.Execute;
|
15
|
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
|
16
|
import org.eclipse.e4.ui.services.IServiceConstants;
|
17
|
import org.eclipse.e4.ui.workbench.modeling.EPartService;
|
18
|
import org.eclipse.ui.ISaveablePart;
|
19
|
import org.eclipse.ui.internal.e4.compatibility.CompatibilityPart;
|
20
|
|
21
|
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
|
22
|
import eu.etaxonomy.taxeditor.workbench.part.ISelectionElementEditingPart;
|
23
|
|
24
|
public class SaveHandler {
|
25
|
|
26
|
@Execute
|
27
|
public void execute(EPartService partService, ECommandService commandService, EHandlerService handlerService
|
28
|
, @Named(IServiceConstants.ACTIVE_PART)MPart activePart) {
|
29
|
if(activePart.isDirty()){
|
30
|
savePart(partService, commandService, handlerService, activePart);
|
31
|
} else {
|
32
|
Object e4WrappedPart = WorkbenchUtility.getE4WrappedPart(activePart.getObject());
|
33
|
if(e4WrappedPart instanceof ISelectionElementEditingPart){
|
34
|
ISelectionElementEditingPart editingPart = (ISelectionElementEditingPart)e4WrappedPart;
|
35
|
Object savablePart = WorkbenchUtility.findSavablePart(editingPart);
|
36
|
if(savablePart instanceof ISaveablePart){
|
37
|
((ISaveablePart) savablePart).doSave(new NullProgressMonitor());
|
38
|
}
|
39
|
else if(savablePart instanceof IE4SavablePart){
|
40
|
((IE4SavablePart) savablePart).save(new NullProgressMonitor());
|
41
|
}
|
42
|
else if(savablePart instanceof MPart){
|
43
|
savePart(partService, commandService, handlerService, (MPart) savablePart);
|
44
|
}
|
45
|
}
|
46
|
}
|
47
|
}
|
48
|
|
49
|
private void savePart(EPartService partService, ECommandService commandService, EHandlerService handlerService,
|
50
|
MPart mPart) {
|
51
|
if(mPart.getObject() instanceof CompatibilityPart){
|
52
|
//FIXME E4 remove when fully migrated
|
53
|
Command command = commandService.getCommand("org.eclipse.ui.file.save");
|
54
|
ParameterizedCommand parameterizedCommand = ParameterizedCommand.generateCommand(command, Collections.EMPTY_MAP);
|
55
|
handlerService.executeHandler(parameterizedCommand);
|
56
|
}
|
57
|
else{
|
58
|
partService.savePart(mPart, false);
|
59
|
}
|
60
|
}
|
61
|
|
62
|
|
63
|
@CanExecute
|
64
|
public boolean canExecute(EPartService partService) {
|
65
|
return !partService.getDirtyParts().isEmpty();
|
66
|
}
|
67
|
|
68
|
}
|