refactoring actions in the treeviewer
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / eu / etaxonomy / taxeditor / operations / name / ChangeHomotypicGroupOperation.java
1 /**
2 * Copyright (C) 2007 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
10 package eu.etaxonomy.taxeditor.operations.name;
11
12 import org.apache.log4j.Logger;
13 import org.eclipse.core.commands.ExecutionException;
14 import org.eclipse.core.commands.operations.IUndoContext;
15 import org.eclipse.core.runtime.IAdaptable;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18
19 import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
20 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
21 import eu.etaxonomy.cdm.model.taxon.Synonym;
22 import eu.etaxonomy.cdm.model.taxon.Taxon;
23 import eu.etaxonomy.taxeditor.operations.AbstractEditorOperation;
24
25 /**
26 * Change a the homotypical group for a given synonym.
27 *
28 * @author n.hoffmann
29 * @created 19.01.2009
30 * @version 1.0
31 */
32 public class ChangeHomotypicGroupOperation extends AbstractEditorOperation {
33
34 @SuppressWarnings("unused")
35 private static final Logger logger = Logger
36 .getLogger(ChangeHomotypicGroupOperation.class);
37
38 /**
39 * The synonym to be moved.
40 */
41 private Synonym synonym;
42 /**
43 * The former homotypical group the synonym belonged to
44 */
45 private HomotypicalGroup oldHomotypicalGroup;
46 /**
47 * The homotypical group the synonym is to be moved to
48 */
49 private HomotypicalGroup newHomotypicalGroup;
50
51 public ChangeHomotypicGroupOperation(String label, IUndoContext undoContext,
52 Taxon taxon, Synonym synonym, HomotypicalGroup newHomotypicalGroup) {
53 super(label, undoContext, taxon);
54
55 this.synonym = synonym;
56 if(synonym == null){
57 throw new IllegalArgumentException(
58 "A null synonym was provided.");
59 }
60
61 this.oldHomotypicalGroup = synonym.getHomotypicGroup();
62 this.newHomotypicalGroup = newHomotypicalGroup;
63 }
64
65 /* (non-Javadoc)
66 * @see org.eclipse.core.commands.operations.AbstractOperation#execute(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
67 */
68 @Override
69 public IStatus execute(IProgressMonitor monitor, IAdaptable info)
70 throws ExecutionException {
71
72 // Get synonym name
73 TaxonNameBase synonymName = this.synonym.getName();
74
75 // TODO pass in homotypical group's taxon in case we are dragging from one editor to another
76
77 // Switch groups
78 oldHomotypicalGroup.removeTypifiedName(synonymName);
79 newHomotypicalGroup.addTypifiedName(synonymName);
80
81 // Redraw editor if it exists
82 return redrawOpenEditor();
83 }
84
85 /* (non-Javadoc)
86 * @see org.eclipse.core.commands.operations.AbstractOperation#redo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
87 */
88 @Override
89 public IStatus redo(IProgressMonitor monitor, IAdaptable info)
90 throws ExecutionException {
91 return execute(monitor, info);
92 }
93
94 /* (non-Javadoc)
95 * @see org.eclipse.core.commands.operations.AbstractOperation#undo(org.eclipse.core.runtime.IProgressMonitor, org.eclipse.core.runtime.IAdaptable)
96 */
97 @Override
98 public IStatus undo(IProgressMonitor monitor, IAdaptable info)
99 throws ExecutionException {
100
101 // Get synonym name
102 TaxonNameBase synonymName = this.synonym.getName();
103 if(synonymName == null){
104 // TODO
105 }
106
107 // TODO pass in homotypical group's taxon in case we are dragging from one editor to another
108
109 // Switch groups
110 newHomotypicalGroup.removeTypifiedName(synonymName);
111 oldHomotypicalGroup.addTypifiedName(synonymName);
112
113 // Redraw editor if it exists
114 return redrawOpenEditor();
115 }
116 }