Revision a380926d
Added by Patrick Plitzner over 6 years ago
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 org.eclipse.core.commands.AbstractHandler; |
|
12 |
import org.eclipse.core.commands.ExecutionEvent; |
|
13 |
import org.eclipse.core.commands.ExecutionException; |
|
11 |
import javax.inject.Named; |
|
12 |
|
|
13 |
import org.eclipse.e4.core.di.annotations.CanExecute; |
|
14 |
import org.eclipse.e4.core.di.annotations.Execute; |
|
15 |
import org.eclipse.e4.ui.model.application.ui.basic.MPart; |
|
16 |
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem; |
|
17 |
import org.eclipse.e4.ui.services.IServiceConstants; |
|
14 | 18 |
import org.eclipse.jface.dialogs.MessageDialog; |
15 |
import org.eclipse.jface.viewers.ISelection; |
|
16 |
import org.eclipse.jface.viewers.IStructuredSelection; |
|
17 |
import org.eclipse.ui.IEditorInput; |
|
18 |
import org.eclipse.ui.IEditorPart; |
|
19 |
import org.eclipse.ui.handlers.HandlerUtil; |
|
20 |
import org.eclipse.ui.texteditor.IDocumentProvider; |
|
21 | 19 |
|
22 | 20 |
import eu.etaxonomy.cdm.api.service.IAgentService; |
23 | 21 |
import eu.etaxonomy.cdm.api.service.UpdateResult; |
24 |
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper; |
|
25 | 22 |
import eu.etaxonomy.cdm.model.agent.Person; |
26 | 23 |
import eu.etaxonomy.cdm.model.agent.Team; |
27 | 24 |
import eu.etaxonomy.cdm.strategy.merge.MergeException; |
28 |
import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityPersistenceService; |
|
29 |
import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotation; |
|
30 |
import eu.etaxonomy.taxeditor.annotatedlineeditor.LineAnnotationModel; |
|
31 |
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor; |
|
25 |
import eu.etaxonomy.taxeditor.bulkeditor.e4.BulkEditorE4; |
|
32 | 26 |
import eu.etaxonomy.taxeditor.model.MessagingUtils; |
33 | 27 |
import eu.etaxonomy.taxeditor.store.CdmStore; |
34 | 28 |
|
... | ... | |
37 | 31 |
* @date 12.05.2015 |
38 | 32 |
* |
39 | 33 |
*/ |
40 |
public class ConvertTeam2PersonHandlerE4 extends AbstractHandler {
|
|
34 |
public class ConvertTeam2PersonHandlerE4 { |
|
41 | 35 |
|
42 |
/** |
|
43 |
* {@inheritDoc} |
|
44 |
*/ |
|
45 |
@Override |
|
46 |
public Person execute(ExecutionEvent event) throws ExecutionException { |
|
47 |
ISelection selection = HandlerUtil.getCurrentSelection(event); |
|
36 |
@Execute |
|
37 |
public void execute(@Named(IServiceConstants.ACTIVE_SELECTION)Team team, |
|
38 |
@Named(IServiceConstants.ACTIVE_PART)MPart activePart) { |
|
48 | 39 |
|
49 |
IEditorPart editor = HandlerUtil.getActiveEditor(event);
|
|
40 |
BulkEditorE4 editor = (BulkEditorE4) activePart.getObject();
|
|
50 | 41 |
|
51 |
IEditorInput input = editor.getEditorInput(); |
|
52 | 42 |
if (editor.isDirty()){ |
53 | 43 |
boolean proceed = MessageDialog.openQuestion(null, |
54 | 44 |
"Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?"); |
55 | 45 |
if (proceed) { |
56 |
editor.doSave(null);
|
|
46 |
editor.save();
|
|
57 | 47 |
} else { |
58 |
return null;
|
|
48 |
return; |
|
59 | 49 |
} |
60 | 50 |
} |
61 |
if((input instanceof IEntityPersistenceService) && (selection instanceof IStructuredSelection)){ |
|
62 |
|
|
63 |
|
|
64 |
IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider(); |
|
65 |
LineAnnotationModel model = |
|
66 |
(LineAnnotationModel) provider.getAnnotationModel(input); |
|
67 |
|
|
68 |
|
|
69 |
IStructuredSelection structuredSelection = (IStructuredSelection) selection; |
|
70 | 51 |
|
71 |
IEntityPersistenceService persistenceService = (IEntityPersistenceService) input; |
|
52 |
Person person = null; |
|
53 |
UpdateResult result = null; |
|
72 | 54 |
|
73 |
Person person ; |
|
74 |
for(Object object : structuredSelection.toList()){ |
|
75 |
LineAnnotation annotation = (LineAnnotation) model.getAnnotation(object); |
|
76 |
person = null; |
|
77 |
UpdateResult result = null; |
|
78 |
if (object instanceof Team){ |
|
79 |
Team team = HibernateProxyHelper.deproxy(object, Team.class); |
|
80 |
try { |
|
81 |
result = CdmStore.getService(IAgentService.class).convertTeam2Person(team.getUuid()); |
|
82 |
} catch (IllegalArgumentException e) { |
|
83 |
MessagingUtils.informationDialog("Can not convert Team to Person", e.getMessage()); |
|
84 |
} catch (MergeException e) { |
|
85 |
MessagingUtils.informationDialog("Can not convert Team to Person", e.getMessage()); |
|
86 |
} |
|
87 |
}else{ |
|
88 |
MessagingUtils.informationDialog("Can not convert Team to Person", "convertTeam2Person can only be called on a team."); |
|
89 |
} |
|
90 |
if (result != null && result.isOk()){ |
|
91 |
person = (Person)result.getCdmEntity(); |
|
92 |
} |
|
93 |
if (person != null){ |
|
94 |
((BulkEditor) editor).removeAnnotatedLine(annotation); |
|
95 |
((BulkEditor) editor).createAnnotatedLine(person); |
|
96 |
} |
|
97 |
} |
|
55 |
try { |
|
56 |
result = CdmStore.getService(IAgentService.class).convertTeam2Person(team.getUuid()); |
|
57 |
} catch (IllegalArgumentException e) { |
|
58 |
MessagingUtils.informationDialog("Can not convert Team to Person", e.getMessage()); |
|
59 |
} catch (MergeException e) { |
|
60 |
MessagingUtils.informationDialog("Can not convert Team to Person", e.getMessage()); |
|
61 |
} |
|
62 |
if (result != null && result.isOk()){ |
|
63 |
person = (Person)result.getCdmEntity(); |
|
98 | 64 |
} |
99 |
return null; |
|
100 |
} |
|
65 |
if (person != null){ |
|
66 |
editor.refresh(); |
|
67 |
} |
|
68 |
} |
|
69 |
|
|
70 |
@CanExecute |
|
71 |
public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION)Object selection, |
|
72 |
@Named(IServiceConstants.ACTIVE_PART)MPart activePart, |
|
73 |
MHandledMenuItem menuItem){ |
|
74 |
boolean canExecute = false; |
|
75 |
canExecute = ((BulkEditorE4)activePart.getObject()).getEditorInput().isConvertingEnabled() && selection instanceof Team; |
|
76 |
menuItem.setVisible(canExecute); |
|
77 |
return canExecute; |
|
78 |
} |
|
101 | 79 |
|
102 | 80 |
} |
Also available in: Unified diff
ref #6932 Migrate bulk editor handlers (part I)