Bug 219206 - FormLayout - add the ability to vertically align multiple items
Summary: FormLayout - add the ability to vertically align multiple items
Status: NEW
Alias: None
Product: Platform
Classification: Eclipse Project
Component: SWT (show other bugs)
Version: 3.4   Edit
Hardware: PC Windows XP
: P3 enhancement with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: Veronika Irvine CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-02-15 19:32 EST by Veronika Irvine CLA
Modified: 2019-09-06 15:36 EDT (History)
1 user (show)

See Also:


Attachments
Image of anchor example showing anchor positions (25.61 KB, image/jpeg)
2008-02-15 19:44 EST, Veronika Irvine CLA
no flags Details
Patch to FormLayout (7.84 KB, patch)
2008-02-15 19:45 EST, Veronika Irvine CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Veronika Irvine CLA 2008-02-15 19:32:09 EST
In FormLayout, when you want to line things up vertically, you need to know which item sticks out the furthest so that you can attach the other items to it.  This can be an issue if your strings get translated and therefore change length or you do not know all the items being laid out (e.g. more items get added dynamically).

I propose that we add the idea of a form anchor.  Widgets can attach to the form anchor and the position of the form anchor is determined by the right most widget attached to the left of the anchor.

I have attached a patch which adds this capability to FormLayout.
Comment 1 Veronika Irvine CLA 2008-02-15 19:44:19 EST
Created attachment 89895 [details]
Image of anchor example showing anchor positions

Below is an example that uses the form anchor.  I have attached an image showing where the anchors in this example.

public static void main(String[] args) {
	final Display display = new Display();
	Shell shell = new Shell(display);
	FormLayout layout = new FormLayout();
	layout.marginWidth = layout.marginBottom = 12;
	shell.setLayout(layout);
	
	FormAnchor anchor1 = new FormAnchor();
	FormAnchor anchor2 = new FormAnchor();
	
	String[] strings = new String[] {
			"a long string:",
			"shrt str:",
			"a really really long string asdad asd as das dasd:",
			"a:"
	};
	
	Control attachTop = null;
	for (int i = 0; i < strings.length; i++) {
		Label label = new Label(shell, SWT.RIGHT);
		label.setText(strings[i]);
		FormData layoutData = new FormData();
		layoutData.left = new FormAttachment(0, 0);
		layoutData.top = new FormAttachment(attachTop, 12);
		layoutData.right = new FormAttachment(anchor1);
		label.setLayoutData(layoutData);
		
		attachTop = label;
		
		Text text = new Text(shell, SWT.BORDER);
		layoutData = new FormData();
		layoutData.left = new FormAttachment(label, 6);
		layoutData.right = new FormAttachment(anchor2);
		layoutData.top = new FormAttachment(attachTop, 0, SWT.CENTER);
		text.setLayoutData(layoutData);
	}
	
	Label label = new Label(shell, SWT.RIGHT);
	label.setText("entry:");
	FormData layoutData = new FormData();
	layoutData.left = new FormAttachment(0, 0);
	layoutData.top = new FormAttachment(attachTop, 12);
	layoutData.right = new FormAttachment(anchor1);
	label.setLayoutData(layoutData);
	
	attachTop = label;
	
	Button b1 = new Button (shell, SWT.PUSH);
	b1.setText("b1");
	layoutData = new FormData();
	layoutData.left = new FormAttachment(label, 6);
	layoutData.top = new FormAttachment(attachTop, 0, SWT.CENTER);
	b1.setLayoutData(layoutData);
	
	Button b2 = new Button (shell, SWT.PUSH);
	b2.setText("b2");
	layoutData = new FormData();
	layoutData.left = new FormAttachment(b1, 6);
	layoutData.top = new FormAttachment(attachTop, 0, SWT.CENTER);
	b2.setLayoutData(layoutData);
	
	Text text = new Text(shell, SWT.BORDER);
	layoutData = new FormData();
	layoutData.left = new FormAttachment(b2, 6);
	layoutData.right = new FormAttachment(anchor2);
	layoutData.top = new FormAttachment(attachTop, 0, SWT.CENTER);
	text.setLayoutData(layoutData);
	
	Button b3 = new Button (shell, SWT.PUSH);
	b3.setText("b3");
	layoutData = new FormData();
	layoutData.left = new FormAttachment(text, 6);
	layoutData.top = new FormAttachment(label, 0, SWT.CENTER);
	b3.setLayoutData(layoutData);
	
	attachTop = label;
	
	Button b4 = new Button(shell, SWT.PUSH);
	b4.setText("b4");
	layoutData = new FormData();
	layoutData.left = new FormAttachment(anchor1, 100);
	layoutData.top = new FormAttachment(attachTop, 12);
	b4.setLayoutData(layoutData);
	
	Button b5 = new Button(shell, SWT.PUSH);
	b5.setText("b5");
	layoutData = new FormData();
	layoutData.right = new FormAttachment(anchor2, -20);
	layoutData.top = new FormAttachment(attachTop, 12);
	b5.setLayoutData(layoutData);
	
	attachTop = b5;
	
	Label l = new Label(shell, SWT.BORDER);
	l.setText("A dfgdfgdfgdfgdfg dfg df gd fg dfg dfg ABCDEFG1");
	layoutData = new FormData();
	layoutData.left = new FormAttachment(anchor1, 70);
	layoutData.right = new FormAttachment(anchor2, 20);
	layoutData.top = new FormAttachment(attachTop, 12);
	l.setLayoutData(layoutData);
	
	shell.pack();
	shell.open();

	while (!shell.isDisposed()) {
		if (!display.readAndDispatch())
			display.sleep();
	}
	display.dispose();
}
Comment 2 Veronika Irvine CLA 2008-02-15 19:45:32 EST
Created attachment 89896 [details]
Patch to FormLayout
Comment 3 Steve Northover CLA 2008-02-16 12:36:06 EST
The idea is good but I hate adding another class.  Can you think of any way that we can add the concept to the classes we have?  CSS has the concept of attachments.  What does it do?

Could we add the use the exact same instance to of an unattached edge, assigned to the same field in a FormData to mean "anchor" these edges to the maximum?  I am thinking aloud here so it probably has problem.  If it makes the code insane, not a good idea?

The use case for FormLayout is to position a control where you want it, then attach other controls around it.  Does thinking this way help out with the "anchor" concept?
Comment 4 Veronika Irvine CLA 2008-02-19 13:38:26 EST
The key thing with the form anchor is that you need to identify the anchor so that you can specify the list of controls attached to the same anchor (because you can have multiple anchors).  I was using the FormAnchor class to provide this identifier.  It would be possible to do this using an anchor index, however, the constructors for FormAttachment already use int for numerator and denominator so th eoption of using int for index is already taken. So what other means is there for indicating that several FormAttachments share the same anchor?
Comment 5 Eclipse Webmaster CLA 2019-09-06 15:36:31 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.