Project

General

Profile

Download (10 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.HashSet;
14
import java.util.Iterator;
15
import java.util.List;
16
import java.util.Set;
17
import java.util.UUID;
18

    
19
import javax.inject.Named;
20

    
21
import org.eclipse.e4.core.di.annotations.CanExecute;
22
import org.eclipse.e4.core.di.annotations.Execute;
23
import org.eclipse.e4.core.di.annotations.Optional;
24
import org.eclipse.e4.ui.di.UISynchronize;
25
import org.eclipse.e4.ui.model.application.MApplication;
26
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
27
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
28
import org.eclipse.e4.ui.model.application.ui.menu.MMenuItem;
29
import org.eclipse.e4.ui.services.IServiceConstants;
30
import org.eclipse.e4.ui.workbench.modeling.EModelService;
31
import org.eclipse.e4.ui.workbench.modeling.EPartService;
32
import org.eclipse.jface.dialogs.MessageDialog;
33
import org.eclipse.jface.viewers.ISelection;
34
import org.eclipse.jface.viewers.IStructuredSelection;
35
import org.eclipse.swt.widgets.Display;
36
import org.eclipse.swt.widgets.Shell;
37

    
38
import eu.etaxonomy.cdm.api.service.IDescriptionService;
39
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
40
import eu.etaxonomy.cdm.model.description.DescriptionBase;
41
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
42
import eu.etaxonomy.cdm.model.description.TaxonDescription;
43
import eu.etaxonomy.cdm.model.taxon.Classification;
44
import eu.etaxonomy.cdm.model.taxon.Taxon;
45
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
46
import eu.etaxonomy.taxeditor.editor.EditorUtil;
47
import eu.etaxonomy.taxeditor.editor.l10n.Messages;
48
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
49
import eu.etaxonomy.taxeditor.editor.view.descriptive.e4.FactualDataPartE4;
50
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.MoveDescriptionElementsOperation;
51
import eu.etaxonomy.taxeditor.model.AbstractUtility;
52
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
53
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
54
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
55
import eu.etaxonomy.taxeditor.store.CdmStore;
56
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
57
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
58

    
59

    
60
/**
61
 *
62
 * @author pplitzner
63
 * @date 15.08.2017
64
 *
65
 */
66
public class MoveDescriptionElementsHandlerE4 implements IPostOperationEnabled{
67
    private UUID newAcceptedTaxonNodeUuid;
68
    private UUID oldAcceptedTaxonNodeUuid;
69
    private TaxonNameEditorE4 editor;
70
    private MApplication application;
71
    private EModelService modelService;
72
    private EPartService partService;
73

    
74
    @Execute
75
    public void execute(@Named(IServiceConstants.ACTIVE_PART) MPart activePart,
76
            @Named(IServiceConstants.ACTIVE_SHELL) Shell shell, MMenuItem menuItem, EPartService partService,
77
            EModelService modelService, MApplication application, UISynchronize sync) {
78

    
79
        this.modelService = modelService;
80
        this.application = application;
81
        this.partService = partService;
82

    
83
        FactualDataPartE4 factualDataPart = (FactualDataPartE4) activePart.getObject();
84
        IStructuredSelection viewselection = (IStructuredSelection) factualDataPart.getViewer().getSelection();
85

    
86
        Object e4WrappedPart = WorkbenchUtility.getE4WrappedPart(factualDataPart.getSelectionProvidingPart());
87
        if(e4WrappedPart instanceof TaxonNameEditorE4){
88
            editor = (TaxonNameEditorE4) e4WrappedPart;
89
        }
90
        Taxon actualTaxon= null;
91

    
92
        List<DescriptionElementBase> elements = new ArrayList<DescriptionElementBase>();
93
        DescriptionBase description = null;
94
        for (Iterator iter = viewselection.iterator(); iter.hasNext();) {
95
            Object object = iter.next();
96
            if (object instanceof DescriptionElementBase) {
97
                UUID uuid = ((DescriptionElementBase) object).getUuid();
98
                description = ((DescriptionElementBase) object).getInDescription();
99
                DescriptionElementBase element ;
100
                if (((DescriptionElementBase) object).getId() > 0){
101
                    element = CdmStore.getService(IDescriptionService.class).loadDescriptionElement(uuid, null);
102
                }else{
103
                    element = ((DescriptionElementBase) object);
104
                }
105

    
106
                elements.add(element);
107
            } else if (object instanceof FeatureNodeContainer) {
108
                for (DescriptionElementBase de : ((FeatureNodeContainer) object).getDescriptionElements()) {
109
                    DescriptionElementBase element ;
110
                    if (de.getId() > 0){
111
                        element = CdmStore.getService(IDescriptionService.class).loadDescriptionElement(de.getUuid(), null);
112
                    }else{
113
                        element = (de);
114
                    }
115
                    description = de.getInDescription();
116
                    elements.add(element);
117
                }
118
            }
119
        }
120

    
121
        if(elements.size() == 0 ){
122
            return;
123
        }
124

    
125
        if (elements.get(0) != null){
126
            description = elements.get(0).getInDescription();
127
        }
128
        Set<UUID> excludeTaxa = new HashSet<>();
129
        if (description!=null && description.isInstanceOf(TaxonDescription.class)){
130
            TaxonDescription taxonDescription = HibernateProxyHelper.deproxy(description, TaxonDescription.class);
131
            actualTaxon = taxonDescription.getTaxon();
132

    
133
        }
134
        Classification classification = null;
135
        TaxonNode actualNode = null;
136
        if (actualTaxon != null){
137
            if (!actualTaxon.getTaxonNodes().isEmpty() && actualTaxon.getTaxonNodes().size() ==1){
138
                actualNode = actualTaxon.getTaxonNodes().iterator().next();
139
                oldAcceptedTaxonNodeUuid = actualNode.getUuid();
140
                excludeTaxa.add(oldAcceptedTaxonNodeUuid);
141
                classification = actualNode.getClassification();
142

    
143
            }
144
        }
145
        TaxonNode newAcceptedTaxonNode = TaxonNodeSelectionDialog.select(shell,
146
                Messages.MoveDescriptionElementsHandler_CHOOSE_ACC_TAXON,
147
                excludeTaxa,
148
                null, classification.getUuid()
149
                );
150
        if (newAcceptedTaxonNode != null){
151
            Taxon targetTaxon = newAcceptedTaxonNode.getTaxon();
152

    
153
            if(targetTaxon == null){
154
                // canceled
155
                return;
156
            }
157
            newAcceptedTaxonNodeUuid = newAcceptedTaxonNode.getUuid();
158

    
159
            MPart targetEditor = EditorUtil.checkForChanges(targetTaxon.getUuid(), partService);
160

    
161
            if ((targetEditor != null && targetEditor.getElementId().equals("eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4")) || this.editor.isDirty()){
162
                boolean proceed = MessageDialog.openQuestion(null,
163
                        Messages.MoveDescriptionToOtherTaxonHandler_SAVE_CHANGES, Messages.MoveDescriptionToOtherTaxonHandler_SAVE_CHANGES_MESSAGE);
164
                if (proceed) {
165
                    if (targetEditor != null){
166
                        e4WrappedPart = WorkbenchUtility.getE4WrappedPart(targetEditor);
167
                        ((TaxonNameEditorE4)e4WrappedPart).save(AbstractUtility.getMonitor());
168
                    }
169
                    if (editor.isDirty()){
170
                        editor.save(AbstractUtility.getMonitor());
171
                    }
172
                } else {
173
                    return;
174
                }
175
            }
176

    
177
            String moveMessage = String.format(Messages.MoveDescriptionElementsHandler_ELEMENTS_MOVED, editor.getTaxon());
178

    
179
            AbstractPostOperation operation = new MoveDescriptionElementsOperation(
180
                    menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(),
181
                    targetTaxon.getUuid(), moveMessage, elements, false, this, null);
182

    
183
            AbstractUtility.executeOperation(operation, sync);
184
        }
185
    }
186

    
187
    /** {@inheritDoc} */
188
    @Override
189
    public boolean postOperation(Object objectAffectedByOperation) {
190

    
191
        editor.getConversationHolder().bind();
192
        editor.getConversationHolder().commit(true);
193
        Display.getDefault().asyncExec(new Runnable(){
194

    
195
            @Override
196
            public void run() {
197
                //AbstractUtility.close(editor.getMultiPageTaxonEditor());
198

    
199
                    //FIXME E4 migrate
200
//                    MultiPageTaxonEditor possibleOpenEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(newAcceptedTaxonNodeUuid);
201
//                    if(possibleOpenEditor != null){
202
//                        AbstractUtility.close(possibleOpenEditor);
203
//                    }
204
                EditorUtil.openTaxonNodeE4(newAcceptedTaxonNodeUuid, modelService, partService, application);
205
                EditorUtil.openTaxonNodeE4(oldAcceptedTaxonNodeUuid, modelService, partService, application);
206
            }
207

    
208
        });
209

    
210

    
211
        return true;
212
    }
213

    
214
    @Override
215
    public boolean onComplete() {
216
        return false;
217
    }
218

    
219
    @CanExecute
220
    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_PART) MPart activePart,
221
            MHandledMenuItem menuItem){
222
        boolean canExecute = false;
223
        FactualDataPartE4 factualDataPart = (FactualDataPartE4) activePart.getObject();
224

    
225
        Object e4WrappedPart = WorkbenchUtility.getE4WrappedPart(factualDataPart.getSelectionProvidingPart());
226
        if(e4WrappedPart instanceof TaxonNameEditorE4){
227
            ISelection selection = factualDataPart.getViewer().getSelection();
228
            if(selection instanceof IStructuredSelection){
229
                Object firstElement = ((IStructuredSelection) selection).getFirstElement();
230
                canExecute = firstElement instanceof FeatureNodeContainer
231
                        || firstElement instanceof DescriptionElementBase;
232
                menuItem.setVisible(canExecute);
233
            }
234
        }
235
        return canExecute;
236
    }
237

    
238

    
239
}
(7-7/9)