Bug 566218 - Provide FillLayoutFactory

Includes the necessary version update as new API has been added.

Change-Id: Ia93860bde461cc170b6ea11a498e5075163a84a3
Signed-off-by: Lars Vogel <Lars.Vogel@vogella.com>
Also-by: Karsten Thoms <karsten.thoms@karakun.com>
Reviewed-on: https://git.eclipse.org/r/c/platform/eclipse.platform.ui/+/167997
Reviewed-by: Marcus Höpfner <marcus.hoepfner@sap.com>
Tested-by: Platform Bot <platform-bot@eclipse.org>
diff --git a/bundles/org.eclipse.jface/META-INF/MANIFEST.MF b/bundles/org.eclipse.jface/META-INF/MANIFEST.MF
index b4e3371..5618b2e 100644
--- a/bundles/org.eclipse.jface/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.jface/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@
 Bundle-ManifestVersion: 2
 Bundle-Name: %pluginName
 Bundle-SymbolicName: org.eclipse.jface;singleton:=true
-Bundle-Version: 3.25.0.qualifier
+Bundle-Version: 3.26.0.qualifier
 Bundle-ClassPath: .
 Bundle-Vendor: %providerName
 Bundle-Localization: plugin
diff --git a/bundles/org.eclipse.jface/pom.xml b/bundles/org.eclipse.jface/pom.xml
index a890b3d..2946dcf 100644
--- a/bundles/org.eclipse.jface/pom.xml
+++ b/bundles/org.eclipse.jface/pom.xml
@@ -20,7 +20,7 @@
   </parent>
   <groupId>org.eclipse.jface</groupId>
   <artifactId>org.eclipse.jface</artifactId>
-  <version>3.25.0-SNAPSHOT</version>
+  <version>3.26.0-SNAPSHOT</version>
   <packaging>eclipse-plugin</packaging>
 
   <properties>
diff --git a/bundles/org.eclipse.jface/src/org/eclipse/jface/layout/FillLayoutFactory.java b/bundles/org.eclipse.jface/src/org/eclipse/jface/layout/FillLayoutFactory.java
new file mode 100644
index 0000000..5a158cf
--- /dev/null
+++ b/bundles/org.eclipse.jface/src/org/eclipse/jface/layout/FillLayoutFactory.java
@@ -0,0 +1,173 @@
+/*******************************************************************************
+ * Copyright (c) 2020 vogella GmbH and others.
+ *
+ * This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ *     Lars Vogel - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.jface.layout;
+import org.eclipse.swt.graphics.Point;
+import org.eclipse.swt.layout.FillLayout;
+import org.eclipse.swt.widgets.Composite;
+
+/**
+ * FillLayoutFactory creates and initializes {@link FillLayout fill layouts}. It
+ * is used as a shorthand for writing "<code>new FillLayout()</code>" and
+ * initializing some fields. In this case the main benefit is a more concise
+ * syntax and the ability to create more than one identical {@link FillLayout}
+ * from the same factory. Changing a property of the factory will affect future
+ * layouts created by the factory, but has no effect on layouts that have
+ * already been created.
+ *
+ * @since 3.26
+ */
+public final class FillLayoutFactory {
+
+	/**
+	 * Template layout. The factory will create copies of this layout.
+	 */
+	private FillLayout layout;
+
+	/**
+	 * Creates a factory that creates copies of the given layout.
+	 *
+	 * @return a new FillLayoutFactory instance with a new FillLayout
+	 */
+	public static FillLayoutFactory fillDefaults() {
+		return new FillLayoutFactory(copyLayout(new FillLayout()));
+	}
+
+	/**
+	 * Creates a new FillLayoutFactory that will create copies of the given layout.
+	 *
+	 * @param l layout to copy
+	 */
+	private FillLayoutFactory(FillLayout l) {
+		this.layout = l;
+	}
+
+	/**
+	 * Creates a factory that creates copies of the given layout.
+	 *
+	 * @param l layout to copy
+	 * @return a new FillLayoutFactory instance that creates copies of the given
+	 *         layout
+	 */
+	public static FillLayoutFactory createFrom(FillLayout l) {
+		return new FillLayoutFactory(copyLayout(l));
+	}
+
+
+	/**
+	 * Creates a copy of the receiver.
+	 *
+	 * @return a copy of the receiver
+	 */
+	public FillLayoutFactory copy() {
+		return new FillLayoutFactory(create());
+	}
+
+
+	/**
+	 * Sets the margins for layouts created with this factory. The margins
+	 * are the distance between the outer cells and the edge of the layout.
+	 *
+	 * @param margins margin size (pixels)
+	 * @return this
+	 */
+	public FillLayoutFactory margins(Point margins) {
+		layout.marginWidth = margins.x;
+		layout.marginHeight = margins.y;
+		return this;
+	}
+
+	/**
+	 * Sets the margins for layouts created with this factory. The margins are the
+	 * distance between the outer cells and the edge of the layout.
+	 *
+	 * @param width  margin width (pixels)
+	 * @param height margin height (pixels)
+	 * @return this
+	 */
+	public FillLayoutFactory margins(int width, int height) {
+		layout.marginWidth = width;
+		layout.marginHeight = height;
+		return this;
+	}
+
+	/**
+	 * Sets the margins for layouts created with this factory. The margins specify
+	 * the number of pixels of width and height of the layout. Note that these
+	 * margins will be added to the ones specified by {@link #margins(int, int)}.
+	 *
+	 * @param width  width margin size (pixels)
+	 * @param height height margin size (pixels)
+	 * @return this
+	 *
+	 * @since 3.3
+	 */
+	public FillLayoutFactory extendedMargins(int width, int height) {
+		layout.marginWidth = layout.marginWidth + width;
+		layout.marginHeight = layout.marginHeight + height;
+		return this;
+	}
+
+
+	/**
+	 * Creates a new {@code FillLayout}, and initializes it with values from the
+	 * factory.
+	 *
+	 * @return a new initialized {@code FillLayout}.
+	 * @see #applyTo
+	 */
+	public FillLayout create() {
+		return copyLayout(layout);
+	}
+
+	/**
+	 * Creates a new {@code FillLayout} and attaches it to the given composite. Does
+	 * not create the layout data of any of the controls in the composite.
+	 *
+	 * @param c composite whose layout will be set
+	 * @see #create
+	 * @see FillLayoutFactory
+	 */
+	public void applyTo(Composite c) {
+		c.setLayout(copyLayout(layout));
+	}
+
+	/**
+	 * Copies the given {@code FillLayout} instance
+	 *
+	 * @param l layout to copy
+	 * @return a new FillLayout
+	 */
+	public static FillLayout copyLayout(FillLayout l) {
+		FillLayout result = new FillLayout();
+		result.marginHeight = l.marginHeight;
+		result.marginWidth = l.marginWidth;
+		result.spacing = l.spacing;
+
+		return result;
+	}
+
+	@Override
+	public String toString() {
+		StringBuilder builder = new StringBuilder();
+		builder.append("FillLayout()\n"); //$NON-NLS-1$
+		if (layout.marginWidth != 0 || layout.marginHeight != 0) {
+			builder.append("    .margins("); //$NON-NLS-1$
+			builder.append(layout.marginWidth);
+			builder.append(", "); //$NON-NLS-1$
+			builder.append(layout.marginHeight);
+			builder.append(")\n"); //$NON-NLS-1$
+		}
+		return builder.toString();
+	}
+}