Browse Source

Code for current Grid mini tutorials

Change-Id: Icc5d99713caca43b537e90a267ff2a93da166572
tags/7.5.0.beta1
Leif Åstrand 9 years ago
parent
commit
93764926d6

+ 90
- 0
uitest/src/com/vaadin/tests/minitutorials/v7_4/ConfiguringColumnWidths.java View File

@@ -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);
}
}

}

+ 54
- 0
uitest/src/com/vaadin/tests/minitutorials/v7_4/ConfiguringGridHeadersAndFooters.java View File

@@ -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 <u>amount</u>");
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);
}
}

+ 77
- 0
uitest/src/com/vaadin/tests/minitutorials/v7_4/FormattingDataInGrid.java View File

@@ -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<? extends String> targetType, Locale locale)
throws Converter.ConversionException {
String stringRepresentation = super.convertToPresentation(
value, targetType, locale);
if (value.intValue() % 2 == 0) {
return "<strong>" + stringRepresentation + "</strong>";
} else {
return "<em>" + stringRepresentation + "</em>";
}
}
});

grid.getColumn("count").setRenderer(new HtmlRenderer());
}
}

+ 56
- 0
uitest/src/com/vaadin/tests/minitutorials/v7_4/GridExampleBean.java View File

@@ -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;
}
}

+ 29
- 0
uitest/src/com/vaadin/tests/minitutorials/v7_4/GridExampleHelper.java View File

@@ -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<GridExampleBean> createContainer() {
BeanItemContainer<GridExampleBean> container = new BeanItemContainer<GridExampleBean>(
GridExampleBean.class);
for (int i = 0; i < 1000; i++) {
container.addItem(new GridExampleBean("Bean " + i, i * i, i / 10d));
}
return container;
}
}

+ 47
- 0
uitest/src/com/vaadin/tests/minitutorials/v7_4/ShowingInlineDataInGrid.java View File

@@ -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);
}
}

+ 34
- 0
uitest/src/com/vaadin/tests/minitutorials/v7_4/UsingGridWithAContainer.java View File

@@ -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);
}
}

+ 91
- 0
uitest/src/com/vaadin/tests/minitutorials/v7_5/ShowingExtraDataForRows.java View File

@@ -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);
}
}

Loading…
Cancel
Save