3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.taxeditor
.bulkeditor
.handler
;
14 import org
.apache
.log4j
.Logger
;
15 import org
.eclipse
.core
.commands
.AbstractHandler
;
16 import org
.eclipse
.core
.commands
.ExecutionEvent
;
17 import org
.eclipse
.core
.commands
.ExecutionException
;
18 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
19 import org
.eclipse
.jface
.text
.source
.Annotation
;
20 import org
.eclipse
.ui
.IEditorInput
;
21 import org
.eclipse
.ui
.IEditorPart
;
22 import org
.eclipse
.ui
.handlers
.HandlerUtil
;
23 import org
.eclipse
.ui
.texteditor
.IDocumentProvider
;
25 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.IEntityContainer
;
26 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotation
;
27 import eu
.etaxonomy
.taxeditor
.annotatedlineeditor
.LineAnnotationModel
;
28 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditor
;
29 import eu
.etaxonomy
.taxeditor
.bulkeditor
.IBulkEditorConstants
;
30 import eu
.etaxonomy
.taxeditor
.bulkeditor
.input
.AbstractBulkEditorInput
;
33 * <p>MergeGroupHandler class.</p>
39 public class MergeGroupHandler
extends AbstractHandler
{
40 private static final Logger logger
= Logger
41 .getLogger(MergeGroupHandler
.class);
44 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
47 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
49 IEditorPart editor
= HandlerUtil
.getActiveEditor(event
);
50 IEditorInput input
= editor
.getEditorInput();
51 if (editor
instanceof BulkEditor
&& input
instanceof AbstractBulkEditorInput
) {
53 IDocumentProvider provider
= ((BulkEditor
) editor
).getDocumentProvider();
54 LineAnnotationModel model
=
55 (LineAnnotationModel
) provider
.getAnnotationModel(input
);
57 // Check whether there are any group annotations
58 Set
<LineAnnotation
> candidateAnnotations
= model
.getAllAnnotationsOfType(IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
);
59 if (candidateAnnotations
.size() == 0) {
60 MessageDialog
.openWarning(HandlerUtil
.getActiveShell(event
),
61 "No merge candidates", "No objects have been chosen for merging.");
65 // Check whether group merge target has been set
66 Annotation targetAnnotation
= model
.getFirstAnnotationOfType(IBulkEditorConstants
.TYPE_MERGE_TARGET
);
67 if (targetAnnotation
== null) {
68 MessageDialog
.openWarning(HandlerUtil
.getActiveShell(event
),
69 "No group merge target set", "No group merge target has been set.");
72 Object targetEntity
= ((IEntityContainer
<?
>) targetAnnotation
).getEntity();
74 logger
.info("Merging group");
75 // model.printAnnotations();
76 for (LineAnnotation annotation
: candidateAnnotations
) {
78 ((BulkEditor
) editor
).removeAnnotatedLine(annotation
);
80 // Mark entity container for merging with target entity
81 ((IEntityContainer
) annotation
).markAsMerged(targetEntity
);
82 logger
.info("Merging " + annotation
+ " with " + targetAnnotation
);
84 // Remove annotation from model
85 model
.removeAnnotation(annotation
);
87 // model.printAnnotations();
89 model
.removeTypeFromAllAnnotations(IBulkEditorConstants
.TYPE_MERGE_CANDIDATE
);
90 model
.removeTypeFromAllAnnotations(IBulkEditorConstants
.TYPE_MERGE_TARGET
);