editor now updatable via updateSite
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / propertysheet / namerelations / wizard / NameRelationsListWizard.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.propertysheet.namerelations.wizard;
11
12 import java.util.HashSet;
13 import java.util.Set;
14
15 import org.eclipse.core.databinding.observable.list.WritableList;
16 import org.eclipse.jface.wizard.IWizardPage;
17 import org.eclipse.jface.wizard.Wizard;
18
19 import eu.etaxonomy.cdm.model.name.NameRelationship;
20 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
21 import eu.etaxonomy.taxeditor.propertysheet.namerelations.wizard.ChooseRelationWizardPage.TemporaryNameRelationship;
22
23 /**
24 * @author p.ciardelli
25 * @created 06.06.2008
26 * @version 1.0
27 */
28 public class NameRelationsListWizard extends Wizard {
29
30 private TaxonNameBase<?, ?> name;
31
32 private WritableList nameRelationsTemp;
33
34 public NameRelationsListWizard(TaxonNameBase<?, ?> name) {
35 super();
36
37 this.name = name;
38
39 initNameRelationsList();
40 }
41
42 /**
43 *
44 */
45 private void initNameRelationsList() {
46 nameRelationsTemp = new WritableList();
47 nameRelationsTemp.addAll(name.getNameRelations());
48 }
49
50 public void addPages() {
51 IWizardPage chooseNamePage = new ListNameRelationsWizardPage(name, nameRelationsTemp);
52 addPage(chooseNamePage);
53 }
54
55 /* (non-Javadoc)
56 * @see org.eclipse.jface.wizard.Wizard#performFinish()
57 */
58 @Override
59 public boolean performFinish() {
60
61 Set<NameRelationship> nameRelations = name.getNameRelations();
62 Set<NameRelationship> removeRelations = new HashSet<NameRelationship>();
63
64 for (NameRelationship relation : nameRelations) {
65
66 // Remove any relations no longer in temporary list
67 if (!nameRelationsTemp.contains(relation)) {
68 removeRelations.add(relation);
69 } else {
70
71 // Replace temporary relationship w real one
72 if (relation instanceof TemporaryNameRelationship) {
73 removeRelations.add(relation);
74 if (name.equals(relation.getFromName())) {
75 name.addRelationshipToName(relation.getToName(),
76 relation.getType(),
77 relation.getRuleConsidered());
78 } else {
79 name.addRelationshipFromName(relation.getFromName(),
80 relation.getType(),
81 relation.getRuleConsidered());
82 }
83 }
84 }
85 }
86 for (NameRelationship relation : removeRelations) {
87 name.removeNameRelationship(relation);
88 }
89
90 return true;
91 }
92
93 /* (non-Javadoc)
94 * @see org.eclipse.jface.wizard.Wizard#performCancel()
95 */
96 public boolean performCancel() {
97 // Remove all temporary relationships
98 Set<NameRelationship> nameRelations = name.getNameRelations();
99 Set<NameRelationship> removeRelations = new HashSet<NameRelationship>();
100
101 for (NameRelationship relation : nameRelations) {
102 if (relation instanceof TemporaryNameRelationship) {
103 removeRelations.add(relation);
104 }
105 }
106 for (NameRelationship relation : removeRelations) {
107 name.removeNameRelationship(relation);
108 }
109 return true;
110 }
111 }