fix #5348 Add "copy" context menu to taxon association details view
authorPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 10 Aug 2016 11:08:00 +0000 (13:08 +0200)
committerPatrick Plitzner <p.plitzner@bgbm.org>
Wed, 10 Aug 2016 11:08:00 +0000 (13:08 +0200)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/section/occurrence/association/TaxonAssociationDetailElement.java

index 34da746194fc0e6ea975b6606d2b577b1747c203..9f472ae16009a5861af00175ab6d7e9a88ca2d38 100644 (file)
@@ -13,18 +13,22 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Set;
 
 import java.util.Collection;
 import java.util.Set;
 
-import org.eclipse.jface.action.MenuManager;
 import org.eclipse.jface.viewers.ArrayContentProvider;
 import org.eclipse.jface.viewers.DoubleClickEvent;
 import org.eclipse.jface.viewers.IDoubleClickListener;
 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.jface.viewers.IStructuredSelection;
 import org.eclipse.jface.viewers.LabelProvider;
 import org.eclipse.jface.viewers.ListViewer;
 import org.eclipse.swt.SWT;
-import org.eclipse.swt.widgets.Control;
+import org.eclipse.swt.dnd.Clipboard;
+import org.eclipse.swt.dnd.TextTransfer;
+import org.eclipse.swt.dnd.Transfer;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.events.SelectionListener;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Menu;
 import org.eclipse.swt.widgets.Label;
 import org.eclipse.swt.widgets.Menu;
-import org.eclipse.ui.handlers.HandlerUtil;
+import org.eclipse.swt.widgets.MenuItem;
 
 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
 
 import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
@@ -55,9 +59,6 @@ public class TaxonAssociationDetailElement extends AbstractCdmDetailElement<Deri
     /** {@inheritDoc} */
     @Override
     protected void createControls(ICdmFormElement formElement, DerivedUnitFacade entity, int style) {
     /** {@inheritDoc} */
     @Override
     protected void createControls(ICdmFormElement formElement, DerivedUnitFacade entity, int style) {
-
-        //TODO add context menu for deleting associations
-
         Collection<TaxonBase<?>> associatedTaxa = CdmStore.getService(IOccurrenceService.class).listIndividualsAssociationTaxa(entity.innerDerivedUnit(), null, null, null, null);
         Collection<SpecimenTypeDesignation> typeDesignations = CdmStore.getService(IOccurrenceService.class).listTypeDesignations(entity.innerDerivedUnit(), null, null, null, null);
         Collection<DeterminationEvent> determinationEvents = CdmStore.getService(IOccurrenceService.class).listDeterminationEvents(entity.innerDerivedUnit(), null, null, null, null);
         Collection<TaxonBase<?>> associatedTaxa = CdmStore.getService(IOccurrenceService.class).listIndividualsAssociationTaxa(entity.innerDerivedUnit(), null, null, null, null);
         Collection<SpecimenTypeDesignation> typeDesignations = CdmStore.getService(IOccurrenceService.class).listTypeDesignations(entity.innerDerivedUnit(), null, null, null, null);
         Collection<DeterminationEvent> determinationEvents = CdmStore.getService(IOccurrenceService.class).listDeterminationEvents(entity.innerDerivedUnit(), null, null, null, null);
@@ -66,14 +67,14 @@ public class TaxonAssociationDetailElement extends AbstractCdmDetailElement<Deri
         associations.addAll(associatedTaxa);
         associations.addAll(typeDesignations);
         associations.addAll(determinationEvents);
         associations.addAll(associatedTaxa);
         associations.addAll(typeDesignations);
         associations.addAll(determinationEvents);
-        
+
         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()){
         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()){
-               ListViewer viewer = new ListViewer(getLayoutComposite(), SWT.SINGLE);
+               final ListViewer viewer = new ListViewer(getLayoutComposite(), SWT.SINGLE);
                viewer.getList().setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
                viewer.setContentProvider(new ArrayContentProvider());
                viewer.setLabelProvider(new LabelProvider(){
                viewer.getList().setLayoutData(LayoutConstants.FILL_HORIZONTALLY(2, 1));
                viewer.setContentProvider(new ArrayContentProvider());
                viewer.setLabelProvider(new LabelProvider(){
@@ -108,6 +109,30 @@ public class TaxonAssociationDetailElement extends AbstractCdmDetailElement<Deri
                });
                viewer.setInput(associations);
                viewer.addDoubleClickListener(this);
                });
                viewer.setInput(associations);
                viewer.addDoubleClickListener(this);
+
+            //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() {
+
+                @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()});
+                    }
+                }
+
+                @Override
+                public void widgetDefaultSelected(SelectionEvent e) {
+                }
+            });
+               viewer.getControl().setMenu(menu);
         }
     }
 
         }
     }