f2657aae1f13b8d4400d0e925b477cc3d0f1a9f9
[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(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 : StoreUtil.getWorkbench();
106
107 if(selection == null){
108 ISelectionService service = (ISelectionService) 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 public ConversationHolder getConversationHolder() {
134 return conversation;
135 }
136
137 /** {@inheritDoc} */
138 public void update(CdmDataChangeMap changeEvents) {}
139
140 /**
141 * @return the workbench
142 */
143 public IWorkbench getWorkbench() {
144 return workbench;
145 }
146
147 /**
148 * @return the selection
149 */
150 public IStructuredSelection getSelection() {
151 return selection;
152 }
153 }