ref #6190 removing svn property place holder in first line of code - java files
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / occurrence / association / TaxonAssociationDetailElement.java
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.TaxonNameBase;
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 Collection<TaxonBase<?>> associatedTaxa = CdmStore.getService(IOccurrenceService.class).listIndividualsAssociationTaxa(entity.innerDerivedUnit(), null, null, null, null);
61 Collection<SpecimenTypeDesignation> typeDesignations = CdmStore.getService(IOccurrenceService.class).listTypeDesignations(entity.innerDerivedUnit(), null, null, null, null);
62 Collection<DeterminationEvent> determinationEvents = CdmStore.getService(IOccurrenceService.class).listDeterminationEvents(entity.innerDerivedUnit(), null, null, null, null);
63
64 Collection<CdmBase> associations = new ArrayList<>();
65 associations.addAll(associatedTaxa);
66 associations.addAll(typeDesignations);
67 associations.addAll(determinationEvents);
68
69 TableWrapLayout layout = (TableWrapLayout) getLayoutComposite().getLayout();
70 layout.bottomMargin = 0;
71 layout.topMargin = 0;
72
73 if(associatedTaxa.isEmpty() && typeDesignations.isEmpty() && determinationEvents.isEmpty()){
74 Label label = formFactory.createLabel(getLayoutComposite(), "No associations");
75 label.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
76 return;
77 }
78 for (CdmBase cdmBase : associations) {
79 final StyledText styledTextWidget = new StyledText(getLayoutComposite(), SWT.WRAP);
80 styledTextWidget.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
81 styledTextWidget.setText(getLabelText(cdmBase));
82 styledTextWidget.setBackground(getLayoutComposite().getBackground());
83 styledTextWidget.setEditable(false);
84 //Set caret null this will hide caret
85 styledTextWidget.setCaret(null);
86
87 //TODO add context menu for deleting associations
88 //context menu
89 Menu menu = new Menu(styledTextWidget);
90 //copy menu
91 MenuItem copyItem = new MenuItem(menu, SWT.PUSH);
92 copyItem.setText("Copy");
93 copyItem.addSelectionListener(new SelectionListener() {
94
95 @Override
96 public void widgetSelected(SelectionEvent e) {
97 Object[] data = new Object[]{styledTextWidget.getText()};
98 Clipboard clipboard = new Clipboard(styledTextWidget.getDisplay());
99 clipboard.setContents(data, new Transfer[]{TextTransfer.getInstance()});
100 }
101 @Override
102 public void widgetDefaultSelected(SelectionEvent e) {
103 }
104 });
105 //"Open in..." menu
106 MenuItem openInItem = new MenuItem(menu, SWT.PUSH);
107 openInItem.setText("Open");
108 if(cdmBase.isInstanceOf(SpecimenTypeDesignation.class)){
109 openInItem.setData(HibernateProxyHelper.deproxy(cdmBase, SpecimenTypeDesignation.class));
110 }
111 openInItem.setData(cdmBase);
112 openInItem.addSelectionListener(new SelectionListener() {
113
114 @Override
115 public void widgetSelected(SelectionEvent e) {
116 new CdmViewerChooser(TaxonAssociationDetailElement.this.getLayoutComposite().getShell()).chooseViewer(e.widget.getData());
117 }
118 @Override
119 public void widgetDefaultSelected(SelectionEvent e) {
120 }
121 });
122 styledTextWidget.setMenu(menu);
123 }
124 }
125
126 private String getLabelText(Object element) {
127 if(element instanceof TaxonBase){
128 return "Associated with "+element.toString();
129 }
130 else if(element instanceof DeterminationEvent){
131 DeterminationEvent determinationEvent = (DeterminationEvent)element;
132 if(determinationEvent.getTaxon()!=null){
133 return "Determined as taxon "+determinationEvent.getTaxon();
134 }
135 if(determinationEvent.getTaxonName()!=null){
136 return "Determined as name "+determinationEvent.getTaxonName();
137 }
138 }
139 else if(element instanceof SpecimenTypeDesignation){
140 SpecimenTypeDesignation typeDesignation = (SpecimenTypeDesignation)element;
141 String label = typeDesignation.getTypeStatus()!=null?typeDesignation.getTypeStatus().getLabel()+" of ":"Type of ";
142 Set<TaxonNameBase> typifiedNames = typeDesignation.getTypifiedNames();
143 for (TaxonNameBase taxonNameBase : typifiedNames) {
144 label += taxonNameBase+", ";
145 }
146 if(label.endsWith(", ")){
147 label = label.substring(0, label.length()-2);
148 }
149 return label;
150 }
151 return "";
152 }
153
154 /** {@inheritDoc} */
155 @Override
156 public void handleEvent(Object eventSource) {
157 //empty
158 }
159
160 @Override
161 public void doubleClick(DoubleClickEvent event) {
162 if(event.getSelection() instanceof IStructuredSelection){
163 Object firstElement = ((IStructuredSelection) event.getSelection()).getFirstElement();
164 if(firstElement instanceof SpecimenTypeDesignation){
165 SpecimenTypeDesignation typeDesignation = (SpecimenTypeDesignation)firstElement;
166 Set<TaxonNameBase> typifiedNames = typeDesignation.getTypifiedNames();
167 if(typifiedNames.size()==1){
168 firstElement = typifiedNames.iterator().next();
169 }
170 if(typifiedNames.size()==0 || typifiedNames.size()>=2){
171 MessagingUtils.informationDialog("Could not open name", "Type designation has either 0 or more than 1 names associated");
172 }
173 }
174 else if(firstElement instanceof DeterminationEvent){
175 if(((DeterminationEvent) firstElement).getTaxon()!=null){
176 firstElement = ((DeterminationEvent) firstElement).getTaxon();
177 }
178 else if(((DeterminationEvent) firstElement).getTaxonName()!=null){
179 firstElement = ((DeterminationEvent) firstElement).getTaxonName();
180 }
181 }
182 new CdmViewerChooser(event.getViewer().getControl().getShell()).chooseViewer(firstElement);
183 }
184 }
185 }