ref #5348 Wrap text of taxon associations
authorPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 10 Aug 2016 13:15:57 +0000 (15:15 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 10 Aug 2016 13:15:57 +0000 (15:15 +0200)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/occurrence/association/TaxonAssociationDetailElement.java

index 9f472ae16009a5861af00175ab6d7e9a88ca2d38..95dc3613ac66ba117a10df5ae58692a96874bb74 100644 (file)
@@ -13,14 +13,11 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Set;
 
-import org.eclipse.jface.viewers.ArrayContentProvider;
 import org.eclipse.jface.viewers.DoubleClickEvent;
 import org.eclipse.jface.viewers.IDoubleClickListener;
-import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.jface.viewers.LabelProvider;
-import org.eclipse.jface.viewers.ListViewer;
 import org.eclipse.swt.SWT;
+import org.eclipse.swt.custom.StyledText;
 import org.eclipse.swt.dnd.Clipboard;
 import org.eclipse.swt.dnd.TextTransfer;
 import org.eclipse.swt.dnd.Transfer;
@@ -29,6 +26,7 @@ import org.eclipse.swt.events.SelectionListener;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Menu;
 import org.eclipse.swt.widgets.MenuItem;
+import org.eclipse.ui.forms.widgets.TableWrapLayout;
 
 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
@@ -68,72 +66,72 @@ public class TaxonAssociationDetailElement extends AbstractCdmDetailElement<Deri
         associations.addAll(typeDesignations);
         associations.addAll(determinationEvents);
 
+        TableWrapLayout layout = (TableWrapLayout) getLayoutComposite().getLayout();
+        layout.bottomMargin = 0;
+        layout.topMargin = 0;
+
         if(associatedTaxa.isEmpty() && typeDesignations.isEmpty() && determinationEvents.isEmpty()){
             Label label = formFactory.createLabel(getLayoutComposite(), "No associations");
             label.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
             return;
         }
-        if(!associations.isEmpty()){
-               final ListViewer viewer = new ListViewer(getLayoutComposite(), SWT.SINGLE);
-               viewer.getList().setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
-               viewer.setContentProvider(new ArrayContentProvider());
-               viewer.setLabelProvider(new LabelProvider(){
-                       @Override
-                       public String getText(Object element) {
-                               if(element instanceof TaxonBase){
-                                       return "Associated with "+element.toString();
-                               }
-                               else if(element instanceof DeterminationEvent){
-                        DeterminationEvent determinationEvent = (DeterminationEvent)element;
-                        if(determinationEvent.getTaxon()!=null){
-                            return "Determined as taxon "+determinationEvent.getTaxon();
-                        }
-                        if(determinationEvent.getTaxonName()!=null){
-                            return "Determined as name "+determinationEvent.getTaxonName();
-                        }
-                               }
-                               else if(element instanceof SpecimenTypeDesignation){
-                        SpecimenTypeDesignation typeDesignation = (SpecimenTypeDesignation)element;
-                        String label = typeDesignation.getTypeStatus()!=null?typeDesignation.getTypeStatus().getLabel()+" of ":"Type of ";
-                        Set<TaxonNameBase> typifiedNames = typeDesignation.getTypifiedNames();
-                        for (TaxonNameBase taxonNameBase : typifiedNames) {
-                            label += taxonNameBase+", ";
-                        }
-                        if(label.endsWith(", ")){
-                            label = label.substring(0, label.length()-2);
-                        }
-                        return label;
-                               }
-                               return "";
-                       }
-               });
-               viewer.setInput(associations);
-               viewer.addDoubleClickListener(this);
+        for (CdmBase cdmBase : associations) {
+            final StyledText styledTextWidget = new StyledText(getLayoutComposite(), SWT.WRAP);
+            styledTextWidget.setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
+            styledTextWidget.setText(getLabelText(cdmBase));
+            styledTextWidget.setBackground(getLayoutComposite().getBackground());
+            styledTextWidget.setEditable(false);
+            //Set caret null this will hide caret
+            styledTextWidget.setCaret(null);
 
             //TODO add context menu for deleting associations
-               //context menu
-               Menu menu = new Menu(viewer.getControl());
-               MenuItem copyItem = new MenuItem(menu, SWT.PUSH);
-               copyItem.setText("Copy");
-               copyItem.addSelectionListener(new SelectionListener() {
+            //context menu
+            Menu menu = new Menu(styledTextWidget);
+            MenuItem copyItem = new MenuItem(menu, SWT.PUSH);
+            copyItem.setText("Copy");
+            copyItem.addSelectionListener(new SelectionListener() {
 
                 @Override
                 public void widgetSelected(SelectionEvent e) {
-                    ISelection selection = viewer.getSelection();
-                    if(!selection.isEmpty() && selection instanceof IStructuredSelection){
-                        Object firstElement = ((IStructuredSelection) selection).getFirstElement();
-                        Object[] data = new Object[]{firstElement.toString()};
-                        Clipboard clipboard = new Clipboard(viewer.getControl().getDisplay());
-                        clipboard.setContents(data, new Transfer[]{TextTransfer.getInstance()});
-                    }
+                    Object[] data = new Object[]{styledTextWidget.getText()};
+                    Clipboard clipboard = new Clipboard(styledTextWidget.getDisplay());
+                    clipboard.setContents(data, new Transfer[]{TextTransfer.getInstance()});
                 }
 
                 @Override
                 public void widgetDefaultSelected(SelectionEvent e) {
                 }
             });
-               viewer.getControl().setMenu(menu);
+            styledTextWidget.setMenu(menu);
+        }
+    }
+
+    private String getLabelText(Object element) {
+        if(element instanceof TaxonBase){
+            return "Associated with "+element.toString();
+        }
+        else if(element instanceof DeterminationEvent){
+            DeterminationEvent determinationEvent = (DeterminationEvent)element;
+            if(determinationEvent.getTaxon()!=null){
+                return "Determined as taxon "+determinationEvent.getTaxon();
+            }
+            if(determinationEvent.getTaxonName()!=null){
+                return "Determined as name "+determinationEvent.getTaxonName();
+            }
+        }
+        else if(element instanceof SpecimenTypeDesignation){
+            SpecimenTypeDesignation typeDesignation = (SpecimenTypeDesignation)element;
+            String label = typeDesignation.getTypeStatus()!=null?typeDesignation.getTypeStatus().getLabel()+" of ":"Type of ";
+            Set<TaxonNameBase> typifiedNames = typeDesignation.getTypifiedNames();
+            for (TaxonNameBase taxonNameBase : typifiedNames) {
+                label += taxonNameBase+", ";
+            }
+            if(label.endsWith(", ")){
+                label = label.substring(0, label.length()-2);
+            }
+            return label;
         }
+        return "";
     }
 
     /** {@inheritDoc} */