Eclipse SWT:启用垂直滚动时无法滚动文本

时间:2019-03-04 07:10:39

标签: java eclipse-plugin swt

我在Text内有一个Group,它在Composite内并且位于ScrolledComposite内。所有元素都在EditorPart内。

ScrolledComposite mySc
|- Composite myComposite
   |- Group myGroup
      |- Text myText

我可以滚动(使用鼠标滚轮)到EditorPart中的所有元素上,但是当光标位于“文本”区域上时,滚动停止。 我只想在具有键盘焦点的情况下在文本内滚动。

文本myText的实例:

myText = new Text(myGroup, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);

如果没有SWT.V_SCROLL,它可以工作,但是我没有滚动条,也没有在文本中滚动的可能性。

我以为我可以在文本上没有焦点控制的情况下在父级上使用forceFocus()

myText.addListener(SWT.MouseWheel, new Listener() {
    @Override
    public void handleEvent(Event event) {
        if (!commandText.isFocusControl()) {
            System.out.println("no focus");
            Control wheelControl = myText.getParent();

            Point cursorPos = wheelControl.toControl(event.display.getCursorLocation());
            event.x = cursorPos.x;
            event.y = cursorPos.y;
            event.widget = wheelControl;
            wheelControl.forceFocus();
            wheelControl.notifyListeners(SWT.MouseWheel, event);
        } else {
            System.out.println("Focus control");
        }
    }
});

但是它不起作用。完全没有变化。它只能正确打印“焦点控制”和“没有焦点”。

编辑

这是一个最小的工作示例:

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.graphics.Point;
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.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

public class MyEditor extends EditorPart {

    private Text myText;
    private boolean dirty = false;

    public MyEditor() {
        super();
    }

    @Override
    public void init(IEditorSite site, IEditorInput input) {
        setSite(site);
        setInput(input);
    }


    @Override
    public void doSave(IProgressMonitor monitor) {
        return;
    }


    @Override
    public void doSaveAs() {
        return;
    }


    @Override
    public boolean isDirty() {
        return dirty;
    }


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


    @Override
    public void createPartControl(Composite parent) {
        parent.setLayout(new FillLayout());

        ScrolledComposite mySc = new ScrolledComposite(parent, SWT.V_SCROLL);
        Composite myComposite = new Composite(mySc, SWT.BORDER);
        myComposite.setLayout(new GridLayout(1, false));

        // Set the child as the scrolled content of the ScrolledComposite
        mySc.setContent(myComposite);
        // Expand both horizontally and vertically
        mySc.setExpandHorizontal(true);
        mySc.setExpandVertical(true);

        Group myGroup = new Group(myComposite, SWT.NONE);
        myGroup.setText("Hello or something");
        myGroup.setLayout(new GridLayout(1, false));
        GridData gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.verticalIndent = 10;
        myGroup.setLayoutData(gd);

        Label aLabel = new Label(myGroup, SWT.NONE);
        aLabel.setText("You can write here: ");

        myText = new Text(myGroup, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
        myText.setText("Some Default Text");
        gd = new GridData(GridData.FILL_HORIZONTAL);
        gd.heightHint = 300;
        gd.horizontalIndent = 10;
        myText.setLayoutData(gd);

        myText.addListener(SWT.MouseWheel, new Listener() {
            @Override
            public void handleEvent(Event event) {
                if (!myText.isFocusControl() ) {
                    System.out.println("no focus");
                    Control wheelControl = myText.getParent();

                    Point cursorPos = wheelControl.toControl(event.display.getCursorLocation());
                    event.x = cursorPos.x;
                    event.y = cursorPos.y;
                    event.widget = wheelControl;
                    wheelControl.forceFocus();
                    wheelControl.notifyListeners(SWT.MouseWheel, event);
                    myText.setCapture(false);
                } else {
                    System.out.println("Focus control");
                    myText.setCapture(true);
                }
            }
        });

        mySc.setMinSize(myComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        // TODO Auto-generated method stub
    }

    @Override
    public void setFocus() {
        // TODO Auto-generated method stub
    }
}

1 个答案:

答案 0 :(得分:0)

我发现的解决方案是禁用垂直滚动条。这也将禁用鼠标滚轮滚动。还可以使用SWT的addMouseWheelListener()mouseScrolled()方法而不是addListener()。比起使用ScrolledComposite的getOrigin()方法来滚动。

myText.addMouseWheelListener(new MouseWheelListener() {
    @Override
    public void mouseScrolled(MouseEvent e) {
        if (!myText.isFocusControl() ) {
            myText.getVerticalBar().setEnabled(false);
            if (e.count == 3) {
                mySc.setOrigin(sc.getOrigin().x, mySc.getOrigin().y - 30);
            } else if (e.count == -3) {
                mySc.setOrigin(sc.getOrigin().x, mySc.getOrigin().y + 30);
            }
        } else {
            myText.getVerticalBar().setEnabled(true);
        }
    }
});

count始终返回3或-3,具体取决于滚动方向。向上/向下滚动的值30对我来说是好的,或者出于其他目的可能会或多或少。我尚未检查Windows机器上的行为。