Merge branch 'release/5.19.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / newWizard / AbstractNewEntityWizard.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 package eu.etaxonomy.taxeditor.newWizard;
10
11 import org.eclipse.e4.ui.workbench.IWorkbench;
12 import org.eclipse.jface.viewers.IStructuredSelection;
13 import org.eclipse.jface.wizard.Wizard;
14 import org.eclipse.swt.widgets.Display;
15
16 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
17 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
18 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
19 import eu.etaxonomy.taxeditor.store.CdmStore;
20 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
21
22 /**
23 * @author n.hoffmann
24 * @created Jun 1, 2010
25 */
26 public abstract class AbstractNewEntityWizard<T>
27 extends Wizard
28 implements IConversationEnabled {
29
30 protected ConversationHolder conversation;
31
32 protected CdmFormFactory formFactory;
33
34 private T entity;
35
36 protected IStructuredSelection selection;
37
38 public AbstractNewEntityWizard(){
39 setWindowTitle(String.format("New %s", getEntityName()));
40 }
41
42 /**
43 * FIXME there might be a smarter way to do this,
44 */
45 protected abstract String getEntityName();
46
47 @Override
48 public boolean performFinish() {
49 saveEntity();
50
51 conversation.commit();
52 conversation.close();
53 conversation = null;
54 return true;
55 }
56
57 public T getEntity() {
58 return entity;
59 }
60
61 public void setEntity(T entity){
62 this.entity = entity;
63 }
64
65 /**
66 * Adds the entity to the current persistence context
67 */
68 protected abstract void saveEntity();
69
70 public void init(IWorkbench workbench, IStructuredSelection selection) {
71 formFactory = new CdmFormFactory(Display.getCurrent(), null);
72 conversation = CdmStore.createConversation();
73 this.selection = selection;
74 entity = createNewEntity();
75
76 }
77
78 protected abstract T createNewEntity();
79
80 @Override
81 public ConversationHolder getConversationHolder() {
82 return conversation;
83 }
84
85 @Override
86 public void update(CdmDataChangeMap changeEvents) {}
87
88 public IStructuredSelection getSelection() {
89 return selection;
90 }
91 }