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
|
|
19
|
import org.apache.log4j.Logger;
|
20
|
import org.eclipse.core.runtime.IProgressMonitor;
|
21
|
import org.eclipse.core.runtime.IStatus;
|
22
|
import org.eclipse.core.runtime.Status;
|
23
|
import org.eclipse.core.runtime.jobs.Job;
|
24
|
import org.eclipse.jface.viewers.ISelection;
|
25
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
26
|
import org.eclipse.jface.viewers.TableViewer;
|
27
|
import org.eclipse.jface.viewers.TableViewerColumn;
|
28
|
import org.eclipse.jface.viewers.Viewer;
|
29
|
import org.eclipse.swt.SWT;
|
30
|
import org.eclipse.swt.widgets.Composite;
|
31
|
import org.eclipse.swt.widgets.Display;
|
32
|
import org.eclipse.swt.widgets.Table;
|
33
|
import org.eclipse.ui.ISelectionListener;
|
34
|
import org.eclipse.ui.IWorkbenchPart;
|
35
|
|
36
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
37
|
import eu.etaxonomy.cdm.api.service.ICommonService;
|
38
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
39
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
40
|
import eu.etaxonomy.taxeditor.bulkeditor.BulkEditor;
|
41
|
import eu.etaxonomy.taxeditor.model.DescriptionHelper;
|
42
|
import eu.etaxonomy.taxeditor.model.LineSelection;
|
43
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
44
|
import eu.etaxonomy.taxeditor.store.singlesource.widget.DisplayProxy;
|
45
|
import eu.etaxonomy.taxeditor.view.AbstractCdmViewPart;
|
46
|
|
47
|
/**
|
48
|
* <p>ReferencingObjectsView class.</p>
|
49
|
*
|
50
|
* @author p.ciardelli
|
51
|
* @created 08.07.2009
|
52
|
* @version 1.0
|
53
|
*/
|
54
|
public class ReferencingObjectsView extends AbstractCdmViewPart implements ISelectionListener{
|
55
|
private static final Logger logger = Logger
|
56
|
.getLogger(ReferencingObjectsView.class);
|
57
|
|
58
|
/** Constant <code>ID="eu.etaxonomy.taxeditor.bulkeditor.refer"{trunked}</code> */
|
59
|
public static final String ID = "eu.etaxonomy.taxeditor.bulkeditor.view.referencingobjects";
|
60
|
|
61
|
private TableViewer viewer;
|
62
|
|
63
|
private CdmBase referencedObject;
|
64
|
|
65
|
/* (non-Javadoc)
|
66
|
* @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#createViewer(org.eclipse.swt.widgets.Composite)
|
67
|
*/
|
68
|
/** {@inheritDoc} */
|
69
|
@Override
|
70
|
public void createViewer(Composite parent) {
|
71
|
selectionService.addSelectionListener(this);
|
72
|
|
73
|
viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
|
74
|
| SWT.V_SCROLL | SWT.FULL_SELECTION);
|
75
|
createColumns(viewer);
|
76
|
viewer.setContentProvider(new ReferencingObjectsContentProvider());
|
77
|
viewer.setLabelProvider(new ReferencingObjectsLabelProvider());
|
78
|
// viewer.setInput(new ArrayList<CdmBase>());
|
79
|
}
|
80
|
|
81
|
/**
|
82
|
* Create the columns for the table
|
83
|
* @param viewer
|
84
|
*/
|
85
|
private void createColumns(TableViewer viewer) {
|
86
|
Table table = viewer.getTable();
|
87
|
String[] titles = {"Class", "Description", "UUID", "Object ID" };
|
88
|
int[] bounds = { 100, 200, 100, 70};
|
89
|
|
90
|
for (int i = 0; i < titles.length; i++) {
|
91
|
TableViewerColumn column = new TableViewerColumn(viewer, SWT.NONE);
|
92
|
column.getColumn().setText(titles[i]);
|
93
|
column.getColumn().setWidth(bounds[i]);
|
94
|
column.getColumn().setResizable(true);
|
95
|
column.getColumn().setMoveable(true);
|
96
|
}
|
97
|
table.setHeaderVisible(true);
|
98
|
table.setLinesVisible(true);
|
99
|
}
|
100
|
|
101
|
/**
|
102
|
* <p>updateReferencingObjects</p>
|
103
|
*
|
104
|
* @param entity a {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity} object.
|
105
|
*/
|
106
|
public void updateReferencingObjects(final IdentifiableEntity entity) {
|
107
|
|
108
|
final Display display = DisplayProxy.getDefault();
|
109
|
|
110
|
Job job = new Job("Update Referencing Objects") {
|
111
|
|
112
|
@Override
|
113
|
protected IStatus run(IProgressMonitor monitor) {
|
114
|
monitor.beginTask("Calculating referencing objects", 10);
|
115
|
|
116
|
monitor.worked(3);
|
117
|
|
118
|
display.asyncExec(new Runnable() {
|
119
|
|
120
|
@Override
|
121
|
public void run() {
|
122
|
ConversationHolder conversation = CdmStore.createConversation();
|
123
|
List<CdmBase> referencingObjects = getReferencingObjects(entity);
|
124
|
updateView(referencingObjects);
|
125
|
conversation.close();
|
126
|
}
|
127
|
});
|
128
|
|
129
|
monitor.done();
|
130
|
|
131
|
|
132
|
return Status.OK_STATUS;
|
133
|
}
|
134
|
};
|
135
|
|
136
|
job.setPriority(Job.DECORATE);
|
137
|
job.schedule();
|
138
|
}
|
139
|
|
140
|
private List<CdmBase> getReferencingObjects(Object entity) {
|
141
|
try {
|
142
|
if(entity instanceof CdmBase){
|
143
|
referencedObject = (CdmBase) entity;
|
144
|
Set<CdmBase> setOfReferencingObjects = CdmStore.getService(ICommonService.class).getReferencingObjects(referencedObject);
|
145
|
|
146
|
List<CdmBase> referencingObjects = new ArrayList<CdmBase>(setOfReferencingObjects);
|
147
|
|
148
|
Collections.sort(referencingObjects, new ReferencingClassComparator());
|
149
|
|
150
|
return referencingObjects;
|
151
|
}
|
152
|
} catch (Exception e) {
|
153
|
logger.error("Error retrieving referencing objects", e);
|
154
|
}
|
155
|
return null;
|
156
|
}
|
157
|
|
158
|
class ReferencingClassComparator implements Comparator<CdmBase> {
|
159
|
|
160
|
/* (non-Javadoc)
|
161
|
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
|
162
|
*/
|
163
|
public int compare(CdmBase o1, CdmBase o2) {
|
164
|
String string1 = o1.getClass().getSimpleName();
|
165
|
String string2 = o2.getClass().getSimpleName();
|
166
|
int classCompare = string1.compareToIgnoreCase(string2);
|
167
|
if (classCompare == 0) {
|
168
|
string1 = DescriptionHelper.getObjectDescription(o1);
|
169
|
string2 = DescriptionHelper.getObjectDescription(o2);
|
170
|
return string1.compareToIgnoreCase(string2);
|
171
|
} else {
|
172
|
return classCompare;
|
173
|
}
|
174
|
}
|
175
|
}
|
176
|
|
177
|
/**
|
178
|
*
|
179
|
*/
|
180
|
private void updateView(List<CdmBase> referencingObjects) {
|
181
|
viewer.setInput(referencingObjects);
|
182
|
setContentDescription("'" + referencedObject + "' is referenced by:");
|
183
|
showViewer();
|
184
|
}
|
185
|
|
186
|
/** {@inheritDoc} */
|
187
|
public void selectionChanged(IWorkbenchPart part, ISelection selection) {
|
188
|
if(! (part instanceof BulkEditor)){
|
189
|
setContentDescription("");
|
190
|
showEmptyPage();
|
191
|
return;
|
192
|
}
|
193
|
|
194
|
if(! (selection instanceof LineSelection)){
|
195
|
return;
|
196
|
}
|
197
|
LineSelection lineSelection = (LineSelection) selection;
|
198
|
|
199
|
if(lineSelection.size() != 1){
|
200
|
return;
|
201
|
}
|
202
|
|
203
|
if(! (lineSelection.getFirstElement() instanceof IdentifiableEntity)){
|
204
|
return;
|
205
|
}
|
206
|
|
207
|
showViewer(part, lineSelection);
|
208
|
}
|
209
|
|
210
|
/* (non-Javadoc)
|
211
|
* @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#showViewer(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
|
212
|
*/
|
213
|
/** {@inheritDoc} */
|
214
|
@Override
|
215
|
public void showViewer(IWorkbenchPart part, IStructuredSelection selection) {
|
216
|
this.part = part;
|
217
|
|
218
|
updateReferencingObjects((IdentifiableEntity) selection.getFirstElement());
|
219
|
}
|
220
|
|
221
|
/** {@inheritDoc} */
|
222
|
@Override
|
223
|
public void dispose() {
|
224
|
selectionService.removePostSelectionListener(this);
|
225
|
selectionService.removeSelectionListener(this);
|
226
|
super.dispose();
|
227
|
}
|
228
|
|
229
|
/* (non-Javadoc)
|
230
|
* @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#getViewer()
|
231
|
*/
|
232
|
/** {@inheritDoc} */
|
233
|
@Override
|
234
|
public Viewer getViewer() {
|
235
|
return viewer;
|
236
|
}
|
237
|
|
238
|
/* (non-Javadoc)
|
239
|
* @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#dirtyStateChanged()
|
240
|
*/
|
241
|
/** {@inheritDoc} */
|
242
|
@Override
|
243
|
public void changed(Object object) {
|
244
|
// no editing in this view
|
245
|
}
|
246
|
|
247
|
/* (non-Javadoc)
|
248
|
* @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#onComplete()
|
249
|
*/
|
250
|
/** {@inheritDoc} */
|
251
|
@Override
|
252
|
public boolean onComplete() {
|
253
|
return false;
|
254
|
}
|
255
|
}
|