Project

General

Profile

Download (7.92 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.l10n.Messages;
42
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
43
import eu.etaxonomy.taxeditor.editor.view.descriptive.e4.FactualDataPartE4;
44
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.MoveDescriptionElementsOperation;
45
import eu.etaxonomy.taxeditor.model.AbstractUtility;
46
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
47
import eu.etaxonomy.taxeditor.model.MessagingUtils;
48
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
49
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
50
import eu.etaxonomy.taxeditor.store.CdmStore;
51
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
52
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
53

    
54

    
55
/**
56
 *
57
 * @author pplitzner
58
 * @date 15.08.2017
59
 *
60
 */
61
public class MoveDescriptionElementsHandlerE4 implements IPostOperationEnabled{
62
    private UUID newAcceptedTaxonNodeUuid;
63
    private TaxonNameEditorE4 editor;
64

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

    
70
        FactualDataPartE4 factualDataPart = (FactualDataPartE4) activePart.getObject();
71
        Object e4WrappedPart = WorkbenchUtility.getE4WrappedPart(factualDataPart.getSelectionProvidingPart());
72
        if(e4WrappedPart instanceof TaxonNameEditorE4){
73
            editor = (TaxonNameEditorE4) e4WrappedPart;
74
        }
75
        Taxon actualTaxon= null;
76

    
77
        List<DescriptionElementBase> elements = new ArrayList<DescriptionElementBase>();
78

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

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

    
93
        if(elements.size() == 0){
94
            return;
95
        }
96

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

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

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

    
128
            String moveMessage = String.format(Messages.MoveDescriptionElementsHandler_ELEMENTS_MOVED, editor.getTaxon());
129

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

    
134
            AbstractUtility.executeOperation(operation);
135
        }
136
    }
137

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

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

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

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

    
165
        });
166

    
167

    
168
        return true;
169
    }
170

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

    
176
    @CanExecute
177
    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_PART) MPart activePart,
178
            MHandledMenuItem menuItem){
179
        boolean canExecute = false;
180
        FactualDataPartE4 factualDataPart = (FactualDataPartE4) activePart.getObject();
181

    
182
        Object e4WrappedPart = WorkbenchUtility.getE4WrappedPart(factualDataPart.getSelectionProvidingPart());
183
        if(e4WrappedPart instanceof TaxonNameEditorE4){
184
            ISelection selection = factualDataPart.getViewer().getSelection();
185
            if(selection instanceof IStructuredSelection){
186
                Object firstElement = ((IStructuredSelection) selection).getFirstElement();
187
                canExecute = firstElement instanceof FeatureNodeContainer
188
                        || firstElement instanceof DescriptionElementBase;
189
                menuItem.setVisible(canExecute);
190
            }
191
        }
192
        return canExecute;
193
    }
194

    
195

    
196
}
(6-6/8)