1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2017 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
|
package eu.etaxonomy.taxeditor.editor.descriptiveDataSet;
|
11
|
|
12
|
import java.util.Collection;
|
13
|
import java.util.List;
|
14
|
import java.util.Map;
|
15
|
|
16
|
import javax.annotation.PostConstruct;
|
17
|
import javax.annotation.PreDestroy;
|
18
|
import javax.inject.Inject;
|
19
|
|
20
|
import org.eclipse.core.runtime.IProgressMonitor;
|
21
|
import org.eclipse.e4.ui.di.Focus;
|
22
|
import org.eclipse.e4.ui.model.application.MApplication;
|
23
|
import org.eclipse.e4.ui.services.EMenuService;
|
24
|
import org.eclipse.e4.ui.workbench.modeling.EModelService;
|
25
|
import org.eclipse.e4.ui.workbench.modeling.EPartService;
|
26
|
import org.eclipse.e4.ui.workbench.modeling.ESelectionService;
|
27
|
import org.eclipse.jface.viewers.ArrayContentProvider;
|
28
|
import org.eclipse.jface.viewers.DoubleClickEvent;
|
29
|
import org.eclipse.jface.viewers.IDoubleClickListener;
|
30
|
import org.eclipse.jface.viewers.ISelection;
|
31
|
import org.eclipse.jface.viewers.ISelectionChangedListener;
|
32
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
33
|
import org.eclipse.jface.viewers.LabelProvider;
|
34
|
import org.eclipse.jface.viewers.ListViewer;
|
35
|
import org.eclipse.swt.widgets.Composite;
|
36
|
import org.eclipse.ui.IMemento;
|
37
|
|
38
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
39
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
40
|
import eu.etaxonomy.cdm.api.service.IDescriptiveDataSetService;
|
41
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
42
|
import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
|
43
|
import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
|
44
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
45
|
import eu.etaxonomy.taxeditor.model.IContextListener;
|
46
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
|
47
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
|
48
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
49
|
|
50
|
/**
|
51
|
* @author pplitzner
|
52
|
* @date 12.12.2017
|
53
|
*
|
54
|
*/
|
55
|
public class DescriptiveDataSetNavigator implements IConversationEnabled, ICdmEntitySessionEnabled, IContextListener{
|
56
|
|
57
|
private ListViewer viewer;
|
58
|
|
59
|
@Inject
|
60
|
private ESelectionService selService;
|
61
|
|
62
|
private ISelectionChangedListener selectionChangedListener;
|
63
|
|
64
|
private ConversationHolder conversation;
|
65
|
|
66
|
private ICdmEntitySession cdmEntitySession;
|
67
|
|
68
|
private List<DescriptiveDataSet> descriptiveDataSets;
|
69
|
|
70
|
public DescriptiveDataSetNavigator() {
|
71
|
CdmStore.getContextManager().addContextListener(this);
|
72
|
}
|
73
|
|
74
|
@PostConstruct
|
75
|
public void create(Composite parent, EPartService partService, EModelService modelService, MApplication application,
|
76
|
EMenuService menuService){
|
77
|
viewer = new ListViewer(parent);
|
78
|
viewer.setContentProvider(new ArrayContentProvider());
|
79
|
viewer.setLabelProvider(new LabelProvider(){
|
80
|
/**
|
81
|
* {@inheritDoc}
|
82
|
*/
|
83
|
@Override
|
84
|
public String getText(Object element) {
|
85
|
if(element instanceof DescriptiveDataSet){
|
86
|
return ((DescriptiveDataSet) element).getLabel();
|
87
|
}
|
88
|
return super.getText(element);
|
89
|
}
|
90
|
});
|
91
|
|
92
|
viewer.addDoubleClickListener(new IDoubleClickListener() {
|
93
|
|
94
|
@Override
|
95
|
public void doubleClick(DoubleClickEvent event) {
|
96
|
ISelection selection = event.getSelection();
|
97
|
if(selection instanceof IStructuredSelection){
|
98
|
DescriptiveDataSet descriptiveDataSetUuid = (DescriptiveDataSet) ((IStructuredSelection) selection).getFirstElement();
|
99
|
if(descriptiveDataSetUuid!=null){
|
100
|
EditorUtil.openDescriptiveDataSetEditor(descriptiveDataSetUuid.getUuid(), modelService, partService, application);
|
101
|
}
|
102
|
}
|
103
|
}
|
104
|
});
|
105
|
|
106
|
//propagate selection
|
107
|
selectionChangedListener = (event -> selService.setSelection(event.getSelection()));
|
108
|
viewer.addSelectionChangedListener(selectionChangedListener);
|
109
|
|
110
|
//create context menu
|
111
|
menuService.registerContextMenu(viewer.getControl(), "eu.etaxonomy.taxeditor.editor.popupmenu.descriptiveDataSetNavigator"); //$NON-NLS-1$
|
112
|
|
113
|
init();
|
114
|
}
|
115
|
|
116
|
public void init(){
|
117
|
if (CdmStore.isActive()){
|
118
|
if(conversation == null) {
|
119
|
conversation = CdmStore.createConversation();
|
120
|
}
|
121
|
if(cdmEntitySession == null){
|
122
|
cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
|
123
|
}
|
124
|
descriptiveDataSets = CdmStore.getService(IDescriptiveDataSetService.class).list(DescriptiveDataSet.class, null, null, null, null);
|
125
|
viewer.setInput(descriptiveDataSets);
|
126
|
}
|
127
|
}
|
128
|
|
129
|
public void addDescriptiveDataSet(DescriptiveDataSet descriptiveDataSetSet){
|
130
|
descriptiveDataSets.add(descriptiveDataSetSet);
|
131
|
refresh();
|
132
|
}
|
133
|
|
134
|
public void removeDescriptiveDataSet(DescriptiveDataSet descriptiveDataSetSet){
|
135
|
descriptiveDataSets.remove(descriptiveDataSetSet);
|
136
|
refresh();
|
137
|
}
|
138
|
|
139
|
public void refresh(){
|
140
|
refresh(false);
|
141
|
}
|
142
|
|
143
|
public void refresh(boolean reloadDescriptiveDataSets){
|
144
|
if(reloadDescriptiveDataSets){
|
145
|
cdmEntitySession.dispose();
|
146
|
cdmEntitySession = CdmStore.getCurrentSessionManager().newSession(this, true);
|
147
|
IDescriptiveDataSetService service = CdmStore.getService(IDescriptiveDataSetService.class);
|
148
|
descriptiveDataSets = service.list(DescriptiveDataSet.class, null, null, null, null);
|
149
|
}
|
150
|
viewer.setInput(descriptiveDataSets);
|
151
|
viewer.refresh();
|
152
|
}
|
153
|
|
154
|
/**
|
155
|
* {@inheritDoc}
|
156
|
*/
|
157
|
@Override
|
158
|
public void update(CdmDataChangeMap arg0) {
|
159
|
}
|
160
|
|
161
|
/**
|
162
|
* {@inheritDoc}
|
163
|
*/
|
164
|
@Override
|
165
|
public ICdmEntitySession getCdmEntitySession() {
|
166
|
return cdmEntitySession;
|
167
|
}
|
168
|
|
169
|
/**
|
170
|
* {@inheritDoc}
|
171
|
*/
|
172
|
@Override
|
173
|
public <T extends CdmBase> Collection<T> getRootEntities() {
|
174
|
return null;
|
175
|
}
|
176
|
|
177
|
/**
|
178
|
* {@inheritDoc}
|
179
|
*/
|
180
|
@Override
|
181
|
public Map<Object, List<String>> getPropertyPathsMap() {
|
182
|
return null;
|
183
|
}
|
184
|
|
185
|
/**
|
186
|
* {@inheritDoc}
|
187
|
*/
|
188
|
@Override
|
189
|
public ConversationHolder getConversationHolder() {
|
190
|
return conversation;
|
191
|
}
|
192
|
|
193
|
/** {@inheritDoc} */
|
194
|
@PreDestroy
|
195
|
public void dispose() {
|
196
|
if (conversation != null) {
|
197
|
conversation.close();
|
198
|
conversation = null;
|
199
|
}
|
200
|
if(cdmEntitySession != null) {
|
201
|
cdmEntitySession.dispose();
|
202
|
cdmEntitySession = null;
|
203
|
}
|
204
|
}
|
205
|
|
206
|
/** {@inheritDoc} */
|
207
|
@Focus
|
208
|
public void setFocus() {
|
209
|
if (getConversationHolder() != null) {
|
210
|
getConversationHolder().bind();
|
211
|
}
|
212
|
if(cdmEntitySession != null) {
|
213
|
cdmEntitySession.bind();
|
214
|
}
|
215
|
}
|
216
|
|
217
|
/**
|
218
|
* {@inheritDoc}
|
219
|
*/
|
220
|
@Override
|
221
|
public void contextAboutToStop(IMemento memento, IProgressMonitor monitor) {
|
222
|
// TODO Auto-generated method stub
|
223
|
|
224
|
}
|
225
|
|
226
|
/**
|
227
|
* {@inheritDoc}
|
228
|
*/
|
229
|
@Override
|
230
|
public void contextStop(IMemento memento, IProgressMonitor monitor) {
|
231
|
}
|
232
|
|
233
|
/**
|
234
|
* {@inheritDoc}
|
235
|
*/
|
236
|
@Override
|
237
|
public void contextStart(IMemento memento, IProgressMonitor monitor) {
|
238
|
if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed()){
|
239
|
init();
|
240
|
}
|
241
|
}
|
242
|
|
243
|
/**
|
244
|
* {@inheritDoc}
|
245
|
*/
|
246
|
@Override
|
247
|
public void contextRefresh(IProgressMonitor monitor) {
|
248
|
}
|
249
|
|
250
|
/**
|
251
|
* {@inheritDoc}
|
252
|
*/
|
253
|
@Override
|
254
|
public void workbenchShutdown(IMemento memento, IProgressMonitor monitor) {
|
255
|
}
|
256
|
|
257
|
}
|