]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ArrayPropertyImagerFactory.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.swt / src / org / simantics / browsing / ui / swt / ArrayPropertyImagerFactory.java
diff --git a/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ArrayPropertyImagerFactory.java b/bundles/org.simantics.browsing.ui.swt/src/org/simantics/browsing/ui/swt/ArrayPropertyImagerFactory.java
new file mode 100644 (file)
index 0000000..8acc81a
--- /dev/null
@@ -0,0 +1,88 @@
+/*******************************************************************************\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.swt;\r
+\r
+import org.eclipse.jface.resource.ImageDescriptor;\r
+import org.simantics.browsing.ui.BuiltinKeys;\r
+import org.simantics.browsing.ui.BuiltinKeys.ImagerKey;\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.common.ColumnKeys;\r
+import org.simantics.browsing.ui.common.imagers.ContainerImager;\r
+import org.simantics.browsing.ui.common.property.IArrayProperty;\r
+import org.simantics.browsing.ui.content.Imager;\r
+import org.simantics.browsing.ui.content.ImagerFactory;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.ui.icons.ImageUtil;\r
+import org.simantics.utils.ui.BundleUtils;\r
+\r
+/**\r
+ * A basic imager factory with support for both Resource and IArrayProperty.\r
+ * \r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class ArrayPropertyImagerFactory implements ImagerFactory {\r
+\r
+    @SuppressWarnings("unchecked")\r
+    @Override\r
+    public Imager create(final PrimitiveQueryUpdater updater, final NodeContext context, final ImagerKey key) {\r
+        assert updater != null;\r
+        assert context != null;\r
+\r
+        @SuppressWarnings("rawtypes")\r
+        final ContainerImager result = new ContainerImager();\r
+        final DataSource<ReadGraph> source = updater.getDataSource(ReadGraph.class);\r
+        if (source == null)\r
+            return result;\r
+\r
+        Object o = context.getConstant(BuiltinKeys.INPUT);\r
+        if (o instanceof IArrayProperty) {\r
+            IArrayProperty prop = (IArrayProperty) o;\r
+\r
+            if (prop.getRange().size() > 1) {\r
+                // Only show the segment image for non-single-item slices of the\r
+                // property root node. Otherwise just use the image resolved\r
+                // through normal means.\r
+                if (prop.isSlice()) {\r
+                    result.setImage(ColumnKeys.PROPERTY,\r
+                            BundleUtils.getImageDescriptorFromPlugin(\r
+                                    "org.simantics.browsing.ui.common", "/icons/segment_edit.gif"));\r
+                    return result;\r
+                }\r
+            } else {\r
+                // No image for single-item slices of the property.\r
+                return result;\r
+            }\r
+        }\r
+\r
+        final Resource inputResource = (Resource) context.getAdapter(Resource.class);\r
+        if (inputResource == null)\r
+            return result;\r
+\r
+        source.schedule(graph -> {\r
+            ImageDescriptor descriptor;\r
+            try {\r
+                descriptor = ImageUtil.adaptImageDescriptor(graph, inputResource);\r
+                result.setImage(ColumnKeys.PROPERTY, descriptor);\r
+                updater.scheduleReplace(context, key, result);\r
+            } catch (DatabaseException e) {\r
+                e.printStackTrace();\r
+            }\r
+        });\r
+\r
+        return result;\r
+    }\r
+\r
+}\r