1

I would like to have a resizable text area in one of my Eclipse Plugins. It should have a gripper on the lower right corner that can be dragged to change the size of the text area, similar to following html example:

<!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 static image: Result as Image

I am already able to create an SWT multi-line text:

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

Instead of the scroll bar I would like to have a gripper that is able to resize the text field vertically and horizontally.

The documentation for the Text element is here and I could not find a gripper option: http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fswt%2Fwidgets%2FText.html

How can I add a gripper to the text?

Is there a resizable panel with a gripper that I can use to wrap the text? A "Sash" only seems to be resizable in one direction: Can you create a resizable control in SWT?

Is there a finished control that provides the functionality I am looking for? (I had a look at the nebula widgets but there does not seem to be a text area component with gripper.)

Community
  • 1
  • 1
Stefan
  • 10,010
  • 7
  • 61
  • 117
  • 1
    Maybe [this](http://stackoverflow.com/questions/12500208/how-do-i-make-it-possible-to-resize-a-composite-by-dragging-the-corner) could be useful? – Baz Nov 16 '16 at 10:07

1 Answers1

1

With the help of Baz I found following solution. Its not perfect but kind of working and may be useful as a starting point for others. Please not that the parent layout does not yet adapt to the new size. And the example below does not yet set min values for the rect sizes.

    //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

Stefan
  • 10,010
  • 7
  • 61
  • 117