4226d3e40b92c30143c4df1b22da7ce842d60ad8
[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, ConversationHolder conversation){
48 this.conversation = conversation;
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, ConversationHolder conversation){
59 this(vocabularyClass, conversation);
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(true);
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 fields <code>term</code>.</p>
105 *
106 * @param text
107 * @param label a {@link java.lang.String} object.
108 * @param abbreviatedLabel a {@link java.lang.String} object.
109 */
110 public void setTerm(String text, String label, String abbreviatedLabel) {
111 Representation representation = getTerm().getRepresentation(CdmStore.getDefaultLanguage());
112 representation.setText(text);
113 representation.setLabel(label);
114 representation.setAbbreviatedLabel(abbreviatedLabel);
115 }
116
117 /**
118 * <p>Getter for the field <code>term</code>.</p>
119 *
120 * @return the term
121 */
122 public T getTerm() {
123 if(term == null){
124 try {
125 term = (T) termClass.newInstance();
126
127 term.addRepresentation(Representation.NewInstance("", "", "", CdmStore.getDefaultLanguage()));
128 } catch (InstantiationException e) {
129 StoreUtil.error(this.getClass(), "InstantiationException when setting term", e);
130 } catch (IllegalAccessException e) {
131 StoreUtil.error(this.getClass(), "IllegalAccessException when setting term", e);
132 }
133 }
134 return term;
135 }
136
137 public Representation getRepresentation(){
138 return getTerm().getRepresentation(CdmStore.getDefaultLanguage());
139 }
140
141 /**
142 * <p>isEditMode</p>
143 *
144 * @return the editMode
145 */
146 public boolean isEditMode() {
147 return editMode;
148 }
149
150 /**
151 * <p>Getter for the field <code>termClass</code>.</p>
152 *
153 * @return a {@link java.lang.Class} object.
154 */
155 public Class<T> getTermClass() {
156 return termClass;
157 }
158
159 /* (non-Javadoc)
160 * @see eu.etaxonomy.cdm.persistence.hibernate.ICdmPostDataChangeObserver#update(eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap)
161 */
162 @Override
163 public void update(CdmDataChangeMap changeEvents) {
164 // not needed
165 }
166
167 /* (non-Javadoc)
168 * @see eu.etaxonomy.cdm.api.conversation.IConversationEnabled#getConversationHolder()
169 */
170 @Override
171 public ConversationHolder getConversationHolder() {
172 return conversation;
173 }
174
175 }