2016-12-09 9 views
1

私は自分の問題を説明できることを願っています。 私は、左側からツリーの数と右側から1つのツリーを含むことができるダイアログに取り組んでいます。 私は左から右へ線を描く必要がありますが、なんらかの理由で同じ木のアイテム境界を取得しています。 たとえば、ツリー1で最初のアイテムが選択されると、ツリー2の最初のアイテムと同じ境界位置が返されます。 どのようにしてアイテムの境界を取得するのですか?ウィザードを基準に制御境界を取得する方法

import org.eclipse.draw2d.ColorConstants; 
import org.eclipse.jface.viewers.ISelectionChangedListener; 
import org.eclipse.jface.viewers.IStructuredSelection; 
import org.eclipse.jface.viewers.SelectionChangedEvent; 
import org.eclipse.jface.viewers.TreeViewer; 
import org.eclipse.swt.SWT; 
import org.eclipse.swt.custom.SashForm; 
import org.eclipse.swt.custom.ScrolledComposite; 
import org.eclipse.swt.events.SelectionEvent; 
import org.eclipse.swt.events.SelectionListener; 
import org.eclipse.swt.graphics.Rectangle; 
import org.eclipse.swt.layout.FillLayout; 
import org.eclipse.swt.layout.GridData; 
import org.eclipse.swt.layout.GridLayout; 
import org.eclipse.swt.widgets.Composite; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Event; 
import org.eclipse.swt.widgets.Label; 
import org.eclipse.swt.widgets.Listener; 
import org.eclipse.swt.widgets.Shell; 
import org.eclipse.swt.widgets.TreeItem; 

public class SWTSashForm 
{ 
    public static void main (String [] args) { 
     Display display = new Display(); 
     Shell shell = new Shell(display); 

     shell.setLayout(new FillLayout()); 

     SashForm sashForm = new SashForm(shell, SWT.HORIZONTAL); 


     Composite composite = new Composite(sashForm, SWT.NONE); 
     composite.setLayout(new GridLayout()); 
     composite.setLayoutData(new GridData(GridData.FILL_BOTH)); 
     composite.setBackground(ColorConstants.white); 


     for (int i = 0; i < 3; i++) { 

      Label lbl = new Label(composite, SWT.NONE); 
      lbl.setText("Tree " + i); 

      // Configure scrolled composite 
      ScrolledComposite scrolledComposite = new ScrolledComposite(composite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER); 
      scrolledComposite.setLayout(new GridLayout()); 
      scrolledComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 
      scrolledComposite.setExpandVertical(true); 
      scrolledComposite.setExpandHorizontal(true); 
      scrolledComposite.setAlwaysShowScrollBars(true); 
      scrolledComposite.setMinSize(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT)); 

      // Add content to scrolled composite 
      Composite scrolledContent = new Composite(scrolledComposite, SWT.NONE); 
      scrolledContent.setLayout(new GridLayout()); 
      GridData gridData = new GridData(); 
      gridData.horizontalAlignment = SWT.FILL; 
      gridData.grabExcessHorizontalSpace = true; 
      gridData.verticalAlignment = SWT.FILL; 
      gridData.grabExcessVerticalSpace = true; 
      scrolledContent.setLayoutData(gridData); 
      scrolledComposite.setContent(scrolledContent); 

      final TreeViewer tree = new TreeViewer(scrolledContent); 
      for(int loopIndex0 = 0; loopIndex0 < 10; loopIndex0++) { 
       TreeItem treeItem0 = new TreeItem(tree.getTree(), 0); 
       treeItem0.setText("Level 0 Item "+ loopIndex0); 

       for(int loopIndex1 = 0; loopIndex1 < 10; loopIndex1++) { 
        TreeItem treeItem1 = new TreeItem(treeItem0, 0); 
        treeItem1.setText("Level 1 Item "+ loopIndex1); 

        for(int loopIndex2 = 0; loopIndex2 < 10; loopIndex2++) { 
         TreeItem treeItem2 = new TreeItem(treeItem1, 0); 
         treeItem2.setText("Level 2 Item "+ loopIndex2); 
        } 
       } 
      } 
      tree.getTree().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true)); 


      tree.getTree().addListener(SWT.Selection, new Listener() { 
       public void handleEvent(Event e) { 
        TreeItem[] selection = tree.getTree().getSelection(); 
        for (int i = 0; i < selection.length; i++){ 
         TreeItem item = selection[i]; 
         Rectangle bounds = item.getBounds(); 
         System.out.println("Tree item bounds y " + bounds.y ); 
        } 
       } }); 
     } 
     new TreeViewer(sashForm); 
     shell.open(); 

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

} 

答えて

2

項目の境界は常に親木からの相対である:ここでは

は、コードがあります。

Point displayPos = tree.getTree().toDisplay(bounds.x, bounds.y); 

をしてから使用して(たとえば、シェルなど)別のコントロールに相対的であるために、この位置を変換します:あなたが使用してディスプレイからの相対的な位置を変換することができ

Point pos = shell.toControl(displayPos); 
+0

感謝、その作品! –

関連する問題