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