3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.bulkeditor
.referencingobjects
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Collections
;
15 import java
.util
.Comparator
;
16 import java
.util
.List
;
18 import java
.util
.UUID
;
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
.ISelection
;
26 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
27 import org
.eclipse
.jface
.viewers
.TableViewer
;
28 import org
.eclipse
.jface
.viewers
.TableViewerColumn
;
29 import org
.eclipse
.jface
.viewers
.Viewer
;
30 import org
.eclipse
.swt
.SWT
;
32 import org
.eclipse
.swt
.widgets
.Composite
;
33 import org
.eclipse
.swt
.widgets
.Display
;
34 import org
.eclipse
.swt
.widgets
.Table
;
35 import org
.eclipse
.ui
.INullSelectionListener
;
36 import org
.eclipse
.ui
.ISelectionListener
;
38 import org
.eclipse
.ui
.IWorkbenchPart
;
40 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
41 import eu
.etaxonomy
.cdm
.api
.service
.IAgentService
;
42 import eu
.etaxonomy
.cdm
.api
.service
.ICommonService
;
43 import eu
.etaxonomy
.cdm
.api
.service
.IGroupService
;
45 import eu
.etaxonomy
.cdm
.api
.service
.INameService
;
46 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
47 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
48 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
49 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
;
55 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
56 import eu
.etaxonomy
.cdm
.model
.common
.User
;
57 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
58 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
59 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
60 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
62 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditor
;
63 import eu
.etaxonomy
.taxeditor
.model
.DescriptionHelper
;
64 import eu
.etaxonomy
.taxeditor
.model
.LineSelection
;
65 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
66 import eu
.etaxonomy
.taxeditor
.view
.AbstractCdmViewPart
;
69 * <p>ReferencingObjectsView class.</p>
75 public class ReferencingObjectsView
extends AbstractCdmViewPart
{
76 private static final Logger logger
= Logger
77 .getLogger(ReferencingObjectsView
.class);
79 /** Constant <code>ID="eu.etaxonomy.taxeditor.bulkeditor.refer"{trunked}</code> */
80 public static final String ID
= "eu.etaxonomy.taxeditor.bulkeditor.view.referencingobjects";
82 private TableViewer viewer
;
84 private String referencedObjectTitleCache
;
85 private ConversationHolder conversation
;
88 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#createViewer(org.eclipse.swt.widgets.Composite)
92 public void createViewer(Composite parent
) {
93 selectionService
.addSelectionListener(this);
94 conversation
= CdmStore
.createConversation();
95 viewer
= new TableViewer(parent
, SWT
.MULTI
| SWT
.H_SCROLL
96 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
);
97 createColumns(viewer
);
98 viewer
.setContentProvider(new ReferencingObjectsContentProvider());
99 viewer
.setLabelProvider(new ReferencingObjectsLabelProvider());
100 // viewer.setInput(new ArrayList<CdmBase>());
104 * Create the columns for the table
107 private void createColumns(TableViewer viewer
) {
108 Table table
= viewer
.getTable();
109 String
[] titles
= {"Class", "Description", "UUID", "Object ID" };
110 int[] bounds
= { 100, 200, 100, 70};
112 for (int i
= 0; i
< titles
.length
; i
++) {
113 TableViewerColumn column
= new TableViewerColumn(viewer
, SWT
.NONE
);
114 column
.getColumn().setText(titles
[i
]);
115 column
.getColumn().setWidth(bounds
[i
]);
116 column
.getColumn().setResizable(true);
117 column
.getColumn().setMoveable(true);
119 table
.setHeaderVisible(true);
120 table
.setLinesVisible(true);
124 * <p>updateReferencingObjects</p>
126 * @param entity a {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity} object.
128 public void updateReferencingObjects(final UUID entityUUID
, final Class objectClass
) {
130 final Display display
= Display
.getCurrent();
132 Job job
= new Job("Update Referencing Objects") {
135 protected IStatus
run(IProgressMonitor monitor
) {
136 monitor
.beginTask("Calculating referencing objects", 10);
140 display
.asyncExec(new Runnable() {
144 if (entityUUID
!= null){
146 List
<CdmBase
> referencingObjects
= getReferencingObjects(entityUUID
, objectClass
);
148 updateView(referencingObjects
);
158 return Status
.OK_STATUS
;
162 job
.setPriority(Job
.DECORATE
);
166 private List
<CdmBase
> getReferencingObjects(UUID entity
, Class objectClass
) {
167 CdmBase referencedObject
= null;
169 if (objectClass
.getSuperclass().equals(TeamOrPersonBase
.class) ){
170 referencedObject
= CdmStore
.getService(IAgentService
.class).load(entity
);
171 } else if (objectClass
.getSuperclass().equals(TaxonNameBase
.class)){
172 referencedObject
= CdmStore
.getService(INameService
.class).load(entity
);
173 } else if (objectClass
.getSuperclass().equals(TaxonBase
.class)){
174 referencedObject
= CdmStore
.getService(ITaxonService
.class).load(entity
);
175 } else if (objectClass
.equals(Reference
.class)){
176 referencedObject
= CdmStore
.getService(IReferenceService
.class).load(entity
);
177 } else if (objectClass
.getSuperclass().equals(SpecimenOrObservationBase
.class)){
178 referencedObject
= CdmStore
.getService(IOccurrenceService
.class).load(entity
);
179 } else if (objectClass
.isAssignableFrom(User
.class)){
180 referencedObject
= CdmStore
.getService(IUserService
.class).load(entity
);
181 } else if (objectClass
.isAssignableFrom(Group
.class)){
182 referencedObject
= CdmStore
.getService(IGroupService
.class).load(entity
);
184 //referencedObject =(CdmBase) CdmStore.getService(IIdentifiableEntityService.class).load(referencedObject.getUuid());
185 Set
<CdmBase
> setOfReferencingObjects
= null;
187 if (referencedObject
!= null){
188 referencedObjectTitleCache
= ((IdentifiableEntity
)referencedObject
).getTitleCache();
189 setOfReferencingObjects
= CdmStore
.getService(ICommonService
.class).getReferencingObjects(referencedObject
);
191 if (setOfReferencingObjects
!= null){
192 List
<CdmBase
> referencingObjects
= new ArrayList
<CdmBase
>(setOfReferencingObjects
);
194 Collections
.sort(referencingObjects
, new ReferencingClassComparator());
196 return referencingObjects
;
199 } catch (Exception e
) {
200 logger
.error("Error retrieving referencing objects", e
);
205 class ReferencingClassComparator
implements Comparator
<CdmBase
> {
208 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
210 public int compare(CdmBase o1
, CdmBase o2
) {
211 String string1
= o1
.getClass().getSimpleName();
212 String string2
= o2
.getClass().getSimpleName();
213 int classCompare
= string1
.compareToIgnoreCase(string2
);
214 if (classCompare
== 0) {
215 string1
= DescriptionHelper
.getObjectDescription(o1
);
216 string2
= DescriptionHelper
.getObjectDescription(o2
);
217 return string1
.compareToIgnoreCase(string2
);
227 private void updateView(List
<CdmBase
> referencingObjects
) {
228 if (viewer
!= null && !viewer
.getControl().isDisposed()){
229 viewer
.setInput(referencingObjects
);
230 if (referencedObjectTitleCache
!= null){
231 setContentDescription("'" + referencedObjectTitleCache
+ "' is referenced by:");
233 setContentDescription("");
240 public void selectionChanged(IWorkbenchPart part
, ISelection selection
) {
241 if(! (part
instanceof BulkEditor
)){
242 setContentDescription("");
247 if(! (selection
instanceof LineSelection
)){
250 LineSelection lineSelection
= (LineSelection
) selection
;
252 if(lineSelection
.size() != 1){
256 if(! (lineSelection
.getFirstElement() instanceof IdentifiableEntity
)){
260 showViewer(part
, lineSelection
);
264 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#showViewer(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
268 public void showViewer(IWorkbenchPart part
, IStructuredSelection selection
) {
271 updateReferencingObjects(((IdentifiableEntity
) selection
.getFirstElement()).getUuid(),selection
.getFirstElement().getClass() );
276 public void dispose() {
277 conversation
.close();
278 selectionService
.removePostSelectionListener(this);
279 selectionService
.removeSelectionListener(this);
285 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#getViewer()
289 public Viewer
getViewer() {
294 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#dirtyStateChanged()
298 public void changed(Object object
) {
299 // no editing in this view
303 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#onComplete()
307 public boolean onComplete() {
311 public void refresh(){
313 selectionService
.removePostSelectionListener(this);
314 selectionService
.removeSelectionListener(this);
315 selectionService
.addPostSelectionListener(this);
316 selectionService
.addSelectionListener(this);