寻找带有抓手的可调整大小的SWT(JFace)文本区域组件

时间:2016-11-16 08:20:58

标签: java swt textarea resizable

我想在我的一个Eclipse插件中有一个可调整大小的文本区域。它应该在右下角有一个抓取器,可以拖动来改变文本区域的大小,类似于以下html示例:

<!DOCTYPE html>
<html>
<body>

<textarea rows="4" cols="50">
This is a resizable html text area with a gripper at the lower right corner. How to create something similar with SWT JFace?</textarea>

</body>
</html>

作为静态图像的结果: Result as Image

我已经能够创建一个SWT多行文本:

Text  textArea = toolkit.createText(parentContainer, "Default text", SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
textArea.setEnabled(isEnabled());
textArea.setToolTipText("tooltip");

GridData areaData = new GridData();
areaData.grabExcessHorizontalSpace = true;
areaData.horizontalAlignment = GridData.FILL;
areaData.verticalAlignment = GridData.FILL;
areaData.grabExcessVerticalSpace = true;
areaData.heightHint = 80;
areaData.widthHint = 200;
textArea.setLayoutData(areaData);

enter image description here

我希望有一个能够垂直和水平调整文本字段大小的抓手,而不是滚动条。

Text元素的文档在这里,我找不到抓取器选项: http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fswt%2Fwidgets%2FText.html

如何在文本中添加抓手?

是否有可调整大小的面板,带有可用于包装文本的抓手? “Sash”似乎只能在一个方向上调整大小: Can you create a resizable control in SWT?

是否有完成的控件提供我正在寻找的功能? (我看过星云小部件,但似乎没有带夹子的文本区域组件。)

1 个答案:

答案 0 :(得分:1)

在Baz的帮助下,我找到了以下解决方案。它不完美,但有点工作,可能是其他人的起点。请注意,父布局尚未适应新的尺寸。以下示例尚未设置rect尺寸的最小值。

    //toolkit
    FormToolkit toolkit = new FormToolkit(Display.getCurrent());

    //create content composite for label, text area and gripper
    contentContainer = toolkit.createComposite(parent);

    GridData fillHorizontal = new GridData();
    fillHorizontal.grabExcessHorizontalSpace = true;
    fillHorizontal.horizontalAlignment = GridData.FILL;
    contentContainer.setLayoutData(fillHorizontal);

    GridLayout gridLayout = new GridLayout(1, true);
    gridLayout.horizontalSpacing = 0;
    gridLayout.verticalSpacing = 2;
    gridLayout.marginHeight = 2;
    gridLayout.marginWidth = 0;
    contentContainer.setLayout(gridLayout);

    //label
    String currentLabel = getLabel();
    toolkit.createLabel(contentContainer, currentLabel);

    //text area
    textArea = toolkit.createText(contentContainer, get(), SWT.MULTI | SWT.BORDER | SWT.WRAP);
    textArea.setEnabled(isEnabled());
    textArea.setToolTipText(tooltip);

    GridData areaData = new GridData();
    areaData.grabExcessHorizontalSpace = true;
    areaData.grabExcessVerticalSpace = true;
    areaData.horizontalAlignment = GridData.FILL;
    areaData.verticalAlignment = GridData.FILL;
    areaData.widthHint = 200;
    areaData.heightHint = 80;
    textArea.setLayoutData(areaData);

    //gripper
    org.eclipse.swt.widgets.Label gripper = toolkit.createLabel(contentContainer, "");
    gripper.setImage(Activator.getImage("tracker.png"));

    GridData tragData = new GridData();
    tragData.horizontalAlignment = GridData.END;
    gripper.setLayoutData(tragData);

    Listener trackerListener = new Listener() {

        @Override
        public void handleEvent(Event e) {

            Tracker tracker = new Tracker(contentContainer.getParent(), SWT.RESIZE | SWT.DOWN | SWT.RIGHT);
            Rectangle maxRect = contentContainer.getParent().getBounds();
            Rectangle rect = contentContainer.getBounds();
            tracker.setRectangles(new Rectangle[] { rect });
            if (tracker.open()) {
                Rectangle after = tracker.getRectangles()[0];
                Rectangle newRect = new Rectangle(
                        after.x,
                        after.y,
                        Math.min(after.width, maxRect.width - 10),
                        Math.min(after.height, maxRect.height - 10));
                contentContainer.setBounds(newRect);
            }
            tracker.dispose();
        }
    };

    gripper.addListener(SWT.MouseDown, trackerListener);

enter image description here