Project

General

Profile

Download (3.27 KB) Statistics
| Branch: | Tag: | Revision:
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 = 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 Object findSavablePart(ISelectionElementEditingPart part){
50
	    Object selectionProvidingPart = part.getSelectionProvidingPart();
51
	    if(selectionProvidingPart instanceof ISaveablePart || selectionProvidingPart instanceof IE4SavablePart || selectionProvidingPart instanceof MPart){
52
	        return selectionProvidingPart;
53
	    }
54
	    else if(selectionProvidingPart instanceof ISelectionElementEditingPart){
55
	        return findSavablePart((ISelectionElementEditingPart) selectionProvidingPart);
56
	    }
57
	    return null;
58
	}
59

    
60

    
61
    private void savePart(EPartService partService, ECommandService commandService, EHandlerService handlerService,
62
            MPart mPart) {
63
        if(mPart.getObject() instanceof CompatibilityPart){
64
            //FIXME E4 remove when fully migrated
65
            Command command = commandService.getCommand("org.eclipse.ui.file.save");
66
            ParameterizedCommand parameterizedCommand = ParameterizedCommand.generateCommand(command, Collections.EMPTY_MAP);
67
            handlerService.executeHandler(parameterizedCommand);
68
        }
69
        else{
70
            partService.savePart(mPart, false);
71
        }
72
    }
73

    
74

    
75
	@CanExecute
76
	public boolean canExecute(EPartService partService) {
77
	    return !partService.getDirtyParts().isEmpty();
78
	}
79

    
80
}
(6-6/8)