Project

General

Profile

Download (6.01 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.cdm.persistence.permission.Role;
36
import eu.etaxonomy.taxeditor.editor.EditorUtil;
37
import eu.etaxonomy.taxeditor.navigation.l10n.Messages;
38
import eu.etaxonomy.taxeditor.navigation.navigator.TaxonNavigatorLabels;
39
import eu.etaxonomy.taxeditor.navigation.navigator.operation.SetPublishForSubtreeOperation;
40
import eu.etaxonomy.taxeditor.operation.e4.CdmHandlerE4;
41
import eu.etaxonomy.taxeditor.store.CdmStore;
42
import eu.etaxonomy.taxeditor.ui.dialog.configurator.SetPublishForSubtreeWizard;
43
import eu.etaxonomy.taxeditor.workbench.part.IE4SavablePart;
44

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

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

    
59

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

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

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

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

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

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

    
115

    
116
        configurator = PublishForSubtreeConfigurator.NewInstance(taxonNode.getUuid(), true, true, true, true, null);
117
        SetPublishForSubtreeWizard wizard = new SetPublishForSubtreeWizard(configurator);
118

    
119
        WizardDialog dialog = new WizardDialog(shell, wizard);
120

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

    
128
    @CanExecute
129
    private boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)TreeSelection selection, MHandledMenuItem menuItem){
130
        boolean canExecute = false;
131
        canExecute = selection.size()==1
132
                && selection.getFirstElement() instanceof TaxonNodeDto;
133
        canExecute &= CdmStore.currentAuthentiationHasOneOfRoles(Role.ROLE_PUBLISH, Role.ROLE_ADMIN);
134
        menuItem.setEnabled(canExecute);
135
        return canExecute;
136
    }
137

    
138

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

    
152
        return operation;
153
    }
154

    
155

    
156
    @Override
157
    public void onComplete() {
158

    
159
    }
160

    
161

    
162
    @Override
163
    protected Object getTrigger() {
164
        return this;
165
    }
166

    
167
}
(22-22/25)