From d62e2e42e2d451c423dcb45fac99a8c7940cce0f Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 11 Apr 2011 17:24:27 -0700 Subject: Add avatar label widget to be used in task editor Change-Id: I892d30cf0e8e013bfdbf4940eefc5be4d69da696 Signed-off-by: Kevin Sawicki Signed-off-by: Chris Aniszczyk --- .../mylyn/github/ui/internal/AvatarLabel.java | 161 +++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 org.eclipse.mylyn.github.ui/src/org/eclipse/mylyn/github/ui/internal/AvatarLabel.java 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 00000000..881de7bb --- /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(); + } + }); + } + +} -- cgit v1.2.3