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