Project

General

Profile

Download (5.61 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.core.di.annotations.CanExecute;
10
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
11
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
12
import org.eclipse.e4.ui.services.IServiceConstants;
13
import org.eclipse.jface.dialogs.MessageDialog;
14
import org.eclipse.jface.viewers.TreeSelection;
15
import org.eclipse.swt.widgets.Display;
16
import org.eclipse.swt.widgets.Shell;
17

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

    
30
public class MoveFactualDataHandlerE4 {
31

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

    
36
        TaxonNavigatorE4 taxonNavigator = (TaxonNavigatorE4) activePart.getObject();
37

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

    
70
                    taxonNavigator.getConversationHolder().bind();
71
                    taxonNavigator.getConversationHolder().commit();
72

    
73
                    Display.getDefault().asyncExec(new Runnable(){
74

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

    
96
                    });
97
                }
98
            }
99
        }
100
    }
101

    
102
    @CanExecute
103
    private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
104
        boolean canExecute = false;
105
        menuItem.setVisible(canExecute);
106
        canExecute = selection.getFirstElement() instanceof TaxonNode;
107
        return canExecute;
108
    }
109

    
110
}
(6-6/11)