Revision e6f456aa
Added by Patrick Plitzner about 6 years ago
eu.etaxonomy.taxeditor.workbench/src/main/java/eu/etaxonomy/taxeditor/workbench/SaveHandler.java | ||
---|---|---|
1 | 1 |
|
2 | 2 |
package eu.etaxonomy.taxeditor.workbench; |
3 | 3 |
|
4 |
import java.util.Collections; |
|
5 |
|
|
6 | 4 |
import javax.inject.Named; |
7 | 5 |
|
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 | 6 |
import org.eclipse.e4.core.di.annotations.CanExecute; |
14 | 7 |
import org.eclipse.e4.core.di.annotations.Execute; |
15 | 8 |
import org.eclipse.e4.ui.model.application.ui.basic.MPart; |
16 | 9 |
import org.eclipse.e4.ui.services.IServiceConstants; |
17 | 10 |
import org.eclipse.e4.ui.workbench.modeling.EPartService; |
18 |
import org.eclipse.ui.ISaveablePart; |
|
19 |
import org.eclipse.ui.internal.e4.compatibility.CompatibilityPart; |
|
20 | 11 |
|
21 |
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart; |
|
22 | 12 |
import eu.etaxonomy.taxeditor.workbench.part.ISelectionElementEditingPart; |
23 | 13 |
|
24 | 14 |
public class SaveHandler { |
25 | 15 |
|
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 |
} |
|
16 |
@CanExecute |
|
17 |
public boolean canExecute(EPartService partService) { |
|
18 |
MPart activePart = partService.getActivePart(); |
|
19 |
if(activePart==null){ |
|
20 |
return false; |
|
46 | 21 |
} |
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); |
|
22 |
boolean isSavable = activePart.isDirty(); |
|
23 |
if(!isSavable){ |
|
24 |
if(activePart.getObject() instanceof ISelectionElementEditingPart){ |
|
25 |
MPart savablePart = WorkbenchUtility.findSavablePart((ISelectionElementEditingPart) activePart.getObject()); |
|
26 |
isSavable = savablePart.isDirty(); |
|
27 |
} |
|
59 | 28 |
} |
29 |
return isSavable; |
|
60 | 30 |
} |
61 | 31 |
|
62 | 32 |
|
63 |
@CanExecute |
|
64 |
public boolean canExecute(EPartService partService) { |
|
65 |
return !partService.getDirtyParts().isEmpty(); |
|
66 |
} |
|
33 |
@Execute |
|
34 |
void execute(EPartService partService, @Named(IServiceConstants.ACTIVE_PART) MPart part) { |
|
35 |
if(part.isDirty()){ |
|
36 |
partService.savePart(part, false); |
|
37 |
} |
|
38 |
else if(part.getObject() instanceof ISelectionElementEditingPart){ |
|
39 |
MPart savablePart = WorkbenchUtility.findSavablePart((ISelectionElementEditingPart) part.getObject()); |
|
40 |
partService.savePart(savablePart, false); |
|
41 |
} |
|
42 |
} |
|
67 | 43 |
|
68 | 44 |
} |
Also available in: Unified diff
Fix state handling of save button/dirty flag for indirect edits(details)