-/*******************************************************************************\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
+/*******************************************************************************
+ * Copyright (c) 2007, 2013 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ * Semantum Oy - issue #4214
+ *******************************************************************************/
+package org.simantics.ui.icons;
+
+import java.io.ByteArrayInputStream;
+import java.util.Arrays;
+
+import org.eclipse.jface.resource.ImageDescriptor;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.SWTException;
+import org.eclipse.swt.graphics.ImageData;
+import org.eclipse.swt.graphics.ImageLoader;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.RequestProcessor;
+import org.simantics.db.Resource;
+import org.simantics.db.common.primitiverequest.PossibleValue;
+import org.simantics.db.exception.DatabaseException;
+
+/**
+ * @author Hannu Niemistö
+ * @author Tuukka Lehtonen
+ */
+public class GraphImageDescriptor extends ImageDescriptor {
+
+ private Resource resource;
+ private long resourceId;
+ private byte[] data;
+ private int hash;
+
+ public GraphImageDescriptor(ReadGraph graph, Resource resource) throws DatabaseException {
+ this.resource = resource;
+ this.resourceId = resource.getResourceId();
+ this.data = getImageDataBytes(graph);
+ this.hash = (Arrays.hashCode(this.data) * 31 + (int)(resourceId>>>32)) * 31 + ((int)resourceId);
+ }
+
+ private byte[] getImageDataBytes(RequestProcessor processor) throws DatabaseException {
+ return processor.syncRequest(new PossibleValue<byte[]>(resource, Bindings.BYTE_ARRAY));
+ }
+
+ private ImageData toImageData(byte[] data) throws SWTException {
+ ImageData[] images = new ImageLoader().load(new ByteArrayInputStream(data));
+ if (images.length == 0)
+ SWT.error(SWT.ERROR_INVALID_IMAGE, null, "No images found in image data of resource $" + resourceId);
+ return images[0];
+ }
+
+ @Override
+ public ImageData getImageData() {
+ try {
+ ImageData imageData = toImageData(data);
+ return imageData;
+ } catch (SWTException e) {
+ return null;
+ }
+ }
+
+ public Resource getResource() {
+ return resource;
+ }
+
+ @Override
+ public String toString() {
+ return super.toString() + "[" + resource + "]";
+ }
+
+ @Override
+ public int hashCode() {
+ return hash;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ GraphImageDescriptor other = (GraphImageDescriptor) obj;
+ if (!resource.equals(other.resource))
+ return false;
+ if (!Arrays.equals(data, other.data))
+ return false;
+ return true;
+ }
+
+}