Project

General

Profile

« Previous | Next » 

Revision cad985fd

Added by Patrick Plitzner almost 6 years ago

ref #7010 Adapt bulk editor handlers for multiple selection

View differences:

eu.etaxonomy.taxeditor.bulkeditor/src/main/java/eu/etaxonomy/taxeditor/bulkeditor/e4/handler/ConvertTeam2PersonHandlerE4.java
8 8
*/
9 9
package eu.etaxonomy.taxeditor.bulkeditor.e4.handler;
10 10

  
11
import java.util.Iterator;
12

  
11 13
import javax.inject.Named;
12 14

  
13 15
import org.eclipse.core.runtime.NullProgressMonitor;
......
17 19
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
18 20
import org.eclipse.e4.ui.services.IServiceConstants;
19 21
import org.eclipse.jface.dialogs.MessageDialog;
22
import org.eclipse.jface.viewers.IStructuredSelection;
20 23
import org.eclipse.jface.viewers.StructuredSelection;
21 24

  
22 25
import eu.etaxonomy.cdm.api.service.IAgentService;
23 26
import eu.etaxonomy.cdm.api.service.UpdateResult;
24 27
import eu.etaxonomy.cdm.model.agent.Person;
25 28
import eu.etaxonomy.cdm.model.agent.Team;
26
import eu.etaxonomy.cdm.model.common.CdmBase;
27 29
import eu.etaxonomy.cdm.strategy.merge.MergeException;
28 30
import eu.etaxonomy.taxeditor.bulkeditor.e4.BulkEditorE4;
29 31
import eu.etaxonomy.taxeditor.model.MessagingUtils;
......
37 39
public class ConvertTeam2PersonHandlerE4 {
38 40

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

  
43 44
        BulkEditorE4 editor = (BulkEditorE4) activePart.getObject();
45
        if (editor.isDirty()){
46
            boolean proceed = MessageDialog.openQuestion(null,
47
                    "Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
48
            if (proceed) {
49
                editor.save(new NullProgressMonitor());
50
            } else {
51
                return;
52
            }
53
        }
54
        IStructuredSelection selection = editor.getSelection();
55
        Iterator iterator = selection.iterator();
56
        for(selection.iterator();iterator.hasNext();){
57
            Team team = (Team) iterator.next();
58
            Person person = null;
59
            UpdateResult result = null;
44 60

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

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

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

  
73 76
    @CanExecute
74 77
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
75 78
            MHandledMenuItem menuItem){
76 79
        boolean canExecute = false;
77
        StructuredSelection selection = (StructuredSelection)((BulkEditorE4)activePart.getObject()).getSelection();
78
        canExecute = ((BulkEditorE4)activePart.getObject()).getEditorInput().isConvertingEnabled() && selection.getFirstElement() instanceof Team && selection.size() == 1;
80
        BulkEditorE4 bulkEditorE4 = (BulkEditorE4)activePart.getObject();
81
        StructuredSelection selection = (StructuredSelection)bulkEditorE4.getSelection();
82
        canExecute = bulkEditorE4.getEditorInput().isConvertingEnabled();
83
        Iterator iterator = selection.iterator();
84
        for(selection.iterator();iterator.hasNext();){
85
            if(!(iterator.next() instanceof Team)){
86
                canExecute = false;
87
                break;
88
            }
89
        }
79 90
        menuItem.setVisible(canExecute);
80 91
        return canExecute;
81 92
    }

Also available in: Unified diff