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