]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.ui/src/org/simantics/ui/icons/GraphImageDescriptor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.ui / src / org / simantics / ui / icons / GraphImageDescriptor.java
diff --git a/bundles/org.simantics.ui/src/org/simantics/ui/icons/GraphImageDescriptor.java b/bundles/org.simantics.ui/src/org/simantics/ui/icons/GraphImageDescriptor.java
new file mode 100644 (file)
index 0000000..10a7e59
--- /dev/null
@@ -0,0 +1,95 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2013 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
+ *     Semantum Oy - issue #4214\r
+ *******************************************************************************/\r
+package org.simantics.ui.icons;\r
+\r
+import java.io.ByteArrayInputStream;\r
+import java.util.Arrays;\r
+\r
+import org.eclipse.jface.resource.ImageDescriptor;\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.SWTException;\r
+import org.eclipse.swt.graphics.ImageData;\r
+import org.eclipse.swt.graphics.ImageLoader;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.RequestProcessor;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.primitiverequest.PossibleValue;\r
+import org.simantics.db.exception.DatabaseException;\r
+\r
+/**\r
+ * @author Hannu Niemistö\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class GraphImageDescriptor extends ImageDescriptor {\r
+\r
+       private Resource resource;\r
+    private long resourceId;\r
+    private byte[] data;\r
+    private int hash;\r
+\r
+    public GraphImageDescriptor(ReadGraph graph, Resource resource) throws DatabaseException {\r
+        this.resource = resource;\r
+        this.resourceId = resource.getResourceId();\r
+        this.data = getImageDataBytes(graph);\r
+        this.hash = (Arrays.hashCode(this.data) * 31 + (int)(resourceId>>>32)) * 31 + ((int)resourceId);\r
+    }\r
+\r
+    private byte[] getImageDataBytes(RequestProcessor processor) throws DatabaseException {\r
+        return processor.syncRequest(new PossibleValue<byte[]>(resource, Bindings.BYTE_ARRAY));\r
+    }\r
+\r
+    private ImageData toImageData(byte[] data) throws SWTException {\r
+        ImageData[] images = new ImageLoader().load(new ByteArrayInputStream(data));\r
+        if (images.length == 0)\r
+            SWT.error(SWT.ERROR_INVALID_IMAGE, null, "No images found in image data of resource $" + resourceId);\r
+        return images[0];\r
+    }\r
+\r
+    @Override\r
+    public ImageData getImageData() {\r
+        try {\r
+            ImageData imageData = toImageData(data);\r
+            return imageData;\r
+        } catch (SWTException e) {\r
+            return null;\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public String toString() {\r
+        return super.toString() + "[" + resource + "]";\r
+    }\r
+\r
+    @Override\r
+    public int hashCode() {\r
+        return hash;\r
+    }\r
+\r
+    @Override\r
+    public boolean equals(Object obj) {\r
+        if (this == obj)\r
+            return true;\r
+        if (obj == null)\r
+            return false;\r
+        if (getClass() != obj.getClass())\r
+            return false;\r
+        GraphImageDescriptor other = (GraphImageDescriptor) obj;\r
+        if (!resource.equals(other.resource))\r
+            return false;\r
+        if (!Arrays.equals(data, other.data))\r
+            return false;\r
+        return true;\r
+    }\r
+\r
+}\r