/******************************************************************************* * Copyright (c) 2013, 2015 Dirk Fauth and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Dirk Fauth <dirk.fauth@googlemail.com> - initial API and implementation *******************************************************************************/ package org.eclipse.nebula.widgets.nattable.examples._600_GlazedLists; import java.io.Serializable; import java.util.HashMap; import java.util.Map; import org.eclipse.nebula.widgets.nattable.NatTable; import org.eclipse.nebula.widgets.nattable.config.DefaultNatTableStyleConfiguration; import org.eclipse.nebula.widgets.nattable.data.IDataProvider; import org.eclipse.nebula.widgets.nattable.data.IRowDataProvider; import org.eclipse.nebula.widgets.nattable.data.IRowIdAccessor; import org.eclipse.nebula.widgets.nattable.data.ListDataProvider; import org.eclipse.nebula.widgets.nattable.data.ReflectiveColumnPropertyAccessor; import org.eclipse.nebula.widgets.nattable.dataset.person.Person; import org.eclipse.nebula.widgets.nattable.dataset.person.PersonService; import org.eclipse.nebula.widgets.nattable.examples.AbstractNatExample; import org.eclipse.nebula.widgets.nattable.examples.runner.StandaloneNatExampleRunner; import org.eclipse.nebula.widgets.nattable.extension.glazedlists.DetailGlazedListsEventLayer; import org.eclipse.nebula.widgets.nattable.extension.glazedlists.hideshow.GlazedListsRowHideShowLayer; import org.eclipse.nebula.widgets.nattable.grid.data.DefaultColumnHeaderDataProvider; import org.eclipse.nebula.widgets.nattable.grid.data.DefaultCornerDataProvider; import org.eclipse.nebula.widgets.nattable.grid.data.DefaultRowHeaderDataProvider; import org.eclipse.nebula.widgets.nattable.grid.layer.ColumnHeaderLayer; import org.eclipse.nebula.widgets.nattable.grid.layer.CornerLayer; import org.eclipse.nebula.widgets.nattable.grid.layer.DefaultColumnHeaderDataLayer; import org.eclipse.nebula.widgets.nattable.grid.layer.DefaultRowHeaderDataLayer; import org.eclipse.nebula.widgets.nattable.grid.layer.GridLayer; import org.eclipse.nebula.widgets.nattable.grid.layer.RowHeaderLayer; import org.eclipse.nebula.widgets.nattable.layer.DataLayer; import org.eclipse.nebula.widgets.nattable.layer.ILayer; import org.eclipse.nebula.widgets.nattable.persistence.command.DisplayPersistenceDialogCommandHandler; import org.eclipse.nebula.widgets.nattable.selection.SelectionLayer; import org.eclipse.nebula.widgets.nattable.ui.menu.AbstractHeaderMenuConfiguration; import org.eclipse.nebula.widgets.nattable.ui.menu.PopupMenuBuilder; import org.eclipse.nebula.widgets.nattable.viewport.ViewportLayer; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; import ca.odell.glazedlists.EventList; import ca.odell.glazedlists.FilterList; import ca.odell.glazedlists.GlazedLists; /** * Simple example showing how to add the row hide/show functionality to a grid * that is build using GlazedLists and how to add the corresponding actions to * the row header menu. */ public class _606_GlazedListsRowHideShowExample extends AbstractNatExample { public static void main(String[] args) throws Exception { StandaloneNatExampleRunner.run(new _606_GlazedListsRowHideShowExample()); } @Override public String getDescription() { return "This example shows the usage of the row hide/show functionality within a grid and " + "its corresponding actions in the row header menu using the GlazedLists extension. " + "If you perform a right click on the row header, you are able to hide the current selected " + "row or show all rows again."; } @Override public Control createExampleControl(Composite parent) { // property names of the Person class String[] propertyNames = { "firstName", "lastName", "gender", "married", "birthday" }; // mapping from property to label, needed for column header labels Map<String, String> propertyToLabelMap = new HashMap<>(); propertyToLabelMap.put("firstName", "Firstname"); propertyToLabelMap.put("lastName", "Lastname"); propertyToLabelMap.put("gender", "Gender"); propertyToLabelMap.put("married", "Married"); propertyToLabelMap.put("birthday", "Birthday"); // build the body layer stack // Usually you would create a new layer stack by extending // AbstractIndexLayerTransform and setting the ViewportLayer as // underlying layer. But in this case using the ViewportLayer directly // as body layer is also working. // first wrap the base list in a GlazedLists EventList and a FilterList // so it is possible to filter EventList<Person> eventList = GlazedLists.eventList(PersonService.getPersons(10)); FilterList<Person> filterList = new FilterList<>(eventList); // use the GlazedListsDataProvider for some performance tweaks final IRowDataProvider<Person> bodyDataProvider = new ListDataProvider<>(filterList, new ReflectiveColumnPropertyAccessor<Person>(propertyNames)); // create the IRowIdAccessor that is necessary for row hide/show final IRowIdAccessor<Person> rowIdAccessor = new IRowIdAccessor<Person>() { @Override public Serializable getRowId(Person rowObject) { return rowObject.getId(); } }; DataLayer bodyDataLayer = new DataLayer(bodyDataProvider); // add a DetailGlazedListsEventLayer event layer that is responsible for // updating the grid on list changes DetailGlazedListsEventLayer<Person> glazedListsEventLayer = new DetailGlazedListsEventLayer<>(bodyDataLayer, filterList); GlazedListsRowHideShowLayer<Person> rowHideShowLayer = new GlazedListsRowHideShowLayer<>(glazedListsEventLayer, bodyDataProvider, rowIdAccessor, filterList); SelectionLayer selectionLayer = new SelectionLayer(rowHideShowLayer); ViewportLayer viewportLayer = new ViewportLayer(selectionLayer); // build the column header layer IDataProvider columnHeaderDataProvider = new DefaultColumnHeaderDataProvider(propertyNames, propertyToLabelMap); DataLayer columnHeaderDataLayer = new DefaultColumnHeaderDataLayer(columnHeaderDataProvider); ILayer columnHeaderLayer = new ColumnHeaderLayer(columnHeaderDataLayer, viewportLayer, selectionLayer); // build the row header layer IDataProvider rowHeaderDataProvider = new DefaultRowHeaderDataProvider(bodyDataProvider); DataLayer rowHeaderDataLayer = new DefaultRowHeaderDataLayer(rowHeaderDataProvider); ILayer rowHeaderLayer = new RowHeaderLayer(rowHeaderDataLayer, viewportLayer, selectionLayer); // build the corner layer IDataProvider cornerDataProvider = new DefaultCornerDataProvider(columnHeaderDataProvider, rowHeaderDataProvider); DataLayer cornerDataLayer = new DataLayer(cornerDataProvider); ILayer cornerLayer = new CornerLayer(cornerDataLayer, rowHeaderLayer, columnHeaderLayer); // build the grid layer GridLayer gridLayer = new GridLayer(viewportLayer, columnHeaderLayer, rowHeaderLayer, cornerLayer); // turn the auto configuration off as we want to add our header menu // configuration NatTable natTable = new NatTable(parent, gridLayer, false); // as the autoconfiguration of the NatTable is turned off, we have to // add the DefaultNatTableStyleConfiguration manually natTable.addConfiguration(new DefaultNatTableStyleConfiguration()); // add the header menu configuration for adding the column header menu // with hide/show actions natTable.addConfiguration(new AbstractHeaderMenuConfiguration(natTable) { @Override protected PopupMenuBuilder createRowHeaderMenu(NatTable natTable) { return new PopupMenuBuilder(natTable) .withHideRowMenuItem() .withShowAllRowsMenuItem(); } @Override protected PopupMenuBuilder createCornerMenu(NatTable natTable) { return super.createCornerMenu(natTable) .withShowAllRowsMenuItem() .withStateManagerMenuItemProvider(); } }); natTable.configure(); natTable.registerCommandHandler(new DisplayPersistenceDialogCommandHandler(natTable)); return natTable; } }