Project

General

Profile

Download (5.85 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2017 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.taxeditor.navigation.navigator.e4.handler;
11

    
12
import java.util.Collection;
13
import java.util.Iterator;
14

    
15
import javax.inject.Named;
16

    
17
import org.eclipse.core.commands.operations.AbstractOperation;
18
import org.eclipse.core.runtime.IStatus;
19
import org.eclipse.core.runtime.NullProgressMonitor;
20
import org.eclipse.core.runtime.Status;
21
import org.eclipse.e4.core.di.annotations.CanExecute;
22
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
23
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
24
import org.eclipse.e4.ui.services.IServiceConstants;
25
import org.eclipse.jface.dialogs.MessageDialog;
26
import org.eclipse.jface.viewers.IStructuredSelection;
27
import org.eclipse.jface.viewers.TreeSelection;
28
import org.eclipse.jface.window.Window;
29
import org.eclipse.jface.wizard.WizardDialog;
30
import org.eclipse.swt.widgets.Shell;
31

    
32
import eu.etaxonomy.cdm.api.service.config.PublishForSubtreeConfigurator;
33
import eu.etaxonomy.cdm.model.taxon.Classification;
34
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
35
import eu.etaxonomy.taxeditor.bulkeditor.e4.BulkEditorE4;
36
import eu.etaxonomy.taxeditor.editor.EditorUtil;
37
import eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4;
38
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
39
import eu.etaxonomy.taxeditor.navigation.navigator.operation.SetPublishForSubtreeOperation;
40
import eu.etaxonomy.taxeditor.operation.e4.RemotingCdmHandlerE4;
41
import eu.etaxonomy.taxeditor.ui.dialog.configurator.SetPublishForSubtreeWizard;
42
import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
43

    
44
/**
45
 * @author k.luther
46
 * @date 11.10.2017
47
 *
48
 */
49
public class SetPublishFlagForSubtreeHandlerE4 extends RemotingCdmHandlerE4 {
50

    
51
    /**
52
     * @param label
53
     */
54
    public SetPublishFlagForSubtreeHandlerE4() {
55
        //FIXME add missing l10n property
56
//        super(TaxonNavigatorLabels.CHANGE_PUBLISH_FOR_SUBTREE);
57
        super("");
58
    }
59

    
60

    
61
    private TaxonNodeDto taxonNode;
62
    private PublishForSubtreeConfigurator configurator;
63

    
64

    
65
    @Override
66
    public IStatus allowOperations(IStructuredSelection selection, Shell shell, MPart activePart,
67
            MHandledMenuItem menuItem) {
68
     // check that only a single taxon tree node has been selected
69
        if(selection.size() > 1) {  }
70

    
71
        // check for no taxon tree node selected
72
        if(selection.size() == 0) {
73
            return new Status(IStatus.ERROR,
74
                    "unknown", //$NON-NLS-1$
75
                    TaxonNavigatorLabels.NO_TAXON_SELECTION_MESSAGE);
76
        }
77

    
78
        // check that selected object is a taxon node
79
        Object obj = selection.iterator().next();
80
        if(obj instanceof TaxonNodeDto) {
81
             taxonNode = (TaxonNodeDto)obj;
82
        } else if(obj instanceof Classification){
83
            taxonNode = new TaxonNodeDto(((Classification)obj).getRootNode());
84
        }else{
85
            return new Status(IStatus.ERROR,
86
                    "unknown", //$NON-NLS-1$
87
                    TaxonNavigatorLabels.SELECTED_OBJECT_NOT_TREE_NODE_MESSAGE);
88
        }
89
        Collection<MPart> dirtyParts = EditorUtil.checkForChanges(null, partService);
90

    
91
        if (dirtyParts != null && !dirtyParts.isEmpty()){
92
            boolean proceed = MessageDialog.openQuestion(null,
93
                    "There are unsaved changes", "Do you want to save the changes?");
94
            Iterator<MPart> partIterator = dirtyParts.iterator();
95
            while( partIterator.hasNext() ){
96
                MPart part = partIterator.next();
97
                if (proceed) {
98
                    if (part != null){
99
                        if (part.getElementId().equals("eu.etaxonomy.taxeditor.editor.name.e4.TaxonNameEditorE4")){
100
                            TaxonNameEditorE4 targetEditor = (TaxonNameEditorE4) WorkbenchUtility.getE4WrappedPart(part);
101
                            targetEditor.save(new NullProgressMonitor());
102
                        }else if (part.getElementId().equals("bulkeditor.editor")){
103
                            BulkEditorE4 targetEditor = (BulkEditorE4) WorkbenchUtility.getE4WrappedPart(part);
104
                            targetEditor.save(new NullProgressMonitor());
105
                        }
106

    
107
                    }
108

    
109
                }
110
            }
111
        }
112

    
113

    
114
        configurator = new PublishForSubtreeConfigurator(taxonNode.getUuid());
115
        SetPublishForSubtreeWizard wizard = new SetPublishForSubtreeWizard(configurator);
116

    
117
        WizardDialog dialog = new WizardDialog(shell, wizard);
118

    
119
        if (dialog.open() == Window.OK) {
120
            return Status.OK_STATUS;
121
        }else{
122
            return Status.CANCEL_STATUS;
123
        }
124
    }
125

    
126
    @CanExecute
127
    private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
128
        boolean canExecute = false;
129
        canExecute = selection.size()==1
130
                && selection.getFirstElement() instanceof TaxonNodeDto;
131
        menuItem.setVisible(canExecute);
132
        return canExecute;
133
    }
134

    
135

    
136
    @Override
137
    public AbstractOperation prepareOperation(IStructuredSelection selection, Shell shell, MPart activePart,
138
            MHandledMenuItem menuItem) {
139
        SetPublishForSubtreeOperation operation =
140
                new SetPublishForSubtreeOperation(getTrigger(),
141
                        false,
142
                        taxonNode.getUuid(),
143
                        partService,
144
                        activePart,
145
                        application,
146
                        modelService,
147
                        configurator);
148

    
149
        return operation;
150
    }
151

    
152

    
153
    @Override
154
    public void onComplete() {
155

    
156
    }
157

    
158

    
159
    @Override
160
    protected Object getTrigger() {
161
        return this;
162
    }
163

    
164
}
(17-17/20)