summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKevin Sawicki2011-04-11 20:24:27 (EDT)
committer Chris Aniszczyk2011-04-12 09:34:54 (EDT)
commitd62e2e42e2d451c423dcb45fac99a8c7940cce0f (patch)
tree7a94a38f14cca2b6ccac0a77542ba180a6905d12
parent80a1f56f72843996c600bda01bfd315e73f815ca (diff)
downloadegit-github-d62e2e42e2d451c423dcb45fac99a8c7940cce0f.zip
egit-github-d62e2e42e2d451c423dcb45fac99a8c7940cce0f.tar.gz
egit-github-d62e2e42e2d451c423dcb45fac99a8c7940cce0f.tar.bz2
Add avatar label widget to be used in task editorrefs/changes/63/3063/2
Change-Id: I892d30cf0e8e013bfdbf4940eefc5be4d69da696 Signed-off-by: Kevin Sawicki <kevin@github.com> Signed-off-by: Chris Aniszczyk <caniszczyk@gmail.com>
-rw-r--r--org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/AvatarLabel.java161
1 files changed, 161 insertions, 0 deletions
diff --git a/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/AvatarLabel.java b/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/AvatarLabel.java
new file mode 100644
index 0000000..881de7b
--- /dev/null
+++ b/org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/AvatarLabel.java
@@ -0,0 +1,161 @@
+/*******************************************************************************
+ * Copyright (c) 2011 GitHub Inc.
+ * 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:
+ * Kevin Sawicki (GitHub Inc.) - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.mylyn.github.ui.internal;
+
+import org.eclipse.jface.layout.GridDataFactory;
+import org.eclipse.jface.layout.GridLayoutFactory;
+import org.eclipse.mylyn.github.ui.internal.AvatarStore.IAvatarCallback;
+import org.eclipse.mylyn.tasks.core.IRepositoryPerson;
+import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.DisposeEvent;
+import org.eclipse.swt.events.DisposeListener;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.graphics.ImageData;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.forms.IFormColors;
+import org.eclipse.ui.forms.widgets.FormToolkit;
+
+/**
+ * Avatar label displaying an image and label for a avatar url.
+ */
+public class AvatarLabel implements IAvatarCallback {
+
+ /**
+ * AVATAR_SIZE
+ */
+ public static final int AVATAR_SIZE = 42;
+
+ private Composite displayArea;
+ private Composite avatarImage;
+ private AvatarStore store;
+ private IRepositoryPerson person;
+ private TaskAttribute attribute;
+
+ /**
+ * Create avatar label
+ *
+ * @param store
+ * @param person
+ * @param attribute
+ */
+ public AvatarLabel(AvatarStore store, IRepositoryPerson person,
+ TaskAttribute attribute) {
+ this.store = store;
+ this.person = person;
+ this.attribute = attribute;
+ }
+
+ /**
+ * Set visible
+ *
+ * @param visible
+ * @return this label
+ */
+ public AvatarLabel setVisible(boolean visible) {
+ if (!displayArea.isDisposed()) {
+ displayArea.setVisible(visible);
+ ((GridData) displayArea.getLayoutData()).exclude = !visible;
+ displayArea.getParent().layout(true, true);
+ }
+ return this;
+ }
+
+ /**
+ * Layout label
+ *
+ * @return this label
+ */
+ public AvatarLabel layout() {
+ if (!displayArea.isDisposed())
+ displayArea.getParent().layout(true, true);
+ return this;
+ }
+
+ /**
+ * Get main composite
+ *
+ * @return composite
+ */
+ public Composite getControl() {
+ return this.displayArea;
+ }
+
+ /**
+ * Create label
+ *
+ * @param parent
+ * @param toolkit
+ * @return this label
+ */
+ public AvatarLabel create(Composite parent, FormToolkit toolkit) {
+ displayArea = new Composite(parent, SWT.NONE);
+ GridLayoutFactory.fillDefaults().margins(2, 2).spacing(1, 1)
+ .applyTo(displayArea);
+ GridDataFactory.fillDefaults().span(1, 2).grab(false, false)
+ .applyTo(displayArea);
+ toolkit.adapt(displayArea, false, false);
+
+ avatarImage = new Composite(displayArea, SWT.NONE);
+ if (person != null)
+ avatarImage.setToolTipText(person.getPersonId());
+
+ GridDataFactory.swtDefaults().grab(false, false)
+ .align(SWT.CENTER, SWT.CENTER).hint(AVATAR_SIZE, AVATAR_SIZE)
+ .applyTo(avatarImage);
+
+ Label label = toolkit.createLabel(displayArea, attribute.getMetaData()
+ .getLabel());
+ label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
+ label.setToolTipText(avatarImage.getToolTipText());
+
+ ImageData data = store.getAvatar(attribute.getValue());
+ if (data != null)
+ setImage(store.getScaledImage(AVATAR_SIZE, data));
+ else {
+ store.loadAvatar(attribute.getValue(), this);
+ setVisible(false);
+ }
+
+ return this;
+ }
+
+ private AvatarLabel setImage(final Image image) {
+ if (!avatarImage.isDisposed()) {
+ avatarImage.setBackgroundImage(image);
+ avatarImage.addDisposeListener(new DisposeListener() {
+
+ public void widgetDisposed(DisposeEvent e) {
+ image.dispose();
+ }
+ });
+ }
+ return this;
+ }
+
+ /**
+ * @see org.eclipse.mylyn.github.ui.internal.AvatarStore.IAvatarCallback#loaded(org.eclipse.swt.graphics.ImageData,
+ * org.eclipse.mylyn.github.ui.internal.AvatarStore)
+ */
+ public void loaded(final ImageData data, final AvatarStore store) {
+ PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {
+
+ public void run() {
+ Image image = store.getScaledImage(AVATAR_SIZE, data);
+ setImage(image).setVisible(true).layout();
+ }
+ });
+ }
+
+}