SWT: Bild in einer Tabelle

drexpaq

Grünschnabel
Hallo,

ich habe mit ITableLabelProvider eine Tabelle befüllt. Leider möchte ich jetzt ein Image statt einem Text in eine Zelle packen. Das Bild würde ich gern über Image.createFromURL erzeugen lassen.
Kann mir jemand man sagen wo und wie ich das Bild einfügen kann?

Code:
package demoRCP;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.jface.resource.ImageDescriptor;
import java.net.URL.*;

import flickrdemo.Recents;

public class View extends ViewPart {
	
	private static final int ROWS = 10;
	private static final int COLUMNS = 6;
	
	public static final String ID = "Demo2.view";

	private TableViewer viewer;

	/**
	 * The content provider class is responsible for providing objects to the
	 * view. It can wrap existing objects in adapters or simply return objects
	 * as-is. These objects may be sensitive to the current input of the view,
	 * or ignore it and always show the same content (like Task List, for
	 * example).
	 */
	class ViewContentProvider implements IStructuredContentProvider {
		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
		}

		public void dispose() {
		}

		public Object[] getElements(Object parent) {
			List<String> l = Recents.getRecentPhotos(ROWS);
			
			List<String[]> buffer = new ArrayList<String[]>();
			
			for (String id : l)
			{				
				String[] row = new String[COLUMNS];
				row[0] = id;
				row[1] = Recents.getOwner(id);
				row[2] = Recents.getDateTaken(id).toString();
				row[3] = Recents.getTags(id).toString();
				row[4] = Recents.getURL(id);
				
				buffer.add(row);
				
			}
			
			return buffer.toArray();
		}
	}

	class ViewLabelProvider extends LabelProvider implements
			ITableLabelProvider {
		
		public String getColumnText(Object element, int columnIndex) {
			String[] row = (String[]) element;
			String result = row[columnIndex];			
			return result;
		}

		public Image getColumnImage(Object obj, int index) {			
			return getImage(obj);
		}

		public Image getImage(Object obj) {
			return PlatformUI.getWorkbench().getSharedImages().getImage(
					ISharedImages.IMG_OBJ_ELEMENT);
		}
	}

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		viewer = new TableViewer(parent, SWT.NONE);
		viewer.setContentProvider(new ViewContentProvider());
		viewer.setLabelProvider(new ViewLabelProvider());
		final Table table = viewer.getTable();
		viewer.setColumnProperties(initColumnProperties(table));
		viewer.setInput(this);
		viewer.getTable().setHeaderVisible(true);
		getSite().setSelectionProvider(viewer);
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		viewer.getControl().setFocus();
	}
	
	private String[] initColumnProperties(final Table table) {
		String[] result = new String[]{"ID", "Owner", "Date", "Tags", "URL", "Thumb"};
		for (int i = 0; i < COLUMNS; i++) {
			TableColumn tableColumn = new TableColumn(table, SWT.NONE);
			tableColumn.setText(result[i]);				
			if (i == 0) {
				tableColumn.setWidth(100);
			} else {
				tableColumn.setWidth(150);
			}
		}
		return result;
	}
}
 
Zurück