1
|
/**
|
2
|
* Copyright (C) 2014 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.taxeditor.ui.section.occurrence.association;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Collection;
|
13
|
import java.util.Set;
|
14
|
|
15
|
import org.eclipse.jface.viewers.DoubleClickEvent;
|
16
|
import org.eclipse.jface.viewers.IDoubleClickListener;
|
17
|
import org.eclipse.jface.viewers.IStructuredSelection;
|
18
|
import org.eclipse.swt.SWT;
|
19
|
import org.eclipse.swt.custom.StyledText;
|
20
|
import org.eclipse.swt.dnd.Clipboard;
|
21
|
import org.eclipse.swt.dnd.TextTransfer;
|
22
|
import org.eclipse.swt.dnd.Transfer;
|
23
|
import org.eclipse.swt.events.SelectionEvent;
|
24
|
import org.eclipse.swt.events.SelectionListener;
|
25
|
import org.eclipse.swt.widgets.Label;
|
26
|
import org.eclipse.swt.widgets.Menu;
|
27
|
import org.eclipse.swt.widgets.MenuItem;
|
28
|
import org.eclipse.ui.forms.widgets.TableWrapLayout;
|
29
|
|
30
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
|
31
|
import eu.etaxonomy.cdm.api.service.IOccurrenceService;
|
32
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
33
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
34
|
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
|
35
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
36
|
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
|
37
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
38
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
39
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
40
|
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
|
41
|
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
|
42
|
import eu.etaxonomy.taxeditor.ui.element.LayoutConstants;
|
43
|
import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
|
44
|
import eu.etaxonomy.taxeditor.view.CdmViewerChooser;
|
45
|
|
46
|
/**
|
47
|
* @author pplitzner
|
48
|
* @date Dec 1, 2014
|
49
|
*
|
50
|
*/
|
51
|
public class TaxonAssociationDetailElement extends AbstractCdmDetailElement<DerivedUnitFacade> implements IDoubleClickListener{
|
52
|
|
53
|
public TaxonAssociationDetailElement(CdmFormFactory formFactory, ICdmFormElement formElement) {
|
54
|
super(formFactory, formElement);
|
55
|
}
|
56
|
|
57
|
/** {@inheritDoc} */
|
58
|
@Override
|
59
|
protected void createControls(ICdmFormElement formElement, DerivedUnitFacade entity, int style) {
|
60
|
//FIXME constructor code is just copied to update method
|
61
|
//this has to be separated so that the elements are only once created in the constructor
|
62
|
//and updated in the update() method
|
63
|
this.update(entity);
|
64
|
}
|
65
|
|
66
|
@Override
|
67
|
protected void update(DerivedUnitFacade entity) {
|
68
|
super.update(entity);
|
69
|
Collection<TaxonBase<?>> associatedTaxa = CdmStore.getService(IOccurrenceService.class).listIndividualsAssociationTaxa(entity.innerDerivedUnit(), null, null, null, null);
|
70
|
Collection<SpecimenTypeDesignation> typeDesignations = CdmStore.getService(IOccurrenceService.class).listTypeDesignations(entity.innerDerivedUnit(), null, null, null, null);
|
71
|
Collection<DeterminationEvent> determinationEvents = CdmStore.getService(IOccurrenceService.class).listDeterminationEvents(entity.innerDerivedUnit(), null, null, null, null);
|
72
|
|
73
|
Collection<CdmBase> associations = new ArrayList<>();
|
74
|
associations.addAll(associatedTaxa);
|
75
|
associations.addAll(typeDesignations);
|
76
|
associations.addAll(determinationEvents);
|
77
|
|
78
|
TableWrapLayout layout = (TableWrapLayout) getLayoutComposite().getLayout();
|
79
|
layout.bottomMargin = 0;
|
80
|
layout.topMargin = 0;
|
81
|
|
82
|
if(associatedTaxa.isEmpty() && typeDesignations.isEmpty() && determinationEvents.isEmpty()){
|
83
|
Label label = formFactory.createLabel(getLayoutComposite(), "No associations");
|
84
|
label.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
|
85
|
return;
|
86
|
}
|
87
|
for (CdmBase cdmBase : associations) {
|
88
|
final StyledText styledTextWidget = new StyledText(getLayoutComposite(), SWT.WRAP);
|
89
|
styledTextWidget.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
|
90
|
styledTextWidget.setText(getLabelText(cdmBase));
|
91
|
styledTextWidget.setBackground(getLayoutComposite().getBackground());
|
92
|
styledTextWidget.setEditable(false);
|
93
|
//Set caret null this will hide caret
|
94
|
styledTextWidget.setCaret(null);
|
95
|
|
96
|
//TODO add context menu for deleting associations
|
97
|
//context menu
|
98
|
Menu menu = new Menu(styledTextWidget);
|
99
|
//copy menu
|
100
|
MenuItem copyItem = new MenuItem(menu, SWT.PUSH);
|
101
|
copyItem.setText("Copy");
|
102
|
copyItem.addSelectionListener(new SelectionListener() {
|
103
|
|
104
|
@Override
|
105
|
public void widgetSelected(SelectionEvent e) {
|
106
|
Object[] data = new Object[]{styledTextWidget.getText()};
|
107
|
Clipboard clipboard = new Clipboard(styledTextWidget.getDisplay());
|
108
|
clipboard.setContents(data, new Transfer[]{TextTransfer.getInstance()});
|
109
|
}
|
110
|
@Override
|
111
|
public void widgetDefaultSelected(SelectionEvent e) {
|
112
|
}
|
113
|
});
|
114
|
//"Open in..." menu
|
115
|
MenuItem openInItem = new MenuItem(menu, SWT.PUSH);
|
116
|
openInItem.setText("Open");
|
117
|
if(cdmBase.isInstanceOf(SpecimenTypeDesignation.class)){
|
118
|
openInItem.setData(HibernateProxyHelper.deproxy(cdmBase, SpecimenTypeDesignation.class));
|
119
|
}
|
120
|
openInItem.setData(cdmBase);
|
121
|
openInItem.addSelectionListener(new SelectionListener() {
|
122
|
|
123
|
@Override
|
124
|
public void widgetSelected(SelectionEvent e) {
|
125
|
new CdmViewerChooser(TaxonAssociationDetailElement.this.getLayoutComposite().getShell()).chooseViewer(e.widget.getData());
|
126
|
}
|
127
|
@Override
|
128
|
public void widgetDefaultSelected(SelectionEvent e) {
|
129
|
}
|
130
|
});
|
131
|
styledTextWidget.setMenu(menu);
|
132
|
}
|
133
|
}
|
134
|
|
135
|
private String getLabelText(Object element) {
|
136
|
if(element instanceof TaxonBase){
|
137
|
return "Associated with "+element.toString();
|
138
|
}
|
139
|
else if(element instanceof DeterminationEvent){
|
140
|
DeterminationEvent determinationEvent = (DeterminationEvent)element;
|
141
|
if(determinationEvent.getTaxon()!=null){
|
142
|
return "Determined as taxon "+determinationEvent.getTaxon();
|
143
|
}
|
144
|
if(determinationEvent.getTaxonName()!=null){
|
145
|
return "Determined as name "+determinationEvent.getTaxonName();
|
146
|
}
|
147
|
}
|
148
|
else if(element instanceof SpecimenTypeDesignation){
|
149
|
SpecimenTypeDesignation typeDesignation = (SpecimenTypeDesignation)element;
|
150
|
String label = typeDesignation.getTypeStatus()!=null?typeDesignation.getTypeStatus().getLabel()+" of ":"Type of ";
|
151
|
Set<TaxonName> typifiedNames = typeDesignation.getTypifiedNames();
|
152
|
for (TaxonName taxonNameBase : typifiedNames) {
|
153
|
label += taxonNameBase+", ";
|
154
|
}
|
155
|
if(label.endsWith(", ")){
|
156
|
label = label.substring(0, label.length()-2);
|
157
|
}
|
158
|
return label;
|
159
|
}
|
160
|
return "";
|
161
|
}
|
162
|
|
163
|
/** {@inheritDoc} */
|
164
|
@Override
|
165
|
public void handleEvent(Object eventSource) {
|
166
|
//empty
|
167
|
}
|
168
|
|
169
|
@Override
|
170
|
public void doubleClick(DoubleClickEvent event) {
|
171
|
if(event.getSelection() instanceof IStructuredSelection){
|
172
|
Object firstElement = ((IStructuredSelection) event.getSelection()).getFirstElement();
|
173
|
if(firstElement instanceof SpecimenTypeDesignation){
|
174
|
SpecimenTypeDesignation typeDesignation = (SpecimenTypeDesignation)firstElement;
|
175
|
Set<TaxonName> typifiedNames = typeDesignation.getTypifiedNames();
|
176
|
if(typifiedNames.size()==1){
|
177
|
firstElement = typifiedNames.iterator().next();
|
178
|
}
|
179
|
if(typifiedNames.size()==0 || typifiedNames.size()>=2){
|
180
|
MessagingUtils.informationDialog("Could not open name", "Type designation has either 0 or more than 1 names associated");
|
181
|
}
|
182
|
}
|
183
|
else if(firstElement instanceof DeterminationEvent){
|
184
|
if(((DeterminationEvent) firstElement).getTaxon()!=null){
|
185
|
firstElement = ((DeterminationEvent) firstElement).getTaxon();
|
186
|
}
|
187
|
else if(((DeterminationEvent) firstElement).getTaxonName()!=null){
|
188
|
firstElement = ((DeterminationEvent) firstElement).getTaxonName();
|
189
|
}
|
190
|
}
|
191
|
new CdmViewerChooser(event.getViewer().getControl().getShell()).chooseViewer(firstElement);
|
192
|
}
|
193
|
}
|
194
|
}
|