Java tutorial
/* * JBoss, Home of Professional Open Source. * Copyright 2010, Red Hat, Inc., and individual contributors * as indicated by the @author tags. See the copyright.txt file in the * distribution for a full listing of individual contributors. * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this software; if not, write to the Free * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA, or see the FSF site: http://www.fsf.org. */ package org.jboss.as.console.client.shared.subsys.elytron.ui; import java.util.ArrayList; import java.util.Collections; import java.util.List; import com.google.gwt.dom.client.Style; import com.google.gwt.user.cellview.client.Column; import com.google.gwt.user.cellview.client.ColumnSortEvent; import com.google.gwt.user.cellview.client.TextColumn; import com.google.gwt.user.client.ui.HasHorizontalAlignment; import com.google.gwt.user.client.ui.IsWidget; import com.google.gwt.user.client.ui.VerticalPanel; import com.google.gwt.user.client.ui.Widget; import com.google.gwt.view.client.ListDataProvider; import com.google.gwt.view.client.ProvidesKey; import com.google.gwt.view.client.SingleSelectionModel; import org.jboss.ballroom.client.widgets.tables.DefaultCellTable; import org.jboss.ballroom.client.widgets.tables.DefaultPager; import org.jboss.dmr.client.ModelNode; /** * @author Claudio Miranda <claudio@redhat.com> */ public class NewIdentityAttributesView implements IsWidget { private final ProvidesKey<ModelNode> nameProvider; private DefaultCellTable<ModelNode> table; private ListDataProvider<ModelNode> dataProvider; private final SingleSelectionModel<ModelNode> selectionModel; NewIdentityAttributesView() { this.nameProvider = modelNode -> modelNode.get("name"); selectionModel = new SingleSelectionModel<>(nameProvider); } @SuppressWarnings("unchecked") public Widget asWidget() { VerticalPanel panel = new VerticalPanel(); panel.addStyleName("fill-layout-width"); // table table = new DefaultCellTable<>(5, nameProvider); dataProvider = new ListDataProvider<>(nameProvider); dataProvider.addDataDisplay(table); table.setSelectionModel(selectionModel); // columns Column<ModelNode, String> nameColumn = new TextColumn<ModelNode>() { @Override public String getValue(ModelNode node) { return node.get("name").asString(); } }; nameColumn.setSortable(true); Column<ModelNode, String> valueColumn = new TextColumn<ModelNode>() { @Override public String getValue(ModelNode node) { return node.get("value").asString(); } }; valueColumn.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER); ColumnSortEvent.ListHandler<ModelNode> sortHandler = new ColumnSortEvent.ListHandler<>( dataProvider.getList()); sortHandler.setComparator(nameColumn, (o1, o2) -> o1.get("name").asString().toLowerCase() .compareTo(o2.get("name").asString().toLowerCase())); table.addColumn(nameColumn, "From"); table.addColumn(valueColumn, "To"); table.setColumnWidth(nameColumn, 30, Style.Unit.PCT); table.setColumnWidth(valueColumn, 70, Style.Unit.PCT); table.addColumnSortHandler(sortHandler); table.getColumnSortList().push(nameColumn); panel.add(table); DefaultPager pager = new DefaultPager(); pager.setDisplay(table); panel.add(pager); return panel; } public void update(List<ModelNode> models) { table.setRowCount(models.size(), true); Collections.sort(models, (o1, o2) -> o1.get("name").asString().toLowerCase() .compareTo(o2.get("name").asString().toLowerCase())); List<ModelNode> dataList = dataProvider.getList(); dataList.clear(); // cannot call setList() as that breaks the sort handler dataList.addAll(models); // Make sure the new values are properly sorted ColumnSortEvent.fire(table, table.getColumnSortList()); } public void clearValues() { dataProvider.setList(new ArrayList<>()); } }