Project

General

Profile

Download (5.17 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.taxeditor.navigation.navigator.e4.handler;
2

    
3
import java.util.ArrayList;
4
import java.util.List;
5
import java.util.UUID;
6

    
7
import javax.inject.Named;
8

    
9
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
10
import org.eclipse.e4.ui.services.IServiceConstants;
11
import org.eclipse.jface.dialogs.MessageDialog;
12
import org.eclipse.jface.viewers.TreeSelection;
13
import org.eclipse.swt.widgets.Display;
14
import org.eclipse.swt.widgets.Shell;
15

    
16
import eu.etaxonomy.cdm.api.service.IDescriptionService;
17
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
18
import eu.etaxonomy.cdm.api.service.ITaxonService;
19
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
20
import eu.etaxonomy.cdm.model.taxon.Taxon;
21
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
22
import eu.etaxonomy.taxeditor.navigation.NavigationUtil;
23
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
24
import eu.etaxonomy.taxeditor.navigation.navigator.e4.TaxonNavigatorE4;
25
import eu.etaxonomy.taxeditor.store.CdmStore;
26
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
27

    
28
public class MoveFactualDataHandlerE4 {
29

    
30
    public void execute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection,
31
            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell,
32
            @Named(IServiceConstants.ACTIVE_PART)MPart activePart) {
33

    
34
        TaxonNavigatorE4 taxonNavigator = (TaxonNavigatorE4) activePart.getObject();
35

    
36
        if(taxonNavigator!=null){
37
            Object object = selection.getFirstElement();
38
            if(object instanceof TaxonNode){
39
                TaxonNode taxonNode = HibernateProxyHelper.deproxy(object, TaxonNode.class);
40
                final TaxonNode sourceTaxonNode = CdmStore.getService(ITaxonNodeService.class).load(taxonNode.getUuid());
41
                if(NavigationUtil.isDirty(sourceTaxonNode)){
42
                    MessageDialog.openWarning(shell, Messages.MoveFactualDataHandler_UNSAVED_SOURCE, Messages.MoveFactualDataHandler_UNSAVED_SOURCE_MESSAGE);
43
                    return;
44
                }
45
                //reload to avoid session conflicts
46
                Taxon taxon = HibernateProxyHelper.deproxy(CdmStore.getService(ITaxonService.class).load(sourceTaxonNode.getTaxon().getUuid()), Taxon.class);
47
                if(taxon!=null){
48
                 // Choose the target taxon
49
                    List<UUID> excludeTaxa = new ArrayList<UUID>();
50
                    excludeTaxa.add(taxon.getUuid());
51
                    TaxonNode dialogTaxonNode = TaxonNodeSelectionDialog.select(shell,
52
                            taxonNavigator.getConversationHolder(),
53
                            Messages.MoveFactualDataHandler_CHOOSE_TAXON,
54
                            excludeTaxa,
55
                            sourceTaxonNode,
56
                            sourceTaxonNode.getClassification());
57
                    if (dialogTaxonNode == null) {
58
                        return;
59
                    }
60
                    //reload to avoid session conflicts
61
                    final TaxonNode targetTaxonNode = CdmStore.getService(ITaxonNodeService.class).load(dialogTaxonNode.getUuid());
62
                    if(NavigationUtil.isDirty(targetTaxonNode)){
63
                        MessageDialog.openWarning(shell, Messages.MoveFactualDataHandler_UNSAVED_TARGET, Messages.MoveFactualDataHandler_UNSAVED_TARGET_MESSAGE);
64
                        return;
65
                    }
66
                    CdmStore.getService(IDescriptionService.class).moveTaxonDescriptions(taxon.getUuid(),targetTaxonNode.getTaxon().getUuid());
67

    
68
                    taxonNavigator.getConversationHolder().bind();
69
                    taxonNavigator.getConversationHolder().commit();
70

    
71
                    Display.getDefault().asyncExec(new Runnable(){
72

    
73
                        @Override
74
                        public void run() {
75
                            //FIXME E4 refresh factual view via events or similar
76
//                            try {
77
//                                //close and re-open to refresh factual data view
78
//                                MultiPageTaxonEditor sourceEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(sourceTaxonNode.getUuid());
79
//                                MultiPageTaxonEditor targetEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(targetTaxonNode.getUuid());
80
//                                if(targetEditor != null){
81
//                                    AbstractUtility.close(sourceEditor);
82
//                                    AbstractUtility.close(targetEditor);
83
//                                }
84
//                                EditorUtil.openTaxonNodeE4(sourceTaxonNode.getUuid());
85
//                                EditorUtil.openTaxonNodeE4(targetTaxonNode.getUuid());
86
//                            } catch (PartInitException e) {
87
//                                MessagingUtils.error(this.getClass(), e);
88
//                                throw new RuntimeException(e);
89
//                            } catch (Exception e) {
90
//                                MessagingUtils.warningDialog(Messages.MoveFactualDataHandler_CREATE_FAILED, this, e.getMessage());
91
//                            }
92
                        }
93

    
94
                    });
95
                }
96
            }
97
        }
98
    }
99

    
100
}
(6-6/11)