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
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.eclipse
.jface
.viewers
.ISelection
;
21 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
22 import org
.eclipse
.jface
.viewers
.TableViewer
;
23 import org
.eclipse
.jface
.viewers
.TableViewerColumn
;
24 import org
.eclipse
.jface
.viewers
.Viewer
;
25 import org
.eclipse
.swt
.SWT
;
26 import org
.eclipse
.swt
.widgets
.Composite
;
27 import org
.eclipse
.swt
.widgets
.Display
;
28 import org
.eclipse
.swt
.widgets
.Table
;
29 import org
.eclipse
.ui
.INullSelectionListener
;
30 import org
.eclipse
.ui
.IWorkbenchPart
;
32 import eu
.etaxonomy
.cdm
.api
.service
.IAgentService
;
33 import eu
.etaxonomy
.cdm
.api
.service
.ICommonService
;
34 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
35 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
36 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
37 import eu
.etaxonomy
.taxeditor
.bulkeditor
.BulkEditor
;
38 import eu
.etaxonomy
.taxeditor
.model
.DescriptionHelper
;
39 import eu
.etaxonomy
.taxeditor
.model
.LineSelection
;
40 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
41 import eu
.etaxonomy
.taxeditor
.view
.AbstractCdmViewPart
;
44 * <p>ReferencingObjectsView class.</p>
50 public class ReferencingObjectsView
extends AbstractCdmViewPart
implements INullSelectionListener
{
51 private static final Logger logger
= Logger
52 .getLogger(ReferencingObjectsView
.class);
54 /** Constant <code>ID="eu.etaxonomy.taxeditor.bulkeditor.refer"{trunked}</code> */
55 public static final String ID
= "eu.etaxonomy.taxeditor.bulkeditor.view.referencingobjects";
57 private TableViewer viewer
;
59 private CdmBase referencedObject
;
62 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#createViewer(org.eclipse.swt.widgets.Composite)
66 public void createViewer(Composite parent
) {
67 selectionService
.addSelectionListener(this);
69 viewer
= new TableViewer(parent
, SWT
.MULTI
| SWT
.H_SCROLL
70 | SWT
.V_SCROLL
| SWT
.FULL_SELECTION
);
71 createColumns(viewer
);
72 viewer
.setContentProvider(new ReferencingObjectsContentProvider());
73 viewer
.setLabelProvider(new ReferencingObjectsLabelProvider());
74 // viewer.setInput(new ArrayList<CdmBase>());
78 * Create the columns for the table
81 private void createColumns(TableViewer viewer
) {
82 Table table
= viewer
.getTable();
83 String
[] titles
= {"Class", "Description", "UUID", "Object ID" };
84 int[] bounds
= { 100, 200, 100, 70};
86 for (int i
= 0; i
< titles
.length
; i
++) {
87 TableViewerColumn column
= new TableViewerColumn(viewer
, SWT
.NONE
);
88 column
.getColumn().setText(titles
[i
]);
89 column
.getColumn().setWidth(bounds
[i
]);
90 column
.getColumn().setResizable(true);
91 column
.getColumn().setMoveable(true);
93 table
.setHeaderVisible(true);
94 table
.setLinesVisible(true);
98 * <p>updateReferencingObjects</p>
100 * @param entity a {@link eu.etaxonomy.cdm.model.common.IdentifiableEntity} object.
102 public void updateReferencingObjects(final IdentifiableEntity entity
) {
104 final Display display
= Display
.getCurrent();
106 //TODO see ticket #4537 (Re-enable asynchronous loading for ReferencingObjectsView)
107 //this is a workaround for #1569
108 //Job job = new Job("Update Referencing Objects") {
111 // protected IStatus run(IProgressMonitor monitor) {
112 // monitor.beginTask("Calculating referencing objects", 10);
114 // monitor.worked(3);
116 // display.asyncExec(new Runnable() {
119 // public void run() {
120 // ConversationHolder conversation = CdmStore.createConversation();
121 List
<CdmBase
> referencingObjects
= getReferencingObjects(entity
);
122 updateView(referencingObjects
);
123 // conversation.close();
130 // return Status.OK_STATUS;
134 // job.setPriority(Job.DECORATE);
138 private List
<CdmBase
> getReferencingObjects(Object entity
) {
140 if(entity
instanceof CdmBase
){
141 referencedObject
= (CdmBase
) entity
;
142 if (referencedObject
instanceof TeamOrPersonBase
){
143 referencedObject
= CdmStore
.getService(IAgentService
.class).load(referencedObject
.getUuid());
145 //referencedObject =(CdmBase) CdmStore.getService(IIdentifiableEntityService.class).load(referencedObject.getUuid());
146 Set
<CdmBase
> setOfReferencingObjects
= CdmStore
.getService(ICommonService
.class).getReferencingObjects(referencedObject
);
147 if (setOfReferencingObjects
!= null){
148 List
<CdmBase
> referencingObjects
= new ArrayList
<CdmBase
>(setOfReferencingObjects
);
150 Collections
.sort(referencingObjects
, new ReferencingClassComparator());
152 return referencingObjects
;
155 } catch (Exception e
) {
156 logger
.error("Error retrieving referencing objects", e
);
161 class ReferencingClassComparator
implements Comparator
<CdmBase
> {
164 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
167 public int compare(CdmBase o1
, CdmBase o2
) {
168 String string1
= o1
.getClass().getSimpleName();
169 String string2
= o2
.getClass().getSimpleName();
170 int classCompare
= string1
.compareToIgnoreCase(string2
);
171 if (classCompare
== 0) {
172 string1
= DescriptionHelper
.getObjectDescription(o1
);
173 string2
= DescriptionHelper
.getObjectDescription(o2
);
174 return string1
.compareToIgnoreCase(string2
);
184 private void updateView(List
<CdmBase
> referencingObjects
) {
185 viewer
.setInput(referencingObjects
);
186 setContentDescription("'" + referencedObject
+ "' is referenced by:");
192 public void selectionChanged(IWorkbenchPart part
, ISelection selection
) {
196 if(! (part
instanceof BulkEditor
)){
197 setContentDescription("");
202 if(! (selection
instanceof LineSelection
)){
205 LineSelection lineSelection
= (LineSelection
) selection
;
207 if(lineSelection
.size() != 1){
211 if(! (lineSelection
.getFirstElement() instanceof IdentifiableEntity
)){
215 showViewer(part
, lineSelection
);
219 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#showViewer(org.eclipse.ui.IWorkbenchPart, org.eclipse.jface.viewers.ISelection)
223 public void showViewer(IWorkbenchPart part
, IStructuredSelection selection
) {
226 updateReferencingObjects((IdentifiableEntity
) selection
.getFirstElement());
231 public void dispose() {
232 selectionService
.removePostSelectionListener(this);
233 selectionService
.removeSelectionListener(this);
239 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#getViewer()
243 public Viewer
getViewer() {
248 * @see eu.etaxonomy.taxeditor.model.AbstractCdmViewPart#dirtyStateChanged()
252 public void changed(Object object
) {
253 // no editing in this view
257 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#onComplete()
261 public boolean onComplete() {
265 public void refresh(){
266 selectionService
.removePostSelectionListener(this);
267 selectionService
.removeSelectionListener(this);
268 selectionService
.addPostSelectionListener(this);
269 selectionService
.addSelectionListener(this);