ref #7439 Propagate row selection of nattable
[taxeditor.git] / eu.etaxonomy.taxeditor.bulkeditor / src / main / java / eu / etaxonomy / taxeditor / bulkeditor / e4 / handler / ConvertPerson2TeamHandlerE4.java
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 import org.eclipse.jface.viewers.StructuredSelection;
21
22 import eu.etaxonomy.cdm.api.service.IAgentService;
23 import eu.etaxonomy.cdm.api.service.UpdateResult;
24 import eu.etaxonomy.cdm.model.agent.Person;
25 import eu.etaxonomy.cdm.model.agent.Team;
26 import eu.etaxonomy.cdm.model.common.CdmBase;
27 import eu.etaxonomy.cdm.strategy.merge.MergeException;
28 import eu.etaxonomy.taxeditor.bulkeditor.e4.BulkEditorE4;
29 import eu.etaxonomy.taxeditor.bulkeditor.internal.TaxeditorBulkeditorPlugin;
30 import eu.etaxonomy.taxeditor.model.MessagingUtils;
31 import eu.etaxonomy.taxeditor.store.CdmStore;
32
33 /**
34 * @author k.luther
35 * @date 12.05.2015
36 *
37 */
38 public class ConvertPerson2TeamHandlerE4 {
39
40
41 @Execute
42 public void execute(@Named(IServiceConstants.ACTIVE_SELECTION)CdmBase selection,
43 @Named(IServiceConstants.ACTIVE_PART)MPart activePart) {
44
45 BulkEditorE4 editor = (BulkEditorE4) activePart.getObject();
46
47 if (editor.isDirty()){
48 boolean proceed = MessageDialog.openQuestion(null,
49 "Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
50 if (proceed) {
51 editor.save(new NullProgressMonitor());
52 } else {
53 return;
54 }
55 }
56
57 Team team =null;
58 UpdateResult result = null;
59 try {
60 result = CdmStore.getService(IAgentService.class).convertPerson2Team(selection.getUuid());
61 } catch (IllegalArgumentException e) {
62 MessagingUtils.errorDialog("Can not convert Person into a Team", null, e.getLocalizedMessage(), TaxeditorBulkeditorPlugin.PLUGIN_ID,e, true);
63 } catch (MergeException e) {
64 MessagingUtils.informationDialog("Convert not possible", "Person can not be transformed into team as it is already part of a team.");
65 }
66 if (result != null &&result.isOk()){
67 team = (Team)result.getCdmEntity();
68 }
69 if (team != null){
70 editor.refresh();
71 }
72 }
73
74 @CanExecute
75 public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
76 MHandledMenuItem menuItem){
77 boolean canExecute = false;
78 StructuredSelection selection = (StructuredSelection)((BulkEditorE4)activePart.getObject()).getSelection();
79 canExecute = ((BulkEditorE4)activePart.getObject()).getEditorInput().isConvertingEnabled() && selection.getFirstElement() instanceof Person && selection.size() == 1;
80 menuItem.setVisible(canExecute);
81 return canExecute;
82 }
83
84 }