Project

General

Profile

Download (5.83 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.editor.EditorUtil;
36
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
37
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
38
import eu.etaxonomy.taxeditor.navigation.navigator.operation.SetPublishForSubtreeOperation;
39
import eu.etaxonomy.taxeditor.operation.e4.RemotingCdmHandlerE4;
40
import eu.etaxonomy.taxeditor.ui.dialog.configurator.SetPublishForSubtreeWizard;
41
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
42

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

    
50
    /**
51
     * @param label
52
     */
53
    public SetPublishFlagForSubtreeHandlerE4() {
54
       super(""); //$NON-NLS-1$
55
    }
56

    
57

    
58
    private TaxonNodeDto taxonNode;
59
    private PublishForSubtreeConfigurator configurator;
60
    private static final String NO = Messages.NO;
61
    private static final String CANCEL = Messages.RemotingMoveTaxonNodeHandler_CANCEL;
62
    private static final String YES = Messages.YES;
63

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

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

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

    
90
        if (dirtyParts != null && !dirtyParts.isEmpty()){
91
            String[] buttonLables = {YES, NO,CANCEL};
92
            MessageDialog dialog = new MessageDialog(null,  Messages.SetPublishFlagForSubtreeHandlerE4_UnsavedChanges, null, Messages.SetPublishFlagForSubtreeHandlerE4_UnsavedChangesQuestion, MessageDialog.QUESTION_WITH_CANCEL, buttonLables, 0);
93
            dialog.open();
94
            int returnCode = dialog.getReturnCode();
95
            boolean proceed = false;
96
            if (returnCode == 0){
97
                proceed = true;
98
            }else if (returnCode == 2){
99
                return Status.CANCEL_STATUS;
100
            }
101

    
102
            Iterator<IE4SavablePart> partIterator = dirtyParts.iterator();
103
            while( partIterator.hasNext() ){
104
                IE4SavablePart part = partIterator.next();
105
                if (proceed) {
106
                    if (part != null){
107
                        part.save(new NullProgressMonitor());
108
                    }
109
                }
110
            }
111
        }
112

    
113

    
114
        configurator = PublishForSubtreeConfigurator.NewInstance(taxonNode.getUuid(), true, true, true, true, null);
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.setEnabled(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
}
(21-21/24)