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);
}
}