[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.platform.swt] Re: Windows fires SelectionEvent on tree.setSelection

Here you are...

fabian

Tom Schindl schrieb:
Hi,

Well you could try. If you need immediate help you can use IRC (#eclipse-dev at irc.freenode.net, nick: tomsontom), I'm online today in the late afternoon. So if you ping have snippet post it to this thread and then ping me on IRC.

Greetings from the western part of Austria :-)

Tom

Fabian Zeindl schrieb:
Tom Schindl schrieb:
Looks like a timing issue or are do doing something with Display#asyncExec()? From what you say I construct the following.

You've got a TextWidget and TreeWidget and when entry text you are updating the selection in your Tree and the other way round, right?

true.

I guess we need some code to reproduce this. You should try to write a small snippet you can paste here and we can run.

ok I'll do that. Will you be online again at some time this weekend? Unfortunately my solution is due on monday :-/.


greetings / GrÃÃe
fabian

Tom

Fabian Zeindl schrieb:
Another strange problem: When I remove the SelectionListener right before the setSelection and add it afterwards it still gets called when calling setSelection. Why is that?
I don't know whether this helps, but the SelectionListener gets called _after_ the method which calls setSelection finished. The method itself is a ModifyListener.


fabian

Tom Schindl schrieb:
It is a bug if SWT fires a SelectionEvent when setting a selection programmatically!

Tom

Fabian Zeindl schrieb:
Hi,
for some unknown reason Win32 fires a SelectionEvent when I do tree.setSelection(TreeItem item) where gnu/linux doesn't fire one.
Is there a reason for this behaviour? Funnily enough, when I change the code to be simpler, Windows doesn't fire an event on setSelection.
Is this an SWT-Bug?


thanks in advance
fabian







import org.eclipse.swt.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.events.*;
import java.util.*;

public class Test
{
	public static void main(String[] args)
	{
		new Test();
	}

	private Text text1, text2;
	private Tree tree;
	private TextModifyListener textlistener = new TextModifyListener();
	private TreeSelectionListener treelistener = new TreeSelectionListener();
	private ArrayList<TreeItem> lastCreatedItems = new ArrayList<TreeItem>();

	public Test()
	{
		Display display = new Display();
		Shell shell = new Shell();
		shell.setLayout( new GridLayout(2,false) );

		// Text 1
		text1 = new Text(shell, SWT.BORDER);
		text1.setLayoutData( new GridData(SWT.NONE,SWT.NONE,true,true) );
		text1.addModifyListener(textlistener);

		// Tree
		tree = new Tree(shell, SWT.BORDER);
		tree.setLayoutData( new GridData(SWT.FILL,SWT.FILL,true,true,1,2) );
		tree.addSelectionListener(treelistener);

		// Kinder erzeugen
		TreeItem item = new TreeItem(tree, SWT.NONE);
		item.setText("parent1");
		item = new TreeItem(item, SWT.NONE);
		item.setText("child1");
		item = new TreeItem(tree, SWT.NONE);
		item.setText("parent2");
		item = new TreeItem(item, SWT.NONE);
		item.setText("child2");

		// Text 2
		text2 = new Text(shell, SWT.BORDER);
		text2.setLayoutData( new GridData(SWT.NONE,SWT.NONE,true,true) );
		text2.addModifyListener(textlistener);

		shell.open();
		while (!shell.isDisposed())
			if (!display.readAndDispatch())
				display.sleep();
	}

	private void createTreeItems(ArrayList<String> category)
	{
		// run through list to find last non-empty entry
		int lastNonEmptyLevel = -1;
		for (int i = (category.size()-1); i >= 0; i--)
		{
			if (!category.get(i).matches("\\s*"))
			{
				lastNonEmptyLevel = i;
				break;
			}
		}  

		// delete all manually created items
		for (TreeItem item : lastCreatedItems)
			item.dispose();
		lastCreatedItems.clear();

		// create TreeItems for category
		TreeItem parent = null;
		for (int i=0; i <= lastNonEmptyLevel; i++)
		{   
			// Search for matching item and created
			TreeItem item = getMatchingChild(parent, category.get(i));
			if (item == null)
			{   
				item = createChild(parent, category.get(i));
				lastCreatedItems.add( item );
			}

			// expand item
			if (parent != null)
				parent.setExpanded(true);

			parent = item;
		}  

		// Removing the Listener doesn't work
		// this.tree.removeSelectionListener( this.treelistener );

		// comment out -> no SelectionEvents
		if (parent != null) {
			tree.setSelection(parent);
		}

		// this.tree.addSelectionListener( this.treelistener );
	}

	private class TextModifyListener implements ModifyListener
	{   
		public void modifyText(ModifyEvent e)
		{   
			System.out.println( "text: ModifyEvent!" );

			ArrayList<String> list = new ArrayList<String>();
			list.add(text1.getText());
			list.add(text2.getText());
			createTreeItems( list );

			System.out.println( "text: ModifyEvent dispatched!" );
		}
	}

	private class TreeSelectionListener extends SelectionAdapter
	{   
		public void widgetSelected(SelectionEvent e)
		{   
			System.out.println( "tree: SelectionEvent: " + e.hashCode() );

			if (tree.getSelectionCount() > 0)
			{
				TreeItem selectedItem = tree.getSelection()[0];

				// depth of selection
				int depth = 0;
				TreeItem parent = selectedItem;
				while ((parent = parent.getParentItem()) != null)
					depth ++;

				// fill TextFields
				switch (depth)
				{   
					case 0:
						text1.setText( selectedItem.getText() );
						text2.setText("");
						break;
					case 1:
						text1.setText( selectedItem.getParentItem().getText() );
						text2.setText( selectedItem.getText() );
						break;
				}
			}

			System.out.println( "tree: SelectionEvent dispatched!" );
		}
	}

	private TreeItem getMatchingChild(TreeItem parent, String text)
	{   
		TreeItem[] children = (parent != null ? parent.getItems() : this.tree.getItems());

		for (TreeItem item : children)
		{   
			if (item.getText().equals(text))
			{   
				return item;
			}
		}

		return null;
	}

	private TreeItem createChild(TreeItem parent, String text)
	{   
		TreeItem[] children = (parent != null ? parent.getItems() : this.tree.getItems());

		int index=0;
		while (index < children.length && children[index].getText().compareTo(text) < 0)
		{   
			index++;
		}  

		TreeItem newItem = (parent != null ? new TreeItem(parent, SWT.NONE, index) : new TreeItem(this.tree, SWT.NONE, index));
		newItem.setText(text);

		return newItem;
	}
}