Draw 2 d学習ノート8 GridLayout Toolbar Layoutレイアウトマネージャ

5083 ワード

http://www.cnblogs.com/bjzhanghao/archive/2006/09/05/495747.html
この文章は八進制です.
public class Draw2DLayoutExample {
	static Figure canvas;//Parent figure which uses XYLayout as its layout manager
	static RectangleFigure containerFig;//canvas's only child, which uses ToolbarLayout
	static RectangleFigure innerContainerFig;//containerFig's only child, which uses ToolbarLayout, too
	static RectangleFigure firstGreenFig;//innerContainerFig's first child, which has no layout manager
	static Dimension dimension = new Dimension(40, 20);

	public static void main(String args[]) {
		Shell shell = new Shell();
		shell.setLayout(new GridLayout(1, false));

		//Create control buttons
		Button button = new Button(shell, SWT.PUSH);
		GridData gd = new GridData();
		button.setLayoutData(gd);
		button.setText("Add Red");
		Button button2 = new Button(shell, SWT.PUSH);
		gd = new GridData();
		button2.setLayoutData(gd);
		button2.setText("Add Green");
		Button button3 = new Button(shell, SWT.PUSH);
		gd = new GridData();
		button3.setLayoutData(gd);
		button3.setText("Enlarge Green");

		//Draw2d area
		LightweightSystem lws = new LightweightSystem(shell);

		//The canvas figure which fills right half of shell
		canvas = new Figure();
		canvas.setLayoutManager(new XYLayout());
		lws.setContents(canvas);
		System.out.println(canvas.getLayoutManager());

		//A rectangle figure
		containerFig = new RectangleFigure();
		canvas.add(containerFig);
		canvas.getLayoutManager().setConstraint(containerFig, new Rectangle(120, 10, -1, -1));
		
		ToolbarLayout layout = new ToolbarLayout();
		layout.setVertical(true);
		layout.setSpacing(3);
		layout.setStretchMinorAxis(false);
		containerFig.setLayoutManager(layout);
		containerFig.setBorder(new MarginBorder(5));
		RectangleFigure fig = new RectangleFigure();
		fig.setBackgroundColor(ColorConstants.red);
		fig.setSize(dimension);
		containerFig.add(fig);

		//A inner container figure
		innerContainerFig = new RectangleFigure();
		ToolbarLayout layout2 = new ToolbarLayout();
		layout2.setVertical(false);
		layout2.setSpacing(3);
		layout2.setStretchMinorAxis(false);
		innerContainerFig.setLayoutManager(layout2);
		innerContainerFig.setBorder(new MarginBorder(5));
		containerFig.add(innerContainerFig);

		//The first green figure in innerContainerFig
		firstGreenFig = new RectangleFigure();
		firstGreenFig.setBackgroundColor(ColorConstants.green);
		firstGreenFig.setSize(dimension);
		innerContainerFig.add(firstGreenFig);

		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				RectangleFigure fig = new RectangleFigure();
				fig.setBackgroundColor(ColorConstants.red);
				fig.setPreferredSize(dimension);
				containerFig.add(fig);
			}
		});

		button2.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				RectangleFigure fig = new RectangleFigure();
				fig.setBackgroundColor(ColorConstants.green);
				fig.setPreferredSize(dimension);
				innerContainerFig.add(fig);
			}
		});

		button3.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				//Make this figure bigger, and see if the outer figure grows accordingly
				firstGreenFig.setPreferredSize(100, 100);
			}
		});

		shell.setSize(500, 400);
		shell.open();
		shell.setText("Draw2D Layout Example");
		Display display = Display.getDefault();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}
}
  Draw2d 学习笔记八 GridLayout ToolbarLayout布局管理器 
文中で言及しました
Draw 2 DにおけるFigure類のset PreferredSizeとsetSizeの違いは、setSize()メソッドはrevalidate()を呼び出すことなく、repaint()を呼び出すだけで関連する「汚い」領域を書き直すことである.set PreferrediSize()方法は、フィッティングにおいてget PreferredSize(int,int)の実装において、もしこのfigurにlayoutmanagerがなければ、現在のsizeに戻ることができる.
例えば、親パターンがXYLayoutを使用して、サブパターンがToolbarLayoutを使用する場合、サブパターンにサブパターンが追加されたと仮定し、add()方法がrevalidate()の呼び出しを引き起こし、親パターンのxylayoutはサブパターンがconstrantを有しているかどうかをチェックします.サブパターンのToolbarLayoutを利用してサブパターンの新しいサイズを計算します.このサイズはサブパターンに含まれるサブパターンの数に関連しています.
 
figureはいつ、revalidateを呼び出すかを指摘しました.