Bug 128574

Summary: TreeEditors don't follow Items when expanding items
Product: [Eclipse Project] Platform Reporter: Rutger Ovidius <ovidr>
Component: SWTAssignee: Steve Northover <steve_northover>
Status: NEW --- QA Contact:
Severity: normal    
Priority: P3 CC: jussi.suistomaa, michael.krkoska, peter.kullmann, veronika_irvine, wcrisman
Version: 3.2   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   
See Also: https://bugs.eclipse.org/bugs/show_bug.cgi?id=551999
Whiteboard:
Bug Depends on: 73928    
Bug Blocks:    

Description Rutger Ovidius CLA 2006-02-19 23:08:10 EST
This example shows:

1. After the delay the node is expanded, and the tree editors no longer align with the items to which they are attached.  (Same when collapsing)

2. If you use the arrow up/down keyboard keys to move up and down the items, the TreeEdiors don't stick with their items as the scrollbar scrolls down.

3.2M5 Win32.

----------------

  public static void addTTE(Tree t, TreeItem ti) {
    System.err.println(ti);
    TreeEditor editor = new TreeEditor(t);
    ti.setData("editor", editor);
    editor.horizontalAlignment = SWT.LEFT;
    editor.grabHorizontal = true;
    editor.setEditor(new myCanvas(t, SWT.NONE), ti, 1);
  }

  static class myCanvas extends Canvas implements PaintListener {
    Image i;

    public myCanvas(Composite parent, int style) {
      super(parent, SWT.NO_BACKGROUND);
      addPaintListener(this);
      i = new Image(getDisplay(), 16, 16);
      GC gc = new GC(i);
      gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_BLUE));
      gc.fillRectangle(0, 0, 16, 16);
      gc.dispose();
    }

    public void paintControl(PaintEvent e) {
      e.gc.drawImage(i, 0, 0, 16, 16, e.x, e.y, e.width, e.height);
    }
  }

  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    final Tree tree = new Tree(shell, SWT.VIRTUAL | SWT.BORDER | SWT.MULTI);
    new TreeColumn(tree, SWT.LEFT).setWidth(150);
    new TreeColumn(tree, SWT.LEFT).setWidth(150);
    tree.setHeaderVisible(true);
    
    tree.addListener(SWT.SetData, new Listener() {
      public void handleEvent(Event event) {
        TreeItem item = (TreeItem) event.item;
        TreeItem parentItem = item.getParentItem();
        String text = null;
        if (parentItem == null) {
          text = "node " + tree.indexOf(item);
        } else {
          text = parentItem.getText() + " - " + parentItem.indexOf(item);
        }
       
        int ind = tree.indexOf(item);
        if (ind == 1)
          item.setItemCount(5);
        
        if (ind > 1 && ind < 5) {
          text = "HERE >>>";
          addTTE(tree, item);
           
        }
        item.setText(text);
        
      }
    });
    tree.setItemCount(20);

    shell.setSize(400, 300);
    shell.open();

    display.timerExec(4000, new Runnable() {
      public void run() {
        TreeItem[] items = tree.getItems();
        for (int i =0; i < items.length; ++i)
          items[i].setExpanded(true);
      }
      
    });
    
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
Comment 1 Veronika Irvine CLA 2006-07-05 16:35:04 EDT
Solution depends on Bug 73928.
Comment 2 Steve Northover CLA 2006-07-06 09:26:13 EDT
*** Bug 142556 has been marked as a duplicate of this bug. ***
Comment 3 Steve Northover CLA 2007-02-20 14:09:05 EST
*** Bug 174585 has been marked as a duplicate of this bug. ***
Comment 4 Steve Northover CLA 2007-03-31 10:00:19 EDT
Here is a work around for this code:

import org.eclipse.swt.*;
import org.eclipse.swt.custom.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class PR_128574 {
	public static void addTTE(Tree t, TreeItem ti) {
		System.err.println(ti);
		TreeEditor editor = new TreeEditor(t);
		ti.setData("editor", editor);
		editor.horizontalAlignment = SWT.LEFT;
		editor.grabHorizontal = true;
		Canvas canvas = new myCanvas(t, SWT.NONE);
		editor.setEditor(canvas, ti, 1);
		canvas.setData ("_EDITOR", editor);
	}

	static class myCanvas extends Canvas implements PaintListener {
		Image i;

		public myCanvas(Composite parent, int style) {
			super(parent, SWT.NO_BACKGROUND);
			addPaintListener(this);
			i = new Image(getDisplay(), 16, 16);
			GC gc = new GC(i);
			gc.setBackground(getDisplay().getSystemColor(SWT.COLOR_BLUE));
			gc.fillRectangle(0, 0, 16, 16);
			gc.dispose();
		}

		public void paintControl(PaintEvent e) {
			e.gc.drawImage(i, 0, 0, 16, 16, e.x, e.y, e.width, e.height);
		}
	}

	public static void main(String[] args) {
		Display display = new Display();
		final Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		final Tree tree = new Tree(shell, SWT.VIRTUAL | SWT.BORDER | SWT.MULTI);
		new TreeColumn(tree, SWT.LEFT).setWidth(150);
		new TreeColumn(tree, SWT.LEFT).setWidth(150);
		tree.setHeaderVisible(true);
		tree.addListener(SWT.SetData, new Listener() {
			public void handleEvent(Event event) {
				TreeItem item = (TreeItem) event.item;
				TreeItem parentItem = item.getParentItem();
				String text = null;
				if (parentItem == null) {
					text = "node " + tree.indexOf(item);
				} else {
					text = parentItem.getText() + " - "
							+ parentItem.indexOf(item);
				}
				int ind = tree.indexOf(item);
				if (ind == 1)
					item.setItemCount(5);
				if (ind > 1 && ind < 5) {
					text = "HERE >>>";
					addTTE(tree, item);
				}
				item.setText(text);
			}
		});
		tree.setItemCount(20);
		shell.setSize(400, 300);
		shell.open();
		display.timerExec(4000, new Runnable() {
			public void run() {
				TreeItem[] items = tree.getItems();
				for (int i = 0; i < items.length; ++i)
					items[i].setExpanded(true);
				Control [] children = tree.getChildren ();
				for (int i=0; i<children.length; i++) {
					TreeEditor editor = (TreeEditor) children [i].getData("_EDITOR");
					if (editor != null) editor.layout();
				}
			}

		});
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}
Comment 5 Steve Northover CLA 2007-03-31 10:18:17 EDT
You could also use Control.notifyListeners() to send a fake SWT.Expand event.
Comment 6 Steve Northover CLA 2007-03-31 11:44:00 EDT
Changed the title to reflect the issue 1 (the use of Tree.setExpanded()).  The keyboard issues should be fixed. 
Comment 7 Peter Kullmann CLA 2007-07-02 09:51:41 EDT
The fix for the keyboard issue has a side-effect for our application:
Since ControlEditor hooks key events in its parent Composite, the
Composite takes the focus and doesn't give it down to the actual
control when tabbed into. In our application (which shows multiple sibling ControlEditors side by side as a form) this means that tabbing no longer works. 

Is there a workaround for this? Subclassing the Composite? Using my own implementation of ControlEditor? Unfortunately
the NO_FOCUS style is not available in a "normal" Composite (only in
Canvasses).
Comment 8 Duong Nguyen CLA 2008-02-27 11:30:50 EST
*** Bug 214617 has been marked as a duplicate of this bug. ***
Comment 9 Peter Kullmann CLA 2009-05-06 07:56:29 EDT
(In reply to comment #7)
> The fix for the keyboard issue has a side-effect for our application:
> Since ControlEditor hooks key events in its parent Composite, the
> Composite takes the focus and doesn't give it down to the actual
> control when tabbed into. In our application (which shows multiple sibling
> ControlEditors side by side as a form) this means that tabbing no longer works. 
> 
> Is there a workaround for this? Subclassing the Composite? Using my own
> implementation of ControlEditor? Unfortunately
> the NO_FOCUS style is not available in a "normal" Composite (only in
> Canvasses).
> 

A workable fix is to subclass the composite and overwrite forceFocus() to always return false.
Comment 10 Eclipse Webmaster CLA 2019-09-06 16:11:08 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.