Project

General

Profile

Download (3.47 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2007 EDIT
4
* European Distributed Institute of Taxonomy 
5
* http://www.e-taxonomy.eu
6
* 
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.
9
*/
10
package eu.etaxonomy.taxeditor.bulkeditor.handler;
11

    
12
import java.util.Set;
13

    
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;
24

    
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;
31

    
32
/**
33
 * <p>MergeGroupHandler class.</p>
34
 *
35
 * @author p.ciardelli
36
 * @created 25.06.2009
37
 * @version 1.0
38
 */
39
public class MergeGroupHandler extends AbstractHandler {
40
	private static final Logger logger = Logger
41
			.getLogger(MergeGroupHandler.class);
42

    
43
	/* (non-Javadoc)
44
	 * @see org.eclipse.core.commands.AbstractHandler#execute(org.eclipse.core.commands.ExecutionEvent)
45
	 */
46
	/** {@inheritDoc} */
47
	public Object execute(ExecutionEvent event) throws ExecutionException {
48
				
49
		IEditorPart editor = HandlerUtil.getActiveEditor(event);
50
		IEditorInput input = editor.getEditorInput();
51
		if (editor instanceof BulkEditor && input instanceof AbstractBulkEditorInput) {
52

    
53
			IDocumentProvider provider = ((BulkEditor) editor).getDocumentProvider();
54
			LineAnnotationModel model = 
55
					(LineAnnotationModel) provider.getAnnotationModel(input);
56
			
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.");
62
				return null;				
63
			}
64
			
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.");
70
				return null;
71
			}			
72
			Object targetEntity = ((IEntityContainer<?>) targetAnnotation).getEntity();
73
			
74
			logger.info("Merging group");
75
//			model.printAnnotations();
76
			for (LineAnnotation annotation : candidateAnnotations) {
77
				
78
				((BulkEditor) editor).removeAnnotatedLine(annotation);
79
				
80
				// Mark entity container for merging with target entity
81
				((IEntityContainer) annotation).markAsMerged(targetEntity);
82
				logger.info("Merging " + annotation + " with " + targetAnnotation);
83
				
84
				// Remove annotation from model
85
				model.removeAnnotation(annotation);
86
			}
87
//			model.printAnnotations();	
88
			
89
			model.removeTypeFromAllAnnotations(IBulkEditorConstants.TYPE_MERGE_CANDIDATE);
90
			model.removeTypeFromAllAnnotations(IBulkEditorConstants.TYPE_MERGE_TARGET);
91
		}
92
		return null;
93
	}
94
}
(2-2/7)