--- /dev/null
+/*******************************************************************************\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 org.simantics.browsing.ui.BuiltinKeys.ImageDecoratorKey;\r
+import org.simantics.browsing.ui.DataSource;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.PrimitiveQueryUpdater;\r
+import org.simantics.browsing.ui.content.ImageDecorator;\r
+import org.simantics.browsing.ui.graph.impl.request.ResourceQuery;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.exception.DatabaseException;\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.utils.ui.ErrorLogger;\r
+\r
+public abstract class FinalImageDecoratorContributionImpl implements ImageDecorator {\r
+\r
+ final ImageDecorator FRESH = new ImageDecorator.Stub();\r
+ final ImageDecorator PENDING = new ImageDecorator.Stub();\r
+ final ImageDecorator NO_DECORATION = new ImageDecorator.Stub();\r
+\r
+ protected final PrimitiveQueryUpdater updater;\r
+ private final ResourceQuery<ImageDecorator> imageQuery;\r
+ private final ImageDecoratorKey key;\r
+\r
+ /**\r
+ * The current decorator result produced for this contribution. The value\r
+ * should never be <code>null</code>, but one of {@link #FRESH},\r
+ * {@link #PENDING}, {@link #NO_DECORATION} instead.\r
+ */\r
+ protected ImageDecorator decorator = FRESH;\r
+\r
+ final protected NodeContext context;\r
+\r
+ public Object getIdentity(ImageDecoratorKey key) {\r
+ return key;\r
+ }\r
+\r
+ private void request() {\r
+\r
+ final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);\r
+ assert(source != null);\r
+\r
+ final Procedure<ImageDecorator> procedure = createProcedure();\r
+ \r
+ source.schedule(graph -> {\r
+ if(procedure instanceof Listener<?>)\r
+ graph.asyncRequest(imageQuery, (Listener<ImageDecorator>)procedure);\r
+ else\r
+ graph.asyncRequest(imageQuery, procedure);\r
+ });\r
+\r
+ }\r
+\r
+ @Override\r
+ public <Image> Image decorateImage(Image image, String column, int itemIndex) {\r
+ if(FRESH == decorator) {\r
+ decorator = PENDING;\r
+ request();\r
+ }\r
+ return decorator.decorateImage(image, column, itemIndex);\r
+ }\r
+\r
+ public FinalImageDecoratorContributionImpl(final PrimitiveQueryUpdater updater, final NodeContext context, final ImageDecoratorKey key) {\r
+\r
+ this.updater = updater;\r
+ this.context = context;\r
+ this.key = key;\r
+\r
+ imageQuery = new ResourceQuery<ImageDecorator>(getIdentity(key), context) {\r
+\r
+ @Override\r
+ public ImageDecorator perform(ReadGraph graph) throws DatabaseException {\r
+ try {\r
+ return getDecorator(graph, context);\r
+ } catch (PendingVariableException e) {\r
+ return PENDING;\r
+ } catch (DatabaseException e) {\r
+ throw e;\r
+ } catch (Throwable t) {\r
+ ErrorLogger.defaultLogError("FinalImageDecoratorContributionImpl.imageQuery produced unexpected exception.", t);\r
+ return null;\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public String toString() {\r
+ return FinalImageDecoratorContributionImpl.this + " with context " + context;\r
+ }\r
+\r
+ };\r
+ \r
+ }\r
+\r
+ protected Procedure<ImageDecorator> createProcedure() {\r
+\r
+ return new Procedure<ImageDecorator>() {\r
+\r
+ @Override\r
+ public void execute(ImageDecorator result) {\r
+ replaceResult(result);\r
+ }\r
+\r
+ @Override\r
+ public void exception(Throwable t) {\r
+ ErrorLogger.defaultLogError("FinalImageDecoratorContributionImpl.imageQuery failed, see exception for details.", t);\r
+ }\r
+\r
+ };\r
+\r
+ }\r
+\r
+ protected void replaceResult(ImageDecorator result) {\r
+ // Never let decorator become null, use a stub decorator instead.\r
+ if (result == null)\r
+ result = NO_DECORATION;\r
+\r
+ decorator = result;\r
+ updater.scheduleReplace(context, key, this);\r
+ }\r
+\r
+ // OVERRIDE\r
+\r
+ public abstract ImageDecorator getDecorator(ReadGraph graph, NodeContext context) throws DatabaseException;\r
+\r
+}\r