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
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
15 import java.io.ByteArrayInputStream;
\r
16 import java.util.Arrays;
\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
31 * @author Hannu Niemistö
\r
32 * @author Tuukka Lehtonen
\r
34 public class GraphImageDescriptor extends ImageDescriptor {
\r
36 private Resource resource;
\r
37 private long resourceId;
\r
38 private byte[] data;
\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
48 private byte[] getImageDataBytes(RequestProcessor processor) throws DatabaseException {
\r
49 return processor.syncRequest(new PossibleValue<byte[]>(resource, Bindings.BYTE_ARRAY));
\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
60 public ImageData getImageData() {
\r
62 ImageData imageData = toImageData(data);
\r
64 } catch (SWTException e) {
\r
70 public String toString() {
\r
71 return super.toString() + "[" + resource + "]";
\r
75 public int hashCode() {
\r
80 public boolean equals(Object obj) {
\r
85 if (getClass() != obj.getClass())
\r
87 GraphImageDescriptor other = (GraphImageDescriptor) obj;
\r
88 if (!resource.equals(other.resource))
\r
90 if (!Arrays.equals(data, other.data))
\r