From 93764926d6a014cd14e1f3cffea3ef22ce7e40fb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Tue, 24 Feb 2015 13:30:51 +0200 Subject: [PATCH] Code for current Grid mini tutorials Change-Id: Icc5d99713caca43b537e90a267ff2a93da166572 --- .../v7_4/ConfiguringColumnWidths.java | 90 ++++++++++++++++++ .../ConfiguringGridHeadersAndFooters.java | 54 +++++++++++ .../v7_4/FormattingDataInGrid.java | 77 ++++++++++++++++ .../minitutorials/v7_4/GridExampleBean.java | 56 ++++++++++++ .../minitutorials/v7_4/GridExampleHelper.java | 29 ++++++ .../v7_4/ShowingInlineDataInGrid.java | 47 ++++++++++ .../v7_4/UsingGridWithAContainer.java | 34 +++++++ .../v7_5/ShowingExtraDataForRows.java | 91 +++++++++++++++++++ 8 files changed, 478 insertions(+) create mode 100644 uitest/src/com/vaadin/tests/minitutorials/v7_4/ConfiguringColumnWidths.java create mode 100644 uitest/src/com/vaadin/tests/minitutorials/v7_4/ConfiguringGridHeadersAndFooters.java create mode 100644 uitest/src/com/vaadin/tests/minitutorials/v7_4/FormattingDataInGrid.java create mode 100644 uitest/src/com/vaadin/tests/minitutorials/v7_4/GridExampleBean.java create mode 100644 uitest/src/com/vaadin/tests/minitutorials/v7_4/GridExampleHelper.java create mode 100644 uitest/src/com/vaadin/tests/minitutorials/v7_4/ShowingInlineDataInGrid.java create mode 100644 uitest/src/com/vaadin/tests/minitutorials/v7_4/UsingGridWithAContainer.java create mode 100644 uitest/src/com/vaadin/tests/minitutorials/v7_5/ShowingExtraDataForRows.java diff --git a/uitest/src/com/vaadin/tests/minitutorials/v7_4/ConfiguringColumnWidths.java b/uitest/src/com/vaadin/tests/minitutorials/v7_4/ConfiguringColumnWidths.java new file mode 100644 index 0000000000..3204ebd11c --- /dev/null +++ b/uitest/src/com/vaadin/tests/minitutorials/v7_4/ConfiguringColumnWidths.java @@ -0,0 +1,90 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.minitutorials.v7_4; + +import com.vaadin.annotations.Theme; +import com.vaadin.server.VaadinRequest; +import com.vaadin.ui.Grid; +import com.vaadin.ui.Grid.SelectionMode; +import com.vaadin.ui.UI; + +@Theme("valo") +public class ConfiguringColumnWidths extends UI { + + @Override + protected void init(VaadinRequest request) { + Grid grid = new Grid(GridExampleHelper.createContainer()); + grid.setSelectionMode(SelectionMode.NONE); + grid.setColumnOrder("name", "amount", "count"); + + setupCase(grid, 3); + + setContent(grid); + } + + private void setupCase1(Grid grid) { + grid.getColumn("name").setExpandRatio(1); + } + + private void setupCase2(Grid grid) { + grid.getColumn("name").setExpandRatio(1); + grid.getColumn("amount").setWidth(100); + grid.getColumn("count").setWidth(100); + } + + private void setupCase3(Grid grid) { + grid.setWidth("400px"); + grid.getColumn("name").setExpandRatio(1); + grid.getColumn("amount").setWidth(100); + grid.getColumn("count").setWidth(100); + } + + private void setupCase4(Grid grid) { + grid.setWidth("400px"); + grid.getColumn("name").setMinimumWidth(250); + grid.getColumn("amount").setWidth(100); + grid.getColumn("count").setWidth(100); + } + + private void setupCase5(Grid grid) { + grid.setWidth("400px"); + grid.setFrozenColumnCount(1); + grid.getColumn("name").setMinimumWidth(250); + grid.getColumn("amount").setWidth(100); + grid.getColumn("count").setWidth(100); + } + + private void setupCase6(Grid grid) { + grid.setWidth("700px"); + grid.setFrozenColumnCount(1); + grid.getColumn("name").setMinimumWidth(250); + grid.getColumn("amount").setWidth(100); + grid.getColumn("count").setWidth(100); + } + + private void setupCase(Grid grid, int number) { + if (number == 0) { + return; + } + try { + getClass().getDeclaredMethod("setupCase" + number, Grid.class) + .invoke(this, grid); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + +} diff --git a/uitest/src/com/vaadin/tests/minitutorials/v7_4/ConfiguringGridHeadersAndFooters.java b/uitest/src/com/vaadin/tests/minitutorials/v7_4/ConfiguringGridHeadersAndFooters.java new file mode 100644 index 0000000000..57e807a224 --- /dev/null +++ b/uitest/src/com/vaadin/tests/minitutorials/v7_4/ConfiguringGridHeadersAndFooters.java @@ -0,0 +1,54 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.minitutorials.v7_4; + +import com.vaadin.annotations.Theme; +import com.vaadin.server.VaadinRequest; +import com.vaadin.ui.Button; +import com.vaadin.ui.Grid; +import com.vaadin.ui.Grid.FooterCell; +import com.vaadin.ui.Grid.HeaderCell; +import com.vaadin.ui.Grid.HeaderRow; +import com.vaadin.ui.UI; + +@Theme("valo") +public class ConfiguringGridHeadersAndFooters extends UI { + @Override + protected void init(VaadinRequest request) { + Grid grid = new Grid(GridExampleHelper.createContainer()); + grid.setColumnOrder("name", "amount", "count"); + + grid.getDefaultHeaderRow().getCell("amount") + .setHtml("The amount"); + grid.getDefaultHeaderRow().getCell("count") + .setComponent(new Button("Button caption")); + + grid.getColumn("name").setHeaderCaption("Bean name"); + + HeaderRow extraHeader = grid.prependHeaderRow(); + HeaderCell joinedCell = extraHeader.join("amount", "count"); + joinedCell.setText("Joined cell"); + + FooterCell footer = grid.appendFooterRow().join("name", "amount", + "count"); + footer.setText("Right aligned footer"); + + getPage().getStyles().add(".footer-right { text-align: right }"); + footer.setStyleName("footer-right"); + + setContent(grid); + } +} diff --git a/uitest/src/com/vaadin/tests/minitutorials/v7_4/FormattingDataInGrid.java b/uitest/src/com/vaadin/tests/minitutorials/v7_4/FormattingDataInGrid.java new file mode 100644 index 0000000000..3fd9c87262 --- /dev/null +++ b/uitest/src/com/vaadin/tests/minitutorials/v7_4/FormattingDataInGrid.java @@ -0,0 +1,77 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.minitutorials.v7_4; + +import java.text.NumberFormat; +import java.util.Locale; + +import com.vaadin.annotations.Theme; +import com.vaadin.data.util.converter.Converter; +import com.vaadin.data.util.converter.StringToIntegerConverter; +import com.vaadin.server.VaadinRequest; +import com.vaadin.ui.Grid; +import com.vaadin.ui.Grid.CellReference; +import com.vaadin.ui.Grid.CellStyleGenerator; +import com.vaadin.ui.UI; +import com.vaadin.ui.renderers.HtmlRenderer; +import com.vaadin.ui.renderers.NumberRenderer; + +@Theme("valo") +public class FormattingDataInGrid extends UI { + + @Override + protected void init(VaadinRequest request) { + Grid grid = new Grid(GridExampleHelper.createContainer()); + + setContent(grid); + + grid.setCellStyleGenerator(new CellStyleGenerator() { + @Override + public String getStyle(CellReference cellReference) { + if ("amount".equals(cellReference.getPropertyId())) { + Double value = (Double) cellReference.getValue(); + if (value.doubleValue() == Math.round(value.doubleValue())) { + return "integer"; + } + } + return null; + } + }); + + getPage().getStyles().add(".integer { color: blue; }"); + + NumberFormat poundformat = NumberFormat.getCurrencyInstance(Locale.UK); + NumberRenderer poundRenderer = new NumberRenderer(poundformat); + grid.getColumn("amount").setRenderer(poundRenderer); + + grid.getColumn("count").setConverter(new StringToIntegerConverter() { + @Override + public String convertToPresentation(Integer value, + Class targetType, Locale locale) + throws Converter.ConversionException { + String stringRepresentation = super.convertToPresentation( + value, targetType, locale); + if (value.intValue() % 2 == 0) { + return "" + stringRepresentation + ""; + } else { + return "" + stringRepresentation + ""; + } + } + }); + + grid.getColumn("count").setRenderer(new HtmlRenderer()); + } +} diff --git a/uitest/src/com/vaadin/tests/minitutorials/v7_4/GridExampleBean.java b/uitest/src/com/vaadin/tests/minitutorials/v7_4/GridExampleBean.java new file mode 100644 index 0000000000..c39f458a6b --- /dev/null +++ b/uitest/src/com/vaadin/tests/minitutorials/v7_4/GridExampleBean.java @@ -0,0 +1,56 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.minitutorials.v7_4; + +public class GridExampleBean { + private String name; + private int count; + private double amount; + + public GridExampleBean() { + + } + + public GridExampleBean(String name, int count, double amount) { + this.name = name; + this.count = count; + this.amount = amount; + } + + public String getName() { + return name; + } + + public int getCount() { + return count; + } + + public double getAmount() { + return amount; + } + + public void setName(String name) { + this.name = name; + } + + public void setCount(int count) { + this.count = count; + } + + public void setAmount(double amount) { + this.amount = amount; + } +} \ No newline at end of file diff --git a/uitest/src/com/vaadin/tests/minitutorials/v7_4/GridExampleHelper.java b/uitest/src/com/vaadin/tests/minitutorials/v7_4/GridExampleHelper.java new file mode 100644 index 0000000000..539d3431e8 --- /dev/null +++ b/uitest/src/com/vaadin/tests/minitutorials/v7_4/GridExampleHelper.java @@ -0,0 +1,29 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.minitutorials.v7_4; + +import com.vaadin.data.util.BeanItemContainer; + +public class GridExampleHelper { + public static BeanItemContainer createContainer() { + BeanItemContainer container = new BeanItemContainer( + GridExampleBean.class); + for (int i = 0; i < 1000; i++) { + container.addItem(new GridExampleBean("Bean " + i, i * i, i / 10d)); + } + return container; + } +} diff --git a/uitest/src/com/vaadin/tests/minitutorials/v7_4/ShowingInlineDataInGrid.java b/uitest/src/com/vaadin/tests/minitutorials/v7_4/ShowingInlineDataInGrid.java new file mode 100644 index 0000000000..72f101ab7a --- /dev/null +++ b/uitest/src/com/vaadin/tests/minitutorials/v7_4/ShowingInlineDataInGrid.java @@ -0,0 +1,47 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.minitutorials.v7_4; + +import com.vaadin.annotations.Theme; +import com.vaadin.server.VaadinRequest; +import com.vaadin.shared.ui.grid.HeightMode; +import com.vaadin.ui.Grid; +import com.vaadin.ui.UI; + +@Theme("valo") +public class ShowingInlineDataInGrid extends UI { + + @Override + protected void init(VaadinRequest request) { + final Grid grid = new Grid(); + + grid.addColumn("Name").setSortable(true); + grid.addColumn("Score", Integer.class); + + grid.addRow("Alice", 15); + grid.addRow("Bob", -7); + grid.addRow("Carol", 8); + grid.addRow("Dan", 0); + grid.addRow("Eve", 20); + + grid.select(2); + + grid.setHeightByRows(grid.getContainerDataSource().size()); + grid.setHeightMode(HeightMode.ROW); + + setContent(grid); + } +} diff --git a/uitest/src/com/vaadin/tests/minitutorials/v7_4/UsingGridWithAContainer.java b/uitest/src/com/vaadin/tests/minitutorials/v7_4/UsingGridWithAContainer.java new file mode 100644 index 0000000000..4cf3f14c2b --- /dev/null +++ b/uitest/src/com/vaadin/tests/minitutorials/v7_4/UsingGridWithAContainer.java @@ -0,0 +1,34 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.minitutorials.v7_4; + +import com.vaadin.server.VaadinRequest; +import com.vaadin.ui.Grid; +import com.vaadin.ui.UI; + +public class UsingGridWithAContainer extends UI { + @Override + protected void init(VaadinRequest request) { + Grid grid = new Grid(); + grid.setContainerDataSource(GridExampleHelper.createContainer()); + + grid.getColumn("name").setHeaderCaption("Bean name"); + grid.removeColumn("count"); + grid.setColumnOrder("name", "amount"); + + setContent(grid); + } +} diff --git a/uitest/src/com/vaadin/tests/minitutorials/v7_5/ShowingExtraDataForRows.java b/uitest/src/com/vaadin/tests/minitutorials/v7_5/ShowingExtraDataForRows.java new file mode 100644 index 0000000000..eaa099f3eb --- /dev/null +++ b/uitest/src/com/vaadin/tests/minitutorials/v7_5/ShowingExtraDataForRows.java @@ -0,0 +1,91 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.minitutorials.v7_5; + +import com.vaadin.event.ItemClickEvent; +import com.vaadin.event.ItemClickEvent.ItemClickListener; +import com.vaadin.server.ExternalResource; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.minitutorials.v7_4.GridExampleBean; +import com.vaadin.tests.minitutorials.v7_4.GridExampleHelper; +import com.vaadin.ui.Button; +import com.vaadin.ui.Button.ClickEvent; +import com.vaadin.ui.Component; +import com.vaadin.ui.Grid; +import com.vaadin.ui.Grid.DetailsGenerator; +import com.vaadin.ui.Grid.RowReference; +import com.vaadin.ui.Image; +import com.vaadin.ui.Label; +import com.vaadin.ui.Notification; +import com.vaadin.ui.UI; +import com.vaadin.ui.VerticalLayout; + +public class ShowingExtraDataForRows extends UI { + @Override + protected void init(VaadinRequest request) { + final Grid grid = new Grid(); + grid.setContainerDataSource(GridExampleHelper.createContainer()); + + grid.setDetailsGenerator(new DetailsGenerator() { + @Override + public Component getDetails(RowReference rowReference) { + // Find the bean to generate details for + final GridExampleBean bean = (GridExampleBean) rowReference + .getItemId(); + + // A basic label with bean data + Label label = new Label("Extra data for " + bean.getName()); + + // An image with extra details about the bean + Image image = new Image(); + image.setWidth("300px"); + image.setHeight("150px"); + image.setSource(new ExternalResource( + "http://dummyimage.com/300x150/000/fff&text=" + + bean.getCount())); + + // A button just for the sake of the example + Button button = new Button("Click me", + new Button.ClickListener() { + @Override + public void buttonClick(ClickEvent event) { + Notification.show("Button clicked for " + + bean.getName()); + } + }); + + // Wrap up all the parts into a vertical layout + VerticalLayout layout = new VerticalLayout(label, image, button); + layout.setSpacing(true); + layout.setMargin(true); + return layout; + } + }); + + grid.addItemClickListener(new ItemClickListener() { + @Override + public void itemClick(ItemClickEvent event) { + if (event.isDoubleClick()) { + Object itemId = event.getItemId(); + grid.setDetailsVisible(itemId, + !grid.isDetailsVisible(itemId)); + } + } + }); + + setContent(grid); + } +} -- 2.39.5