]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.graph.impl/src/org/simantics/browsing/ui/graph/impl/contribution/FinalLabelerContributionImpl.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.graph.impl / src / org / simantics / browsing / ui / graph / impl / contribution / FinalLabelerContributionImpl.java
index 83bdbe264afeae90aaff43d579b9daa985b84e69..b283d662e76f92b3b50148ecf6e264585a9adf4b 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.browsing.ui.graph.impl.contribution;\r
-\r
-import java.util.Collections;\r
-import java.util.Map;\r
-\r
-import org.simantics.Simantics;\r
-import org.simantics.browsing.ui.BuiltinKeys;\r
-import org.simantics.browsing.ui.BuiltinKeys.LabelerKey;\r
-import org.simantics.browsing.ui.DataSource;\r
-import org.simantics.browsing.ui.GraphExplorer.ModificationContext;\r
-import org.simantics.browsing.ui.NodeContext;\r
-import org.simantics.browsing.ui.PrimitiveQueryUpdater;\r
-import org.simantics.browsing.ui.common.labelers.LabelerContent;\r
-import org.simantics.browsing.ui.common.labelers.LabelerStub;\r
-import org.simantics.browsing.ui.common.node.IModifiableNode;\r
-import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.UndoContext;\r
-import org.simantics.db.common.utils.Logger;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.exception.InvalidVariableException;\r
-import org.simantics.db.layer0.exception.PendingVariableException;\r
-import org.simantics.db.procedure.Listener;\r
-import org.simantics.db.procedure.Procedure;\r
-import org.simantics.db.request.Read;\r
-import org.simantics.utils.ui.ErrorLogger;\r
-\r
-public abstract class FinalLabelerContributionImpl extends LabelerStub {\r
-\r
-    protected final PrimitiveQueryUpdater       updater;\r
-    private final ResourceQuery<LabelerContent> labelQuery;\r
-\r
-    final protected NodeContext                context;\r
-    final private BuiltinKeys.LabelerKey    key;\r
-\r
-    public Object getIdentity(LabelerKey key) {\r
-        return key;\r
-    }\r
-\r
-    public FinalLabelerContributionImpl(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelerKey key) {\r
-\r
-        this.updater = updater;\r
-        this.context = context;\r
-        this.key = key;\r
-\r
-        labelQuery = new ResourceQuery<LabelerContent>(getIdentity(key), context) {\r
-\r
-            @Override\r
-            public LabelerContent perform(ReadGraph graph) throws DatabaseException {\r
-                try {\r
-                    int cat = category(graph, context);\r
-                    Map<String, String> lbls = labels(graph, context);\r
-                    // Make sure that null is not returned.\r
-                    if (lbls == null)\r
-                        throw new NullPointerException("FinalLabelerContributionImpl.labels is not allowed to return null, but " + FinalLabelerContributionImpl.this.getClass() + " just did it");\r
-                    return new LabelerContent(cat, lbls);\r
-                } catch (PendingVariableException e) {\r
-                    return LabelerContent.NO_CONTENT;\r
-                } catch (InvalidVariableException e) {\r
-                    //ErrorLogger.defaultLogError("FinalLabelerContributionImpl.labelQuery failed due to invalid variable.", e);\r
-                    return LabelerContent.NO_CONTENT;\r
-                } catch (DatabaseException e) {\r
-                    throw e;\r
-                } catch (Throwable t) {\r
-                    ErrorLogger.defaultLogError("FinalLabelerContributionImpl.labelQuery produced unexpected exception.", t);\r
-                    return LabelerContent.NO_CONTENT;\r
-                }\r
-            }\r
-\r
-            @Override\r
-            public String toString() {\r
-                return FinalLabelerContributionImpl.this + " with context " + context;\r
-            }\r
-\r
-        };\r
-\r
-    }\r
-\r
-    protected Procedure<LabelerContent> createProcedure() {\r
-\r
-        return new Procedure<LabelerContent>() {\r
-\r
-            @Override\r
-            public void execute(LabelerContent result) {\r
-                replaceResult(result);\r
-            }\r
-\r
-            @Override\r
-            public void exception(Throwable t) {\r
-                ErrorLogger.defaultLogError("FinalLabelerContributionImpl.labelQuery failed, see exception for details.", t);\r
-            }\r
-\r
-        };\r
-\r
-    }\r
-\r
-    protected void replaceResult(LabelerContent result) {\r
-        setContent(result);\r
-        updater.scheduleReplace(context, key, this);\r
-    }\r
-\r
-    @Override\r
-    public Map<String, String> getLabels() {\r
-\r
-        if (content == LabelerContent.NO_CONTENT) {\r
-\r
-            final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);\r
-            assert(source != null);\r
-\r
-            final Procedure<LabelerContent> procedure = createProcedure();\r
-            \r
-            source.schedule(graph -> {\r
-                if(procedure instanceof Listener<?>)\r
-                    graph.asyncRequest(labelQuery, (Listener<LabelerContent>)procedure);\r
-                else\r
-                    graph.asyncRequest(labelQuery, procedure);\r
-            });\r
-\r
-        }\r
-\r
-        if(content == null) return Collections.emptyMap();\r
-\r
-        return content.labels;\r
-\r
-    }\r
-\r
-    @Override\r
-    public Modifier getModifier(final ModificationContext modificationContext, final String columnKey) {\r
-       \r
-        Object obj = context.getConstant(BuiltinKeys.INPUT);\r
-        if (obj instanceof IModifiableNode) {\r
-            return ((IModifiableNode) obj).getModifier(columnKey);\r
-        }\r
-        try {\r
-                       return Simantics.getSession().syncRequest(new Read<Modifier>() {\r
-\r
-                               @Override\r
-                               public Modifier perform(ReadGraph graph) throws DatabaseException {\r
-                                   UndoContext undoContext = null == modificationContext ? null :\r
-                                       (UndoContext)modificationContext.getAdapter(UndoContext.class);\r
-                                       return FinalLabelerContributionImpl.this.getModifier(graph, undoContext, context, columnKey);\r
-                               }\r
-                               \r
-                       });\r
-               } catch (DatabaseException e) {\r
-                       Logger.defaultLogError(e);\r
-                       return null;\r
-               }\r
-        \r
-    }\r
-\r
-    // OVERRIDE\r
-\r
-    public Modifier getModifier(ReadGraph graph, UndoContext undoContext, NodeContext context, String columnKey) throws DatabaseException {\r
-       return null;\r
-    }\r
-\r
-    public abstract Map<String, String> labels(ReadGraph graph, NodeContext context) throws DatabaseException;\r
-\r
-    public abstract int category(ReadGraph graph, NodeContext context) throws DatabaseException;\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.browsing.ui.graph.impl.contribution;
+
+import java.util.Collections;
+import java.util.Map;
+
+import org.simantics.Simantics;
+import org.simantics.browsing.ui.BuiltinKeys;
+import org.simantics.browsing.ui.BuiltinKeys.LabelerKey;
+import org.simantics.browsing.ui.DataSource;
+import org.simantics.browsing.ui.GraphExplorer.ModificationContext;
+import org.simantics.browsing.ui.NodeContext;
+import org.simantics.browsing.ui.PrimitiveQueryUpdater;
+import org.simantics.browsing.ui.common.labelers.LabelerContent;
+import org.simantics.browsing.ui.common.labelers.LabelerStub;
+import org.simantics.browsing.ui.common.node.IModifiableNode;
+import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.UndoContext;
+import org.simantics.db.common.utils.Logger;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.exception.InvalidVariableException;
+import org.simantics.db.layer0.exception.PendingVariableException;
+import org.simantics.db.procedure.Listener;
+import org.simantics.db.procedure.Procedure;
+import org.simantics.db.request.Read;
+import org.simantics.utils.ui.ErrorLogger;
+
+public abstract class FinalLabelerContributionImpl extends LabelerStub {
+
+    protected final PrimitiveQueryUpdater       updater;
+    private final ResourceQuery<LabelerContent> labelQuery;
+
+    final protected NodeContext                context;
+    final private BuiltinKeys.LabelerKey    key;
+
+    public Object getIdentity(LabelerKey key) {
+        return key;
+    }
+
+    public FinalLabelerContributionImpl(final PrimitiveQueryUpdater updater, final NodeContext context, final LabelerKey key) {
+
+        this.updater = updater;
+        this.context = context;
+        this.key = key;
+
+        labelQuery = new ResourceQuery<LabelerContent>(getIdentity(key), context) {
+
+            @Override
+            public LabelerContent perform(ReadGraph graph) throws DatabaseException {
+                try {
+                    int cat = category(graph, context);
+                    Map<String, String> lbls = labels(graph, context);
+                    // Make sure that null is not returned.
+                    if (lbls == null)
+                        throw new NullPointerException("FinalLabelerContributionImpl.labels is not allowed to return null, but " + FinalLabelerContributionImpl.this.getClass() + " just did it");
+                    return new LabelerContent(cat, lbls);
+                } catch (PendingVariableException e) {
+                    return LabelerContent.NO_CONTENT;
+                } catch (InvalidVariableException e) {
+                    //ErrorLogger.defaultLogError("FinalLabelerContributionImpl.labelQuery failed due to invalid variable.", e);
+                    return LabelerContent.NO_CONTENT;
+                } catch (DatabaseException e) {
+                    throw e;
+                } catch (Throwable t) {
+                    ErrorLogger.defaultLogError("FinalLabelerContributionImpl.labelQuery produced unexpected exception.", t);
+                    return LabelerContent.NO_CONTENT;
+                }
+            }
+
+            @Override
+            public String toString() {
+                return FinalLabelerContributionImpl.this + " with context " + context;
+            }
+
+        };
+
+    }
+
+    protected Procedure<LabelerContent> createProcedure() {
+
+        return new Procedure<LabelerContent>() {
+
+            @Override
+            public void execute(LabelerContent result) {
+                replaceResult(result);
+            }
+
+            @Override
+            public void exception(Throwable t) {
+                ErrorLogger.defaultLogError("FinalLabelerContributionImpl.labelQuery failed, see exception for details.", t);
+            }
+
+        };
+
+    }
+
+    protected void replaceResult(LabelerContent result) {
+        setContent(result);
+        updater.scheduleReplace(context, key, this);
+    }
+
+    @Override
+    public Map<String, String> getLabels() {
+
+        if (content == LabelerContent.NO_CONTENT) {
+
+            final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);
+            assert(source != null);
+
+            final Procedure<LabelerContent> procedure = createProcedure();
+            
+            source.schedule(graph -> {
+                if(procedure instanceof Listener<?>)
+                    graph.asyncRequest(labelQuery, (Listener<LabelerContent>)procedure);
+                else
+                    graph.asyncRequest(labelQuery, procedure);
+            });
+
+        }
+
+        if(content == null) return Collections.emptyMap();
+
+        return content.labels;
+
+    }
+
+    @Override
+    public Modifier getModifier(final ModificationContext modificationContext, final String columnKey) {
+       
+        Object obj = context.getConstant(BuiltinKeys.INPUT);
+        if (obj instanceof IModifiableNode) {
+            return ((IModifiableNode) obj).getModifier(columnKey);
+        }
+        try {
+                       return Simantics.getSession().syncRequest(new Read<Modifier>() {
+
+                               @Override
+                               public Modifier perform(ReadGraph graph) throws DatabaseException {
+                                   UndoContext undoContext = null == modificationContext ? null :
+                                       (UndoContext)modificationContext.getAdapter(UndoContext.class);
+                                       return FinalLabelerContributionImpl.this.getModifier(graph, undoContext, context, columnKey);
+                               }
+                               
+                       });
+               } catch (DatabaseException e) {
+                       Logger.defaultLogError(e);
+                       return null;
+               }
+        
+    }
+
+    // OVERRIDE
+
+    public Modifier getModifier(ReadGraph graph, UndoContext undoContext, NodeContext context, String columnKey) throws DatabaseException {
+       return null;
+    }
+
+    public abstract Map<String, String> labels(ReadGraph graph, NodeContext context) throws DatabaseException;
+
+    public abstract int category(ReadGraph graph, NodeContext context) throws DatabaseException;
+
+}