Project

General

Profile

Download (3.53 KB) Statistics
| Branch: | Tag: | Revision:
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.ui.INewWizard;
15
import org.eclipse.ui.ISelectionService;
16
import org.eclipse.ui.IWorkbench;
17

    
18
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
19
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
20
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
21
import eu.etaxonomy.taxeditor.model.AbstractUtility;
22
import eu.etaxonomy.taxeditor.store.CdmStore;
23
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
24

    
25
/**
26
 * <p>Abstract AbstractNewEntityWizard class.</p>
27
 *
28
 * @author n.hoffmann
29
 * @created Jun 1, 2010
30
 * @version 1.0
31
 */
32
public abstract class AbstractNewEntityWizard<T> extends Wizard implements
33
		INewWizard, IConversationEnabled {
34

    
35
	private ConversationHolder conversation;
36

    
37
	protected CdmFormFactory formFactory;
38

    
39
	private T entity;
40

    
41
	private IWorkbench workbench;
42

    
43
	private IStructuredSelection selection;
44

    
45
	/**
46
	 * <p>Constructor for AbstractNewEntityWizard.</p>
47
	 *
48
	 * @param <T> a T object.
49
	 */
50
	public AbstractNewEntityWizard(){
51
		setWindowTitle(String.format("New %s", getEntityName()));
52
	}
53

    
54
	/**
55
	 * FIXME there might be a smarter way to do this,
56
	 *
57
	 * @return
58
	 */
59
	protected abstract String getEntityName();
60

    
61
	/* (non-Javadoc)
62
	 * @see org.eclipse.jface.wizard.Wizard#performFinish()
63
	 */
64
	/** {@inheritDoc} */
65
	@Override
66
	public boolean performFinish() {
67
		saveEntity();
68

    
69
		conversation.commit();
70
		conversation.close();
71
		return true;
72
	}
73

    
74
	/**
75
	 * <p>Getter for the field <code>entity</code>.</p>
76
	 *
77
	 * @return a T object.
78
	 */
79
	public T getEntity() {
80
		return entity;
81
	}
82

    
83
	/**
84
	 * <p>Setter for the field <code>entity</code>.</p>
85
	 *
86
	 * @param entity a T object.
87
	 */
88
	public void setEntity(T entity){
89
		this.entity = entity;
90
	}
91

    
92
	/**
93
	 * Adds the entity to the current persistence context
94
	 */
95
	protected abstract void saveEntity();
96

    
97
	/* (non-Javadoc)
98
	 * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench, org.eclipse.jface.viewers.IStructuredSelection)
99
	 */
100
	/** {@inheritDoc} */
101
	@Override
102
	public void init(IWorkbench workbench, IStructuredSelection selection) {
103
		this.workbench = workbench != null ? workbench : AbstractUtility.getWorkbench();
104

    
105
		if(selection == null){
106
			ISelectionService service = this.workbench.getActiveWorkbenchWindow().getSelectionService();
107
			if(service.getSelection() instanceof IStructuredSelection){
108
				selection = (IStructuredSelection) service.getSelection();
109
			}
110
		}
111
		this.selection = selection;
112

    
113
//		formFactory = new CdmFormFactory(Display.getCurrent(), null);
114
	 	conversation = CdmStore.createConversation();
115
	 	entity = createNewEntity();
116

    
117
	}
118

    
119
	/**
120
	 * <p>createNewEntity</p>
121
	 *
122
	 * @return a T object.
123
	 */
124
	protected abstract T createNewEntity();
125

    
126
	/**
127
	 * <p>getConversationHolder</p>
128
	 *
129
	 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
130
	 */
131
	@Override
132
    public ConversationHolder getConversationHolder() {
133
		return conversation;
134
	}
135

    
136
	/** {@inheritDoc} */
137
	@Override
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
}
(1-1/23)