|
| 1 | +package com.vogella.swt.widgets.customwidgets; |
| 2 | + |
| 3 | +import org.eclipse.swt.SWT; |
| 4 | +import org.eclipse.swt.graphics.Color; |
| 5 | +import org.eclipse.swt.layout.FillLayout; |
| 6 | +import org.eclipse.swt.layout.FormAttachment; |
| 7 | +import org.eclipse.swt.layout.FormData; |
| 8 | +import org.eclipse.swt.layout.FormLayout; |
| 9 | +import org.eclipse.swt.widgets.Composite; |
| 10 | + |
| 11 | +/** |
| 12 | + * Instances of this class represent a composite with two layers |
| 13 | + * |
| 14 | + * The layers can contain arbitrary layouts and widgets |
| 15 | + * <p> |
| 16 | + * |
| 17 | + * <ul> |
| 18 | + * <li>{@link #getContentComposite()} allows to retrieve the Composite for the |
| 19 | + * main content</li> |
| 20 | + * <li>{@link #getOverlayComposite()} Retrieve the Composite for the |
| 21 | + * overlay</li> |
| 22 | + * </ul> |
| 23 | + * |
| 24 | + * </p> |
| 25 | + * <dl> |
| 26 | + * <dt><b>Styles:</b></dt> |
| 27 | + * <dd>TOP, BOTTOM, LEFT, RIGHT</dd> |
| 28 | + * </dl> |
| 29 | + * <p> |
| 30 | + * Note: Only one of the styles TOP, BOTTOM, RIGHT, and CENTER may be specified. |
| 31 | + * </p> |
| 32 | + * <p> |
| 33 | + * </p> |
| 34 | + * |
| 35 | + * @noextend This class is not intended to be subclassed by clients. |
| 36 | + */ |
| 37 | + |
| 38 | +public class LayeredComposite extends Composite { |
| 39 | + private Color backgroundColor; |
| 40 | + private Composite mainContentArea; |
| 41 | + private Composite overlayComposite; |
| 42 | + |
| 43 | + public LayeredComposite(Composite parent, int style) { |
| 44 | + super(parent, style); |
| 45 | + // Uncommend to test if the clients are fill the full area |
| 46 | + // backgroundColor = new Color(null, 255, 0, 0); |
| 47 | + // setBackground(backgroundColor); |
| 48 | + // this.addDisposeListener(e -> { |
| 49 | + // backgroundColor.dispose(); |
| 50 | + // }); |
| 51 | + |
| 52 | + FormLayout formLayout = new FormLayout(); |
| 53 | + this.setLayout(formLayout); |
| 54 | + mainContentArea = new Composite(this, SWT.NONE); |
| 55 | + mainContentArea.setLayout(new FillLayout()); |
| 56 | + mainContentArea.setLayoutData(fillCompleteArea()); |
| 57 | + |
| 58 | + overlayComposite = new Composite(this, SWT.NONE); |
| 59 | + overlayComposite.setLayout(new FillLayout()); |
| 60 | + overlayComposite.moveAbove(null); |
| 61 | + FormData formDataUpperLayer = definePosition(style); |
| 62 | + overlayComposite.setLayoutData(formDataUpperLayer); |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Retrieve the main Composite. Content of this composite maybe overlayed by |
| 68 | + * the overlayComposite |
| 69 | + * |
| 70 | + * @return Composite the Composite that represents the main content |
| 71 | + */ |
| 72 | + public Composite getContentComposite() { |
| 73 | + return mainContentArea; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Retrieve the overlay Composite. |
| 78 | + * |
| 79 | + * @return Composite the Composite that represents the main content |
| 80 | + */ |
| 81 | + |
| 82 | + public Composite getOverlayComposite() { |
| 83 | + return overlayComposite; |
| 84 | + } |
| 85 | + |
| 86 | + private FormData definePosition(int style) { |
| 87 | + checkStyle(style); |
| 88 | + FormData formDataUpperLayer = new FormData(); |
| 89 | + if (style == SWT.BOTTOM) { |
| 90 | + formDataUpperLayer.bottom = new FormAttachment(100, 0); |
| 91 | + formDataUpperLayer.left = new FormAttachment(0, 0); |
| 92 | + formDataUpperLayer.right = new FormAttachment(100, 0); |
| 93 | + } else if (style == SWT.TOP) { |
| 94 | + formDataUpperLayer.top = new FormAttachment(0, 0); |
| 95 | + formDataUpperLayer.left = new FormAttachment(0, 0); |
| 96 | + formDataUpperLayer.right = new FormAttachment(100, 0); |
| 97 | + } else if (style == SWT.RIGHT) { |
| 98 | + formDataUpperLayer.top = new FormAttachment(0, 0); |
| 99 | + formDataUpperLayer.right = new FormAttachment(100, 0); |
| 100 | + } else if (style == SWT.LEFT) { |
| 101 | + formDataUpperLayer.top = new FormAttachment(0, 0); |
| 102 | + formDataUpperLayer.left = new FormAttachment(0, 0); |
| 103 | + formDataUpperLayer.right = new FormAttachment(100, 0); |
| 104 | + } |
| 105 | + |
| 106 | + return formDataUpperLayer; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Ensure that only valid styles are passed to the container |
| 111 | + * |
| 112 | + * @param style |
| 113 | + */ |
| 114 | + static void checkStyle(int style) { |
| 115 | + if ((style & (SWT.BOTTOM | SWT.TOP | SWT.RIGHT | SWT.LEFT)) == 0) { |
| 116 | + throw new IllegalArgumentException("Style must be one of SWT.BOTTOM, SWT.TOP, SWT.RIGHT, SWT.LEFT"); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private FormData fillCompleteArea() { |
| 121 | + FormData formData = new FormData(); |
| 122 | + formData.top = new FormAttachment(0, 0); |
| 123 | + formData.bottom = new FormAttachment(100, 0); |
| 124 | + formData.left = new FormAttachment(0, 0); |
| 125 | + formData.right = new FormAttachment(100, 0); |
| 126 | + return formData; |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments