0

I'm trying to place text in the centre of a RectangleFigure. I need the words to wrap around according to the bounds of the figure so I'm using a text flow. The problem is that despite setting the bounds of the RectangleFigure, it occupies the entire shell and the text flow wraps around the width of the shell and not the RectangleFigure. Can someone please point me in the right direction of fixing this? Thanks. * I've added short testable code illustrating my problem.

import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.RectangleFigure;
import org.eclipse.draw2d.SimpleRaisedBorder;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.draw2d.text.BlockFlow;
import org.eclipse.draw2d.text.BlockFlowLayout;
import org.eclipse.draw2d.text.FlowFigureLayout;
import org.eclipse.draw2d.text.FlowPage;
import org.eclipse.draw2d.text.ParagraphTextLayout;
import org.eclipse.draw2d.text.TextFlow;
import org.eclipse.draw2d.text.TextLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
/**
 * PROBLEM: Cannot restrict bounds of text flow to a particular rectangle
 *
 */

public class TestFlow {

    protected Shell shell;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        try {
            TestFlow window = new TestFlow();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    /**
     * Create contents of the window.
     */
    protected void createContents() {
        shell = new Shell();
        shell.setSize(450, 300);
        shell.setText("SWT Application");

        shell.setLayout(new FillLayout());

        FigureCanvas canvas = new FigureCanvas(shell);
        canvas.getViewport().setContentsTracksWidth(true);
        Figure panel = new Figure();
        panel.setLayoutManager(new StackLayout());

        RectangleFigure rect=new RectangleFigure();
        rect.setBounds(new Rectangle(10, 10, 5, 25));
        rect.setBorder(new SimpleRaisedBorder());
        rect.setLayoutManager(new StackLayout());

        TextFlow content= new TextFlow("I'm such a long sentence --> You don't even know what hit you" +
                " Can't help it if there's no one else.");
        FlowPage fp = new FlowPage();

        fp.setBorder(new SimpleRaisedBorder());
        fp.setHorizontalAligment(PositionConstants.CENTER);// ALIGNS THE WHOLE THING CENTRE

        fp.add(content);
        rect.add(fp);
        panel.add(rect);
        //panel.add(fp);
        canvas.setContents(panel);

    }

}
Asher
  • 811
  • 3
  • 10
  • 19

1 Answers1

0

First of all thanks for your code, it helped me to figure out a problem of my own (adding text within a figure). Now to your question:

Use another rectangle figure within the first one. The first one will always occupy the whole space for some reason. I added some code that should explain it, the first rectangle will always be resized, no matter what size you set. I came across this problem a while ago while creating an eclipse view.

...

RectangleFigure rect=new RectangleFigure();
rect.setBounds(new Rectangle(10, 10, 5, 25));
rect.setBorder(new SimpleRaisedBorder());
rect.setLayoutManager(new StackLayout());

RectangleFigure rect2=new RectangleFigure();
rect2.setBounds(new Rectangle(10, 10, 5, 25));
rect2.setBorder(new SimpleRaisedBorder());
rect2.setLayoutManager(new StackLayout());

...

fp.add(content);
rect2.add(fp);
rect.add(rect2);
panel.add(rect);
buzz
  • 739
  • 5
  • 5