]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2013 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *     Semantum Oy - issue #4214\r
12  *******************************************************************************/\r
13 package org.simantics.ui.icons;\r
14 \r
15 import java.io.ByteArrayInputStream;\r
16 import java.util.Arrays;\r
17 \r
18 import org.eclipse.jface.resource.ImageDescriptor;\r
19 import org.eclipse.swt.SWT;\r
20 import org.eclipse.swt.SWTException;\r
21 import org.eclipse.swt.graphics.ImageData;\r
22 import org.eclipse.swt.graphics.ImageLoader;\r
23 import org.simantics.databoard.Bindings;\r
24 import org.simantics.db.ReadGraph;\r
25 import org.simantics.db.RequestProcessor;\r
26 import org.simantics.db.Resource;\r
27 import org.simantics.db.common.primitiverequest.PossibleValue;\r
28 import org.simantics.db.exception.DatabaseException;\r
29 \r
30 /**\r
31  * @author Hannu Niemistö\r
32  * @author Tuukka Lehtonen\r
33  */\r
34 public class GraphImageDescriptor extends ImageDescriptor {\r
35 \r
36         private Resource resource;\r
37     private long resourceId;\r
38     private byte[] data;\r
39     private int hash;\r
40 \r
41     public GraphImageDescriptor(ReadGraph graph, Resource resource) throws DatabaseException {\r
42         this.resource = resource;\r
43         this.resourceId = resource.getResourceId();\r
44         this.data = getImageDataBytes(graph);\r
45         this.hash = (Arrays.hashCode(this.data) * 31 + (int)(resourceId>>>32)) * 31 + ((int)resourceId);\r
46     }\r
47 \r
48     private byte[] getImageDataBytes(RequestProcessor processor) throws DatabaseException {\r
49         return processor.syncRequest(new PossibleValue<byte[]>(resource, Bindings.BYTE_ARRAY));\r
50     }\r
51 \r
52     private ImageData toImageData(byte[] data) throws SWTException {\r
53         ImageData[] images = new ImageLoader().load(new ByteArrayInputStream(data));\r
54         if (images.length == 0)\r
55             SWT.error(SWT.ERROR_INVALID_IMAGE, null, "No images found in image data of resource $" + resourceId);\r
56         return images[0];\r
57     }\r
58 \r
59     @Override\r
60     public ImageData getImageData() {\r
61         try {\r
62             ImageData imageData = toImageData(data);\r
63             return imageData;\r
64         } catch (SWTException e) {\r
65             return null;\r
66         }\r
67     }\r
68 \r
69     @Override\r
70     public String toString() {\r
71         return super.toString() + "[" + resource + "]";\r
72     }\r
73 \r
74     @Override\r
75     public int hashCode() {\r
76         return hash;\r
77     }\r
78 \r
79     @Override\r
80     public boolean equals(Object obj) {\r
81         if (this == obj)\r
82             return true;\r
83         if (obj == null)\r
84             return false;\r
85         if (getClass() != obj.getClass())\r
86             return false;\r
87         GraphImageDescriptor other = (GraphImageDescriptor) obj;\r
88         if (!resource.equals(other.resource))\r
89             return false;\r
90         if (!Arrays.equals(data, other.data))\r
91             return false;\r
92         return true;\r
93     }\r
94 \r
95 }\r