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
;
37 import org
.eclipse
.ui
.IWorkbenchPart
;
39 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
40 import eu
.etaxonomy
.cdm
.api
.service
.IAgentService
;
41 import eu
.etaxonomy
.cdm
.api
.service
.ICommonService
;
42 import eu
.etaxonomy
.cdm
.api
.service
.IGroupService
;
44 import eu
.etaxonomy
.cdm
.api
.service
.INameService
;
45 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
46 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
47 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
48 import eu
.etaxonomy
.cdm
.api
.service
.IUserService
;
50 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
51 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
52 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
.TaxonNameBase
;
57 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
58 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
59 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
61 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditor
;
62 import eu
.etaxonomy
.taxeditor
.model
.DescriptionHelper
;
63 import eu
.etaxonomy
.taxeditor
.model
.LineSelection
;
64 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
65 import eu
.etaxonomy
.taxeditor
.view
.AbstractCdmViewPart
;
68 * <p>ReferencingObjectsView class.</p>
74 public class ReferencingObjectsView
extends AbstractCdmViewPart
implements INullSelectionListener
{
75 private static final Logger logger
= Logger
76 .getLogger(ReferencingObjectsView
.class);
78 /** Constant <code>ID="eu.etaxonomy.taxeditor.bulkeditor.refer"{trunked}</code> */
79 public static final String ID
= "eu.etaxonomy.taxeditor.bulkeditor.view.referencingobjects";
81 private TableViewer viewer
;
83 private String referencedObjectTitleCache
;
86 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#createViewer(org.eclipse.swt.widgets.Composite)
90 public void createViewer(Composite parent
) {
91 selectionService
.addSelectionListener(this);
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.setInput(new ArrayList<CdmBase>());
102 * Create the columns for the table
105 private void createColumns(TableViewer viewer
) {
106 Table table
= viewer
.getTable();
107 String
[] titles
= {"Class", "Description", "UUID", "Object ID" };
108 int[] bounds
= { 100, 200, 100, 70};
110 for (int i
= 0; i
< titles
.length
; i
++) {
111 TableViewerColumn column
= new TableViewerColumn(viewer
, SWT
.NONE
);
112 column
.getColumn().setText(titles
[i
]);
113 column
.getColumn().setWidth(bounds
[i
]);
114 column
.getColumn().setResizable(true);
115 column
.getColumn().setMoveable(true);
117 table
.setHeaderVisible(true);
118 table
.setLinesVisible(true);
122 * <p>updateReferencingObjects</p>
124 * @param entity a {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity} object.
126 public void updateReferencingObjects(final UUID entityUUID
, final Class objectClass
) {
128 final Display display
= Display
.getCurrent();
130 Job job
= new Job("Update Referencing Objects") {
133 protected IStatus
run(IProgressMonitor monitor
) {
134 monitor
.beginTask("Calculating referencing objects", 10);
138 display
.asyncExec(new Runnable() {
142 if (entityUUID
!= null){
143 ConversationHolder conversation
= CdmStore
.createConversation();
144 List
<CdmBase
> referencingObjects
= getReferencingObjects(entityUUID
, objectClass
);
145 conversation
.close();
146 updateView(referencingObjects
);
154 return Status
.OK_STATUS
;
158 job
.setPriority(Job
.DECORATE
);
162 private List
<CdmBase
> getReferencingObjects(UUID entity
, Class objectClass
) {
163 CdmBase referencedObject
= null;
165 if (objectClass
.getSuperclass().equals(TeamOrPersonBase
.class) ){
166 referencedObject
= CdmStore
.getService(IAgentService
.class).load(entity
);
167 } else if (objectClass
.getSuperclass().equals(TaxonNameBase
.class)){
168 referencedObject
= CdmStore
.getService(INameService
.class).load(entity
);
169 } else if (objectClass
.getSuperclass().equals(TaxonBase
.class)){
170 referencedObject
= CdmStore
.getService(ITaxonService
.class).load(entity
);
171 } else if (objectClass
.getSuperclass().equals(Reference
.class)){
172 referencedObject
= CdmStore
.getService(IReferenceService
.class).load(entity
);
173 } else if (objectClass
.getSuperclass().equals(SpecimenOrObservationBase
.class)){
174 referencedObject
= CdmStore
.getService(IOccurrenceService
.class).load(entity
);
175 } else if (objectClass
.isAssignableFrom(User
.class)){
176 referencedObject
= CdmStore
.getService(IUserService
.class).load(entity
);
177 } else if (objectClass
.isAssignableFrom(Group
.class)){
178 referencedObject
= CdmStore
.getService(IGroupService
.class).load(entity
);
180 //referencedObject =(CdmBase) CdmStore.getService(IIdentifiableEntityService.class).load(referencedObject.getUuid());
181 Set
<CdmBase
> setOfReferencingObjects
= null;
183 if (referencedObject
!= null){
184 referencedObjectTitleCache
= ((IdentifiableEntity
)referencedObject
).getTitleCache();
185 setOfReferencingObjects
= CdmStore
.getService(ICommonService
.class).getReferencingObjects(referencedObject
);
187 if (setOfReferencingObjects
!= null){
188 List
<CdmBase
> referencingObjects
= new ArrayList
<CdmBase
>(setOfReferencingObjects
);
190 Collections
.sort(referencingObjects
, new ReferencingClassComparator());
192 return referencingObjects
;
195 } catch (Exception e
) {
196 logger
.error("Error retrieving referencing objects", e
);
201 class ReferencingClassComparator
implements Comparator
<CdmBase
> {
204 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
206 public int compare(CdmBase o1
, CdmBase o2
) {
207 String string1
= o1
.getClass().getSimpleName();
208 String string2
= o2
.getClass().getSimpleName();
209 int classCompare
= string1
.compareToIgnoreCase(string2
);
210 if (classCompare
== 0) {
211 string1
= DescriptionHelper
.getObjectDescription(o1
);
212 string2
= DescriptionHelper
.getObjectDescription(o2
);
213 return string1
.compareToIgnoreCase(string2
);
223 private void updateView(List
<CdmBase
> referencingObjects
) {
224 if (viewer
!= null && !viewer
.getControl().isDisposed()){
225 viewer
.setInput(referencingObjects
);
226 /*if (referencedObjectTitleCache != null){
227 setContentDescription("'" + referencedObjectTitleCache + "' is referenced by:");
229 setContentDescription("");
236 public void selectionChanged(IWorkbenchPart part
, ISelection selection
) {
237 if(! (part
instanceof BulkEditor
)){
238 setContentDescription("");
243 if(! (selection
instanceof LineSelection
)){
246 LineSelection lineSelection
= (LineSelection
) selection
;
248 if(lineSelection
.size() != 1){
252 if(! (lineSelection
.getFirstElement() instanceof IdentifiableEntity
)){
256 showViewer(part
, lineSelection
);
260 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#showViewer(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
264 public void showViewer(IWorkbenchPart part
, IStructuredSelection selection
) {
267 updateReferencingObjects(((IdentifiableEntity
) selection
.getFirstElement()).getUuid(),selection
.getFirstElement().getClass() );
272 public void dispose() {
273 selectionService
.removePostSelectionListener(this);
274 selectionService
.removeSelectionListener(this);
280 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#getViewer()
284 public Viewer
getViewer() {
289 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#dirtyStateChanged()
293 public void changed(Object object
) {
294 // no editing in this view
298 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#onComplete()
302 public boolean onComplete() {
306 public void refresh(){
307 selectionService
.removePostSelectionListener(this);
308 selectionService
.removeSelectionListener(this);
309 selectionService
.addPostSelectionListener(this);
310 selectionService
.addSelectionListener(this);