Project

General

Profile

Download (3.79 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.taxeditor.editor.view.media.handler;
10

    
11
import javax.inject.Named;
12

    
13
import org.eclipse.e4.core.di.annotations.CanExecute;
14
import org.eclipse.e4.core.di.annotations.Execute;
15
import org.eclipse.e4.ui.di.UISynchronize;
16
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
17
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
18
import org.eclipse.e4.ui.model.application.ui.menu.MMenuItem;
19
import org.eclipse.e4.ui.services.IServiceConstants;
20
import org.eclipse.jface.viewers.ISelection;
21
import org.eclipse.jface.viewers.TreePath;
22
import org.eclipse.jface.viewers.TreeSelection;
23

    
24
import eu.etaxonomy.cdm.model.description.DescriptionBase;
25
import eu.etaxonomy.cdm.model.media.Media;
26
import eu.etaxonomy.cdm.model.taxon.Taxon;
27
import eu.etaxonomy.taxeditor.editor.EditorUtil;
28
import eu.etaxonomy.taxeditor.editor.e4.TaxonEditorInput;
29
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditor;
30
import eu.etaxonomy.taxeditor.editor.view.media.e4.MediaViewPartE4;
31
import eu.etaxonomy.taxeditor.editor.view.media.operation.MoveMediaInListOperation;
32
import eu.etaxonomy.taxeditor.model.AbstractUtility;
33
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
34
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
35

    
36
/**
37
 * @author pplitzner
38
 * @since Jun 6, 2018
39
 */
40
public abstract class AbstractMoveImageHandler {
41

    
42
    @Execute
43
    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
44
            MMenuItem menuItem,
45
            UISynchronize sync) {
46

    
47
        MediaViewPartE4 mediaView = (MediaViewPartE4) activePart.getObject();
48

    
49
        TaxonNameEditor editor = null;
50
        Object e4WrappedPart = WorkbenchUtility.getE4WrappedPart(mediaView.getSelectionProvidingPart());
51
        if (e4WrappedPart instanceof TaxonNameEditor) {
52
            editor = (TaxonNameEditor) e4WrappedPart;
53
            TaxonEditorInput input = editor.getEditorInput();
54
            Taxon taxon =input.getTaxon();
55

    
56
            TreeSelection selection = (TreeSelection) mediaView.getViewer().getSelection();
57
            TreePath[] paths = selection.getPaths();
58
            int count = paths[0].getSegmentCount();
59
            DescriptionBase<?> description = null;
60
            for (int i = 0; i < count; i++ ) {
61
                if (paths[0].getSegment(i) instanceof DescriptionBase) {
62
                    description = (DescriptionBase<?>) paths[0].getSegment(i);
63
                    break;
64
                }
65
            }
66
            AbstractPostOperation<?> operation = null;
67
            // TODO use undo context specific to editor
68
            operation = new MoveMediaInListOperation(menuItem.getLocalizedLabel(),
69
                    EditorUtil.getUndoContext(),
70
                    taxon,
71
                    description,
72
                    (Media)selection.getFirstElement(),
73
                    moveImageOperation(),
74
                    mediaView);
75
            AbstractUtility.executeOperation(operation, sync);
76
        }
77
    }
78

    
79
    public abstract int moveImageOperation();
80

    
81
    @CanExecute
82
    private boolean canExecute(MHandledMenuItem menuItem,
83
            @Named(IServiceConstants.ACTIVE_PART) MPart activePart){
84
        boolean canExecute = false;
85
        MediaViewPartE4 mediaView = (MediaViewPartE4) activePart.getObject();
86
        ISelection selection = mediaView.getViewer().getSelection();
87
        canExecute = selection instanceof TreeSelection
88
                && ((TreeSelection) selection).size()==1
89
                && ((TreeSelection) selection).getFirstElement() instanceof Media;
90
        menuItem.setVisible(canExecute);
91
        return canExecute;
92
    }
93
}
(1-1/7)