2016-12-09 1 views
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

감사

당신은 사용하여 디스플레이에 상대적으로 위치를 변환 할 수 있습니다 , 그 작품! –

관련 문제