下面列出了怎么用org.eclipse.swt.widgets.Layout的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void createControl(Composite parent) {
super.createControl(parent);
Control control = getControl();
if (control instanceof Composite) {
Composite composite = (Composite)control;
Link label = new Link(composite, SWT.WRAP);
label.setText(Messages.firefoxAttachNote);
Layout layout = composite.getLayout();
if (layout instanceof GridLayout) {
GridLayout gridLayout = (GridLayout)layout;
GridDataFactory.swtDefaults()
.align(SWT.BEGINNING, SWT.TOP)
.grab(true, false)
.indent(0, 10)
.span(gridLayout.numColumns, 1)
.applyTo(label);
}
}
}
/**
* Creates a vertical spacer for separating components. If applied to a
* <code>GridLayout</code>, this method will automatically span all of the columns of the parent
* to make vertical space
*
* @param parent the parent composite to add this spacer to
* @param xLines - space height (in standard font height units)
*/
public static void createVerticalSpacer(Composite parent, double xLines) {
Label lbl = new Label(parent, SWT.NONE);
GC gc = new GC(parent);
int cyLine;
try {
cyLine = gc.textExtent("Wq").y; //$NON-NLS-1$
} finally {
gc.dispose();
}
int cy = (int)((double)cyLine * xLines);
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
Layout layout = parent.getLayout();
if(layout instanceof GridLayout) {
gd.horizontalSpan = ((GridLayout)parent.getLayout()).numColumns;
}
gd.heightHint = cy;
lbl.setLayoutData(gd);
}
public static void createVerticalSpacer(Composite parent, double xLines) {
Label lbl = new Label(parent, SWT.NONE);
GC gc = new GC(parent);
int cyLine;
try{
cyLine = gc.textExtent("Wq").y; //$NON-NLS-1$
}
finally{
gc.dispose();
}
int cy = (int)((double)cyLine * xLines);
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
Layout layout = parent.getLayout();
if(layout instanceof GridLayout) {
gd.horizontalSpan = ((GridLayout)parent.getLayout()).numColumns;
}
gd.heightHint = cy;
lbl.setLayoutData(gd);
}
private void installTopMargin(final Composite body) {
Composite c = body;
CTabFolder tabFolder = (c instanceof CTabFolder) ? (CTabFolder) c : null;
while ((tabFolder == null) && (c.getParent() != null)) {
c = c.getParent();
tabFolder = (c instanceof CTabFolder) ? (CTabFolder) c : null;
}
if (tabFolder != null) {
final Layout l = tabFolder.getParent().getLayout();
if (l instanceof FillLayout) {
final FillLayout fl = (FillLayout) l;
fl.marginHeight = 6;
}
}
}
/**
* Creates a new {@link ArtifactEditor}.
*
* @param parent parent Container.
* @param style SWT style for the container.
* @param swtTextStyle SWT style for textboxes. Useful to set them
* SWT.READ_ONLY for instance.
*/
public ArtifactEditor(
Composite parent, Integer style, Integer swtTextStyle) {
super(parent, style, swtTextStyle);
// widgets
icon = new Label(this, SWT.NONE);
Label labelName = new Label(this, SWT.NONE);
path = new Text(this, swtTextStyle);
// layout
Layout layout = new GridLayout(3, false);
this.setLayout(layout);
GridData iconGridData = new GridData(SWT.FILL, SWT.FILL, false, false);
iconGridData.minimumHeight = 16;
iconGridData.minimumWidth = 16;
icon.setLayoutData(iconGridData);
labelName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
path.setLayoutData(
new GridData(SWT.FILL, SWT.FILL, true, false));
// content
icon.setImage(MavenActivator.IMAGE_MAVEN);
labelName.setText("Path");
}
/**
* Creates a new {@link PropertyEditor}.
*
* @param parent parent Container.
* @param style SWT style for the container.
* @param swtTextStyle SWT style for textboxes. Useful to set them
* SWT.READ_ONLY for instance.
*/
public PropertyEditor(
Composite parent, Integer style, Integer swtTextStyle) {
super(parent, style, swtTextStyle);
// widgets
icon = new Label(this, SWT.NONE);
Label labelName = new Label(this, SWT.NONE);
path = new Text(this, swtTextStyle);
// layout
Layout layout = new GridLayout(3, false);
this.setLayout(layout);
GridData iconGridData = new GridData(SWT.FILL, SWT.FILL, false, false);
iconGridData.minimumHeight = 16;
iconGridData.minimumWidth = 16;
icon.setLayoutData(iconGridData);
labelName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
path.setLayoutData(
new GridData(SWT.FILL, SWT.FILL, true, false));
// content
icon.setImage(MavenActivator.IMAGE_MAVEN);
labelName.setText("Path");
}
/**
* Recomputes the pads based on the layouts between this {@code Composite}
* and the {@link #scrolledAncestor}.
*/
private void recomputePads() {
totalPadLeft = 0;
totalPadRight = 0;
PadFinder padFinder = new PadFinder();
Composite parent = getParent();
while (parent != null && parent != scrolledAncestor) {
Layout layout = parent.getLayout();
totalPadLeft += padFinder.computeLeftPad(layout);
totalPadRight += padFinder.computeLeftPad(layout);
parent = parent.getParent();
}
return;
}
private void updateCompositeLayout(Composite composite) {
Layout l = composite.getLayout();
if (l instanceof GridLayout) {
GridLayout layout = (GridLayout) l;
layout.marginHeight = convertVerticalDLUsToPixels(getDefaultMargins());
layout.marginWidth = convertHorizontalDLUsToPixels(getDefaultMargins());
layout.verticalSpacing = convertVerticalDLUsToPixels(getDefaultSpacing());
layout.horizontalSpacing = convertHorizontalDLUsToPixels(getDefaultSpacing());
composite.setLayout(layout);
}
for (Control t : composite.getChildren()) {
if (t instanceof Composite) {
updateCompositeLayout((Composite) t);
}
}
}
@Override
protected Layout createLayout() {
//Workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=438641
return new RulerLayout(GAP_SIZE_1) {
@Override
protected void layout(Composite composite, boolean flushCache) {
StyledText textWidget = getTextWidget();
if (textWidget == null) {
Log.log("Error: textWidget is already null. SourceViewer: " + BaseSourceViewer.this + " control: "
+ BaseSourceViewer.this.getControl());
return;
}
super.layout(composite, flushCache);
}
};
}
private void findHeader(Composite row) {
Control[] children = row.getParent().getChildren();
for (int i = 0; i < children.length; i++) {
if (children[i] instanceof Composite) {
Composite child = (Composite) children[i];
Layout layout = child.getLayout();
if (layout instanceof HeaderLayout) {
delegate = (HeaderLayout) layout;
addListenersToDelegate(row, (HeaderLayout) layout);
return;
}
}
}
}
private Layout createExtensionGridLayout ()
{
final GridLayout gridLayout = new GridLayout ( 1, false );
gridLayout.horizontalSpacing = gridLayout.verticalSpacing = 0;
gridLayout.marginHeight = gridLayout.marginWidth = 0;
return gridLayout;
}
private void init() {
composite = toolkit.createComposite(parent, SWT.NULL);
Layout layout = new GridLayout(3, false);
composite.setLayout(layout);
composite.setLayoutData(gridData);
texts.clear();
parent.layout();
}
/**
* GUI's
*/
private Control getItemControl( CTabFolder folder){
Layout layout = new GridLayout();
((GridLayout) layout).numColumns = 1;
Composite itemComposite = new Composite(folder, SWT.NULL);
itemComposite.setLayout(layout);
buildTopControl(itemComposite);
buildBottomControl(itemComposite);
return itemComposite;
}
/**
* Creates a vertical spacer for separating components. If applied to a
* <code>GridLayout</code>, this method will automatically span all of the columns of the parent
* to make vertical space
*
* @param parent the parent composite to add this spacer to
* @param numlines the number of vertical lines to make as space
*/
public static void createVerticalSpacer(Composite parent, int numlines) {
Label lbl = new Label(parent, SWT.NONE);
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
Layout layout = parent.getLayout();
if(layout instanceof GridLayout) {
gd.horizontalSpan = ((GridLayout)parent.getLayout()).numColumns;
}
gd.heightHint = numlines;
lbl.setLayoutData(gd);
}
private static List<GridLayout> getGridLayouts(Composite innerComposite) {
List<GridLayout> gridLayouts = new ArrayList<>();
if (innerComposite == null) {
return gridLayouts;
}
Arrays.asList(innerComposite.getChildren()).forEach(control -> {
if (control instanceof Composite) {
Layout layout = ((Composite) control).getLayout();
if (layout instanceof GridLayout) {
gridLayouts.add((GridLayout) layout);
}
}
});
return gridLayouts;
}
/**
* @param parent parent Container.
* @param style SWT style for the container.
* @param swtTextStyle SWT style for textboxes. Useful to set them
* SWT.READ_ONLY for instance.
*/
public FieldEditor(Composite parent, Integer style, Integer swtTextStyle) {
super(parent, style, swtTextStyle);
// widgets
icon = new Label(this, SWT.NONE);
name = new Text(this, swtTextStyle);
Label labelType = new Label(this, SWT.NONE);
type = new TypeEditor(this, SWT.NONE, swtTextStyle);
Label labelContainer = new Label(this, SWT.NONE);
container = new TypeEditor(this, SWT.NONE, swtTextStyle);
// layout
Layout layout = new GridLayout(2, false);
this.setLayout(layout);
GridData iconGridData = new GridData(SWT.FILL, SWT.FILL, false, false);
iconGridData.minimumHeight = 16;
iconGridData.minimumWidth = 16;
icon.setLayoutData(iconGridData);
name.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
labelType.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
type.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
labelContainer.setLayoutData(
new GridData(SWT.FILL, SWT.CENTER, false, false));
container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
// content
icon.setImage(JavaActivator.IMAGE_FIELD);
labelType.setText("Type");
labelContainer.setText("Container");
}
public MethodEditor(Composite parent, Integer style, Integer swtTextStyle) {
super(parent, style, swtTextStyle);
icon = new Label(this, SWT.NONE);
name = new Text(this, swtTextStyle);
Label labelSignature = new Label(this, SWT.NONE);
signature = new Text(this, swtTextStyle);
Label labelContainer = new Label(this, SWT.NONE);
container = new TypeEditor(this, SWT.NONE, swtTextStyle);
Layout layout = new GridLayout(2, false);
this.setLayout(layout);
GridData iconGridData = new GridData(SWT.FILL, SWT.FILL, false, false);
iconGridData.minimumHeight = 16;
iconGridData.minimumWidth = 16;
icon.setLayoutData(iconGridData);
name.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
labelSignature.setLayoutData(
new GridData(SWT.FILL, SWT.CENTER, false, false));
signature.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
labelContainer.setLayoutData(
new GridData(SWT.FILL, SWT.CENTER, false, false));
container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
icon.setImage(JavaActivator.IMAGE_METHOD);
labelSignature.setText("Signature");
labelContainer.setText("Class");
}
public Layout getLayout(int itemCount) {
// simple layout: a row of items
GridLayout layout= new GridLayout(itemCount, true);
layout.horizontalSpacing= 1;
layout.verticalSpacing= 0;
layout.marginHeight= 1;
layout.marginWidth= 1;
return layout;
}
protected void refresh() {
adjustItemNumber();
if (fInput == null)
return;
if (fInput.fAnnotations == null)
return;
if (fInput.fViewer != null)
fInput.fViewer.addViewportListener(fViewportListener);
fShell.setRegion(fLayouter.getShellRegion(fInput.fAnnotations.length));
Layout layout= fLayouter.getLayout(fInput.fAnnotations.length);
fComposite.setLayout(layout);
Control[] children= fComposite.getChildren();
for (int i= 0; i < fInput.fAnnotations.length; i++) {
Canvas canvas= (Canvas) children[i];
Item item= new Item();
item.canvas= canvas;
item.fAnnotation= fInput.fAnnotations[i];
canvas.setData(item);
canvas.redraw();
}
}
/**
* Returns the height hint for the given control.
*
* @param hHint
* the width hint
* @param c
* the control
*
* @return the height hint
*/
public static int getHeightHint( int hHint, Control c )
{
if ( c instanceof Composite )
{
Layout layout = ( (Composite) c ).getLayout( );
if ( layout instanceof ColumnLayout )
return hHint;
}
return SWT.DEFAULT;
}
/**
* Overrides the default behavior to store a reference to the layout. This
* is required for the following reason: when there are no Entries, a label
* is displayed. A different layout is necessary to display the label by
* itself.
*
* @param layout
* The new layout.
*/
@Override
public void setLayout(Layout layout) {
super.setLayout(layout);
// If the layout is not null, store a reference to it so we can revert
// to it if new EntryComposites are added when the empty label is
// displayed.
if (layout != null) {
this.layout = layout;
}
return;
}
@Override
public void setLayout(Layout layout) {
if (!(layout instanceof StackLayout))
throw new UnsupportedOperationException(
"Only supported for StackLayout");
super.setLayout(layout);
}
public CustomPageBookLayout(Layout originalParentLayout) {
if (originalParentLayout instanceof CustomPageBookLayout) {
//It's there by some other view of ours (switched directly between them).
CustomPageBookLayout customPageBookLayout = (CustomPageBookLayout) originalParentLayout;
this.originalParentLayout = customPageBookLayout.originalParentLayout;
} else {
this.originalParentLayout = originalParentLayout;
}
}
/**
* Creates a vertical spacer for separating components. If applied to a
* <code>GridLayout</code>, this method will automatically span all of the columns of the parent
* to make vertical space
*
* @param parent the parent composite to add this spacer to
* @param numlines the number of vertical lines to make as space
* @since 3.3
*/
public static void createVerticalSpacer(Composite parent, int numlines) {
Label lbl = new Label(parent, SWT.NONE);
GridData gd = new GridData(GridData.FILL_HORIZONTAL);
Layout layout = parent.getLayout();
if(layout instanceof GridLayout) {
gd.horizontalSpan = ((GridLayout)parent.getLayout()).numColumns;
}
gd.heightHint = numlines;
lbl.setLayoutData(gd);
}
@Override
protected Layout getLayout() {
final GridLayout layout = new GridLayout(3, false);
layout.verticalSpacing = 10;
layout.horizontalSpacing = 10;
return layout;
}
@Override
protected Layout getLayout() {
final GridLayout layout = new GridLayout(2, false);
layout.verticalSpacing = 0;
layout.horizontalSpacing = 10;
return layout;
}
/**
* Creates a new entry in the folder.
*
* @param title
* @param image
* @param index
* @param hideable
* @param layout
* @return
*/
public Composite createItem(String title, Image image, int index, boolean hideable, Layout layout) {
Composite composite = new Composite(folder, SWT.NONE);
composite.setLayout(layout);
CTabItem item = new CTabItem(folder, SWT.NULL, index);
item.setText(title);
if (image!=null) item.setImage(image);
item.setShowClose(false);
item.setControl(composite);
entries.add(new TitledFolderEntry(title, composite, image, index, hideable));
return composite;
}
/**
* @see org.eclipse.swt.widgets.Composite#setLayout(org.eclipse.swt.widgets.Layout)
*/
@Override
public void setLayout(Layout layout) {
checkWidget();
return;
}
/**
* @see org.eclipse.swt.widgets.Composite#getLayout()
*/
@Override
public Layout getLayout() {
return content.getLayout();
}
/**
* @see org.eclipse.swt.widgets.Composite#setLayout(org.eclipse.swt.widgets.Layout)
*/
@Override
public void setLayout(final Layout layout) {
content.setLayout(layout);
}