1 /*******************************************************************************
2 * Copyright (c) 2007, 2013 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 * Semantum Oy - issue #4214
12 *******************************************************************************/
13 package org.simantics.ui.icons;
15 import java.io.ByteArrayInputStream;
16 import java.util.Arrays;
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;
31 * @author Hannu Niemistö
32 * @author Tuukka Lehtonen
34 public class GraphImageDescriptor extends ImageDescriptor {
36 private Resource resource;
37 private long resourceId;
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);
48 private byte[] getImageDataBytes(RequestProcessor processor) throws DatabaseException {
49 return processor.syncRequest(new PossibleValue<byte[]>(resource, Bindings.BYTE_ARRAY));
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);
60 public ImageData getImageData() {
62 ImageData imageData = toImageData(data);
64 } catch (SWTException e) {
69 public Resource getResource() {
74 public String toString() {
75 return super.toString() + "[" + resource + "]";
79 public int hashCode() {
84 public boolean equals(Object obj) {
89 if (getClass() != obj.getClass())
91 GraphImageDescriptor other = (GraphImageDescriptor) obj;
92 if (!resource.equals(other.resource))
94 if (!Arrays.equals(data, other.data))