2 * Copyright (C) 2015 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.taxeditor
.bulkeditor
.e4
.handler
;
11 import java
.util
.Iterator
;
13 import javax
.inject
.Named
;
15 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
16 import org
.eclipse
.e4
.core
.di
.annotations
.CanExecute
;
17 import org
.eclipse
.e4
.core
.di
.annotations
.Execute
;
18 import org
.eclipse
.e4
.ui
.model
.application
.ui
.basic
.MPart
;
19 import org
.eclipse
.e4
.ui
.model
.application
.ui
.menu
.MHandledMenuItem
;
20 import org
.eclipse
.e4
.ui
.services
.IServiceConstants
;
21 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
22 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
23 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
25 import eu
.etaxonomy
.cdm
.api
.service
.IAgentService
;
26 import eu
.etaxonomy
.cdm
.api
.service
.UpdateResult
;
27 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
28 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
29 import eu
.etaxonomy
.cdm
.strategy
.merge
.MergeException
;
30 import eu
.etaxonomy
.taxeditor
.bulkeditor
.e4
.BulkEditorE4
;
31 import eu
.etaxonomy
.taxeditor
.bulkeditor
.internal
.TaxeditorBulkeditorPlugin
;
32 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
33 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
39 public class ConvertPerson2TeamHandlerE4
{
43 public void execute(@Named(IServiceConstants
.ACTIVE_PART
)MPart activePart
) {
45 BulkEditorE4 editor
= (BulkEditorE4
) activePart
.getObject();
46 if (editor
.isDirty()){
47 boolean proceed
= MessageDialog
.openQuestion(null,
48 "Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
50 editor
.save(new NullProgressMonitor());
55 IStructuredSelection selection
= editor
.getSelection();
56 Iterator
<?
> iterator
= selection
.iterator();
57 for(selection
.iterator();iterator
.hasNext();){
59 Person person
= (Person
) iterator
.next();
61 UpdateResult result
= null;
63 result
= CdmStore
.getService(IAgentService
.class).convertPerson2Team(person
.getUuid());
64 } catch (IllegalArgumentException e
) {
65 MessagingUtils
.errorDialog("Can not convert Person into a Team", null, e
.getLocalizedMessage(), TaxeditorBulkeditorPlugin
.PLUGIN_ID
,e
, true);
66 } catch (MergeException e
) {
67 MessagingUtils
.informationDialog("Convert not possible", "Person can not be transformed into team as it is already part of a team.");
69 if (result
!= null &&result
.isOk()){
70 team
= (Team
)result
.getCdmEntity();
71 editor
.getEditorInput().getModel().remove(person
);
72 editor
.getEditorInput().getModel().add(team
);
78 public boolean canExecute(@Named(IServiceConstants
.ACTIVE_PART
)MPart activePart
,
79 MHandledMenuItem menuItem
){
80 boolean canExecute
= false;
81 BulkEditorE4 bulkEditorE4
= (BulkEditorE4
)activePart
.getObject();
82 StructuredSelection selection
= (StructuredSelection
)bulkEditorE4
.getSelection();
83 canExecute
= !selection
.isEmpty() && bulkEditorE4
.getEditorInput().isConvertingEnabled();
84 Iterator
<?
> iterator
= selection
.iterator();
85 for(selection
.iterator();iterator
.hasNext();){
86 if(!(iterator
.next() instanceof Person
)){
91 menuItem
.setVisible(canExecute
);