Project

General

Profile

Download (7.82 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 * Copyright (C) 2007 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

    
10
package eu.etaxonomy.taxeditor.editor.view.descriptive.e4.handler;
11

    
12
import java.util.ArrayList;
13
import java.util.List;
14
import java.util.UUID;
15

    
16
import javax.inject.Named;
17

    
18
import org.eclipse.e4.core.di.annotations.CanExecute;
19
import org.eclipse.e4.core.di.annotations.Execute;
20
import org.eclipse.e4.core.di.annotations.Optional;
21
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
22
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
23
import org.eclipse.e4.ui.model.application.ui.menu.MMenuItem;
24
import org.eclipse.e4.ui.services.IServiceConstants;
25
import org.eclipse.jface.viewers.ISelection;
26
import org.eclipse.jface.viewers.IStructuredSelection;
27
import org.eclipse.swt.widgets.Display;
28
import org.eclipse.swt.widgets.Shell;
29
import org.eclipse.ui.PartInitException;
30

    
31
import eu.etaxonomy.cdm.api.service.IDescriptionService;
32
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
33
import eu.etaxonomy.cdm.model.common.CdmBase;
34
import eu.etaxonomy.cdm.model.description.DescriptionBase;
35
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
36
import eu.etaxonomy.cdm.model.description.TaxonDescription;
37
import eu.etaxonomy.cdm.model.taxon.Classification;
38
import eu.etaxonomy.cdm.model.taxon.Taxon;
39
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
40
import eu.etaxonomy.taxeditor.editor.EditorUtil;
41
import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
42
import eu.etaxonomy.taxeditor.editor.Page;
43
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
44
import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
45
import eu.etaxonomy.taxeditor.editor.view.descriptive.e4.FactualDataPartE4;
46
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.MoveDescriptionElementsOperation;
47
import eu.etaxonomy.taxeditor.model.AbstractUtility;
48
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
49
import eu.etaxonomy.taxeditor.model.MessagingUtils;
50
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
51
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
52
import eu.etaxonomy.taxeditor.store.CdmStore;
53
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
54
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
55

    
56

    
57
/**
58
 *
59
 * @author pplitzner
60
 * @date 15.08.2017
61
 *
62
 */
63
public class MoveDescriptionElementsHandler implements IPostOperationEnabled{
64
    private UUID newAcceptedTaxonNodeUuid;
65
    private TaxonNameEditor editor;
66

    
67
    @Execute
68
    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
69
            @Optional@Named(IServiceConstants.ACTIVE_SELECTION) Object selection,
70
            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell, MMenuItem menuItem) {
71

    
72
        FactualDataPartE4 factualDataPart = (FactualDataPartE4) activePart.getObject();
73
        Object e4WrappedPart = WorkbenchUtility.getE4WrappedPart(factualDataPart.getSelectionProvidingPart());
74
        if(e4WrappedPart instanceof MultiPageTaxonEditor){
75
            editor = (TaxonNameEditor) ((MultiPageTaxonEditor) e4WrappedPart).getPage(Page.NAME);
76
        }
77
        Taxon actualTaxon= null;
78

    
79
        List<DescriptionElementBase> elements = new ArrayList<DescriptionElementBase>();
80

    
81
        //			for(Object element : structuredSelection.toArray()){
82
        if(selection instanceof DescriptionElementBase){
83
            UUID uuid = ((DescriptionElementBase) selection).getUuid();
84

    
85
            elements.add(CdmStore.getService(IDescriptionService.class).loadDescriptionElement(uuid, null));
86
        } else if(selection instanceof FeatureNodeContainer){
87
            for(DescriptionElementBase de : ((FeatureNodeContainer)selection).getDescriptionElements()){
88
                elements.add(
89
                        CdmStore.getService(IDescriptionService.class).loadDescriptionElement(de.getUuid(), null)
90
                        );
91
            }
92
        }
93
        //			}
94

    
95
        if(elements.size() == 0){
96
            return;
97
        }
98

    
99
        DescriptionBase description = elements.get(0).getInDescription();
100
        List<UUID> excludeTaxa = new ArrayList<UUID>();
101
        if (description!=null && description.isInstanceOf(TaxonDescription.class)){
102
            TaxonDescription taxonDescription = HibernateProxyHelper.deproxy(description, TaxonDescription.class);
103
            actualTaxon = taxonDescription.getTaxon();
104
            excludeTaxa.add(actualTaxon.getUuid());
105
        }
106
        Classification classification = null;
107
        TaxonNode actualNode = null;
108
        if (actualTaxon != null){
109
            if (!actualTaxon.getTaxonNodes().isEmpty() && actualTaxon.getTaxonNodes().size() ==1){
110
                actualNode = actualTaxon.getTaxonNodes().iterator().next();
111
                classification = actualNode.getClassification();
112

    
113
            }
114
        }
115
        TaxonNode newAcceptedTaxonNode = TaxonNodeSelectionDialog.select(shell,
116
                editor.getConversationHolder(),
117
                Messages.MoveDescriptionElementsHandler_CHOOSE_ACC_TAXON,
118
                excludeTaxa,
119
                null, classification
120
                );
121
        if (newAcceptedTaxonNode != null){
122
            Taxon targetTaxon = newAcceptedTaxonNode.getTaxon();
123

    
124
            if(targetTaxon == null){
125
                // canceled
126
                return;
127
            }
128
            newAcceptedTaxonNodeUuid = newAcceptedTaxonNode.getUuid();
129

    
130
            String moveMessage = String.format(Messages.MoveDescriptionElementsHandler_ELEMENTS_MOVED, editor.getEditor().getTaxon());
131

    
132
            AbstractPostOperation operation = new MoveDescriptionElementsOperation(
133
                    menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(),
134
                    targetTaxon.getUuid(), moveMessage, elements, false, this);
135

    
136
            AbstractUtility.executeOperation(operation);
137
        }
138
    }
139

    
140
    /** {@inheritDoc} */
141
    @Override
142
    public boolean postOperation(CdmBase objectAffectedByOperation) {
143

    
144
        editor.getConversationHolder().bind();
145
        editor.getConversationHolder().commit(true);
146
        Display.getDefault().asyncExec(new Runnable(){
147

    
148
            @Override
149
            public void run() {
150
                //AbstractUtility.close(editor.getMultiPageTaxonEditor());
151

    
152
                try {
153
                    MultiPageTaxonEditor possibleOpenEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(newAcceptedTaxonNodeUuid);
154
                    if(possibleOpenEditor != null){
155
                        AbstractUtility.close(possibleOpenEditor);
156
                    }
157
                    EditorUtil.openTaxonNode(newAcceptedTaxonNodeUuid);
158
                } catch (PartInitException e) {
159
                    MessagingUtils.error(this.getClass(), e);
160
                    throw new RuntimeException(e);
161
                } catch (Exception e) {
162
                    MessagingUtils.warningDialog(Messages.MoveDescriptionElementsHandler_CREATE_FAILURE, this, e.getMessage());
163
                }
164
            }
165

    
166
        });
167

    
168

    
169
        return true;
170
    }
171

    
172
    @Override
173
    public boolean onComplete() {
174
        return false;
175
    }
176

    
177
    @CanExecute
178
    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_PART) MPart activePart,
179
            MHandledMenuItem menuItem){
180
        boolean canExecute = false;
181
        FactualDataPartE4 factualDataPart = (FactualDataPartE4) activePart.getObject();
182
        ISelection selection = factualDataPart.getViewer().getSelection();
183
        if(selection instanceof IStructuredSelection){
184
            Object firstElement = ((IStructuredSelection) selection).getFirstElement();
185
             canExecute = firstElement instanceof FeatureNodeContainer
186
                    || firstElement instanceof DescriptionElementBase;
187
            menuItem.setVisible(canExecute);
188
        }
189
        return canExecute;
190
    }
191

    
192

    
193
}
(6-6/8)