]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/widgets/GraphExplorerComposite.java
Merge branch 'feature/funcwrite'
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / widgets / GraphExplorerComposite.java
index 3ee82ae3b319b944c25b29337d68a27da05d7489..f583d7e7f1df374f0466232cf40d786a4b14cf2b 100644 (file)
@@ -16,6 +16,7 @@ import java.util.Collections;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
+import java.util.function.BiFunction;
 import java.util.function.Consumer;
 
 import org.eclipse.core.runtime.IAdaptable;
@@ -102,12 +103,11 @@ import org.simantics.db.AsyncReadGraph;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.Session;
-import org.simantics.db.common.request.ResourceRead;
-import org.simantics.db.common.utils.Logger;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.SelectionHints;
+import org.simantics.db.layer0.request.PossibleVariable;
+import org.simantics.db.layer0.request.PossibleVariableRepresents;
 import org.simantics.db.layer0.variable.Variable;
-import org.simantics.db.layer0.variable.Variables;
 import org.simantics.db.management.ISessionContext;
 import org.simantics.db.management.ISessionContextChangedListener;
 import org.simantics.db.management.ISessionContextProvider;
@@ -126,7 +126,6 @@ import org.simantics.ui.selection.WorkbenchSelectionContentType;
 import org.simantics.ui.selection.WorkbenchSelectionElement;
 import org.simantics.ui.selection.WorkbenchSelectionUtils;
 import org.simantics.utils.ObjectUtils;
-import org.simantics.utils.datastructures.BinaryFunction;
 import org.simantics.utils.datastructures.Function;
 import org.simantics.utils.datastructures.disposable.DisposeState;
 import org.simantics.utils.datastructures.hints.HintListenerAdapter;
@@ -135,10 +134,14 @@ import org.simantics.utils.datastructures.hints.IHintContext.Key;
 import org.simantics.utils.datastructures.hints.IHintListener;
 import org.simantics.utils.datastructures.hints.IHintObservable;
 import org.simantics.utils.datastructures.hints.IHintTracker;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 
 public class GraphExplorerComposite extends Composite implements Widget, IAdaptable {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(GraphExplorerComposite.class);
+
     protected UserSelectedComparableFactoryQueryProcessor userSelectedComparableFactoryQueryProcessor;
     protected UserSelectedViewpointFactoryQueryProcessor  userSelectedViewpointFactoryQueryProcessor;
     protected FilterSelectionRequestQueryProcessor        filterSelectionRequestQueryProcessor;
@@ -248,39 +251,41 @@ public class GraphExplorerComposite extends Composite implements Widget, IAdapta
         @SuppressWarnings("unchecked")
         @Override
         public <T> T getContent(WorkbenchSelectionContentType<T> contentType) {
-
-            if(wse != null) {
+            if (wse != null) {
                 T result = wse.getContent(contentType);
-                if(result != null) return result;
+                if (result != null)
+                    return result;
             }
-            
-            if(contentType instanceof AnyResource) return (T)resource;
-            else if(contentType instanceof AnyVariable) {
-                AnyVariable type = (AnyVariable)contentType;
+
+            if (contentType instanceof AnyResource) {
+                if (resource != null)
+                    return (T) resource;
+                if (variable == null)
+                    return null;
+                try {
+                    return (T) ((AnyResource) contentType).processor.syncRequest(new PossibleVariableRepresents(variable));
+                } catch (DatabaseException e) {
+                    LOGGER.error("Unexpected error occurred while resolving Resource from Variable " + variable, e);
+                }
+            }
+            else if (contentType instanceof AnyVariable) {
+                if (variable != null)
+                    return (T) variable;
+                if (resource == null)
+                    return null;
                 try {
-                       
-                       if(variable != null) return (T)variable;
-                       
-                       if(resource == null) return null;
-                       
-                    return (T) type.processor.sync(new ResourceRead<Variable>(resource) {
-                        @Override
-                        public Variable perform(ReadGraph graph) throws DatabaseException {
-                            return Variables.getPossibleVariable(graph, resource);
-                        }
-                        
-                    });
+                    return (T) ((AnyVariable) contentType).processor.syncRequest(new PossibleVariable(resource));
                 } catch (DatabaseException e) {
-                    Logger.defaultLogError(e);
+                    LOGGER.error("Unexpected error occurred while resolving Variable from Resource " + resource, e);
                 }
             } else if (contentType instanceof ExplorerInputContentType) {
-               return (T)input;
+                return (T) input;
             } else if (contentType instanceof ExplorerColumnContentType) {
-               return (T)explorerState.getActiveColumn();
+                return (T) explorerState.getActiveColumn();
             }
             return null;
         }
-        
+
         @SuppressWarnings("rawtypes")
         @Override
         public Object getAdapter(Class adapter) {
@@ -297,12 +302,12 @@ public class GraphExplorerComposite extends Composite implements Widget, IAdapta
         }
 
     }
-    private BinaryFunction<Object[], GraphExplorer, Object[]> selectionTransformation = new BinaryFunction<Object[], GraphExplorer, Object[]>() {
+    private BiFunction<GraphExplorer, Object[], Object[]> selectionTransformation = new BiFunction<GraphExplorer, Object[], Object[]>() {
 
        private Key[] KEYS = new Key[] { SelectionHints.KEY_MAIN };
        
         @Override
-        public Object[] call(GraphExplorer explorer, Object[] objects) {
+        public Object[] apply(GraphExplorer explorer, Object[] objects) {
             Object[] result = new Object[objects.length];
             for (int i = 0; i < objects.length; i++) {
                SelectionElement context = new SelectionElement(explorer, KEYS, objects[i]);
@@ -529,7 +534,7 @@ public class GraphExplorerComposite extends Composite implements Widget, IAdapta
         //tree.getTree().setLayout(new FillLayout()
         //this.setLayout(LayoutUtils.createNoBorderGridLayout(2, false));
 
-        DropTarget target = new DropTarget(control, DND.DROP_COPY | DND.DROP_LINK);
+        DropTarget target = new DropTarget(control, DND.DROP_COPY | DND.DROP_MOVE | DND.DROP_LINK | DND.DROP_DEFAULT);
         target.setTransfer(getAcceptedDataTypes());
         if (control instanceof Tree)  {
         target.addDropListener(new DropTargetListener() {
@@ -694,7 +699,7 @@ public class GraphExplorerComposite extends Composite implements Widget, IAdapta
     }
 
     protected Transfer[] getAcceptedDataTypes() {
-        return new Transfer[] {  LocalObjectTransfer.getTransfer(), FileTransfer.getInstance() };
+        return new Transfer[] { LocalObjectTransfer.getTransfer(), FileTransfer.getInstance(), TextTransfer.getInstance() };
     }
 
     protected void handleDrop(Object data, NodeContext target) {
@@ -723,7 +728,7 @@ public class GraphExplorerComposite extends Composite implements Widget, IAdapta
                                                        event.data = WorkbenchSelectionUtils.getPossibleJSON(selectionProvider.getSelection());
                                                } catch (DatabaseException e) {
                                                        event.data = "{ type:\"Exception\" }";
-                                                       Logger.defaultLogError(e);
+                                                       LOGGER.error("Failed to get current selection as JSON.", e);
                                                }
                                        } else if (LocalObjectTransfer.getTransfer().isSupportedType(event.dataType)) {
                                                ls.dragSetData(event);
@@ -863,7 +868,7 @@ public class GraphExplorerComposite extends Composite implements Widget, IAdapta
        this.filterAreaSource = provider;
     }
 
-    public void setSelectionTransformation(BinaryFunction<Object[], GraphExplorer, Object[]> transformation) {
+    public void setSelectionTransformation(BiFunction<GraphExplorer, Object[], Object[]> transformation) {
         this.selectionTransformation = transformation;
         if(explorer != null) explorer.setSelectionTransformation(transformation);
     }