Project

General

Profile

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

    
20
import javax.inject.Named;
21

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

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

    
61

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

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

    
81
        this.modelService = modelService;
82
        this.application = application;
83
        this.partService = partService;
84

    
85
        FactualDataPartE4 factualDataPart = (FactualDataPartE4) activePart.getObject();
86
        IStructuredSelection viewselection = (IStructuredSelection) factualDataPart.getViewer().getSelection();
87

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

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

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

    
123
        if(elements.size() == 0 ){
124
            return;
125
        }
126

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

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

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

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

    
161
            Collection<MPart> targetEditors = EditorUtil.checkForChanges(targetTaxon.getUuid(), partService);
162

    
163
            if (targetEditors != null || this.editor.isDirty()){
164
                boolean proceed = MessageDialog.openQuestion(null,
165
                        Messages.MoveDescriptionToOtherTaxonHandler_SAVE_CHANGES, Messages.MoveDescriptionToOtherTaxonHandler_SAVE_CHANGES_MESSAGE);
166
                if( targetEditors.iterator().hasNext() ){
167
                    MPart part = targetEditors.iterator().next();
168
                    if (proceed) {
169
                        if (part != null){
170
                            if (part.getElementId().equals("eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4")){
171
                                TaxonNameEditorE4 targetEditor = (TaxonNameEditorE4) WorkbenchUtility.getE4WrappedPart(part);
172

    
173
                                targetEditor.save(AbstractUtility.getMonitor());
174
                            }
175
                            if (editor.isDirty()){
176
                                editor.save(AbstractUtility.getMonitor());
177
                            }
178
                        } else {
179
                            return;
180
                        }
181
                    }
182
                }
183
            }
184

    
185
//            if ((targetEditor != null && targetEditor.getElementId().equals("eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4")) || this.editor.isDirty()){
186
//                boolean proceed = MessageDialog.openQuestion(null,
187
//                        Messages.MoveDescriptionToOtherTaxonHandler_SAVE_CHANGES, Messages.MoveDescriptionToOtherTaxonHandler_SAVE_CHANGES_MESSAGE);
188
//                if (proceed) {
189
//                    if (targetEditor != null){
190
//                        e4WrappedPart = WorkbenchUtility.getE4WrappedPart(targetEditor);
191
//                        ((TaxonNameEditorE4)e4WrappedPart).save(AbstractUtility.getMonitor());
192
//                    }
193
//                    if (editor.isDirty()){
194
//                        editor.save(AbstractUtility.getMonitor());
195
//                    }
196
//                } else {
197
//                    return;
198
//                }
199
//            }
200

    
201
            String moveMessage = String.format(Messages.MoveDescriptionElementsHandler_ELEMENTS_MOVED, editor.getTaxon());
202

    
203
            AbstractPostTaxonOperation operation = new MoveDescriptionElementsOperation(
204
                    menuItem.getLocalizedLabel(), EditorUtil.getUndoContext(),
205
                    targetTaxon.getUuid(), moveMessage, elements, false, this, null);
206
            editor.getEditorInput().addOperation(operation);
207
            editor.setDirty();
208
            //AbstractUtility.executeOperation(operation, sync);
209
        }
210
    }
211

    
212
    /** {@inheritDoc} */
213
    @Override
214
    public boolean postOperation(Object objectAffectedByOperation) {
215

    
216
        editor.getConversationHolder().bind();
217
        editor.getConversationHolder().commit(true);
218
        Display.getDefault().asyncExec(new Runnable(){
219

    
220
            @Override
221
            public void run() {
222
                //AbstractUtility.close(editor.getMultiPageTaxonEditor());
223

    
224
                    //FIXME E4 migrate
225
//                    MultiPageTaxonEditor possibleOpenEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(newAcceptedTaxonNodeUuid);
226
//                    if(possibleOpenEditor != null){
227
//                        AbstractUtility.close(possibleOpenEditor);
228
//                    }
229
                EditorUtil.openTaxonNodeE4(newAcceptedTaxonNodeUuid, modelService, partService, application);
230
                EditorUtil.openTaxonNodeE4(oldAcceptedTaxonNodeUuid, modelService, partService, application);
231
            }
232

    
233
        });
234

    
235

    
236
        return true;
237
    }
238

    
239
    @Override
240
    public boolean onComplete() {
241
        return false;
242
    }
243

    
244
    @CanExecute
245
    public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_PART) MPart activePart,
246
            MHandledMenuItem menuItem){
247
        boolean canExecute = false;
248
        FactualDataPartE4 factualDataPart = (FactualDataPartE4) activePart.getObject();
249

    
250
        Object e4WrappedPart = WorkbenchUtility.getE4WrappedPart(factualDataPart.getSelectionProvidingPart());
251
        if(e4WrappedPart instanceof TaxonNameEditorE4){
252
            ISelection selection = factualDataPart.getViewer().getSelection();
253
            if(selection instanceof IStructuredSelection){
254
                boolean selectionProviding = factualDataPart.getSelectionProvidingPart().getObject() instanceof TaxonNameEditorE4;
255
                Object firstElement = ((IStructuredSelection) selection).getFirstElement();
256
                canExecute = (firstElement instanceof FeatureNodeContainer && !(((FeatureNodeContainer)firstElement).getContainerTree().getDescription() instanceof TaxonNameDescription))
257
                        || (firstElement instanceof DescriptionElementBase && !(((DescriptionElementBase)firstElement).getInDescription() instanceof TaxonNameDescription)) ;
258
                menuItem.setVisible(canExecute);
259
            }
260
        }
261
        return canExecute;
262
    }
263

    
264

    
265
}
(7-7/9)