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
.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
;
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
;
72 public class ReferencingObjectsView
extends AbstractCdmViewPart
{
73 private static final Logger logger
= Logger
74 .getLogger(ReferencingObjectsView
.class);
76 public static final String ID
= "eu.etaxonomy.taxeditor.bulkeditor.view.referencingobjects";
78 private TableViewer viewer
;
80 private String referencedObjectTitleCache
;
81 private ConversationHolder conversation
;
82 private ICdmEntitySession cdmEntitySession
;
84 public ReferencingObjectsView() {
85 cdmEntitySession
= CdmStore
.getCurrentSessionManager().bindNullSession();
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() {
100 public void modify(Object element
, String property
, Object value
) {
104 public Object
getValue(Object element
, String property
) {
105 ITableLabelProvider tableLabelProvider
= null;
106 if(viewer
.getLabelProvider() instanceof ITableLabelProvider
){
107 tableLabelProvider
= (ITableLabelProvider
) viewer
.getLabelProvider();
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
);
119 public boolean canModify(Object element
, String property
) {
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
);
131 // Propagate selection from viewer
132 getSite().setSelectionProvider(viewer
);
136 * Create the columns for the table
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};
144 CellEditor
[] editors
= new CellEditor
[titles
.length
];
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);
154 table
.setHeaderVisible(true);
155 table
.setLinesVisible(true);
157 viewer
.setCellEditors(editors
);
158 viewer
.setColumnProperties(titles
);
162 public void setFocus() {
163 cdmEntitySession
.bind();
166 public void updateReferencingObjects(final UUID entityUUID
, final Class objectClass
) {
168 final Display display
= Display
.getCurrent();
170 Job job
= new Job("Update Referencing Objects") {
173 protected IStatus
run(IProgressMonitor monitor
) {
174 monitor
.beginTask("Calculating referencing objects", 10);
178 display
.asyncExec(new Runnable() {
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();
189 updateView(referencingObjects
);
198 return Status
.OK_STATUS
;
202 job
.setPriority(Job
.DECORATE
);
206 private List
<CdmBase
> getReferencingObjects(UUID entity
, Class objectClass
) {
207 CdmBase referencedObject
= null;
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
);
224 //referencedObject =(CdmBase) CdmStore.getService(IIdentifiableEntityService.class).load(referencedObject.getUuid());
225 Set
<CdmBase
> setOfReferencingObjects
= null;
227 if (referencedObject
!= null){
228 referencedObjectTitleCache
= ((IdentifiableEntity
)referencedObject
).getTitleCache();
229 setOfReferencingObjects
= CdmStore
.getCommonService().getReferencingObjects(referencedObject
);
231 if (setOfReferencingObjects
!= null){
232 List
<CdmBase
> referencingObjects
= new ArrayList
<CdmBase
>(setOfReferencingObjects
);
234 Collections
.sort(referencingObjects
, new ReferencingClassComparator());
236 return referencingObjects
;
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.");
246 class ReferencingClassComparator
implements Comparator
<CdmBase
> {
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
);
263 private void updateView(List
<CdmBase
> referencingObjects
) {
264 if (viewer
!= null && !viewer
.getControl().isDisposed()){
266 viewer
.setInput(referencingObjects
);
267 if (referencedObjectTitleCache
!= null){
268 setContentDescription("'" + referencedObjectTitleCache
+ "' is referenced by:");
270 setContentDescription("");
274 setContentDescription("The referencing objects view could not be loaded completely. Some Problems occured.");
282 public void selectionChanged(IWorkbenchPart part
, ISelection selection
) {
286 if(! (part
instanceof BulkEditor
)){
287 setContentDescription("");
292 if(! (selection
instanceof LineSelection
)){
295 LineSelection lineSelection
= (LineSelection
) selection
;
297 if(lineSelection
.size() != 1){
301 if(! (lineSelection
.getFirstElement() instanceof IdentifiableEntity
)){
305 showViewer(part
, lineSelection
);
310 public void showViewer(IWorkbenchPart part
, IStructuredSelection selection
) {
313 updateReferencingObjects(((IdentifiableEntity
) selection
.getFirstElement()).getUuid(),selection
.getFirstElement().getClass() );
318 public void dispose() {
319 conversation
.close();
320 selectionService
.removePostSelectionListener(this);
321 selectionService
.removeSelectionListener(this);
322 cdmEntitySession
.dispose();
329 public Viewer
getViewer() {
335 public void changed(Object object
) {
336 // no editing in this view
341 public boolean onComplete() {
345 public void refresh(){
346 selectionService
.removePostSelectionListener(this);
347 selectionService
.removeSelectionListener(this);
348 selectionService
.addPostSelectionListener(this);
349 selectionService
.addSelectionListener(this);