Project

General

Profile

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

    
11
import javax.inject.Named;
12

    
13
import org.eclipse.core.runtime.NullProgressMonitor;
14
import org.eclipse.e4.core.di.annotations.CanExecute;
15
import org.eclipse.e4.core.di.annotations.Execute;
16
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
17
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
18
import org.eclipse.e4.ui.services.IServiceConstants;
19
import org.eclipse.jface.dialogs.MessageDialog;
20

    
21
import eu.etaxonomy.cdm.api.service.IAgentService;
22
import eu.etaxonomy.cdm.api.service.UpdateResult;
23
import eu.etaxonomy.cdm.model.agent.Person;
24
import eu.etaxonomy.cdm.model.agent.Team;
25
import eu.etaxonomy.cdm.model.common.CdmBase;
26
import eu.etaxonomy.cdm.strategy.merge.MergeException;
27
import eu.etaxonomy.taxeditor.bulkeditor.e4.BulkEditorE4;
28
import eu.etaxonomy.taxeditor.model.MessagingUtils;
29
import eu.etaxonomy.taxeditor.store.CdmStore;
30

    
31
/**
32
 * @author k.luther
33
 * @date 12.05.2015
34
 *
35
 */
36
public class ConvertTeam2PersonHandlerE4 {
37

    
38
    @Execute
39
	public void execute(@Named(IServiceConstants.ACTIVE_SELECTION)CdmBase selection,
40
	        @Named(IServiceConstants.ACTIVE_PART)MPart activePart) {
41

    
42
        BulkEditorE4 editor = (BulkEditorE4) activePart.getObject();
43

    
44
		if (editor.isDirty()){
45
			boolean proceed = MessageDialog.openQuestion(null,
46
					"Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
47
			if (proceed) {
48
				editor.save(new NullProgressMonitor());
49
			} else {
50
				return;
51
			}
52
		}
53

    
54
		Person person = null;
55
		UpdateResult result = null;
56
		Team team = (Team) selection;
57

    
58
		try {
59
		    result = CdmStore.getService(IAgentService.class).convertTeam2Person(selection.getUuid());
60
		} catch (IllegalArgumentException e) {
61
		    MessagingUtils.informationDialog("Can not convert Team to Person", e.getMessage());
62
		} catch (MergeException e) {
63
		    MessagingUtils.informationDialog("Can not convert Team to Person", e.getMessage());
64
		}
65
		if (result != null && result.isOk()){
66
		    person = (Person)result.getCdmEntity();
67
		    boolean test = editor.getEditorInput().getModel().remove(team);
68
		    editor.getEditorInput().getModel().add(person);
69
		}
70
		if (person != null){
71
		    editor.refresh();
72
		}
73
    }
74

    
75
    @CanExecute
76
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)CdmBase selection,
77
            @Named(IServiceConstants.ACTIVE_PART)MPart activePart,
78
            MHandledMenuItem menuItem){
79
        boolean canExecute = false;
80
        canExecute = ((BulkEditorE4)activePart.getObject()).getEditorInput().isConvertingEnabled() && selection instanceof Team;
81
        menuItem.setVisible(canExecute);
82
        return canExecute;
83
    }
84

    
85
}
(2-2/9)