Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2011 GitHub Inc. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* Kevin Sawicki (GitHub Inc.) - initial API and implementation |
10 |
*******************************************************************************/ |
11 |
|
12 |
package org.eclipse.mylyn.internal.commons.ui.identity.gravatar; |
13 |
|
14 |
import java.io.ByteArrayInputStream; |
15 |
|
16 |
import org.eclipse.core.runtime.Assert; |
17 |
import org.eclipse.jface.resource.ImageDescriptor; |
18 |
import org.eclipse.mylyn.commons.identity.gravatar.Gravatar; |
19 |
import org.eclipse.swt.SWT; |
20 |
import org.eclipse.swt.SWTException; |
21 |
import org.eclipse.swt.graphics.GC; |
22 |
import org.eclipse.swt.graphics.Image; |
23 |
import org.eclipse.swt.graphics.ImageData; |
24 |
import org.eclipse.swt.graphics.ImageLoader; |
25 |
import org.eclipse.swt.graphics.Rectangle; |
26 |
import org.eclipse.swt.widgets.Display; |
27 |
import org.eclipse.ui.PlatformUI; |
28 |
|
29 |
/** |
30 |
* Gravatar image class. |
31 |
* |
32 |
* @author Kevin Sawicki (kevin@github.com) |
33 |
*/ |
34 |
public class GravatarImage { |
35 |
|
36 |
private final Gravatar gravatar; |
37 |
|
38 |
private ImageData data; |
39 |
|
40 |
/** |
41 |
* Create avatar image from avatar |
42 |
* |
43 |
* @param avatar |
44 |
*/ |
45 |
public GravatarImage(Gravatar avatar) { |
46 |
Assert.isNotNull(avatar, "Avatar cannot be null"); //$NON-NLS-1$ |
47 |
this.gravatar = avatar; |
48 |
} |
49 |
|
50 |
/** |
51 |
* Get avatar image data |
52 |
* |
53 |
* @return image data |
54 |
*/ |
55 |
public ImageData getData() { |
56 |
if (this.data != null) { |
57 |
return this.data; |
58 |
} |
59 |
|
60 |
try { |
61 |
ImageData[] images = new ImageLoader().load(new ByteArrayInputStream(gravatar.getBytes())); |
62 |
if (images.length > 0) { |
63 |
this.data = images[0]; |
64 |
} else { |
65 |
this.data = ImageDescriptor.getMissingImageDescriptor().getImageData(); |
66 |
} |
67 |
} catch (SWTException exception) { |
68 |
this.data = ImageDescriptor.getMissingImageDescriptor().getImageData(); |
69 |
} |
70 |
return this.data; |
71 |
} |
72 |
|
73 |
/** |
74 |
* Get avatar image scaled to specified size. The returned image should be managed and properly disposed of by the |
75 |
* caller. |
76 |
* |
77 |
* @param size |
78 |
* @return scaled image |
79 |
*/ |
80 |
public Image getScaledImage(int size) { |
81 |
Display display = PlatformUI.getWorkbench().getDisplay(); |
82 |
Image image = new Image(display, getData()); |
83 |
Rectangle sourceBounds = image.getBounds(); |
84 |
|
85 |
// Return original image and don't scale if size matches request |
86 |
if (sourceBounds.width == size) { |
87 |
return image; |
88 |
} |
89 |
|
90 |
Image scaled = new Image(display, size, size); |
91 |
GC gc = new GC(scaled); |
92 |
try { |
93 |
gc.setAntialias(SWT.ON); |
94 |
gc.setInterpolation(SWT.HIGH); |
95 |
Rectangle targetBounds = scaled.getBounds(); |
96 |
gc.drawImage(image, 0, 0, sourceBounds.width, sourceBounds.height, 0, 0, targetBounds.width, |
97 |
targetBounds.height); |
98 |
} finally { |
99 |
gc.dispose(); |
100 |
image.dispose(); |
101 |
} |
102 |
return scaled; |
103 |
} |
104 |
|
105 |
} |