]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.views.swt.client/src/org/simantics/views/swt/client/impl/SWTExplorer.java
Usability fixes for GraphExplorerImpl -related WB selection propagation
[simantics/platform.git] / bundles / org.simantics.views.swt.client / src / org / simantics / views / swt / client / impl / SWTExplorer.java
index 1666413e9c56d45ee16e8afeda3baf3e07e17446..e196e673267ddaf8d7a17ad6fc78b10c7eda78c2 100644 (file)
@@ -10,11 +10,8 @@ import org.eclipse.jface.viewers.IPostSelectionProvider;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.ISelectionChangedListener;
 import org.eclipse.jface.viewers.ISelectionProvider;
-import org.eclipse.jface.viewers.SelectionChangedEvent;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Composite;
-import org.eclipse.swt.widgets.Event;
-import org.eclipse.swt.widgets.Listener;
 import org.eclipse.ui.IWorkbenchSite;
 import org.simantics.browsing.ui.Column;
 import org.simantics.browsing.ui.StatePersistor;
@@ -45,9 +42,19 @@ public class SWTExplorer extends SingleSWTViewNode<ModelBrowser> {
        public Boolean useNodeBrowseContexts;
        public Boolean useNodeActionContexts;
        public StatePersistor persistor;
-       
+
        public ISelection lastSelection;
-       
+
+       private ISelectionChangedListener internalToExternalSelectionPropagator = event -> {
+               IWorkbenchSite site = getSite();
+               if (site != null) {
+                       ISelectionProvider sp = site.getSelectionProvider();
+                       if (sp != null) {
+                               sp.setSelection(event.getSelection());
+                       }
+               }
+       };
+
        @Override
        public void createControls(Composite parent) {
 
@@ -70,80 +77,30 @@ public class SWTExplorer extends SingleSWTViewNode<ModelBrowser> {
                if (uiContext != null) {
                        control.setUiContexts(Collections.singleton(uiContext));
                }
-               
+
                control.setStatePersistor(persistor);
-               
+
                control.finish();
 
                setProperties();
-               
+
                control.setInputSource(PassThruInputSource.INSTANCE);
 
-               ISelectionProvider sp = (ISelectionProvider)control.getExplorer().getAdapter(ISelectionProvider.class);
-               sp.addSelectionChangedListener(new ISelectionChangedListener() {
-                       
-                       @Override
-                       public void selectionChanged(SelectionChangedEvent event) {
-                               if (site != null) {
-                                       ISelectionProvider sp = site.getSelectionProvider();
-                                       if (sp != null) {
-                                               sp.setSelection(event.getSelection());
-                                       }
-                               }
-                       }
-                       
-               });
-               
-               IPostSelectionProvider psp = (IPostSelectionProvider)control.getExplorer().getAdapter(IPostSelectionProvider.class);
-               psp.addPostSelectionChangedListener(new ISelectionChangedListener() {
-                       
-                       @Override
-                       public void selectionChanged(SelectionChangedEvent event) {
-                               
-//                             ISelection selection = (ISelection)control.getExplorer().getWidgetSelection();
-                               if (site != null) {
-                                       ISelectionProvider sp = site.getSelectionProvider();
-                                       if (sp != null) {
-                                               sp.setSelection(event.getSelection());
-                                       }
-                               }
-                               
-                       }
-                       
-               });
-               
-               control.addListenerToControl(SWT.Selection, new Listener() {
-
-                       @Override
-                       public void handleEvent(Event event) {
-                               
-                               if(selectionListener != null)
-                                       selectionListener.apply(event);
-                               
-                               ISelection selection = (ISelection)control.getExplorer().getWidgetSelection();
-                               // [Tuukka@2012-04-08] Disabled this because it was causing
-                               // horrible selection feedback effects in the Model Browser
-                               // view that is using it. It causes the browser to react to
-                               // external selections which were initially published as its own
-                               // with a delay.
-                               //System.out.println("selection: " + selection);
-//                             if(publishSelection != null && publishSelection) {
-//                                     if (site != null) {
-//                                             ISelectionProvider sp = site.getSelectionProvider();
-//                                             if (sp != null) {
-//                                                     sp.setSelection(selection);
-//                                             }
-//                                     }
-//                             }
-                               
-                               propertyCallback.apply("selection", selection);
-                               
-                               lastSelection = selection;
-                               
-                       }
-                       
+               ISelectionProvider isp = (ISelectionProvider)control.getExplorer().getAdapter(ISelectionProvider.class);
+               if (isp instanceof IPostSelectionProvider) {
+                       ((IPostSelectionProvider) isp).addPostSelectionChangedListener(internalToExternalSelectionPropagator);
+               } else {
+                       isp.addSelectionChangedListener(internalToExternalSelectionPropagator);
+               }
+
+               control.addListenerToControl(SWT.Selection, event -> {
+                       if(selectionListener != null)
+                               selectionListener.apply(event);
+                       ISelection selection = (ISelection)control.getExplorer().getWidgetSelection();
+                       propertyCallback.apply("selection", selection);
+                       lastSelection = selection;
                });
-                       
+
        }
 
        public void synchronizeColumnsVisible(Boolean columnsVisible) {
@@ -191,5 +148,16 @@ public class SWTExplorer extends SingleSWTViewNode<ModelBrowser> {
 
        final public void synchronizeLayout(LayoutBean layout) {
        }
-       
+
+       @Override
+       public void dispose() {
+               ISelectionProvider isp = (ISelectionProvider)control.getExplorer().getAdapter(ISelectionProvider.class);
+               if (isp instanceof IPostSelectionProvider) {
+                       ((IPostSelectionProvider) isp).removePostSelectionChangedListener(internalToExternalSelectionPropagator);
+               } else {
+                       isp.removeSelectionChangedListener(internalToExternalSelectionPropagator);
+               }
+               super.dispose();
+       }
+
 }