Improve error reporting in the branch dialog
[egit/imyousuf.git] / org.spearce.egit.ui / src / org / spearce / egit / ui / internal / components / CenteredImageLabelProvider.java
blob8752a6082011fd508cd3839a9c4c1676949734b3
1 /*******************************************************************************
2 * Copyright (C) 2008, Marek Zawirski <marek.zawirski@gmail.com>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * See LICENSE for the full license text, also available.
7 *******************************************************************************/
8 package org.spearce.egit.ui.internal.components;
10 import org.eclipse.jface.viewers.OwnerDrawLabelProvider;
11 import org.eclipse.swt.graphics.Image;
12 import org.eclipse.swt.graphics.Rectangle;
13 import org.eclipse.swt.widgets.Event;
14 import org.eclipse.swt.widgets.TableItem;
16 /**
17 * Label provider displaying image centered.
18 * <p>
19 * This implementation is actually workaround for lacking SWT/JFace features.
20 * Code is based on official snippet found on Internet.
22 // FIXME: doesn't work on Mac OS X 10.5 / Eclipse 3.3
23 public abstract class CenteredImageLabelProvider extends OwnerDrawLabelProvider {
24 /**
25 * @param element
26 * element to provide label for.
27 * @return image for provided element.
29 protected abstract Image getImage(final Object element);
31 @Override
32 protected void measure(Event event, Object element) {
33 // empty
36 @Override
37 protected void paint(final Event event, final Object element) {
38 final Image image = getImage(element);
39 final Rectangle bounds = ((TableItem) event.item)
40 .getBounds(event.index);
41 final Rectangle imgBounds = image.getBounds();
42 bounds.width /= 2;
43 bounds.width -= imgBounds.width / 2;
44 bounds.height /= 2;
45 bounds.height -= imgBounds.height / 2;
47 final int x = bounds.width > 0 ? bounds.x + bounds.width : bounds.x;
48 final int y = bounds.height > 0 ? bounds.y + bounds.height : bounds.y;
50 event.gc.drawImage(image, x, y);