1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.forms.widgets.FormToolkit;
public class TestLayout {
static int i = 0;
private static FormToolkit toolkit;
private static ScrolledComposite sc;
public static void main(String[] args) {
Display display = new Display();
final Shell shell = new Shell(display);
shell.setLayout(new GridLayout());
Composite mainComposite = new Composite(shell, SWT.BORDER);
mainComposite.setLayout(new GridLayout());
mainComposite.setLayoutData(new GridData(GridData.FILL_BOTH));
sc = new ScrolledComposite(mainComposite, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
sc.setLayoutData(new GridData(GridData.FILL_BOTH));
sc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
sc.getVerticalBar().setIncrement(sc.getVerticalBar().getIncrement() * 9);
final Composite contentComposite = new Composite(sc, SWT.BORDER);
contentComposite.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
contentComposite.setBackgroundMode(SWT.INHERIT_FORCE);
contentComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
contentComposite.setLayout(new GridLayout());
contentComposite.setBackground(display.getSystemColor(SWT.COLOR_GRAY));
sc.setContent(contentComposite);
contentComposite.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
sc.setMinHeight(contentComposite.computeSize(contentComposite.getSize().x, SWT.DEFAULT).y);
}
});
Label lbl = new Label(contentComposite, SWT.NONE);
lbl.setText("ContentComposite");
toolkit = new FormToolkit(display);
addSection(contentComposite);
addSection(contentComposite);
addSection(contentComposite);
shell.setSize(400, 300);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
toolkit.dispose();
display.dispose();
}
private static void addSection(final Composite contentComposite) {
// *****
// Si vous pensez que c'est à cause du toolkit, utilisez cette ligne
// *****
// final Section section = new Section(contentComposite, Section.DESCRIPTION | Section.TITLE_BAR | Section.TWISTIE | Section.EXPANDED |
// Section.COMPACT);
// final Section section = toolkit.createSection(contentComposite, Section.DESCRIPTION | Section.TITLE_BAR | Section.TWISTIE | Section.EXPANDED);
// section.setText("SECTION");
// section.setLayout(new FillLayout());
// section.setLayoutData(new GridData(GridData.FILL_BOTH));
// final Composite sectionContent = new Composite(section, SWT.BORDER);
final Composite sectionContent = new Composite(contentComposite, SWT.BORDER); // à commenter
sectionContent.setLayout(new GridLayout());
sectionContent.setBackground(contentComposite.getDisplay().getSystemColor(SWT.COLOR_WHITE));
sectionContent.setBackgroundMode(SWT.INHERIT_NONE);
sectionContent.setLayoutData(new GridData(GridData.FILL_BOTH));
// section.setClient(sectionContent);
Label lbl = new Label(sectionContent, SWT.NONE);
lbl.setText("SectionContent");
Button add = new Button(sectionContent, SWT.PUSH);
add.setText("add Element");
final Group group = new Group(sectionContent, SWT.NONE);
group.setLayoutData(new GridData(GridData.FILL_BOTH));
RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
rowLayout.wrap = true;
rowLayout.fill = true;
rowLayout.pack = false;
group.setLayout(rowLayout);
group.setText("Elements");
add.addSelectionListener(new SelectionListener() {
@Override
public void widgetSelected(SelectionEvent e) {
Label lbl = new Label(group, SWT.BORDER);
lbl.setText("added " + i++);
sc.layout(true, true);
}
@Override
public void widgetDefaultSelected(SelectionEvent e) {
}
});
// for (int ind = 0; ind < 10; ind++) {
// lbl = new Label(group, SWT.BORDER);
// System.out.println("adding button " + i);
// lbl.setText("added " + i++);
// }
}
} |
Partager