Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » Eclipse Examples » Table Snippet349 Drag and Drop visual problem
Table Snippet349 Drag and Drop visual problem [message #1019174] Fri, 15 March 2013 07:16 Go to next message
Jürgen Weinberger is currently offline Jürgen WeinbergerFriend
Messages: 36
Registered: August 2012
Member
Hello! I found this Table Snippet349 which describes my Problem pretty well. In the sample code a custom drawn table with different image sizes is described. I changed some things to describe my problem at work.
First i changed the Table to a Tree, this works pretty well with different sizes of the images. Second I added a simple Drag and Drop Support and this is where the trouble starts. When i try to drag an element especially one with a wider image the text of the item gets cut of. It seems that the wider the image the more of the text gets cut of. (See the screenshots).

For me it looks like that the DnD is not using the Paint/Mesaure/Erase Events and instead calulates the size based on the TreeItem which is not correct.

Is there a way to get rid of this problem.
regards weinma


index.php/fa/13885/0/

package test;

/*******************************************************************************
 * Copyright (c) 2000, 2010 IBM Corporation and others.
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

/* 
 * Table example snippet: Show a Table with images of various sizes
 *
 * For more info on custom-drawing TableItem and TreeItem content see 
 * http://www.eclipse.org/articles/article.php?file=Article-CustomDrawingTableAndTreeItems/index.html
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.2
 */
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DragSourceEvent;
import org.eclipse.swt.dnd.DragSourceListener;
import org.eclipse.swt.dnd.TextTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.graphics.FontMetrics;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;

public class TableMain {

public static void main(String [] args) {
	final int COLUMN_COUNT = 1;
	final int TEXT_MARGIN = 3;
	final String KEY_WIDTHS = "widths";
	final String KEY_IMAGES = "images";
	Display display = new Display();
	Image[] images = new Image[4];
	images[0] = createImage(display, 16, 16);
	images[1] = createImage(display, 32, 16);
	images[2] = createImage(display, 48, 16);

	Shell shell = new Shell(display);
	shell.setLayout(new FillLayout());
	Tree tree = new Tree(shell, SWT.NONE);
	for (int i = 0; i < COLUMN_COUNT; i++) {
		new TreeColumn(tree, SWT.NONE);
	}
	for (int i = 0; i < 8; i++) {
		TreeItem item = new TreeItem(tree, SWT.NONE);
		Image[] itemImages = new Image[COLUMN_COUNT];
		item.setData(KEY_IMAGES, itemImages);
		for (int j = 0; j < COLUMN_COUNT; j++) {
			item.setText(j, "item " + i + " col " + j);
			itemImages[j] = images[(i * COLUMN_COUNT + j) % images.length];
		}
	}

	/*
	 * NOTE: MeasureItem, PaintItem and EraseItem are called repeatedly.
	 * Therefore, it is critical for performance that these methods be
	 * as efficient as possible.
	 */
	final int itemHeight = tree.getItemHeight();
	GC gc = new GC(tree);
	FontMetrics metrics = gc.getFontMetrics();
	final int fontHeight = metrics.getHeight();
	gc.dispose();
	Listener paintListener = new Listener() {
		public void handleEvent(Event event) {		
			switch (event.type) {
				case SWT.MeasureItem: {
					int column = event.index;
					TreeItem item = (TreeItem)event.item;
					Image[] images = (Image[])item.getData(KEY_IMAGES);
					Image image = images[column];
					if (image == null) {
						/* don't change the native-calculated event.width */
						break;
					}
					int[] cachedWidths = (int[])item.getData(KEY_WIDTHS);
					if (cachedWidths == null) {
						cachedWidths = new int[COLUMN_COUNT];
						item.setData(KEY_WIDTHS, cachedWidths);
					}
					if (cachedWidths[column] == 0) {
						int width = image.getBounds().width + 2 * TEXT_MARGIN;
						GC gc = new GC(item.getParent());
						width += gc.stringExtent(item.getText()).x;
						gc.dispose();
						cachedWidths[column] = width;
					}
					event.width = cachedWidths[column];
					break;
				}
				case SWT.EraseItem: {
					int column = event.index;
					TreeItem item = (TreeItem)event.item;
					Image[] images = (Image[])item.getData(KEY_IMAGES);
					Image image = images[column];
					if (image == null) {
						break;
					}
					/* disable the native drawing of this item */
					event.detail &= ~SWT.FOREGROUND;
					break;
				}
				case SWT.PaintItem: {
					int column = event.index;
					TreeItem item = (TreeItem)event.item;
					Image[] images = (Image[])item.getData(KEY_IMAGES);
					Image image = images[column];
					if (image == null) {
						/* this item is drawn natively, don't touch it*/
						break;
					}

					int x = event.x;
					event.gc.drawImage(image, x, event.y + (itemHeight - image.getBounds().height) / 2);
					x += image.getBounds().width + TEXT_MARGIN;
					event.gc.drawString(item.getText(column), x, event.y + (itemHeight - fontHeight) / 2);
					break;
				}
			}
		}
	};		
	tree.addListener(SWT.MeasureItem, paintListener);
	tree.addListener(SWT.EraseItem, paintListener);
	tree.addListener(SWT.PaintItem, paintListener);		

	TreeViewer viewer = new TreeViewer(tree);
	viewer.addDragSupport(DND.DROP_COPY | DND.DROP_MOVE, new Transfer[]{TextTransfer.getInstance()}, new DragSourceListener() {
		
		public void dragStart(DragSourceEvent event) {
			// TODO Auto-generated method stub
			
		}
		
		public void dragSetData(DragSourceEvent event) {
			// TODO Auto-generated method stub
			
		}
		
		public void dragFinished(DragSourceEvent event) {
			// TODO Auto-generated method stub
			
		}
	});
	
	for (int i = 0; i < COLUMN_COUNT; i++) {
		tree.getColumn(i).pack();
	}
	shell.pack();
	shell.open();
	while (!shell.isDisposed()) {
		if (!display.readAndDispatch()) display.sleep();
	}
	for (int i = 0; i < images.length; i++) {
		if (images[i] != null) {
			images[i].dispose();
		}
	}
	display.dispose();
}

static Image createImage(Display display, int width, int height) {
	Image result = new Image(display, width, height);
	GC gc = new GC(result);
	for (int x = -height; x < width; x += 4) {
		gc.drawLine(x, 0, x + height, height);
	}
	gc.dispose();
	return result;
}
}
 


index.php/fa/13885/0/
Re: Table Snippet349 Drag and Drop visual problem [message #1023912 is a reply to message #1019174] Mon, 25 March 2013 12:56 Go to previous message
Jürgen Weinberger is currently offline Jürgen WeinbergerFriend
Messages: 36
Registered: August 2012
Member
Could some one please move this topic to the "Eclipse Platform" Forum. I think this is wrong here. Thanks in advance!
Previous Topic:Problem while running the eclipse.
Next Topic:ServiceLoader Example
Goto Forum:
  


Current Time: Sat Dec 20 01:43:11 GMT 2014

Powered by FUDForum. Page generated in 0.03240 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software