da7711b6a19a78a24cb13b9dbf3e6cb9fbfd93cb
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / preference / wizard / VocabularyTermWizard.java
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
11 package eu.etaxonomy.taxeditor.preference.wizard;
12
13 import org.eclipse.jface.wizard.Wizard;
14
15 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
16 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
17 import eu.etaxonomy.cdm.api.service.ITermService;
18 import eu.etaxonomy.cdm.model.common.DefinedTermBase;
19 import eu.etaxonomy.cdm.model.common.Representation;
20 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
21 import eu.etaxonomy.taxeditor.store.CdmStore;
22 import eu.etaxonomy.taxeditor.store.StoreUtil;
23 import eu.etaxonomy.taxeditor.store.TermStore;
24
25 /**
26 * <p>VocabularyTermWizard class.</p>
27 *
28 * @author n.hoffmann
29 * @created 12.06.2009
30 * @version 1.0
31 */
32 public class VocabularyTermWizard<T extends DefinedTermBase> extends Wizard implements IConversationEnabled {
33
34 public T term = null;
35
36 private boolean editMode = false;
37
38 private Class<T> termClass;
39
40 private ConversationHolder conversation;
41
42 /**
43 * <p>Constructor for VocabularyTermWizard.</p>
44 *
45 * @param vocabularyClass a {@link java.lang.Class} object.
46 */
47 public VocabularyTermWizard(Class<T> vocabularyClass){
48 conversation = CdmStore.createConversation();
49 termClass = vocabularyClass;
50 }
51
52 /**
53 * <p>Constructor for VocabularyTermWizard.</p>
54 *
55 * @param vocabularyClass a {@link java.lang.Class} object.
56 * @param selectedTerm a T object.
57 */
58 public VocabularyTermWizard(Class<T> vocabularyClass, T selectedTerm){
59 this(vocabularyClass);
60 term = (T) CdmStore.getService(ITermService.class).load(selectedTerm.getUuid());
61 editMode = true;
62 }
63
64 /* (non-Javadoc)
65 * @see org.eclipse.jface.wizard.Wizard#addPages()
66 */
67 /** {@inheritDoc} */
68 @Override
69 public void addPages() {
70 super.addPages();
71
72 addPage(new VocabularyTermWizardPage());
73 }
74
75 /* (non-Javadoc)
76 * @see org.eclipse.jface.wizard.Wizard#performFinish()
77 */
78 /** {@inheritDoc} */
79 @Override
80 public boolean performFinish() {
81
82 if(editMode){
83 TermStore.updateVocabulary(getTerm());
84 }else{
85 TermStore.saveTerm(getTerm());
86 }
87
88 getConversationHolder().commit(false);
89 getConversationHolder().close();
90
91 return true;
92 }
93
94 /**
95 * <p>Setter for the field <code>term</code>.</p>
96 *
97 * @param term a T object.
98 */
99 public void setTerm(T term) {
100 this.term = term;
101 }
102
103 /**
104 * <p>Setter for the field <code>term</code>.</p>
105 *
106 * @param text
107 * @param text2
108 * @param text3
109 * @param label a {@link java.lang.String} object.
110 * @param abbreviatedLabel a {@link java.lang.String} object.
111 */
112 public void setTerm(String text, String label, String abbreviatedLabel) {
113 Representation representation = getTerm().getRepresentation(CdmStore.getDefaultLanguage());
114 representation.setText(text);
115 representation.setLabel(label);
116 representation.setAbbreviatedLabel(abbreviatedLabel);
117 }
118
119 /**
120 * <p>Getter for the field <code>term</code>.</p>
121 *
122 * @return the term
123 */
124 public T getTerm() {
125 if(term == null){
126 try {
127 term = (T) termClass.newInstance();
128
129 term.addRepresentation(Representation.NewInstance("", "", "", CdmStore.getDefaultLanguage()));
130 } catch (InstantiationException e) {
131 StoreUtil.error(this.getClass(), "InstantiationException when setting term", e);
132 } catch (IllegalAccessException e) {
133 StoreUtil.error(this.getClass(), "IllegalAccessException when setting term", e);
134 }
135 }
136 return term;
137 }
138
139 public Representation getRepresentation(){
140 return getTerm().getRepresentation(CdmStore.getDefaultLanguage());
141 }
142
143 /**
144 * <p>isEditMode</p>
145 *
146 * @return the editMode
147 */
148 public boolean isEditMode() {
149 return editMode;
150 }
151
152 /**
153 * <p>Getter for the field <code>termClass</code>.</p>
154 *
155 * @return a {@link java.lang.Class} object.
156 */
157 public Class<T> getTermClass() {
158 return termClass;
159 }
160
161 /* (non-Javadoc)
162 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
163 */
164 @Override
165 public void update(CdmDataChangeMap changeEvents) {
166 // TODO Auto-generated method stub
167
168 }
169
170 /* (non-Javadoc)
171 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
172 */
173 @Override
174 public ConversationHolder getConversationHolder() {
175 return conversation;
176 }
177
178 }