de.swm.gwt.client.AbstractList Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swm-gwt-client Show documentation
Show all versions of swm-gwt-client Show documentation
Plain GWT Swm erweiterungen, auch zur benutzung in mobilen Geraeten
The newest version!
package de.swm.gwt.client;
import com.google.gwt.cell.client.DateCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.*;
import de.swm.gwt.client.authorization.IAccessRight;
import de.swm.gwt.client.authorization.IRightsDependentUIUpdater;
import de.swm.gwt.client.enums.I18NEnumRenderer;
import de.swm.gwt.client.interfaces.ILocation;
import de.swm.gwt.client.interfaces.IModelData;
import java.util.Date;
/**
* Abstrakte Basisklasse fuer Listen.
*
* @param Klasse der DTOs.
* @author Steiner.Christian
* Marko Krajnc
* copyright (C) 2014 SWM Services GmbH
*/
public abstract class AbstractList extends Composite implements IList {
/**
* Diese Klasse ermöglich verwendung von unterklassen von DataGrid.
*
* @author Marko Krajnc
copyright (C) 2010, SWM Services GmbH
*/
public interface DataGridFactory {
/**
* Create new instance of Datagrid object.
*
* @param pageSize the page size
* @param resources the resources to use for this widget
* @param keyProvider an instance of ProvidesKey, or null if the record
* object should act as its own key
* @param the data type of each row
* @return New Datagrid instance.
*/
public DataGrid newDataGrid(int pageSize,
com.google.gwt.user.cellview.client.DataGrid.Resources resources,
com.google.gwt.view.client.ProvidesKey keyProvider);
}
private static final int PAGE_SIZE = 10;
private static final String NO_DATA_AVAILABLE = "no data available.";
private final AsyncDataProvider dataStore;
private final String i18ValueForEmptyTable;
private final IRightsDependentUIUpdater rightsDependentUIUpdater;
private final DataGridFactory dataGridFactory;
private SingleSelectionModel selectionModel;
private IListPresenter presenter;
private DataGrid dataGrid;
private SimplePager pager;
/**
* Constructor for the AbstractList.
*
* @param dataStore provides the data shown in the dataGrid.
*/
public AbstractList(AsyncDataProvider dataStore) {
this(dataStore, null, NO_DATA_AVAILABLE);
}
/**
* Constructor for the AbstractList.
*
* @param dataStore provides the data shown in the dataGrid.
* @param rightsDependentUiUpdater updates the ui components dependent on the user's access rights.
*/
public AbstractList(AsyncDataProvider dataStore, IRightsDependentUIUpdater rightsDependentUiUpdater) {
this(dataStore, rightsDependentUiUpdater, NO_DATA_AVAILABLE);
}
/**
* Constructor for the AbstractList.
*
* @param dataStore provides the data shown in the dataGrid.
* @param rightsDependentUiUpdater updates the ui components dependent on the user's access rights.
* @param i18ValueForEmptyTable text wenn die Tabelle leer ist
*/
public AbstractList(AsyncDataProvider dataStore, IRightsDependentUIUpdater rightsDependentUiUpdater,
String i18ValueForEmptyTable) {
this(dataStore, rightsDependentUiUpdater, i18ValueForEmptyTable, null);
}
/**
* Constructor for the AbstractList.
*
* @param dataStore provides the data shown in the dataGrid.
* @param rightsDependentUiUpdater updates the ui components dependent on the user's access rights.
* @param i18ValueForEmptyTable text wenn die Tabelle leer ist
* @param dataGridFactory factory for DataGrid object.
*/
public AbstractList(AsyncDataProvider dataStore, IRightsDependentUIUpdater rightsDependentUiUpdater,
String i18ValueForEmptyTable, DataGridFactory dataGridFactory) {
this.rightsDependentUIUpdater = rightsDependentUiUpdater;
this.dataStore = dataStore;
this.i18ValueForEmptyTable = i18ValueForEmptyTable;
this.dataGridFactory = dataGridFactory;
}
/**
* Initialisiert den List controller mit Plain GWT Data Grid. Bei nicht Plain-GWT implementierungen
* sollte die init method enicht aufgerufen werden.
*
* @return initialisiert ein plain GWT Data grid.
*/
public InitializationResult init() {
initDataGrid();
return new InitializationResult() {
@Override
public DataGrid grid() {
return dataGrid;
}
@Override
public SimplePager pager() {
return pager;
}
};
}
/**
* initializes the datagrid columns.
*
* @param dataGrid the grid.
*/
protected abstract void initTableColumns(DataGrid dataGrid);
/**
* Creates the widget shown when the data grid is empty. override to show specific messages.
*
* @return the widget.
*/
protected Label createEmptyTableWidget() {
return new Label(i18ValueForEmptyTable);
}
/**
* setter. *
*
* @param presenter the presenter for the list.
*/
public void setPresenter(IListPresenter presenter) {
this.presenter = presenter;
}
/**
* Initializes the DataGrid. Must be called in the constructor, before uiBinder.createAndBindUi(Widget).
*/
protected void initDataGrid() {
DataGrid.Resources dataGridResources = (SWMGwt.getTheme() != null) ? SWMGwt.getTheme().getCssBundle() : null;
if (dataGridFactory != null) {
dataGrid = dataGridFactory.newDataGrid(getPageSize(), dataGridResources, dataStore.getKeyProvider());
} else {
if (dataGridResources != null) {
dataGrid = new DataGrid(getPageSize(), dataGridResources, dataStore.getKeyProvider());
} else {
dataGrid = new DataGrid(getPageSize(), dataStore.getKeyProvider());
}
if (SWMGwt.getTheme() != null) {
dataGrid.addStyleName(SWMGwt.getTheme().getCssBundle().getGridCss().defaultDataGrid());
}
}
selectionModel = new SingleSelectionModel(dataStore.getKeyProvider());
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
@Override
public void onSelectionChange(SelectionChangeEvent event) {
D selectedObject = selectionModel.getSelectedObject();
onRowSelected(selectedObject);
}
});
dataGrid.setSelectionModel(selectionModel);
dataGrid.setEmptyTableWidget(createEmptyTableWidget());
ColumnSortEvent.AsyncHandler sortHandler = new ColumnSortEvent.AsyncHandler(dataGrid);
dataGrid.addColumnSortHandler(sortHandler);
SimplePager.Resources pagerResources = GWT.create(SimplePager.Resources.class);
pager = new SimplePager(SimplePager.TextLocation.CENTER, pagerResources, false, 0, true);
if (SWMGwt.getTheme() != null) {
pager.addStyleName(SWMGwt.getTheme().getCssBundle().getGridCss().defaultSimplePager());
}
pager.setDisplay(dataGrid);
initTableColumns(dataGrid);
}
/**
* registers access rights to UIObjects.
*
* Registrierte UI-Objekte werden entsprechend den Ausfuehrungsrechten aktiviert oder deaktiviert.
*
* @param uiObject das UI-Objekt
* @param executionRight die Ausfuehrungsrechte.
*/
protected void registerAccessRight(UIObject uiObject, IAccessRight executionRight) {
if (this.rightsDependentUIUpdater != null) {
rightsDependentUIUpdater.register(uiObject, executionRight);
}
}
/**
* Returns the default page size used in this grid.
*
* @return the page size
*/
protected int getPageSize() {
return PAGE_SIZE;
}
/**
* Rendert das Formular in die entsprechende Location.
*
* @param location die Location, in die das Formular gerendert werden soll.
*/
public void render(ILocation location) {
this.getWidget().setVisible(true);
location.add(this.getWidget());
location.render();
}
/**
* Entfernt die Liste aus der Location.
*
* @param location Location, aus der die Liste entfernt werden soll
*/
public void remove(ILocation location) {
location.remove(this.getWidget());
}
/**
* reloads the list data.
*/
public void onLoad() {
if (dataStore.getDataDisplays().isEmpty()) {
dataStore.addDataDisplay(dataGrid);
dataGrid.setVisibleRangeAndClearData(new Range(0, dataGrid.getPageSize()), true);
}
if (rightsDependentUIUpdater != null) {
rightsDependentUIUpdater.updateComponents();
}
RangeChangeEvent.fire(dataGrid, dataGrid.getVisibleRange());
}
/**
* selects a row in the datagrid.
*
* @param toSelect the object/row to select
*/
public void selectRow(D toSelect) {
selectionModel.setSelected(toSelect, true);
}
/**
* handles the selection of a datagrid row, informs the presenter.
*
* @param selectedRow the selected object/row
*/
protected void onRowSelected(D selectedRow) {
if (getPresenter() != null) {
getPresenter().onRowSelected(selectedRow);
}
}
/**
* {@inheritDoc}
*/
public Widget getWidget() {
return this;
}
/**
* Getter for subclasses.
*
* @return the selectionModel.
*/
protected SingleSelectionModel getSelectionModel() {
return selectionModel;
}
/**
* Setter for subclasses.
*
* @param selectionModel sets a new selectionModel
*/
protected void setSelectionModel(SingleSelectionModel selectionModel) {
this.selectionModel = selectionModel;
}
/**
* Getter for subclasses.
*
* @return the IListPresenter.
*/
protected IListPresenter getPresenter() {
return presenter;
}
/**
* Getter for subclasses.
*
* @return the DataGrid
*/
protected DataGrid getDataGrid() {
return dataGrid;
}
/**
* Setter for subclasses.
*
* @param dataGrid sets a new DataGrid
*/
protected void setDataGrid(DataGrid dataGrid) {
this.dataGrid = dataGrid;
}
/**
* Getter for subclasses.
*
* @return the Pager
*/
protected SimplePager getPager() {
return pager;
}
/**
* Setter for subclasses.
*
* @param pager sets a new Pager
*/
protected void setPager(SimplePager pager) {
this.pager = pager;
}
/**
* Getter for subclasses.
*
* @return the AsyncDataProvider.
*/
protected AsyncDataProvider getDataStore() {
return dataStore;
}
/**
* Getter for subclasses.
*
* @return the IRightsDependentUIUpdater.
*/
protected IRightsDependentUIUpdater getAccessRightsChecker() {
return rightsDependentUIUpdater;
}
/**
* Erzeugt eine Tabellenspalte vom Typ Text.
*
* @param propertyDTOConstant die Kontante im DTO mit der ein Attribut referenziert werden kann.
* @return die Spalte.
*/
protected Column createCellTypeText(final String propertyDTOConstant) {
// Last name
Column col1 = new Column(new TextCell()) {
@Override
public String getValue(D object) {
// Nicht so cool: object kann null sein, wenn man ueber den previous page Button zurueckblaettert.
// GWT Issue 7030 https://code.google.com/p/google-web-toolkit/issues/detail?id=7030
if (object != null) {
final Object o = object.get(propertyDTOConstant);
if (o != null) {
return String.valueOf(o);
}
}
return null;
}
};
return col1;
}
/**
* Erzeugt eine Tabellenspalte vom Typ Text.
*
* @param propertyDTOConstant die Kontante im DTO mit der ein Attribut referenziert werden kann.
* @param renderer der renderer fuer das jeweilige Enum
* @return die Spalte.
*/
protected Column createCellTypeEnum(final String propertyDTOConstant,
final I18NEnumRenderer renderer) {
// Last name
Column col1 = new Column(new TextCell()) {
@Override
public String getValue(D object) {
// Nicht so cool: object kann null sein, wenn man ueber den previous page Button zurueckblaettert.
// GWT Issue 7030 https://code.google.com/p/google-web-toolkit/issues/detail?id=7030
if (object != null) {
final Object o = object.get(propertyDTOConstant);
if (o != null && o instanceof I18NEnum) {
return renderer.render((I18NEnum) o);
}
}
return null;
}
};
return col1;
}
/**
* Erzeugt eine Tabellenspalte vom Typ Text.
*
* @param propertyDTOConstant die Kontante im DTO mit der ein Attribut referenziert werden kann.
* @return die Spalte.
*/
protected Column createCellTypeBoolean(final String propertyDTOConstant) {
// Last name
Column col1 = new Column(new TextCell()) {
@Override
public String getValue(D object) {
// Nicht so cool: object kann null sein, wenn man ueber den previous page Button zurueckblaettert.
// GWT Issue 7030 https://code.google.com/p/google-web-toolkit/issues/detail?id=7030
if (object != null) {
final Boolean o = object.get(propertyDTOConstant);
if (o != null && o) {
return "Ja";
}
}
return "Nein";
}
};
return col1;
}
/**
* Erzeugt eine Tabellenspalte vom Typ Date.
*
* @param propertyDTOConstant die Kontante im DTO mit der ein Attribut referenziert werden kann.
* @param format z.B. DateTimeFormat.PredefinedFormat.DATE_SHORT,
* DateTimeFormat.PredefinedFormat.TIME_SHORT,
* DateTimeFormat.PredefinedFormat.DATE_TIME_SHORT
* @return die Spalte.
*/
protected Column createCellTypeDate(final String propertyDTOConstant,
DateTimeFormat.PredefinedFormat format) {
// Last name
Column col1 = new Column(new DateCell(
DateTimeFormat.getFormat(format))) {
@Override
public Date getValue(D object) {
// Nicht so cool: object kann null sein, wenn man ueber den previous page Button zurueckblaettert.
// GWT Issue 7030 https://code.google.com/p/google-web-toolkit/issues/detail?id=7030
if (object != null) {
final Date date = object.get(propertyDTOConstant);
if (date != null) {
return date;
}
}
return null;
}
};
return col1;
}
/**
* Ergebniss der Initialisierung.
*
* @param Klasse der DTOs.
* @author Daniel.Wiese
* copyright 2014 SWM Service GmbH
*/
public static interface InitializationResult {
/**
* Liefert das Data grid.
*
* @return das data grid
*/
DataGrid grid();
/**
* Den mit dem Data Grid verbundenen Pager.
*
* @return der pager.
*/
SimplePager pager();
}
}