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