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