Merge branch 'develop' into LibrAlign
[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
10 package eu.etaxonomy.taxeditor.newWizard;
11
12 import org.eclipse.jface.viewers.IStructuredSelection;
13 import org.eclipse.jface.wizard.Wizard;
14 import org.eclipse.swt.widgets.Display;
15 import org.eclipse.ui.INewWizard;
16 import org.eclipse.ui.ISelectionService;
17 import org.eclipse.ui.IWorkbench;
18
19 import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
20 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
21 import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
22 import eu.etaxonomy.taxeditor.model.AbstractUtility;
23 import eu.etaxonomy.taxeditor.store.CdmStore;
24 import eu.etaxonomy.taxeditor.store.StoreUtil;
25 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
26
27 /**
28 * <p>Abstract AbstractNewEntityWizard class.</p>
29 *
30 * @author n.hoffmann
31 * @created Jun 1, 2010
32 * @version 1.0
33 */
34 public abstract class AbstractNewEntityWizard<T> extends Wizard implements
35 INewWizard, IConversationEnabled {
36
37 private ConversationHolder conversation;
38
39 protected CdmFormFactory formFactory;
40
41 private T entity;
42
43 private IWorkbench workbench;
44
45 private IStructuredSelection selection;
46
47 /**
48 * <p>Constructor for AbstractNewEntityWizard.</p>
49 *
50 * @param <T> a T object.
51 */
52 public AbstractNewEntityWizard(){
53 setWindowTitle(String.format("New %s", getEntityName()));
54 }
55
56 /**
57 * FIXME there might be a smarter way to do this,
58 *
59 * @return
60 */
61 protected abstract String getEntityName();
62
63 /* (non-Javadoc)
64 * @see org.eclipse.jface.wizard.Wizard#performFinish()
65 */
66 /** {@inheritDoc} */
67 @Override
68 public boolean performFinish() {
69 saveEntity();
70
71 conversation.commit();
72 conversation.close();
73 return true;
74 }
75
76 /**
77 * <p>Getter for the field <code>entity</code>.</p>
78 *
79 * @return a T object.
80 */
81 public T getEntity() {
82 return entity;
83 }
84
85 /**
86 * <p>Setter for the field <code>entity</code>.</p>
87 *
88 * @param entity a T object.
89 */
90 public void setEntity(T entity){
91 this.entity = entity;
92 }
93
94 /**
95 * Adds the entity to the current persistence context
96 */
97 protected abstract void saveEntity();
98
99 /* (non-Javadoc)
100 * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
101 */
102 /** {@inheritDoc} */
103 @Override
104 public void init(IWorkbench workbench, IStructuredSelection selection) {
105 this.workbench = workbench != null ? workbench : AbstractUtility.getWorkbench();
106
107 if(selection == null){
108 ISelectionService service = this.workbench.getActiveWorkbenchWindow().getSelectionService();
109 if(service.getSelection() instanceof IStructuredSelection){
110 selection = (IStructuredSelection) service.getSelection();
111 }
112 }
113 this.selection = selection;
114
115 formFactory = new CdmFormFactory(Display.getCurrent(), null);
116 conversation = CdmStore.createConversation();
117 entity = createNewEntity();
118
119 }
120
121 /**
122 * <p>createNewEntity</p>
123 *
124 * @return a T object.
125 */
126 protected abstract T createNewEntity();
127
128 /**
129 * <p>getConversationHolder</p>
130 *
131 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
132 */
133 @Override
134 public ConversationHolder getConversationHolder() {
135 return conversation;
136 }
137
138 /** {@inheritDoc} */
139 @Override
140 public void update(CdmDataChangeMap changeEvents) {}
141
142 /**
143 * @return the workbench
144 */
145 public IWorkbench getWorkbench() {
146 return workbench;
147 }
148
149 /**
150 * @return the selection
151 */
152 public IStructuredSelection getSelection() {
153 return selection;
154 }
155 }