使用预设区域创建的SWT TreeCursor小部件

时间:2017-02-16 16:40:03

标签: eclipse swt

请参阅Attachment_TreeCursor。 TreeCursor是为FormPage内的树创建的(对于FormEditor)。

创建FormEditor:

public Task<User> FindByEmailAsync(string email)
{
  return ..
}

创建一个FormPage:

package test.treecursor.handlers;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.forms.editor.FormEditor;

public class TestEditor extends FormEditor {

private TestEditorPage page;

@Override
protected Composite createPageContainer(final Composite parent) {
  return super.createPageContainer(parent);
}

@Override
protected void addPages() {
try {
  this.page = new TestEditorPage(this);
  addPage(this.page);
}
catch (PartInitException e) {
  e.printStackTrace();
  }
}

 @Override
  public void doSave(final IProgressMonitor monitor) {    
 }

 @Override
   public void doSaveAs() {   
 }

 @Override
 public boolean isSaveAsAllowed() {  
   return false;
 }
}

将此编辑器添加到编辑器扩展点:org.eclipse.ui.editors

package test.treecursor.handlers;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TreeCursor;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.editor.FormPage;

public class TestEditorPage extends FormPage {

 public TestEditorPage(final TestEditor testEditor) {
   super(testEditor, "1", "MyTitle");
 }

 @Override
 protected void createFormContent(final IManagedForm managedForm) {
   Composite composite = managedForm.getForm().getBody();
   composite.setLayout(new GridLayout());
   composite.setLayoutData(new GridData(GridData.FILL_BOTH));
   Tree tree = createTree(composite);
   createTreeColumns(tree);
   final TreeCursor cursor = new TreeCursor(tree, SWT.BORDER);
 }

 private Tree createTree(final Composite composite) {
  Tree tree = new Tree(composite, SWT.MULTI | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL);
   tree.setLayout(composite.getLayout());
   tree.setLayoutData(composite.getLayoutData());
   tree.setHeaderVisible(true);
   tree.setLinesVisible(true);
   return tree;
 }

 private void createTreeColumns(final Tree tree) {
   TreeColumn column1 = new TreeColumn(tree, SWT.LEFT);
   column1.setText("First column");
   column1.setWidth(300);
   TreeColumn column2 = new TreeColumn(tree, SWT.LEFT);
   column2.setText("Second column");
   column2.setWidth(300);
 }
}

如果我将SWT样式设置为SWT.TRANSPARENT,我不会再看到这个“补丁”,但我仍然无法在此区域中选择树中的行。 看起来光标从其Form / ScrollableForm的父组合中获取此大小。在tree.getDisplay()的Display对象上,我可以看到字段RECT clickRect具有left,right,bottom和top的值。这些可以用于创建光标吗?我怎么能摆脱这个?

1 个答案:

答案 0 :(得分:0)

我在createTree方法中看到了2个问题:

  1. tree.setLayout(composite.getLayout());:在Tree上设置布局没有意义;布局对于正确定位子项很有用,但在Tree中您不必这样做;这也在Tree documentation

    中指定
      

    请注意,尽管此类是Composite的子类,但它不是   通常有意义的是添加控制子项,或设置布局   它

    因此,您应该删除说明。

  2. tree.setLayoutData(composite.getLayoutData());:您正在尝试重新使用GridData的{​​{1}},但您不能这样做;来自GridData documentation

      

    注意:不要重复使用GridData对象。复合中的每个控件都是   由GridLayout管理,必须有一个唯一的GridData对象。

    您应该创建另一个Composite对象,仅在GridData中使用。

  3. 一旦解决了这些问题,您所看到的区域就会消失。