fee26b5c69658e984556ce720920011128d52691
[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.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("New Entity");
54 }
55
56 /* (non-Javadoc)
57 * @see org.eclipse.jface.wizard.Wizard#performFinish()
58 */
59 /** {@inheritDoc} */
60 @Override
61 public boolean performFinish() {
62 saveEntity();
63
64 conversation.commit();
65 conversation.close();
66 return true;
67 }
68
69 /**
70 * <p>Getter for the field <code>entity</code>.</p>
71 *
72 * @return a T object.
73 */
74 public T getEntity() {
75 return entity;
76 }
77
78 /**
79 * <p>Setter for the field <code>entity</code>.</p>
80 *
81 * @param entity a T object.
82 */
83 public void setEntity(T entity){
84 this.entity = entity;
85 }
86
87 /**
88 * Adds the entity to the current persistence context
89 */
90 protected abstract void saveEntity();
91
92 /* (non-Javadoc)
93 * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
94 */
95 /** {@inheritDoc} */
96 @Override
97 public void init(IWorkbench workbench, IStructuredSelection selection) {
98 this.workbench = workbench != null ? workbench : StoreUtil.getWorkbench();
99
100 if(selection == null){
101 ISelectionService service = (ISelectionService) this.workbench.getActiveWorkbenchWindow().getSelectionService();
102 if(service.getSelection() instanceof IStructuredSelection){
103 selection = (IStructuredSelection) service.getSelection();
104 }
105 }
106 this.selection = selection;
107
108 formFactory = new CdmFormFactory(Display.getCurrent(), null);
109 conversation = CdmStore.createConversation();
110 entity = createNewEntity();
111
112 }
113
114 /**
115 * <p>createNewEntity</p>
116 *
117 * @return a T object.
118 */
119 protected abstract T createNewEntity();
120
121 /**
122 * <p>getConversationHolder</p>
123 *
124 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
125 */
126 public ConversationHolder getConversationHolder() {
127 return conversation;
128 }
129
130 /** {@inheritDoc} */
131 public void update(CdmDataChangeMap changeEvents) {}
132
133 /**
134 * @return the workbench
135 */
136 public IWorkbench getWorkbench() {
137 return workbench;
138 }
139
140 /**
141 * @return the selection
142 */
143 public IStructuredSelection getSelection() {
144 return selection;
145 }
146 }