Example usage for com.google.gwt.user.cellview.client Column setFieldUpdater

List of usage examples for com.google.gwt.user.cellview.client Column setFieldUpdater

Introduction

In this page you can find the example usage for com.google.gwt.user.cellview.client Column setFieldUpdater.

Prototype

public void setFieldUpdater(FieldUpdater<T, C> fieldUpdater) 

Source Link

Document

Set the FieldUpdater used for updating values in the column.

Usage

From source file:com.anritsu.mcrepositorymanager.client.FilterRSSForm.java

public void buildPackageListTable() {
    packageListTable.setPageSize(200);/*from   w  w  w  .  j a v a 2s . c  o  m*/
    TextColumn<McPackage> packageNameColumn = new TextColumn<McPackage>() {
        @Override
        public String getValue(McPackage pack) {
            return pack.getName();
        }
    };

    Column<McPackage, SafeHtml> packageVersionColumn = new Column<McPackage, SafeHtml>(new SafeHtmlCell()) {
        @Override
        public SafeHtml getValue(McPackage object) {
            SafeHtmlBuilder sb = new SafeHtmlBuilder();
            sb.appendHtmlConstant("<a href='" + object.getDownloadLinks().iterator().next() + "'>");
            sb.appendEscaped(object.getPackageVersion());
            sb.appendHtmlConstant("</a>");
            return sb.toSafeHtml();

        }
    };

    TextColumn<McPackage> packageQ7admOutputVersionColumn = new TextColumn<McPackage>() {
        @Override
        public String getValue(McPackage pack) {
            return pack.getQ7admOutputVersion();
        }
    };

    TextColumn<McPackage> packageAvailabilityColumn = new TextColumn<McPackage>() {
        @Override
        public String getValue(McPackage pack) {
            return pack.getAvailability();
        }
    };

    Column<McPackage, String> selectedForDownloadColumn = new Column<McPackage, String>(new ButtonCell()) {

        @Override
        public String getValue(McPackage object) {
            //Notify.notify(object.getName() + " ->" + object.isAddToRepository());
            return (object.isAddToRepository()) ? "remove from repository" : "add to repository";
        }

    };

    selectedForDownloadColumn.setFieldUpdater(new FieldUpdater<McPackage, String>() {

        @Override
        public void update(int index, McPackage object, String value) {
            object.setAddToRepository(!object.isAddToRepository());
            Notify.notify("Package " + object.getName() + " " + value);
            packageListTable.redraw();
        }

    });

    packageListTable.addColumn(packageNameColumn, "Package Name");
    packageListTable.addColumn(packageQ7admOutputVersionColumn, "Installed version");
    packageListTable.addColumn(packageVersionColumn, "Package Version");
    packageListTable.addColumn(packageAvailabilityColumn, "Availability");
    packageListTable.addColumn(selectedForDownloadColumn, "Add to repository");
}

From source file:com.anritsu.mcrepositorymanager.client.ReleaseManagement.java

public void buildPackageListTable(final CellTable table) {
    table.setPageSize(200);/*from w w  w .j  ava2s  . c  o  m*/

    // Attach a column sort handler 
    ListHandler<RecommendedMcPackage> sortHandler = new ListHandler<RecommendedMcPackage>(
            dataProvider.getList());
    table.addColumnSortHandler(sortHandler);

    // Hide from table
    Column<RecommendedMcPackage, String> hidePackage = new Column<RecommendedMcPackage, String>(
            new ButtonCell(ButtonType.INFO, ButtonSize.EXTRA_SMALL)) {
        @Override
        public String getValue(RecommendedMcPackage object) {
            ((ButtonCell) this.getCell()).setEnabled(object.isShowInTable());
            return "Remove";
        }
    };
    hidePackage.setFieldUpdater(new FieldUpdater<RecommendedMcPackage, String>() {
        @Override
        public void update(int index, RecommendedMcPackage object, String value) {
            object.setShowInTable(false);
            table.redraw();
            getService().removeFromTable(object, removeFromTableCallback);
        }
    });

    // Component Tier
    TextColumn<RecommendedMcPackage> packageTier = new TextColumn<RecommendedMcPackage>() {
        @Override
        public String getValue(RecommendedMcPackage pack) {
            return pack.getTier().replaceAll("Anritsu/MasterClaw/", "");
        }
    };

    // Component Group
    TextColumn<RecommendedMcPackage> packageGroup = new TextColumn<RecommendedMcPackage>() {
        @Override
        public String getValue(RecommendedMcPackage pack) {
            return pack.getGroup();
        }
    };

    // Component name
    Column<RecommendedMcPackage, String> packageName = new Column<RecommendedMcPackage, String>(
            new ButtonCell(ButtonType.LINK)) {
        @Override
        public String getValue(RecommendedMcPackage object) {
            return object.getPackageName();
        }
    };
    packageName.setSortable(true);
    packageName.setDefaultSortAscending(true);
    sortHandler.setComparator(packageName, new Comparator<RecommendedMcPackage>() {
        @Override
        public int compare(RecommendedMcPackage t1, RecommendedMcPackage t2) {
            return t1.getPackageName().compareTo(t2.getPackageName());
        }
    });

    // Component version        
    Column<RecommendedMcPackage, String> packageVersion = new Column<RecommendedMcPackage, String>(
            new DynamicSelectionCell(new ArrayList())) {
        @Override
        public String getValue(RecommendedMcPackage object) {
            ((DynamicSelectionCell) this.getCell()).clear();
            String recommendedVersion = "n\\a";
            if (object.getRecommendedVersion() != null
                    && object.getRecommendedVersion().getPackageVersion() != null
                    && object.getMcVersion().equalsIgnoreCase(mcVersion)) {
                recommendedVersion = object.getRecommendedVersion().getPackageVersion();
            }
            ((DynamicSelectionCell) this.getCell()).addOption(recommendedVersion);
            for (McPackage version : object.getPackageVersions()) {
                String v = version.getPackageVersion();
                if (!v.equalsIgnoreCase(recommendedVersion)) {
                    ((DynamicSelectionCell) this.getCell()).addOption(version.getPackageVersion());
                }

            }
            return recommendedVersion;
        }
    };

    packageVersion.setFieldUpdater(new FieldUpdater<RecommendedMcPackage, String>() {
        @Override
        public void update(int index, RecommendedMcPackage object, String value) {
            object.setPackageInfoModified(true);
            for (McPackage p : object.getPackageVersions()) {
                if (p.getPackageVersion().equalsIgnoreCase(value)) {
                    object.setRecommendedVersion(p);
                }
            }
            table.redrawRow(index);
        }
    });

    // Component availability
    TextColumn<RecommendedMcPackage> packageAvailability = new TextColumn<RecommendedMcPackage>() {
        @Override
        public String getValue(RecommendedMcPackage pack) {
            return pack.getAvailability();
        }
    };
    packageAvailability.setSortable(true);
    sortHandler.setComparator(packageAvailability, new Comparator<RecommendedMcPackage>() {
        @Override
        public int compare(RecommendedMcPackage t1, RecommendedMcPackage t2) {
            return t1.getAvailability().compareTo(t2.getAvailability());
        }
    });

    // Component customers
    TextColumn<RecommendedMcPackage> packageCustomers = new TextColumn<RecommendedMcPackage>() {
        @Override
        public String getValue(RecommendedMcPackage pack) {
            if (pack.getRecommendedVersion() != null
                    && !pack.getRecommendedVersion().getCustomerList().contains("All")) {
                return Arrays.toString(pack.getRecommendedVersion().getCustomerList().toArray());
            }
            return "";
        }
    };

    // Component release date
    TextColumn<RecommendedMcPackage> packageReleaseDate = new TextColumn<RecommendedMcPackage>() {
        @Override
        public String getValue(RecommendedMcPackage pack) {
            StringBuilder dateFormated = new StringBuilder();
            if (pack.getRecommendedVersion() != null && pack.getRecommendedVersion().getReleaseDate() != null) {
                String[] releaseDateArray = pack.getRecommendedVersion().getReleaseDate().toString().split(" ");
                dateFormated.append(releaseDateArray[2]).append(".").append(releaseDateArray[1]).append(".")
                        .append(releaseDateArray[5]);
                return dateFormated.toString();
            }
            return "";
        }
    };

    // Component lessRecommended
    Column<RecommendedMcPackage, Boolean> packageLessRecommended = new Column<RecommendedMcPackage, Boolean>(
            new CheckboxCell(true, false)) {
        @Override
        public Boolean getValue(RecommendedMcPackage pack) {
            if (pack.getRecommendedVersion() != null) {
                return pack.getRecommendedVersion().isLessRecommended();
            }
            return false;
        }
    };
    packageLessRecommended.setFieldUpdater(new FieldUpdater<RecommendedMcPackage, Boolean>() {
        @Override
        public void update(int index, RecommendedMcPackage object, Boolean value) {
            object.setPackageInfoModified(true);
            object.getRecommendedVersion().setLessRecommended(value);
            table.redrawRow(index);
        }

    });

    // Component activity
    TextColumn<RecommendedMcPackage> packageActivity = new TextColumn<RecommendedMcPackage>() {
        @Override
        public String getValue(RecommendedMcPackage pack) {
            if (pack.getRecommendedVersion() != null) {
                Set<String> activitiesSet = new HashSet<String>();
                StringBuilder activities = new StringBuilder();
                for (MCPackageActivities pa : pack.getRecommendedVersion().getActivities()) {
                    if (!pa.getActivityType().equalsIgnoreCase("EPR")) {
                        activitiesSet.add(pa.getActivityType() + pa.getActivityId());
                    }
                }
                for (String s : activitiesSet) {
                    activities.append(s + " ");
                }
                return activities.toString();
            }
            return "";
        }
    };

    // Component notes
    Column<RecommendedMcPackage, String> packageNotes = new Column<RecommendedMcPackage, String>(
            new TextAreaInputCell()) {
        @Override
        public String getValue(RecommendedMcPackage pack) {
            try {
                return pack.getRecommendedVersion().getNotes();
            } catch (Exception exp) {
                return "";
            }

        }
    };
    packageNotes.setFieldUpdater(new FieldUpdater<RecommendedMcPackage, String>() {
        @Override
        public void update(int index, RecommendedMcPackage object, String value) {
            object.setPackageInfoModified(true);
            object.getRecommendedVersion().setNotes(value);
            table.redrawRow(index);
        }
    });

    // Update package
    Column<RecommendedMcPackage, String> updatePackage = new Column<RecommendedMcPackage, String>(
            new ButtonCell(ButtonType.INFO)) {
        @Override
        public String getValue(RecommendedMcPackage object) {
            String result = "Update";
            if (!object.isShowInTable()) {
                result = "Add to table";
                if (object.getRecommendedVersion() != null
                        && !object.getRecommendedVersion().getPackageVersion().equalsIgnoreCase("n\\a")) {
                    ((ButtonCell) this.getCell()).setEnabled(true);
                } else {
                    ((ButtonCell) this.getCell()).setEnabled(false);
                }
                return result;
            } else {
                ((ButtonCell) this.getCell()).setEnabled(object.isPackageInfoModified());
                return result;
            }
        }
    };
    updatePackage.setFieldUpdater(new FieldUpdater<RecommendedMcPackage, String>() {
        @Override
        public void update(int index, RecommendedMcPackage object, String value) {
            object.setRowIndex(index);
            object.setPackageInfoModified(false);
            object.setRecommendedVersionCandidate(false);
            object.setShowInTable(true);
            packageListTable.redraw();
            getService().updateRecommendedVersion(object, updateRecommendedVersionCallback);

        }
    });

    table.addColumn(hidePackage, "Remove");
    table.addColumn(packageTier, "Tier");
    table.addColumn(packageGroup, "Group");
    table.addColumn(packageName, "Package Name");
    table.addColumn(packageVersion, "Recommended version");
    table.addColumn(packageAvailability, "Availability");
    table.addColumn(packageCustomers, "Customers");
    table.addColumn(packageReleaseDate, "Release date");
    table.addColumn(packageLessRecommended, "LR");
    table.addColumn(packageActivity, "Activity");
    table.addColumn(packageNotes, "Notes");
    table.addColumn(updatePackage, "Save changes");
}

From source file:com.chinarewards.gwt.license.client.user.view.UserSearchWidget.java

private void initTableColumns(final SelectionModel<UserVo> selectionModel) {
    Column<UserVo, Boolean> checkColumn = new Column<UserVo, Boolean>(new CheckboxCell()) {
        @Override/*from  ww w .ja  va  2s.  c om*/
        public Boolean getValue(UserVo o) {
            return selectionModel.isSelected(o);
        }
    };
    users = new HashMap<String, UserVo>();
    checkColumn.setFieldUpdater(new FieldUpdater<UserVo, Boolean>() {
        @Override
        public void update(int index, UserVo o, Boolean value) {
            if (value) {
                users.put(o.getId(), o);
            } else {
                users.remove(o.getId());
            }
            selectionModel.setSelected(o, value);
        }
    });
    resultTable.addColumn(checkColumn, "");

    resultTable.addColumn(new TextColumn<UserVo>() {

        @Override
        public String getValue(UserVo o) {
            return o.getName();
        }
    }, "???");

    resultTable.addColumn(new TextColumn<UserVo>() {

        @Override
        public String getValue(UserVo o) {
            return o.getEnterpriseName();
        }
    }, "????");

    resultTable.addColumn(new TextColumn<UserVo>() {

        @Override
        public String getValue(UserVo o) {
            return o.getMobile();
        }
    }, "");

    resultTable.addColumn(new TextColumn<UserVo>() {

        @Override
        public String getValue(UserVo o) {
            return o.getEmail();
        }
    }, "");

    resultTable.addColumn(new TextColumn<UserVo>() {

        @Override
        public String getValue(UserVo o) {
            return o.getCreatedAt() != null ? dateFormat.format(o.getCreatedAt()) : "";
        }
    }, "?");

    resultTable.addColumn(new TextColumn<UserVo>() {

        @Override
        public String getValue(UserVo o) {
            return o.getStatus();
        }
    }, "?");
    resultTable.addColumn(new TextColumn<UserVo>() {

        @Override
        public String getValue(UserVo o) {
            return o.getBalance() + "";
        }
    }, "");

    resultTable.addColumn("?", new HyperLinkCell(), new GetValue<UserVo, String>() {
        @Override
        public String getValue(UserVo userVo) {
            return "";
        }
    }, new FieldUpdater<UserVo, String>() {

        @Override
        public void update(int index, UserVo o, String value) {
            users.put(o.getId(), o);
        }

    });
}

From source file:com.chinarewards.gwt.license.client.widget.ListCellTable.java

private <C> Column<T, C> addColumn(final String text, final Cell<C> cell, final GetValue<T, C> getter,
        final FieldUpdater<T, C> fieldUpdater, final Comparator<T> ascComparator,
        final Comparator<T> descComparator, final Sorting<T> ref, final String sortingText) {
    // Create the column
    final Column<T, C> column = new Column<T, C>(cell) {
        @Override//from w w w  .j  a  va2  s .  co  m
        public C getValue(T object) {
            return getter.getValue(object);
        }
    };
    /**
     * ??HeaderHeader
     */
    if (ref != null) {
        final SortableHeader header = new SortableHeader(text);
        allHeaders.add(header);

        // Hook up sorting
        header.setUpdater(new ValueUpdater<String>() {

            @Override
            public void update(String value) {
                header.setSorted(true);
                header.toggleReverseSort();

                for (SortableHeader otherHeader : allHeaders) {
                    if (otherHeader != header) {
                        otherHeader.setSorted(false);
                        otherHeader.setReverseSort(true);
                    }
                }
                // table.refreshHeaders(); // TODO remove this when
                // confirmed working.
                ListCellTable.this.redrawHeaders();

                if (StringUtil.isEmpty(sortingText)) {
                    ref.sortingCurrentPage(header.getReverseSort() ? descComparator : ascComparator);
                } else {
                    ref.sortingAll(sortingText, header.getReverseSort() ? "desc" : "asc");
                }
            }
        });
        this.addColumn(column, header);
    } else {
        TextHeader header = new TextHeader(text);
        int fal = 0;
        for (TextHeader otherHeader : allTextHeaders) {
            if (otherHeader.getValue() == header.getValue()) {
                header = otherHeader;
                fal = 1;
            }
        }
        if (fal != 1)
            allTextHeaders.add(header);
        this.addColumn(column, header);
    }
    if (fieldUpdater != null) {
        column.setFieldUpdater(fieldUpdater);
    }
    return column;
}

From source file:com.chinarewards.gwt.license.client.widget.ListImageTable.java

private <C> Column<T, C> addColumn(final String text, final Cell<C> cell, final GetValue<T, C> getter,
        final FieldUpdater<T, C> fieldUpdater, final Comparator<T> ascComparator,
        final Comparator<T> descComparator, final Sorting<T> ref, final String sortingText) {
    // Create the column
    final Column<T, C> column = new Column<T, C>(cell) {
        @Override/*  w ww.  ja  v  a2s  . co  m*/
        public C getValue(T object) {
            return getter.getValue(object);
        }
    };
    /**
     * ??HeaderHeader
     */
    if (ref != null) {
        final SortableHeader header = new SortableHeader(text);
        allHeaders.add(header);

        // Hook up sorting
        header.setUpdater(new ValueUpdater<String>() {

            @Override
            public void update(String value) {
                header.setSorted(true);
                header.toggleReverseSort();

                for (SortableHeader otherHeader : allHeaders) {
                    if (otherHeader != header) {
                        otherHeader.setSorted(false);
                        otherHeader.setReverseSort(true);
                    }
                }
                // table.refreshHeaders(); // TODO remove this when
                // confirmed working.
                ListImageTable.this.redrawHeaders();

                if (StringUtil.isEmpty(sortingText)) {
                    ref.sortingCurrentPage(header.getReverseSort() ? descComparator : ascComparator);
                } else {
                    ref.sortingAll(sortingText, header.getReverseSort() ? "desc" : "asc");
                }
            }
        });
        this.addColumn(column, header);
    } else {
        final TextHeader header = new TextHeader(text);
        this.addColumn(column, header);
    }
    if (fieldUpdater != null) {
        column.setFieldUpdater(fieldUpdater);
    }
    return column;
}

From source file:com.gafactory.core.client.ui.grids.BaseListGrid.java

License:Open Source License

protected Column<T, String> addTextEditColumn(FieldUpdater<T, String> updater,
        final Function<T, String> valueGetter, String header, int width, boolean sortable, String dataStore) {
    Column<T, String> column = new Column<T, String>(new EditTextCell()) {

        @Override/*w w  w .j a v  a2  s  .co  m*/
        public String getValue(T object) {
            return valueGetter.apply(object);
        }
    };
    column.setFieldUpdater(updater);

    dataGrid.addColumn(column, header);
    if (width != 0) {
        dataGrid.setColumnWidth(column, width, Style.Unit.PX);
    }
    column.setSortable(sortable);
    column.setDataStoreName(dataStore);

    return column;

}

From source file:com.github.gwtbootstrap.showcase.client.CellTables.java

License:Apache License

private void initTable(AbstractCellTable<Person> exampleTable, final SimplePager pager,
        final Pagination pagination) {
    exampleTable.setEmptyTableWidget(new Label("Please add data."));

    TextColumn<Person> idCol = new TextColumn<Person>() {

        @Override//from   w  w w  .j  a va2s.  c om
        public String getValue(Person object) {
            return String.valueOf(object.getId());
        }
    };

    idCol.setSortable(true);

    exampleTable.addColumn(idCol, "#");

    ListHandler<Person> idColHandler = new ListHandler<Person>(dataProvider.getList());

    idColHandler.setComparator(idCol, new Comparator<Person>() {

        @Override
        public int compare(Person o1, Person o2) {
            return o1.getId().compareTo(o2.getId());
        }
    });

    exampleTable.addColumnSortHandler(idColHandler);

    exampleTable.getColumnSortList().push(idCol);

    TextColumn<Person> userNameCol = new TextColumn<Person>() {

        @Override
        public String getValue(Person object) {
            return object.getUserName();
        }
    };
    userNameCol.setSortable(true);
    exampleTable.addColumn(userNameCol, "User Name");

    ListHandler<Person> userNameColHandler = new ListHandler<Person>(dataProvider.getList());

    userNameColHandler.setComparator(userNameCol, new Comparator<Person>() {

        @Override
        public int compare(Person o1, Person o2) {
            return o1.getUserName().compareTo(o2.getUserName());
        }
    });

    exampleTable.addColumnSortHandler(userNameColHandler);

    TextColumn<Person> ageCol = new TextColumn<Person>() {

        @Override
        public String getValue(Person object) {
            return object.getAge() != null ? String.valueOf(object.getAge()) : "";
        }
    };
    ageCol.setSortable(true);
    exampleTable.addColumn(ageCol, "Age");

    ListHandler<Person> ageColHandler = new ListHandler<Person>(dataProvider.getList());

    ageColHandler.setComparator(ageCol, new Comparator<Person>() {

        @Override
        public int compare(Person o1, Person o2) {
            if (o2.getAge() == null) {
                return 1;
            }

            if (o1.getAge() == null) {
                return -1;
            }
            return o1.getAge().compareTo(o2.getAge());
        }
    });

    exampleTable.addColumnSortHandler(ageColHandler);

    TextColumn<Person> birthDayCol = new TextColumn<Person>() {

        @Override
        public String getValue(Person object) {
            if (object.getBirthDay() != null) {
                return DateTimeFormat.getFormat("dd/MM/yyyy").format(object.getBirthDay());
            } else {
                return "";
            }
        }
    };

    exampleTable.addColumn(birthDayCol, "Birth Day");

    birthDayCol.setSortable(true);

    ListHandler<Person> birthDayColHandler = new ListHandler<Person>(dataProvider.getList());
    birthDayColHandler.setComparator(birthDayCol, new Comparator<Person>() {

        @Override
        public int compare(Person o1, Person o2) {
            if (o2.getBirthDay() == null) {
                return 1;
            }

            if (o1.getBirthDay() == null) {
                return -1;
            }

            return o1.getBirthDay().compareTo(o2.getBirthDay());
        }
    });

    exampleTable.addColumnSortHandler(birthDayColHandler);

    TextColumn<Person> favoriteCol = new TextColumn<Person>() {

        @Override
        public String getValue(Person object) {
            return object.getFavorite().getDisplayLabel();
        }
    };

    favoriteCol.setSortable(true);
    exampleTable.addColumn(favoriteCol, "Favorite");

    ListHandler<Person> favoriteColHandler = new ListHandler<Person>(dataProvider.getList());

    favoriteColHandler.setComparator(favoriteCol, new Comparator<Person>() {

        @Override
        public int compare(Person o1, Person o2) {
            return o1.getFavorite().compareTo(o2.getFavorite());
        }
    });

    exampleTable.addColumnSortHandler(favoriteColHandler);

    exampleTable.addRangeChangeHandler(new RangeChangeEvent.Handler() {

        @Override
        public void onRangeChange(RangeChangeEvent event) {
            rebuildPager(pagination, pager);
        }
    });

    ButtonCell buttonCell = new ButtonCell(IconType.REMOVE, ButtonType.DANGER);

    final TooltipCellDecorator<String> decorator = new TooltipCellDecorator<String>(buttonCell);
    decorator.setText("delete row, if click");

    Column<Person, String> buttonCol = new Column<Person, String>(decorator) {

        @Override
        public String getValue(Person object) {
            return "delete";
        }
    };

    buttonCol.setFieldUpdater(new FieldUpdater<Person, String>() {

        @Override
        public void update(int index, Person object, String value) {
            dataProvider.getList().remove(object);
            dataProvider.flush();
            dataProvider.refresh();
            rebuildPager(pagination, pager);
            rebuildPager(dataGridPagination, dataGridPager);

        }
    });

    exampleTable.addColumn(buttonCol);

    final SingleSelectionModel<Person> selectionModel = new SingleSelectionModel<Person>();

    selectionModel.addSelectionChangeHandler(new Handler() {

        @Override
        public void onSelectionChange(SelectionChangeEvent event) {
            Person person = selectionModel.getSelectedObject();

            CellTables.this.driver.edit(person);
        }
    });

    exampleTable.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.BOUND_TO_SELECTION);

    exampleTable.setSelectionModel(selectionModel);

    pager.setDisplay(exampleTable);

    pagination.clear();

    dataProvider.addDataDisplay(exampleTable);
}

From source file:com.google.gwt.examples.cellview.CellTableFieldUpdaterExample.java

License:Apache License

@Override
public void onModuleLoad() {
    // Create a CellTable with a key provider.
    final CellTable<Contact> table = new CellTable<Contact>(KEY_PROVIDER);

    // Add a text input column to edit the name.
    final TextInputCell nameCell = new TextInputCell();
    Column<Contact, String> nameColumn = new Column<Contact, String>(nameCell) {
        @Override//from   w ww.ja v a 2 s  .co m
        public String getValue(Contact object) {
            // Return the name as the value of this column.
            return object.name;
        }
    };
    table.addColumn(nameColumn, "Name");

    // Add a field updater to be notified when the user enters a new name.
    nameColumn.setFieldUpdater(new FieldUpdater<Contact, String>() {
        @Override
        public void update(int index, Contact object, String value) {
            // Inform the user of the change.
            Window.alert("You changed the name of " + object.name + " to " + value);

            // Push the changes into the Contact. At this point, you could send an
            // asynchronous request to the server to update the database.
            object.name = value;

            // Redraw the table with the new data.
            table.redraw();
        }
    });

    // Push the data into the widget.
    table.setRowData(CONTACTS);

    // Add it to the root panel.
    RootPanel.get().add(table);
}

From source file:com.google.gwt.examples.cellview.CellTableFieldUpdaterExampleComplex.java

License:Apache License

@Override
public void onModuleLoad() {
    // Create a CellTable with a key provider.
    final CellTable<Contact> table = new CellTable<Contact>(KEY_PROVIDER);
    table.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);

    // Add a text input column to edit the name.
    final TextInputCell nameCell = new TextInputCell();
    Column<Contact, String> nameColumn = new Column<Contact, String>(nameCell) {
        @Override//  w  ww.  j a v  a  2  s .  co  m
        public String getValue(Contact object) {
            return object.name;
        }
    };
    table.addColumn(nameColumn, "Name");

    // Add a field updater to be notified when the user enters a new name.
    nameColumn.setFieldUpdater(new FieldUpdater<Contact, String>() {
        @Override
        public void update(int index, Contact object, String value) {
            // Validate the data.
            if (value.length() < 3) {
                Window.alert("Names must be at least three characters long.");

                /*
                 * Clear the view data. The view data contains the pending change and
                 * allows the table to render with the pending value until the data is
                 * committed. If the data is committed into the object, the view data
                 * is automatically cleared out. If the data is not committed because
                 * it is invalid, you must delete.
                 */
                nameCell.clearViewData(KEY_PROVIDER.getKey(object));

                // Redraw the table.
                table.redraw();
                return;
            }

            // Inform the user of the change.
            Window.alert("You changed the name of " + object.name + " to " + value);

            // Push the changes into the Contact. At this point, you could send an
            // asynchronous request to the server to update the database.
            object.name = value;

            // Redraw the table with the new data.
            table.redraw();
        }
    });

    // Add a date column to show the birthday.
    Column<Contact, Date> dateColumn = new Column<Contact, Date>(new DatePickerCell()) {
        @Override
        public Date getValue(Contact object) {
            return object.birthday;
        }
    };
    table.addColumn(dateColumn, "Birthday");

    // Add a field updater to be notified when the user enters a new birthday.
    dateColumn.setFieldUpdater(new FieldUpdater<Contact, Date>() {
        @Override
        public void update(int index, Contact object, Date value) {
            Window.alert("You changed the birthday of " + object.name + " to "
                    + DateTimeFormat.getFormat(PredefinedFormat.DATE_LONG).format(value));

            // Push the changes into the Contact.
            object.birthday = value;

            // Redraw the table with the new data.
            table.redraw();
        }
    });
    // Add a text column to show the address.
    TextColumn<Contact> addressColumn = new TextColumn<Contact>() {
        @Override
        public String getValue(Contact object) {
            return object.address;
        }
    };
    table.addColumn(addressColumn, "Address");

    // Set the total row count. This isn't strictly necessary, but it affects
    // paging calculations, so its good habit to keep the row count up to date.
    table.setRowCount(CONTACTS.size(), true);

    // Push the data into the widget.
    table.setRowData(0, CONTACTS);

    // Add it to the root panel.
    RootPanel.get().add(table);
}

From source file:com.google.gwt.sample.expenses.client.ExpenseDetails.java

License:Apache License

private CellTable<ExpenseProxy> initTable() {
    CellTable.Resources resources = GWT.create(TableResources.class);
    table = new CellTable<ExpenseProxy>(100, resources, Expenses.EXPENSE_RECORD_KEY_PROVIDER);
    Styles.Common common = Styles.common();

    table.addColumnStyleName(0, common.spacerColumn());
    table.addColumnStyleName(1, common.expenseDetailsDateColumn());
    table.addColumnStyleName(3, common.expenseDetailsCategoryColumn());
    table.addColumnStyleName(4, common.expenseDetailsAmountColumn());
    table.addColumnStyleName(5, common.expenseDetailsApprovalColumn());
    table.addColumnStyleName(6, common.spacerColumn());

    // Spacer column.
    table.addColumn(new SpacerColumn<ExpenseProxy>());

    // Created column.
    GetValue<ExpenseProxy, Date> createdGetter = new GetValue<ExpenseProxy, Date>() {
        public Date getValue(ExpenseProxy object) {
            return object.getCreated();
        }/*from   ww  w. ja v  a 2 s .c o  m*/
    };
    defaultComparator = createColumnComparator(createdGetter, false);
    Comparator<ExpenseProxy> createdDesc = createColumnComparator(createdGetter, true);
    addColumn(table, "Created", new DateCell(DateTimeFormat.getFormat("MMM dd yyyy")), createdGetter,
            defaultComparator, createdDesc);
    lastComparator = defaultComparator;

    // Description column.
    addColumn(table, "Description", new TextCell(), new GetValue<ExpenseProxy, String>() {
        public String getValue(ExpenseProxy object) {
            return object.getDescription();
        }
    });

    // Category column.
    addColumn(table, "Category", new TextCell(), new GetValue<ExpenseProxy, String>() {
        public String getValue(ExpenseProxy object) {
            return object.getCategory();
        }
    });

    // Amount column.
    final GetValue<ExpenseProxy, Double> amountGetter = new GetValue<ExpenseProxy, Double>() {
        public Double getValue(ExpenseProxy object) {
            return object.getAmount();
        }
    };
    Comparator<ExpenseProxy> amountAsc = createColumnComparator(amountGetter, false);
    Comparator<ExpenseProxy> amountDesc = createColumnComparator(amountGetter, true);
    addColumn(table, "Amount", new NumberCell(NumberFormat.getCurrencyFormat()),
            new GetValue<ExpenseProxy, Number>() {
                public Number getValue(ExpenseProxy object) {
                    return amountGetter.getValue(object);
                }
            }, amountAsc, amountDesc);

    // Dialog box to obtain a reason for a denial
    denialPopup.addCloseHandler(new CloseHandler<PopupPanel>() {
        public void onClose(CloseEvent<PopupPanel> event) {
            String reasonDenied = denialPopup.getReasonDenied();
            ExpenseProxy record = denialPopup.getExpenseRecord();
            if (reasonDenied == null || reasonDenied.length() == 0) {
                // Clear the view data.
                final Object key = items.getKey(record);
                approvalCell.clearViewData(key);

                // We need to redraw the table to reset the select box.
                syncCommit(record, null);
            } else {
                updateExpenseRecord(record, "Denied", reasonDenied);
            }

            // Return focus to the table.
            table.setFocus(true);
        }
    });

    // Approval column.
    approvalCell = new ApprovalCell();
    Column<ExpenseProxy, String> approvalColumn = addColumn(table, "Approval Status", approvalCell,
            new GetValue<ExpenseProxy, String>() {
                public String getValue(ExpenseProxy object) {
                    return object.getApproval();
                }
            });
    approvalColumn.setFieldUpdater(new FieldUpdater<ExpenseProxy, String>() {
        public void update(int index, final ExpenseProxy object, String value) {
            if ("Denied".equals(value)) {
                denialPopup.setExpenseRecord(object);
                denialPopup.setReasonDenied(object.getReasonDenied());
                denialPopup.popup();
            } else {
                updateExpenseRecord(object, value, "");
            }
        }
    });

    // Spacer column.
    table.addColumn(new SpacerColumn<ExpenseProxy>());

    return table;
}