ref #7230: refresh of name editor and details view after changing taxon of misapplie...
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / e4 / details / DetailsPartE4.java
index a0e320efe62c0bfdffb3991b953c18d11475f017..284496a5768f2821a9871d7b461d53bdbc5b7852 100644 (file)
 package eu.etaxonomy.taxeditor.view.e4.details;
 
 import javax.annotation.PostConstruct;
-import javax.annotation.PreDestroy;
 import javax.inject.Inject;
-import javax.inject.Named;
 
-import org.eclipse.e4.core.di.annotations.Optional;
+import org.eclipse.e4.core.contexts.ContextInjectionFactory;
+import org.eclipse.e4.core.contexts.IEclipseContext;
+import org.eclipse.e4.core.services.log.Logger;
 import org.eclipse.e4.ui.model.application.ui.basic.MPart;
-import org.eclipse.e4.ui.services.IServiceConstants;
 import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.jface.viewers.StructuredSelection;
-import org.eclipse.jface.viewers.Viewer;
+import org.eclipse.swt.SWTException;
 import org.eclipse.swt.widgets.Composite;
-import org.eclipse.ui.IEditorPart;
 
-import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
 import eu.etaxonomy.cdm.model.description.Feature;
-import eu.etaxonomy.cdm.persistence.hibernate.CdmDataChangeMap;
 import eu.etaxonomy.taxeditor.l10n.Messages;
 import eu.etaxonomy.taxeditor.model.AbstractUtility;
 import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
-import eu.etaxonomy.taxeditor.model.IDirtyMarkable;
 import eu.etaxonomy.taxeditor.model.IPartContentHasDetails;
-import eu.etaxonomy.taxeditor.workbench.WorkbenchUtility;
+import eu.etaxonomy.taxeditor.model.IPartContentHasSupplementalData;
+import eu.etaxonomy.taxeditor.model.MessagingUtils;
+import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
+import eu.etaxonomy.taxeditor.view.e4.AbstractCdmEditorPartE4;
+import eu.etaxonomy.taxeditor.view.e4.supplementaldata.SupplementalDataPartE4;
+import eu.etaxonomy.taxeditor.workbench.part.ISelectionElementEditingPart;
 
 /**
  * @author pplitzner
  * @date 18.07.2017
  *
  */
-public class DetailsPartE4 implements IConversationEnabled, IDirtyMarkable{
-
-    private DetailsViewerE4 viewer;
-
-    private MPart selectionProvidingPart;
-
-    @Inject
+public class DetailsPartE4 extends AbstractCdmEditorPartE4 implements IPartContentHasSupplementalData {
+       @Inject
+       private Logger logger;
+    
+       @Inject
     public DetailsPartE4() {
     }
 
     @PostConstruct
-    public void create(Composite parent) {
-        viewer = new DetailsViewerE4(parent, this);
+    public void create(Composite parent, MPart thisPart, IEclipseContext context) {
+
+        this.thisPart = thisPart;
+
+        viewer = ContextInjectionFactory.make(DetailsViewerE4.class, context);
+        ((DetailsViewerE4)viewer).init(parent, this);
+
+        // Propagate selection from viewer
+        selectionChangedListener = (event -> selService.setSelection(AbstractUtility.getElementsFromSelectionChangedEvent(event)));
+        viewer.addSelectionChangedListener(selectionChangedListener);
     }
 
-    @Inject
-    @Optional
-    public void selectionChanged(
-            @Named(IServiceConstants.ACTIVE_SELECTION)Object selection,
-            @Named(IServiceConstants.ACTIVE_PART)MPart activePart,
-            MPart thisPart){
-        if(activePart==thisPart){
-            return;
-        }
-        if(selection==null){
-            showEmptyPage();
+    @Override
+    public void selectionChanged_internal(Object selection, MPart activePart, MPart thisPart) {
+        if (activePart==thisPart){
             return;
         }
 
-        Object partObject = activePart;
-        Object wrappedPart = WorkbenchUtility.getE4WrappedPart(activePart);
-        if(wrappedPart!=null){
-            partObject = wrappedPart;
-        }
-
-        IStructuredSelection structuredSelection;
-        if(!(selection instanceof IStructuredSelection)){
-            structuredSelection = new StructuredSelection(selection);
-        }
-        else{
-            structuredSelection = (IStructuredSelection) selection;
-        }
-        if(structuredSelection.isEmpty()){
-            showEmptyPage();
+        Object partObject = createPartObject(activePart);
+        if (partObject instanceof SupplementalDataPartE4) {
+            // do not show empty page
             return;
         }
-        if((partObject instanceof IEditorPart) || (partObject instanceof IPartContentHasDetails)
-                && partObject instanceof IConversationEnabled) {
+
+        if(partObject instanceof IPartContentHasDetails){
+            IStructuredSelection structuredSelection = createSelection(selection);
+            if(structuredSelection==null || structuredSelection.isEmpty()){
+                showEmptyPage();
+                return;
+            }
+            if(!(partObject instanceof ISelectionElementEditingPart) &&
+                    partObject instanceof IConversationEnabled && ((IConversationEnabled) partObject).getConversationHolder()==null) {
+                //TODO show specific message (refactor EmptyElement to allow specific messages)
+                showEmptyPage();
+                return;
+            }
             // do not show details for feature nodes TODO strange check to avoid having shown the distribution map for every FeatureNodeContainer
             if(structuredSelection.getFirstElement() instanceof FeatureNodeContainer){
                 // do show the map for distributions
-                Feature feature = ((FeatureNodeContainer) ((IStructuredSelection) selection).getFirstElement()).getFeature();
+                Feature feature = ((FeatureNodeContainer) structuredSelection.getFirstElement()).getFeature();
                 if(!feature.equals(Feature.DISTRIBUTION())){
                     showEmptyPage();
                     return;
                 }
             }
-            showViewer(structuredSelection);
-            selectionProvidingPart = activePart;
+//            if (partObject instanceof IDistributionEditor){
+//
+//                showViewer(structuredSelection, activePart, viewer);
+//                return;
+//
+//            }
+            //FIXME this is a temporary workaround to fix selection handling for supplemental data view
+            // Now the supp data view gets double selection from details view and name editor (see #7126)
+            viewer.setSelection(structuredSelection, false);
+            showViewer(structuredSelection, activePart, viewer);
+            return;
         }
         else{
             showEmptyPage();
@@ -105,76 +110,29 @@ public class DetailsPartE4 implements IConversationEnabled, IDirtyMarkable{
         }
     }
 
-    public void showEmptyPage() {
-        if(viewer!=null){
-            viewer.showEmptyPage();
-        }
-        selectionProvidingPart = null;
-    }
-
-    protected String getViewName(){
-        return Messages.DetailsViewPart_VIEWER_NAME;
-    }
-
-    public Viewer getViewer() {
-        return viewer;
-    }
-
-
-    @PreDestroy
-    public void dispose() {
-    }
-
-    public void showViewer(IStructuredSelection selection){
-        if(getViewer()!=null){
-            Object element = selection.getFirstElement();
-            if(selection.getFirstElement()!=null){
-                getViewer().setInput(element);
-            }
-        }
-    }
-
-    /**
-     * {@inheritDoc}
-     */
     @Override
-    public void update(CdmDataChangeMap arg0) {
-        // TODO Auto-generated method stub
+    protected void showEmptyPage() {
+        super.showEmptyPage();
+        if(viewer!=null && viewer.getControl()!=null && !viewer.getControl().isDisposed() ){
+               try{
+                       ((DetailsViewerE4)viewer).destroySections();
+               }catch(SWTException e){
+                       if (PreferencesUtil.isShowUpWidgetIsDisposedMessages() && e.getMessage().equals("Widget is disposed")){
+                    MessagingUtils.errorDialog("Widget is disposed",
+                            null,
+                            MessagingUtils.WIDGET_IS_DISPOSED_MESSAGE,
+                            null,
+                            e,
+                            true);
 
-    }
-
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public ConversationHolder getConversationHolder() {
-        if(selectionProvidingPart != null && selectionProvidingPart instanceof IConversationEnabled) {
-            return ((IConversationEnabled) selectionProvidingPart).getConversationHolder();
+                }
+               }
         }
-        return null;
     }
 
-    /** {@inheritDoc} */
     @Override
-    public void changed(Object object) {
-        Object part = selectionProvidingPart.getObject();
-        if(part instanceof IDirtyMarkable){
-            ((IDirtyMarkable) part).changed(object);
-        }
-        else {
-            IEditorPart editor = AbstractUtility.getActiveEditor();
-            if (editor != null && editor instanceof IDirtyMarkable) {
-                ((IDirtyMarkable) editor).changed(object);
-            }
-        }
+    protected String getViewName(){
+        return Messages.DetailsViewPart_VIEWER_NAME;
     }
 
-    /**
-     * {@inheritDoc}
-     */
-    @Override
-    public void forceDirty() {
-        // TODO Auto-generated method stub
-
-    }
 }