diff options
author | Marcus Hoepfner | 2019-02-15 16:43:10 +0000 |
---|---|---|
committer | Marcus Hoepfner | 2019-02-21 13:26:21 +0000 |
commit | c9556698bdbbc0e4a0af88a33ea126fcfc4335ea (patch) | |
tree | 5feeca45ae36268fb5a217a54a1bd4c936aef07c | |
parent | 8917477118d5be03c48ad67a7a04654af7e0c0b1 (diff) | |
download | eclipse.platform.ui-c9556698bdbbc0e4a0af88a33ea126fcfc4335ea.tar.gz eclipse.platform.ui-c9556698bdbbc0e4a0af88a33ea126fcfc4335ea.tar.xz eclipse.platform.ui-c9556698bdbbc0e4a0af88a33ea126fcfc4335ea.zip |
Bug 544502: added factory for TableColumnI20190222-0440
Change-Id: Icfc9326bd7280061bf65ab1640d7dc2901d3539b
Signed-off-by: Marcus Hoepfner <marcus.hoepfner@sap.com>
4 files changed, 214 insertions, 1 deletions
diff --git a/bundles/org.eclipse.jface/src/org/eclipse/jface/widgets/TableColumnFactory.java b/bundles/org.eclipse.jface/src/org/eclipse/jface/widgets/TableColumnFactory.java new file mode 100644 index 00000000000..31567ccf214 --- /dev/null +++ b/bundles/org.eclipse.jface/src/org/eclipse/jface/widgets/TableColumnFactory.java @@ -0,0 +1,134 @@ +/******************************************************************************* +* Copyright (c) 2019 SAP SE 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: +* SAP SE - initial version +******************************************************************************/ +package org.eclipse.jface.widgets; + +import java.util.function.Consumer; + +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.widgets.Table; +import org.eclipse.swt.widgets.TableColumn; + +/** + * <p> + * <strong>EXPERIMENTAL</strong>. This class has been added as part of a work in + * progress. There is no guarantee that this API will work or that it will + * remain the same. Feel free to use it and give feedback via + * https://bugs.eclipse.org/bugs/buglist.cgi?component=UI&product=Platform, but + * be aware that it might change. + * </p> + * + * This class provides a convenient shorthand for creating and initializing + * {@link TableColumn}. This offers several benefits over creating TableColumn + * normal way: + * + * <ul> + * <li>The same factory can be used many times to create several Table + * instances</li> + * <li>The setters on TableColumnFactory all return "this", allowing them to be + * chained</li> + * <li>TableColumnFactory accepts a Lambda for {@link SelectionEvent} (see + * {@link #onSelect})</li> + * </ul> + * + * Example usage: + * + * <pre> + * TableColumn column = TableColumnFactory.newTableColumn(SWT.CENTER) // + * .text("Table Column") // + * .onSelect(event -> columnClicked(event)) // + * .create(table); + * </pre> + * <p> + * The above example creates a table column, sets text, registers a + * SelectionListener and finally creates the table in "table". + * <p> + * + * <pre> + * TableColumnFactory factory = TableColumnFactory.newTableColumn(SWT.CENTER).onSelect(event -> columnClicked(event)); + * factory.text("Column 1").create(table); + * factory.text("Column 2").create(table); + * factory.text("Column 3").create(table); + * </pre> + * <p> + * The above example creates three table columns using the same instance of + * factory. + * <p> + * + */ +public class TableColumnFactory extends AbstractItemFactory<TableColumnFactory, TableColumn, Table> { + + private TableColumnFactory(int style) { + super(TableColumnFactory.class, table -> new TableColumn(table, style)); + } + + /** + * Creates a new TableColumnFactory with the given style. Refer to + * {@link TableColumn#TableColumn(Table, int)} for possible styles. + * + * @param style + * @return a new TableColumnFactory instance + */ + public static TableColumnFactory newTableColumn(int style) { + return new TableColumnFactory(style); + + } + + /** + * Creates a {@link SelectionListener} and registers it for the widgetSelected + * event. If event is raised it calls the given consumer. The + * {@link SelectionEvent} is passed to the consumer. + * + * @param consumer + * @return this + */ + public TableColumnFactory onSelect(Consumer<SelectionEvent> consumer) { + addProperty(c -> c.addSelectionListener(SelectionListener.widgetSelectedAdapter(consumer))); + return this; + } + + /** + * Sets the alignment. + * + * @param alignment + * @return this + */ + public TableColumnFactory align(int alignment) { + addProperty(c -> c.setAlignment(alignment)); + return this; + } + + /** + * Sets the tooltip. + * + * @param tooltip + * @return this + */ + public TableColumnFactory tooltip(String tooltip) { + addProperty(c -> c.setToolTipText(tooltip)); + return this; + } + + /** + * Sets the width. + * + * @param width + * @return this + */ + public TableColumnFactory width(int width) { + addProperty(c -> c.setWidth(width)); + return this; + } + +} diff --git a/bundles/org.eclipse.jface/src/org/eclipse/jface/widgets/WidgetFactory.java b/bundles/org.eclipse.jface/src/org/eclipse/jface/widgets/WidgetFactory.java index d1b146ff358..83eafc76f6c 100644 --- a/bundles/org.eclipse.jface/src/org/eclipse/jface/widgets/WidgetFactory.java +++ b/bundles/org.eclipse.jface/src/org/eclipse/jface/widgets/WidgetFactory.java @@ -19,6 +19,7 @@ import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Spinner; import org.eclipse.swt.widgets.Table; +import org.eclipse.swt.widgets.TableColumn; import org.eclipse.swt.widgets.Text; /** @@ -123,4 +124,14 @@ public final class WidgetFactory { public static TableFactory table(int style) { return TableFactory.newTable(style); } + + /** + * @param style SWT style applicable for TableColumn. Refer to + * {@link TableColumn#TableColumn(Table, int)} for supported + * styles. + * @return TableColumnFactory + */ + public static TableColumnFactory tableColumn(int style) { + return TableColumnFactory.newTableColumn(style); + } }
\ No newline at end of file diff --git a/tests/org.eclipse.jface.tests/src/org/eclipse/jface/suites/AllUnitTests.java b/tests/org.eclipse.jface.tests/src/org/eclipse/jface/suites/AllUnitTests.java index adeda6bd543..59a1860ec80 100644 --- a/tests/org.eclipse.jface.tests/src/org/eclipse/jface/suites/AllUnitTests.java +++ b/tests/org.eclipse.jface.tests/src/org/eclipse/jface/suites/AllUnitTests.java @@ -16,6 +16,7 @@ import org.eclipse.jface.widgets.TestUnitControlFactory; import org.eclipse.jface.widgets.TestUnitItemFactory; import org.eclipse.jface.widgets.TestUnitLabelFactory; import org.eclipse.jface.widgets.TestUnitSpinnerFactory; +import org.eclipse.jface.widgets.TestUnitTableColumnFactory; import org.eclipse.jface.widgets.TestUnitTableFactory; import org.eclipse.jface.widgets.TestUnitTextFactory; import org.junit.runner.RunWith; @@ -31,7 +32,8 @@ import org.junit.runners.Suite.SuiteClasses; TestUnitSpinnerFactory.class, // TestUnitTextFactory.class, // TestUnitTableFactory.class, // - TestUnitItemFactory.class,// + TestUnitItemFactory.class, // + TestUnitTableColumnFactory.class, // }) public class AllUnitTests { diff --git a/tests/org.eclipse.jface.tests/src/org/eclipse/jface/widgets/TestUnitTableColumnFactory.java b/tests/org.eclipse.jface.tests/src/org/eclipse/jface/widgets/TestUnitTableColumnFactory.java new file mode 100644 index 00000000000..ce0c0094b78 --- /dev/null +++ b/tests/org.eclipse.jface.tests/src/org/eclipse/jface/widgets/TestUnitTableColumnFactory.java @@ -0,0 +1,66 @@ +/******************************************************************************* +* Copyright (c) 2019 SAP SE 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: +* SAP SE - initial version +******************************************************************************/ +package org.eclipse.jface.widgets; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Table; +import org.eclipse.swt.widgets.TableColumn; +import org.junit.Before; +import org.junit.Test; + +public class TestUnitTableColumnFactory extends AbstractFactoryTest { + + private Table table; + + @Override + @Before + public void setup() { + super.setup(); + table = WidgetFactory.table(SWT.NONE).create(shell); + } + + @Test + public void createTableColumn() { + TableColumn tableColumn = TableColumnFactory.newTableColumn(SWT.CENTER).create(table); + + assertEquals(table.getColumn(0), tableColumn); + assertEquals(tableColumn.getParent(), table); + assertEquals(SWT.CENTER, tableColumn.getStyle() & SWT.CENTER); + } + + @Test + public void createTableColumnWithAllProperties() { + final SelectionEvent[] raisedEvents = new SelectionEvent[1]; + TableColumn tableColumn = TableColumnFactory.newTableColumn(SWT.NONE) // + .onSelect(e -> raisedEvents[0] = e) // + .align(SWT.LEFT) // + .tooltip("tooltip") // + .width(10) // + .create(table); + + tableColumn.notifyListeners(SWT.Selection, new Event()); + + assertEquals(1, tableColumn.getListeners(SWT.Selection).length); + assertNotNull(raisedEvents[0]); + + assertEquals(SWT.LEFT, tableColumn.getAlignment()); + assertEquals("tooltip", tableColumn.getToolTipText()); + assertEquals(10, tableColumn.getWidth()); + } +} |