[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.tools.gef] Layout listeners and calculate preferred size

I have 4 viewports and each have their own layout, of course. I have the need for figures in each viewport to determine the size of figures in other viewports. Previously I had done this with syncing of the layouts by performing an evaluation after layout had occurred. I decided to get rid of the syncing and to evaluate the sizes of all figures at once and then push the needed information down to the individual layouts. So when the figures are invalidated, my aggregator is invalidated and when layout is invoked on any of the layouts my aggregator receives the request and if it's invalid as well it evaluates the sizes and pushes the data down to it's layouts. This works great except for when calls are made to layout.calculatePreferredSize(). In my calculate method I call the internal layout. This doesn't fire events to the listeners thus my aggregator couldn't run. I was trying to keep dependencies on the aggregator out of my layout, it was just simpler that way. Does anyone see a way to handle this without creating any dependencies? The way I've hacked it up now is that on my figure I overrode layout and made it public. So in calculate preferred size I check to see if the figure is an instance of my figure and then call layout(). This allows for the LayoutNotifier to be invoked and the event fires.

The other thing I wanted to ask about is the fact that if I add a layout listener to a figure and then set the layout the listeners don't fire. This seems a little odd to me. The reason for it is that the listeners are attached to the internaly LayoutNofitifer and aren't stored at the figure level. So when a layout is set it seems to lose all listeners. I would understand this behavior if I was attaching the listeners to an individual layout but if I'm attaching at the figure level I don't care about the layout instance. This can be seen with the following snippet:

public class LayoutListenerTest {
	void run() {
		Display display = new Display();
		Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		
		FigureCanvas canvas = new FigureCanvas(shell);
		IFigure content = new Figure();
		canvas.setContents(content);

		content.addLayoutListener(new LayoutListener.Stub() {
			public boolean layout(IFigure container) {
				System.out.println("layout"); //$NON-NLS-1$
				return false;
			}
		});
		content.setLayoutManager(new StackLayout());

		IFigure rect = new RectangleFigure();
		content.add(rect);
		
		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}

		display.dispose();
	}

	public static void main(String[] args) {
		new LayoutListenerTest().run();
	}
}

If I were to move the attaching of the listener to after setting the layout I receive notifications.

Thanks,
Brad