From: Marko Grönroos Date: Tue, 16 Sep 2008 08:13:40 +0000 (+0000) Subject: Reverted mass formatting done in [5331] for Form and Generated Column examples. X-Git-Tag: 6.7.0.beta1~4157 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7829562f271bb467e9e291df54ce26ee68454b6c;p=vaadin-framework.git Reverted mass formatting done in [5331] for Form and Generated Column examples. svn changeset:5408/svn branch:trunk --- diff --git a/src/com/itmill/toolkit/demo/featurebrowser/FormExample.java b/src/com/itmill/toolkit/demo/featurebrowser/FormExample.java index cc8465b43d..465e11f05d 100644 --- a/src/com/itmill/toolkit/demo/featurebrowser/FormExample.java +++ b/src/com/itmill/toolkit/demo/featurebrowser/FormExample.java @@ -2,11 +2,16 @@ package com.itmill.toolkit.demo.featurebrowser; import java.util.Vector; +import org.apache.commons.digester.SetRootRule; + import com.itmill.toolkit.data.Container; import com.itmill.toolkit.data.Item; import com.itmill.toolkit.data.Property; import com.itmill.toolkit.data.Validator; +import com.itmill.toolkit.data.Item.PropertySetChangeEvent; +import com.itmill.toolkit.data.Item.PropertySetChangeListener; import com.itmill.toolkit.data.util.BeanItem; +import com.itmill.toolkit.data.util.ObjectProperty; import com.itmill.toolkit.ui.Button; import com.itmill.toolkit.ui.Component; import com.itmill.toolkit.ui.CustomComponent; @@ -14,9 +19,12 @@ import com.itmill.toolkit.ui.ExpandLayout; import com.itmill.toolkit.ui.Field; import com.itmill.toolkit.ui.FieldFactory; import com.itmill.toolkit.ui.Form; +import com.itmill.toolkit.ui.FormLayout; import com.itmill.toolkit.ui.Label; import com.itmill.toolkit.ui.OrderedLayout; +import com.itmill.toolkit.ui.Panel; import com.itmill.toolkit.ui.Select; +import com.itmill.toolkit.ui.Table; import com.itmill.toolkit.ui.TextField; import com.itmill.toolkit.ui.Button.ClickEvent; @@ -32,9 +40,9 @@ import com.itmill.toolkit.ui.Button.ClickEvent; public class FormExample extends CustomComponent { /** Contact information data model. */ public class Contact { - String name = ""; - String address = ""; - int postalCode = 0; + String name = ""; + String address = ""; + int postalCode = 0; String city; } @@ -106,10 +114,10 @@ public class FormExample extends CustomComponent { if (pid.equals("name")) return new TextField("Name"); - + if (pid.equals("address")) return new TextField("Street Address"); - + if (pid.equals("postalCode")) { TextField field = new TextField("Postal Code"); field.setColumns(5); @@ -123,8 +131,7 @@ public class FormExample extends CustomComponent { return ((String) value).matches("[0-9]{5}"); } - public void validate(Object value) - throws InvalidValueException { + public void validate(Object value) throws InvalidValueException { if (!isValid(value)) { throw new InvalidValueException( "Postal code must be a number 10000-99999."); @@ -134,7 +141,7 @@ public class FormExample extends CustomComponent { field.addValidator(postalCodeValidator); return field; } - + if (pid.equals("city")) { Select select = new Select("City"); final String cities[] = new String[] { "Amsterdam", "Berlin", @@ -164,23 +171,22 @@ public class FormExample extends CustomComponent { **/ public class ContactDisplay extends CustomComponent { ContactBean contact; - Label name; - Label address; - Label postalCode; - Label city; + Label name; + Label address; + Label postalCode; + Label city; - public ContactDisplay(ContactBean contact) { + public ContactDisplay (ContactBean contact) { this.contact = contact; - + // Use a Form merely as a layout component. The CSS will add // a border to the form. Form layout = new Form(); setCompositionRoot(layout); layout.setCaption("Data Model State"); - layout - .setDescription("Below is the state of the actual stored data. " - + "It is updated only when the form is committed successfully. " - + "Discarding the form input reverts the form to this state."); + layout.setDescription("Below is the state of the actual stored data. "+ + "It is updated only when the form is committed successfully. "+ + "Discarding the form input reverts the form to this state."); layout.setWidth("400px"); // Manually create read-only components for each of the fields. @@ -192,7 +198,7 @@ public class FormExample extends CustomComponent { postalCode.setCaption("Postal Code:"); city = new Label(contact.getCity()); city.setCaption("City:"); - + layout.getLayout().addComponent(name); layout.getLayout().addComponent(address); layout.getLayout().addComponent(postalCode); @@ -213,11 +219,10 @@ public class FormExample extends CustomComponent { public FormExample() { // The root layout of the custom component. - OrderedLayout root = new OrderedLayout( - OrderedLayout.ORIENTATION_HORIZONTAL); + OrderedLayout root = new OrderedLayout(OrderedLayout.ORIENTATION_HORIZONTAL); root.addStyleName("formroot"); setCompositionRoot(root); - + // Create a form. It will use FormLayout as its layout by default. final Form form = new Form(); root.addComponent(form); @@ -228,10 +233,9 @@ public class FormExample extends CustomComponent { form.setCaption("Contact Information"); // Set description that will appear on top of the form. - form - .setDescription("Please enter valid name and address. Fields marked with * are required. " - + "If you try to commit with invalid values, a form error message is displayed. " - + "(Address is required but failing to give it a value does not display an error.)"); + form.setDescription("Please enter valid name and address. Fields marked with * are required. "+ + "If you try to commit with invalid values, a form error message is displayed. " + + "(Address is required but failing to give it a value does not display an error.)"); // Use custom field factory to create the fields in the form. form.setFieldFactory(new MyFieldFactory()); @@ -265,25 +269,23 @@ public class FormExample extends CustomComponent { // necessary for the validation of the fields to occur immediately when // the input focus changes and not just on commit. form.setImmediate(true); - + // Set buffering so that commit() must be called for the form // before input is written to the data. (Form input is not written // immediately through to the underlying object.) form.setWriteThrough(false); - + // If the state of the bound data source changes, the changes are shown - // immediately in the form, so there is no buffering. (This is the - // default.) + // immediately in the form, so there is no buffering. (This is the default.) form.setReadThrough(true); // Have a read-only component to display the actual current state // of the bean (POJO). final ContactDisplay display = new ContactDisplay(bean); root.addComponent(display); - + // Add Commit and Discard controls to the form. - ExpandLayout footer = new ExpandLayout( - OrderedLayout.ORIENTATION_HORIZONTAL); + ExpandLayout footer = new ExpandLayout(OrderedLayout.ORIENTATION_HORIZONTAL); // The Commit button calls form.commit(). Button commit = new Button("Commit", new Button.ClickListener() { @@ -297,7 +299,7 @@ public class FormExample extends CustomComponent { Button discard = new Button("Discard", form, "discard"); footer.addComponent(commit); footer.setComponentAlignment(commit, ExpandLayout.ALIGNMENT_RIGHT, - ExpandLayout.ALIGNMENT_TOP); + ExpandLayout.ALIGNMENT_TOP); footer.setHeight("25px"); // Has to be set explicitly for ExpandLayout. footer.addComponent(discard); form.setFooter(footer); diff --git a/src/com/itmill/toolkit/demo/featurebrowser/GeneratedColumnExample.java b/src/com/itmill/toolkit/demo/featurebrowser/GeneratedColumnExample.java index 90a5eb925c..8d9dd3abf7 100644 --- a/src/com/itmill/toolkit/demo/featurebrowser/GeneratedColumnExample.java +++ b/src/com/itmill/toolkit/demo/featurebrowser/GeneratedColumnExample.java @@ -27,12 +27,12 @@ import com.itmill.toolkit.ui.Button.ClickEvent; import com.itmill.toolkit.ui.Button.ClickListener; /** - * This example demonstrates the use of generated columns in a table. Generated - * columns can be used for formatting values or calculating them from other - * columns (or properties of the items). + * This example demonstrates the use of generated columns in a table. + * Generated columns can be used for formatting values or calculating + * them from other columns (or properties of the items). * - * For the data model, we use POJOs bound to a custom Container with BeanItem - * items. + * For the data model, we use POJOs bound to a custom Container + * with BeanItem items. * * @author magi */ @@ -48,9 +48,8 @@ public class GeneratedColumnExample extends CustomComponent { public FillUp() { } - public FillUp(int day, int month, int year, double quantity, - double total) { - date = new GregorianCalendar(year, month - 1, day).getTime(); + public FillUp(int day, int month, int year, double quantity, double total) { + date = new GregorianCalendar(year, month-1, day).getTime(); this.quantity = quantity; this.total = total; } @@ -68,8 +67,7 @@ public class GeneratedColumnExample extends CustomComponent { double difference_ms = date.getTime() - other.date.getTime(); double days = difference_ms / 1000 / 3600 / 24; if (days < 0.5) - days = 1.0; // Avoid division by zero if two fill-ups on the - // same day. + days = 1.0; // Avoid division by zero if two fill-ups on the same day. return quantity / days; } @@ -113,10 +111,10 @@ public class GeneratedColumnExample extends CustomComponent { * Most of the interface methods are implemented with just dummy * implementations, as they are not needed in this example. */ - public class MySimpleIndexedContainer implements Container, Indexed { + public class MySimpleIndexedContainer implements Container,Indexed { Vector items; Object itemtemplate; - + public MySimpleIndexedContainer(Object itemtemplate) { this.itemtemplate = itemtemplate; items = new Vector(); // Yeah this is just a test @@ -162,7 +160,7 @@ public class GeneratedColumnExample extends CustomComponent { int pos = ((Integer) itemId).intValue(); if (pos >= 0 && pos < items.size()) { Item item = (Item) items.get(pos); - + // The BeanItem provides the property objects for the items. return item.getItemProperty(propertyId); } @@ -180,7 +178,7 @@ public class GeneratedColumnExample extends CustomComponent { public Item getItem(Object itemId) { if (itemId instanceof Integer) { - int pos = ((Integer) itemId).intValue(); + int pos = ((Integer)itemId).intValue(); if (pos >= 0 && pos < items.size()) return (Item) items.get(pos); } @@ -256,25 +254,25 @@ public class GeneratedColumnExample extends CustomComponent { } public boolean isLastId(Object itemId) { - return ((Integer) itemId).intValue() == (items.size() - 1); + return ((Integer) itemId).intValue() == (items.size()-1); } public Object lastItemId() { - return new Integer(items.size() - 1); + return new Integer(items.size()-1); } public Object nextItemId(Object itemId) { int pos = indexOfId(itemId); - if (pos >= items.size() - 1) + if (pos >= items.size()-1) return null; - return getIdByIndex(pos + 1); + return getIdByIndex(pos+1); } public Object prevItemId(Object itemId) { int pos = indexOfId(itemId); if (pos <= 0) return null; - return getIdByIndex(pos - 1); + return getIdByIndex(pos-1); } } @@ -284,8 +282,7 @@ public class GeneratedColumnExample extends CustomComponent { * Generates the cell containing the Date value. The column is * irrelevant in this use case. */ - public Component generateCell(Table source, Object itemId, - Object columnId) { + public Component generateCell(Table source, Object itemId, Object columnId) { Property prop = source.getItem(itemId).getItemProperty(columnId); if (prop.getType().equals(Date.class)) { Label label = new Label(String.format("%tF", @@ -301,27 +298,24 @@ public class GeneratedColumnExample extends CustomComponent { /** Formats the value in a column containing Double objects. */ class ValueColumnGenerator implements Table.ColumnGenerator { String format; /* Format string for the Double values. */ - + /** Creates double value column formatter with the given format string. */ public ValueColumnGenerator(String format) { this.format = format; } - + /** * Generates the cell containing the Double value. The column is * irrelevant in this use case. */ - public Component generateCell(Table source, Object itemId, - Object columnId) { + public Component generateCell(Table source, Object itemId, Object columnId) { Property prop = source.getItem(itemId).getItemProperty(columnId); if (prop.getType().equals(Double.class)) { Label label = new Label(String.format(format, new Object[] { (Double) prop.getValue() })); - - // Set styles for the column: one indicating that it's a value - // and a more - // specific one with the column name in it. This assumes that - // the column + + // Set styles for the column: one indicating that it's a value and a more + // specific one with the column name in it. This assumes that the column // name is proper for CSS. label.addStyleName("column-type-value"); label.addStyleName("column-" + (String) columnId); @@ -333,8 +327,7 @@ public class GeneratedColumnExample extends CustomComponent { /** Table column generator for calculating price column. */ class PriceColumnGenerator implements Table.ColumnGenerator { - public Component generateCell(Table source, Object itemId, - Object columnId) { + public Component generateCell(Table source, Object itemId, Object columnId) { // Retrieve the item. BeanItem item = (BeanItem) source.getItem(itemId); @@ -361,8 +354,7 @@ public class GeneratedColumnExample extends CustomComponent { /** * Generates a cell containing value calculated from the item. */ - public Component generateCell(Table source, Object itemId, - Object columnId) { + public Component generateCell(Table source, Object itemId, Object columnId) { Indexed indexedSource = (Indexed) source.getContainerDataSource(); // Can not calculate consumption for the first item. @@ -376,10 +368,8 @@ public class GeneratedColumnExample extends CustomComponent { Object prevItemId = indexedSource.prevItemId(itemId); // Retrieve the POJOs. - FillUp fillup = (FillUp) ((BeanItem) indexedSource.getItem(itemId)) - .getBean(); - FillUp prev = (FillUp) ((BeanItem) source.getItem(prevItemId)) - .getBean(); + FillUp fillup = (FillUp) ((BeanItem) indexedSource.getItem(itemId)).getBean(); + FillUp prev = (FillUp) ((BeanItem) source.getItem(prevItemId)).getBean(); // Do the business logic return generateCell(fillup, prev); @@ -422,10 +412,10 @@ public class GeneratedColumnExample extends CustomComponent { public Field createField(Class type, Component uiContext) { // Let the BaseFieldFactory create the fields Field field = super.createField(type, uiContext); - + // ...and just set them as immediate - ((AbstractField) field).setImmediate(true); - + ((AbstractField)field).setImmediate(true); + return field; } } @@ -436,93 +426,76 @@ public class GeneratedColumnExample extends CustomComponent { // Define table columns. These include also the column for the generated // column, because we want to set the column label to something // different than the property ID. - table - .addContainerProperty("date", Date.class, null, "Date", null, - null); - table.addContainerProperty("quantity", Double.class, null, - "Quantity (l)", null, null); - table.addContainerProperty("price", Double.class, null, "Price (€/l)", - null, null); - table.addContainerProperty("total", Double.class, null, "Total (€)", - null, null); - table.addContainerProperty("consumption", Double.class, null, - "Consumption (l/day)", null, null); - table.addContainerProperty("dailycost", Double.class, null, - "Daily Cost (€/day)", null, null); + table.addContainerProperty("date", Date.class, null, "Date", null, null); + table.addContainerProperty("quantity", Double.class, null, "Quantity (l)", null, null); + table.addContainerProperty("price", Double.class, null, "Price (€/l)", null, null); + table.addContainerProperty("total", Double.class, null, "Total (€)", null, null); + table.addContainerProperty("consumption", Double.class, null, "Consumption (l/day)", null, null); + table.addContainerProperty("dailycost", Double.class, null, "Daily Cost (€/day)", null, null); // Define the generated columns and their generators. - table.addGeneratedColumn("date", new DateColumnGenerator()); - table - .addGeneratedColumn("quantity", new ValueColumnGenerator( - "%.2f l")); - table.addGeneratedColumn("price", new PriceColumnGenerator()); - table.addGeneratedColumn("total", new ValueColumnGenerator("%.2f €")); - table.addGeneratedColumn("consumption", - new ConsumptionColumnGenerator()); - table.addGeneratedColumn("dailycost", new DailyCostColumnGenerator()); + table.addGeneratedColumn("date", new DateColumnGenerator()); + table.addGeneratedColumn("quantity", new ValueColumnGenerator("%.2f l")); + table.addGeneratedColumn("price", new PriceColumnGenerator()); + table.addGeneratedColumn("total", new ValueColumnGenerator("%.2f €")); + table.addGeneratedColumn("consumption", new ConsumptionColumnGenerator()); + table.addGeneratedColumn("dailycost", new DailyCostColumnGenerator()); // Create a data source and bind it to the table. - MySimpleIndexedContainer data = new MySimpleIndexedContainer( - new FillUp()); + MySimpleIndexedContainer data = new MySimpleIndexedContainer(new FillUp()); table.setContainerDataSource(data); // Generated columns are automatically placed after property columns, so // we have to set the order of the columns explicitly. - table.setVisibleColumns(new Object[] { "date", "quantity", "price", - "total", "consumption", "dailycost" }); + table.setVisibleColumns(new Object[] { "date", "quantity", "price", "total", "consumption", "dailycost" }); // Add some data. - data.addItem(new BeanItem(new FillUp(19, 2, 2005, 44.96, 51.21))); - data.addItem(new BeanItem(new FillUp(30, 3, 2005, 44.91, 53.67))); - data.addItem(new BeanItem(new FillUp(20, 4, 2005, 42.96, 49.06))); - data.addItem(new BeanItem(new FillUp(23, 5, 2005, 47.37, 55.28))); - data.addItem(new BeanItem(new FillUp(6, 6, 2005, 35.34, 41.52))); - data.addItem(new BeanItem(new FillUp(30, 6, 2005, 16.07, 20.00))); - data.addItem(new BeanItem(new FillUp(2, 7, 2005, 36.40, 36.19))); - data.addItem(new BeanItem(new FillUp(6, 7, 2005, 39.17, 50.90))); - data.addItem(new BeanItem(new FillUp(27, 7, 2005, 43.43, 53.03))); - data.addItem(new BeanItem(new FillUp(17, 8, 2005, 20, 29.18))); - data.addItem(new BeanItem(new FillUp(30, 8, 2005, 46.06, 59.09))); - data.addItem(new BeanItem(new FillUp(22, 9, 2005, 46.11, 60.36))); + data.addItem(new BeanItem(new FillUp(19, 2, 2005, 44.96, 51.21))); + data.addItem(new BeanItem(new FillUp(30, 3, 2005, 44.91, 53.67))); + data.addItem(new BeanItem(new FillUp(20, 4, 2005, 42.96, 49.06))); + data.addItem(new BeanItem(new FillUp(23, 5, 2005, 47.37, 55.28))); + data.addItem(new BeanItem(new FillUp(6, 6, 2005, 35.34, 41.52))); + data.addItem(new BeanItem(new FillUp(30, 6, 2005, 16.07, 20.00))); + data.addItem(new BeanItem(new FillUp(2, 7, 2005, 36.40, 36.19))); + data.addItem(new BeanItem(new FillUp(6, 7, 2005, 39.17, 50.90))); + data.addItem(new BeanItem(new FillUp(27, 7, 2005, 43.43, 53.03))); + data.addItem(new BeanItem(new FillUp(17, 8, 2005, 20, 29.18))); + data.addItem(new BeanItem(new FillUp(30, 8, 2005, 46.06, 59.09))); + data.addItem(new BeanItem(new FillUp(22, 9, 2005, 46.11, 60.36))); data.addItem(new BeanItem(new FillUp(14, 10, 2005, 41.51, 50.19))); data.addItem(new BeanItem(new FillUp(12, 11, 2005, 35.24, 40.00))); data.addItem(new BeanItem(new FillUp(28, 11, 2005, 45.26, 53.27))); // Have a check box that allows the user to make the quantity // and total columns editable. - final CheckBox editable = new CheckBox( - "Edit the input values - calculated columns are regenerated"); + final CheckBox editable = new CheckBox("Edit the input values - calculated columns are regenerated"); editable.setImmediate(true); editable.addListener(new ClickListener() { public void buttonClick(ClickEvent event) { table.setEditable(editable.booleanValue()); - + // The columns may not be generated when we want to have them // editable. if (editable.booleanValue()) { table.removeGeneratedColumn("quantity"); table.removeGeneratedColumn("total"); } else { - // In non-editable mode we want to show the formatted - // values. - table.addGeneratedColumn("quantity", - new ValueColumnGenerator("%.2f l")); - table.addGeneratedColumn("total", new ValueColumnGenerator( - "%.2f €")); + // In non-editable mode we want to show the formatted values. + table.addGeneratedColumn("quantity", new ValueColumnGenerator("%.2f l")); + table.addGeneratedColumn("total", new ValueColumnGenerator("%.2f €")); } // The visible columns are affected by removal and addition of // generated columns so we have to redefine them. - table.setVisibleColumns(new Object[] { "date", "quantity", - "price", "total", "consumption", "dailycost" }); + table.setVisibleColumns( + new Object[] { "date","quantity","price","total","consumption", "dailycost" }); } }); - + // Use a custom field factory to set the edit fields as immediate. // This is used when the table is in editable mode. table.setFieldFactory(new ImmediateFieldFactory()); - - // Setting the table itself as immediate has no relevance in this - // example, + + // Setting the table itself as immediate has no relevance in this example, // because it is relevant only if the table is selectable and we want to // get the selection changes immediately. table.setImmediate(true); @@ -530,14 +503,11 @@ public class GeneratedColumnExample extends CustomComponent { table.setHeight("100%"); ExpandLayout layout = new ExpandLayout(); - layout - .addComponent(new Label( - "Table with column generators that format and calculate cell values.")); + layout.addComponent(new Label("Table with column generators that format and calculate cell values.")); layout.addComponent(table); layout.addComponent(editable); - layout.addComponent(new Label( - "Columns displayed in blue are calculated from Quantity and Total. " - + "Others are simply formatted.")); + layout.addComponent(new Label("Columns displayed in blue are calculated from Quantity and Total. "+ + "Others are simply formatted.")); layout.expand(table); layout.setSizeFull(); setCompositionRoot(layout);