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.bulkeditor.referencingobjects;
|
12
|
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.Collections;
|
15
|
import java.util.Comparator;
|
16
|
import java.util.List;
|
17
|
import java.util.Set;
|
18
|
import java.util.UUID;
|
19
|
|
20
|
import org.apache.log4j.Logger;
|
21
|
import org.eclipse.core.runtime.IProgressMonitor;
|
22
|
import org.eclipse.core.runtime.IStatus;
|
23
|
import org.eclipse.core.runtime.Status;
|
24
|
import org.eclipse.core.runtime.jobs.Job;
|
25
|
import org.eclipse.jface.action.MenuManager;
|
26
|
import org.eclipse.jface.viewers.CellEditor;
|
27
|
import org.eclipse.jface.viewers.ICellModifier;
|
28
|
import org.eclipse.jface.viewers.ISelection;
|
29
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
30
|
import org.eclipse.jface.viewers.ITableLabelProvider;
|
31
|
import org.eclipse.jface.viewers.TableViewer;
|
32
|
import org.eclipse.jface.viewers.TableViewerColumn;
|
33
|
import org.eclipse.jface.viewers.TextCellEditor;
|
34
|
import org.eclipse.jface.viewers.TreeNode;
|
35
|
import org.eclipse.jface.viewers.Viewer;
|
36
|
import org.eclipse.swt.SWT;
|
37
|
import org.eclipse.swt.widgets.Composite;
|
38
|
import org.eclipse.swt.widgets.Control;
|
39
|
import org.eclipse.swt.widgets.Display;
|
40
|
import org.eclipse.swt.widgets.Menu;
|
41
|
import org.eclipse.swt.widgets.Table;
|
42
|
import org.eclipse.ui.IWorkbenchPart;
|
43
|
|
44
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
45
|
import eu.etaxonomy.cdm.api.service.IAgentService;
|
46
|
import eu.etaxonomy.cdm.api.service.IDescriptionService;
|
47
|
import eu.etaxonomy.cdm.api.service.IEventBaseService;
|
48
|
import eu.etaxonomy.cdm.api.service.IGroupService;
|
49
|
import eu.etaxonomy.cdm.api.service.IMediaService;
|
50
|
import eu.etaxonomy.cdm.api.service.INameService;
|
51
|
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
|
52
|
import eu.etaxonomy.cdm.api.service.IReferenceService;
|
53
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
54
|
import eu.etaxonomy.cdm.api.service.IUserService;
|
55
|
import eu.etaxonomy.cdm.api.service.molecular.ISequenceService;
|
56
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
57
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
58
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
59
|
import eu.etaxonomy.cdm.model.common.EventBase;
|
60
|
import eu.etaxonomy.cdm.model.common.Group;
|
61
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
62
|
import eu.etaxonomy.cdm.model.common.User;
|
63
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
64
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
65
|
import eu.etaxonomy.cdm.model.media.Media;
|
66
|
import eu.etaxonomy.cdm.model.molecular.Sequence;
|
67
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
68
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
69
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
70
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
71
|
import eu.etaxonomy.taxeditor.model.DescriptionHelper;
|
72
|
import eu.etaxonomy.taxeditor.session.ICdmEntitySession;
|
73
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
74
|
import eu.etaxonomy.taxeditor.view.AbstractCdmViewPart;
|
75
|
|
76
|
/**
|
77
|
* @author p.ciardelli
|
78
|
* @created 08.07.2009
|
79
|
* @version 1.0
|
80
|
*/
|
81
|
public class ReferencingObjectsView extends AbstractCdmViewPart {
|
82
|
private static final Logger logger = Logger
|
83
|
.getLogger(ReferencingObjectsView.class);
|
84
|
|
85
|
public static final String ID = "eu.etaxonomy.taxeditor.bulkeditor.view.referencingobjects";
|
86
|
|
87
|
private TableViewer viewer;
|
88
|
|
89
|
private String referencedObjectTitleCache;
|
90
|
private ConversationHolder conversation;
|
91
|
private final ICdmEntitySession cdmEntitySession;
|
92
|
|
93
|
public ReferencingObjectsView() {
|
94
|
cdmEntitySession = CdmStore.getCurrentSessionManager().bindNullSession();
|
95
|
}
|
96
|
|
97
|
/** {@inheritDoc} */
|
98
|
@Override
|
99
|
public void createViewer(Composite parent) {
|
100
|
selectionService.addSelectionListener(this);
|
101
|
conversation = CdmStore.createConversation();
|
102
|
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
|
103
|
| SWT.V_SCROLL | SWT.FULL_SELECTION);
|
104
|
createColumns(viewer);
|
105
|
viewer.setContentProvider(new ReferencingObjectsContentProvider());
|
106
|
viewer.setLabelProvider(new ReferencingObjectsLabelProvider());
|
107
|
viewer.setCellModifier(new ICellModifier() {
|
108
|
@Override
|
109
|
public void modify(Object element, String property, Object value) {
|
110
|
}
|
111
|
|
112
|
@Override
|
113
|
public Object getValue(Object element, String property) {
|
114
|
ITableLabelProvider tableLabelProvider = null;
|
115
|
if(viewer.getLabelProvider() instanceof ITableLabelProvider){
|
116
|
tableLabelProvider = (ITableLabelProvider) viewer.getLabelProvider();
|
117
|
}
|
118
|
Object[] columnProperties = viewer.getColumnProperties();
|
119
|
for (int i=0;i<columnProperties.length;i++) {
|
120
|
if(columnProperties[i].equals(property) && tableLabelProvider!=null){
|
121
|
return tableLabelProvider.getColumnText(element, i);
|
122
|
}
|
123
|
}
|
124
|
return "";
|
125
|
}
|
126
|
|
127
|
@Override
|
128
|
public boolean canModify(Object element, String property) {
|
129
|
return true;
|
130
|
}
|
131
|
});
|
132
|
|
133
|
//create context menu
|
134
|
MenuManager menuManager = new MenuManager();
|
135
|
getSite().registerContextMenu(menuManager, viewer);
|
136
|
Control control = viewer.getControl();
|
137
|
Menu menu = menuManager.createContextMenu(control);
|
138
|
control.setMenu(menu);
|
139
|
|
140
|
// Propagate selection from viewer
|
141
|
getSite().setSelectionProvider(viewer);
|
142
|
}
|
143
|
|
144
|
/**
|
145
|
* Create the columns for the table
|
146
|
* @param viewer
|
147
|
*/
|
148
|
private void createColumns(TableViewer viewer) {
|
149
|
Table table = viewer.getTable();
|
150
|
String[] titles = {"Class", "Description", "UUID", "Object ID" };
|
151
|
int[] bounds = { 100, 200, 100, 70};
|
152
|
|
153
|
CellEditor[] editors = new CellEditor[titles.length];
|
154
|
|
155
|
for (int i = 0; i < titles.length; i++) {
|
156
|
TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
|
157
|
editors[i] = new TextCellEditor(table);
|
158
|
column.getColumn().setText(titles[i]);
|
159
|
column.getColumn().setWidth(bounds[i]);
|
160
|
column.getColumn().setResizable(true);
|
161
|
column.getColumn().setMoveable(true);
|
162
|
}
|
163
|
table.setHeaderVisible(true);
|
164
|
table.setLinesVisible(true);
|
165
|
|
166
|
viewer.setCellEditors(editors);
|
167
|
viewer.setColumnProperties(titles);
|
168
|
}
|
169
|
|
170
|
@Override
|
171
|
public void setFocus() {
|
172
|
cdmEntitySession.bind();
|
173
|
}
|
174
|
|
175
|
public void updateReferencingObjects(final UUID entityUUID, final Class objectClass) {
|
176
|
|
177
|
final Display display = Display.getCurrent();
|
178
|
|
179
|
Job job = new Job("Update Referencing Objects") {
|
180
|
|
181
|
@Override
|
182
|
protected IStatus run(IProgressMonitor monitor) {
|
183
|
monitor.beginTask("Calculating referencing objects", 10);
|
184
|
|
185
|
monitor.worked(3);
|
186
|
|
187
|
display.asyncExec(new Runnable() {
|
188
|
|
189
|
@Override
|
190
|
public void run() {
|
191
|
if (entityUUID != null){
|
192
|
ICdmEntitySession previousCdmEntitySession = CdmStore.getCurrentSessionManager().getActiveSession();
|
193
|
cdmEntitySession.bind();
|
194
|
List<CdmBase> referencingObjects = getReferencingObjects(entityUUID, objectClass);
|
195
|
if(previousCdmEntitySession != null) {
|
196
|
previousCdmEntitySession.bind();
|
197
|
}
|
198
|
updateView(referencingObjects);
|
199
|
|
200
|
}
|
201
|
}
|
202
|
});
|
203
|
|
204
|
monitor.done();
|
205
|
|
206
|
|
207
|
return Status.OK_STATUS;
|
208
|
}
|
209
|
};
|
210
|
|
211
|
job.setPriority(Job.DECORATE);
|
212
|
job.schedule();
|
213
|
}
|
214
|
|
215
|
private List<CdmBase> getReferencingObjects(UUID entity, Class objectClass) {
|
216
|
CdmBase referencedObject = null;
|
217
|
try {
|
218
|
if (objectClass.getSuperclass().equals(TeamOrPersonBase.class) ){
|
219
|
referencedObject = CdmStore.getService(IAgentService.class).load(entity);
|
220
|
} else if (objectClass.getSuperclass().equals(NonViralName.class)){
|
221
|
referencedObject = CdmStore.getService(INameService.class).load(entity);
|
222
|
} else if (objectClass.getSuperclass().equals(TaxonBase.class)){
|
223
|
referencedObject = CdmStore.getService(ITaxonService.class).load(entity);
|
224
|
} else if (objectClass.equals(Reference.class)){
|
225
|
referencedObject = CdmStore.getService(IReferenceService.class).load(entity);
|
226
|
} else if (objectClass.getSuperclass().equals(SpecimenOrObservationBase.class)){
|
227
|
referencedObject = CdmStore.getService(IOccurrenceService.class).load(entity);
|
228
|
} else if (objectClass.isAssignableFrom(User.class)){
|
229
|
referencedObject = CdmStore.getService(IUserService.class).load(entity);
|
230
|
} else if (objectClass.isAssignableFrom(Group.class)){
|
231
|
referencedObject = CdmStore.getService(IGroupService.class).load(entity);
|
232
|
} else if (objectClass.isAssignableFrom(Media.class)){
|
233
|
referencedObject = CdmStore.getService(IMediaService.class).load(entity);
|
234
|
} else if (DescriptionBase.class.isAssignableFrom(objectClass)){
|
235
|
referencedObject = CdmStore.getService(IDescriptionService.class).load(entity);
|
236
|
} else if (DescriptionElementBase.class.isAssignableFrom(objectClass)){
|
237
|
referencedObject = CdmStore.getService(IDescriptionService.class).loadDescriptionElement(entity, null);
|
238
|
} else if (objectClass.equals(Sequence.class)){
|
239
|
referencedObject = CdmStore.getService(ISequenceService.class).load(entity, null);
|
240
|
} else if (EventBase.class.isAssignableFrom(objectClass)){
|
241
|
referencedObject = CdmStore.getService(IEventBaseService.class).load(entity, null);
|
242
|
}
|
243
|
//referencedObject =(CdmBase) CdmStore.getService(IIdentifiableEntityService.class).load(referencedObject.getUuid());
|
244
|
Set<CdmBase> setOfReferencingObjects = null;
|
245
|
|
246
|
if (referencedObject != null){
|
247
|
if(referencedObject.isInstanceOf(IdentifiableEntity.class)){
|
248
|
referencedObjectTitleCache = (HibernateProxyHelper.deproxy(referencedObject, IdentifiableEntity.class)).getTitleCache();
|
249
|
}
|
250
|
else if(referencedObject.isInstanceOf(DescriptionElementBase.class)){
|
251
|
referencedObjectTitleCache = DescriptionHelper.getLabel(referencedObject);
|
252
|
}
|
253
|
else{
|
254
|
referencedObjectTitleCache = null;
|
255
|
}
|
256
|
setOfReferencingObjects = CdmStore.getCommonService().getReferencingObjects(referencedObject);
|
257
|
}
|
258
|
if (setOfReferencingObjects != null){
|
259
|
List<CdmBase> referencingObjects = new ArrayList<CdmBase>(setOfReferencingObjects);
|
260
|
|
261
|
Collections.sort(referencingObjects, new ReferencingClassComparator());
|
262
|
|
263
|
return referencingObjects;
|
264
|
|
265
|
}
|
266
|
} catch (Exception e) {
|
267
|
logger.error("Error retrieving referencing objects", e);
|
268
|
setContentDescription("The referencing objects view could not be loaded completely. Some Problems occured.");
|
269
|
}
|
270
|
return null;
|
271
|
}
|
272
|
|
273
|
class ReferencingClassComparator implements Comparator<CdmBase> {
|
274
|
|
275
|
@Override
|
276
|
public int compare(CdmBase o1, CdmBase o2) {
|
277
|
String string1 = o1.getClass().getSimpleName();
|
278
|
String string2 = o2.getClass().getSimpleName();
|
279
|
int classCompare = string1.compareToIgnoreCase(string2);
|
280
|
if (classCompare == 0) {
|
281
|
string1 = DescriptionHelper.getObjectDescription(o1);
|
282
|
string2 = DescriptionHelper.getObjectDescription(o2);
|
283
|
return string1.compareToIgnoreCase(string2);
|
284
|
} else {
|
285
|
return classCompare;
|
286
|
}
|
287
|
}
|
288
|
}
|
289
|
|
290
|
private void updateView(List<CdmBase> referencingObjects) {
|
291
|
if (viewer != null && !viewer.getControl().isDisposed()){
|
292
|
try{
|
293
|
viewer.setInput(referencingObjects);
|
294
|
if (referencedObjectTitleCache != null){
|
295
|
setContentDescription("'" + referencedObjectTitleCache + "' is referenced by:");
|
296
|
} else {
|
297
|
setContentDescription("");
|
298
|
}
|
299
|
showViewer();
|
300
|
}catch(Exception e){
|
301
|
setContentDescription("The referencing objects view could not be loaded completely. Some Problems occured.");
|
302
|
}
|
303
|
|
304
|
}
|
305
|
}
|
306
|
|
307
|
/** {@inheritDoc} */
|
308
|
@Override
|
309
|
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
|
310
|
if(part == this){
|
311
|
return;
|
312
|
}
|
313
|
if(selection instanceof IStructuredSelection){
|
314
|
showViewer(part, (IStructuredSelection) selection);
|
315
|
}
|
316
|
}
|
317
|
|
318
|
/** {@inheritDoc} */
|
319
|
@Override
|
320
|
public void showViewer(IWorkbenchPart part, IStructuredSelection selection) {
|
321
|
// this.part = part;
|
322
|
|
323
|
Object firstElement = selection.getFirstElement();
|
324
|
if(firstElement instanceof TreeNode){
|
325
|
firstElement = ((TreeNode) firstElement).getValue();
|
326
|
}
|
327
|
if(firstElement instanceof CdmBase){
|
328
|
updateReferencingObjects(((CdmBase) firstElement).getUuid(),firstElement.getClass() );
|
329
|
}
|
330
|
else{
|
331
|
updateView(null);
|
332
|
setContentDescription("");
|
333
|
}
|
334
|
}
|
335
|
|
336
|
/** {@inheritDoc} */
|
337
|
@Override
|
338
|
public void dispose() {
|
339
|
conversation.close();
|
340
|
selectionService.removePostSelectionListener(this);
|
341
|
selectionService.removeSelectionListener(this);
|
342
|
cdmEntitySession.dispose();
|
343
|
super.dispose();
|
344
|
|
345
|
}
|
346
|
|
347
|
/** {@inheritDoc} */
|
348
|
@Override
|
349
|
public Viewer getViewer() {
|
350
|
return viewer;
|
351
|
}
|
352
|
|
353
|
/** {@inheritDoc} */
|
354
|
@Override
|
355
|
public void changed(Object object) {
|
356
|
// no editing in this view
|
357
|
}
|
358
|
|
359
|
/** {@inheritDoc} */
|
360
|
@Override
|
361
|
public boolean onComplete() {
|
362
|
return false;
|
363
|
}
|
364
|
|
365
|
public void refresh(){
|
366
|
selectionService.removePostSelectionListener(this);
|
367
|
selectionService.removeSelectionListener(this);
|
368
|
selectionService.addPostSelectionListener(this);
|
369
|
selectionService.addSelectionListener(this);
|
370
|
|
371
|
}
|
372
|
|
373
|
}
|