Browse Source

Refactored client Renderers once again #13334

The following things are refactored in this changeset:
 * Cell interface removed
 * CellInfo -> Cell
 * Renderer interface becomes a single method interface
 * All other methods moved from Renderer to new ComplexRenderer interface

Change-Id: I567868b8dc73783988bce6c11bc23e12d5479172
tags/7.4.0.alpha2
John Ahlroos 10 years ago
parent
commit
0eb12c4c82

+ 43
- 36
client/src/com/vaadin/client/ui/grid/Cell.java View File

@@ -13,63 +13,70 @@
* License for the specific language governing permissions and limitations under
* the License.
*/

package com.vaadin.client.ui.grid;

import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.HasOneWidget;

/**
* A representation of a single cell.
* <p>
* A Cell instance will be provided to the {@link EscalatorUpdater} responsible
* for rendering the cells in a certain {@link RowContainer}.
* Describes a cell
*
* TODO The description is still very vague since the content and naming of this
* class is still under debate and the API is not final. Improve the description
* when API has been finalized.
*
* @since 7.4
* @author Vaadin Ltd
*/
public interface Cell extends HasOneWidget {
public class Cell {

private final int row;

private final int column;

private final Element element;

/**
* Gets the index of the row this cell is in.
* Constructs a new {@link Cell}
*
* @return the index of the row this cell is in
* @param row
* The index of the row
* @param column
* The index of the column
* @param element
* The cell element
*/
public int getRow();
public Cell(int row, int column, Element element) {
super();
this.row = row;
this.column = column;
this.element = element;
}

/**
* Gets the index of the column this cell is in.
* Returns the index of the row the cell resides on
*
* @return the row index
*
* @return the index of the column this cell is in
*/
public int getColumn();
public int getRow() {
return row;
}

/**
* Gets the root element for this cell. The {@link EscalatorUpdater} may
* update the class names of the element, add inline styles and freely
* modify the contents.
* <p>
* Avoid modifying the dimensions, positioning or colspan of the cell
* element.
* Returns the index of the column the cell resides on
*
* @return The root element for this cell. Never <code>null</code>.
* @return the column index
*/
public Element getElement();
public int getColumn() {
return column;
}

/**
* Sets the column span of the cell.
* <p>
* This will overwrite any possible "colspan" attribute in the current
* element (i.e. the object returned by {@link #getElement()}). This will
* also handle internal bookkeeping, skip the rendering of any affected
* adjacent cells, and make sure that the current cell's dimensions are
* handled correctly.
* Returns the element of the cell
*
* @param numberOfCells
* the number of cells to span to the right, or <code>1</code> to
* unset any column spans
* @throws IllegalArgumentException
* if <code>numberOfCells &lt; 1</code>
* @return the element
*/
public void setColSpan(int numberOfCells) throws IllegalArgumentException;
}
public Element getElement() {
return element;
}

}

+ 0
- 82
client/src/com/vaadin/client/ui/grid/CellInfo.java View File

@@ -1,82 +0,0 @@
/*
* Copyright 2000-2014 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.client.ui.grid;

import com.google.gwt.dom.client.Element;

/**
* Describes a cell
*
* TODO The description is still very vague since the content and naming of this
* class is still under debate and the API is not final. Improve the description
* when API has been finalized.
*
* @author Vaadin Ltd
*/
public class CellInfo {

private final int row;

private final int column;

private final Element element;

/**
* Constructs a new {@link CellInfo}
*
* @param row
* The index of the row
* @param column
* The index of the column
* @param element
* The cell element
*/
public CellInfo(int row, int column, Element element) {
super();
this.row = row;
this.column = column;
this.element = element;
}

/**
* Returns the index of the row the cell resides on
*
* @return the row index
*
*/
public int getRow() {
return row;
}

/**
* Returns the index of the column the cell resides on
*
* @return the column index
*/
public int getColumn() {
return column;
}

/**
* Returns the element of the cell
*
* @return the element
*/
public Element getElement() {
return element;
}

}

+ 2
- 2
client/src/com/vaadin/client/ui/grid/EscalatorUpdater.java View File

@@ -36,7 +36,7 @@ public interface EscalatorUpdater {
public static final EscalatorUpdater NULL = new EscalatorUpdater() {
@Override
public void updateCells(final Row row,
final Iterable<Cell> cellsToUpdate) {
final Iterable<FlyweightCell> cellsToUpdate) {
// NOOP
}
};
@@ -61,5 +61,5 @@ public interface EscalatorUpdater {
* You should neither store nor reuse the reference to the list,
* nor to the individual cells
*/
public void updateCells(Row row, Iterable<Cell> cellsToUpdate);
public void updateCells(Row row, Iterable<FlyweightCell> cellsToUpdate);
}

+ 30
- 14
client/src/com/vaadin/client/ui/grid/FlyweightCell.java View File

@@ -25,18 +25,19 @@ import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.ui.grid.FlyweightRow.CellIterator;

/**
* An internal implementation of the {@link Cell} interface.
* A {@link FlyweightCell} represents a cell in the {@link Grid} or
* {@link Escalator} at a certain point in time.
*
* <p>
* These instances are populated into a {@link FlyweightRow} instance, and
* intended to be reused when rendering cells in an escalator.
* Since the {@link FlyweightCell} follows the <code>Flyweight</code>-pattern
* any instance of this object is subject to change without the user knowing it
* and so should not be stored anywhere outside of the method providing these
* instances.
*
* @since 7.4
* @author Vaadin Ltd
* @see FlyweightRow#getCells()
* @see FlyweightRow#addCells(int, int)
* @see FlyweightRow#removeCells(int, int)
*/
class FlyweightCell implements Cell {
public class FlyweightCell {
static final String COLSPAN_ATTR = "colSpan";

private final int column;
@@ -53,19 +54,26 @@ class FlyweightCell implements Cell {
this.escalator = escalator;
}

@Override
/**
* Returns the row index of the cell
*/
public int getRow() {
assertSetup();
return row.getRow();
}

@Override
/**
* Returns the column index of the cell
*/
public int getColumn() {
assertSetup();
return column;
}

@Override
/**
* Returns the element of the cell. Can be either a <code>TD</code> element
* or a <code>TH</code> element.
*/
public Element getElement() {
return (Element) row.getElement().getChild(column);
}
@@ -109,7 +117,6 @@ class FlyweightCell implements Cell {
+ "inappropriately.";
}

@Override
public void setColSpan(final int numberOfCells) {
/*-
* This will default to 1 if unset, as per DOM specifications:
@@ -157,12 +164,18 @@ class FlyweightCell implements Cell {
}
}

@Override
/**
* @deprecated Will be removed in further refactorings
*/
@Deprecated
public Widget getWidget() {
return Escalator.getWidgetFromCell(getElement());
}

@Override
/**
* @deprecated Will be removed in further refactorings
*/
@Deprecated
public void setWidget(Widget widget) {

Widget oldWidget = getWidget();
@@ -197,7 +210,10 @@ class FlyweightCell implements Cell {
}
}

@Override
/**
* @deprecated Will be removed in further refactorings
*/
@Deprecated
public void setWidget(IsWidget w) {
setWidget(Widget.asWidgetOrNull(w));
}

+ 4
- 4
client/src/com/vaadin/client/ui/grid/FlyweightRow.java View File

@@ -35,7 +35,7 @@ import com.google.gwt.dom.client.Node;
*/
class FlyweightRow implements Row {

static class CellIterator implements Iterator<Cell> {
static class CellIterator implements Iterator<FlyweightCell> {
/** A defensive copy of the cells in the current row. */
private final ArrayList<FlyweightCell> cells;
private int cursor = 0;
@@ -188,11 +188,11 @@ class FlyweightRow implements Row {
* @see #setup(Element, int, int[])
* @see #teardown()
*/
Iterable<Cell> getCells() {
Iterable<FlyweightCell> getCells() {
assertSetup();
return new Iterable<Cell>() {
return new Iterable<FlyweightCell>() {
@Override
public Iterator<Cell> iterator() {
public Iterator<FlyweightCell> iterator() {
return new CellIterator(cells);
}
};

+ 8
- 7
client/src/com/vaadin/client/ui/grid/Grid.java View File

@@ -510,7 +510,7 @@ public class Grid<T> extends Composite {
public abstract Renderer<String> getGroupRenderer(ColumnGroup<T> group);

@Override
public void updateCells(Row row, Iterable<Cell> cellsToUpdate) {
public void updateCells(Row row, Iterable<FlyweightCell> cellsToUpdate) {

int rowIndex;
if (inverted) {
@@ -521,7 +521,7 @@ public class Grid<T> extends Composite {

if (firstRowIsVisible() && rowIndex == 0) {
// column headers
for (Cell cell : cellsToUpdate) {
for (FlyweightCell cell : cellsToUpdate) {
GridColumn<?, T> column = getColumnFromVisibleIndex(cell
.getColumn());
if (column != null) {
@@ -551,7 +551,7 @@ public class Grid<T> extends Composite {

assert groupRow != null;

for (Cell cell : cellsToUpdate) {
for (FlyweightCell cell : cellsToUpdate) {
GridColumn<?, T> column = getColumnFromVisibleIndex(cell
.getColumn());
ColumnGroup<T> group = getGroupForColumn(groupRow, column);
@@ -653,7 +653,8 @@ public class Grid<T> extends Composite {
return new EscalatorUpdater() {

@Override
public void updateCells(Row row, Iterable<Cell> cellsToUpdate) {
public void updateCells(Row row,
Iterable<FlyweightCell> cellsToUpdate) {
int rowIndex = row.getRow();
if (dataSource == null) {
setCellsLoading(cellsToUpdate);
@@ -666,7 +667,7 @@ public class Grid<T> extends Composite {
return;
}

for (Cell cell : cellsToUpdate) {
for (FlyweightCell cell : cellsToUpdate) {
GridColumn column = getColumnFromVisibleIndex(cell
.getColumn());
if (column != null) {
@@ -676,8 +677,8 @@ public class Grid<T> extends Composite {
}
}

private void setCellsLoading(Iterable<Cell> cellsToUpdate) {
for (Cell cell : cellsToUpdate) {
private void setCellsLoading(Iterable<FlyweightCell> cellsToUpdate) {
for (FlyweightCell cell : cellsToUpdate) {
cell.getElement().setInnerText("...");
}
}

+ 1
- 51
client/src/com/vaadin/client/ui/grid/Renderer.java View File

@@ -15,9 +15,6 @@
*/
package com.vaadin.client.ui.grid;

import java.util.Collection;

import com.google.gwt.dom.client.NativeEvent;

/**
* Renderer for rending a value &lt;T&gt; into cell.
@@ -34,44 +31,6 @@ import com.google.gwt.dom.client.NativeEvent;
*/
public interface Renderer<T> {

/**
* Called at initialization stage. Perform any initialization here e.g.
* attach handlers, attach widgets etc.
*
* @param cell
* The cell. Note that the cell is a flyweight and should not be
* stored outside of the method as it will change.
*/
void init(Cell cell);

/**
* Returns the events that the renderer should consume. These are also the
* events that the Grid will pass to
* {@link #onBrowserEvent(CellInfo, NativeEvent)} when they occur.
* <code>null</code> if no events are consumed
*
* @return the consumed events, or null if no events are consumed
*
* @see com.google.gwt.dom.client.BrowserEvents
*/
Collection<String> getConsumedEvents();

/**
* Called whenever a registered event is triggered in the column the
* renderer renders.
* <p>
* The events that triggers this needs to be returned by the
* {@link #getConsumedEvents()} method.
*
* @param cellInfo
* Object containing information about the cell the event was
* triggered on.
*
* @param event
* The original DOM event
*/
void onBrowserEvent(CellInfo cell, NativeEvent event);

/**
* Called whenever the {@link Grid} updates a cell
*
@@ -82,14 +41,5 @@ public interface Renderer<T> {
* @param data
* The column data object
*/
void render(Cell cell, T data);

/**
* Called when the cell is "activated" by pressing <code>enter</code> or
* double clicking
*
* @return <code>true</code> if event was handled and should not be
* interpreted as a generic gesture by Grid.
*/
boolean onActivate();
void render(FlyweightCell cell, T data);
}

+ 0
- 51
client/src/com/vaadin/client/ui/grid/renderers/AbstractRenderer.java View File

@@ -1,51 +0,0 @@
/*
* Copyright 2000-2014 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.client.ui.grid.renderers;

import java.util.Collection;

import com.google.gwt.dom.client.NativeEvent;
import com.vaadin.client.ui.grid.Cell;
import com.vaadin.client.ui.grid.CellInfo;
import com.vaadin.client.ui.grid.Renderer;

/**
* Abstract base class for renderers.
*
* @author Vaadin Ltd
*/
public abstract class AbstractRenderer<T> implements Renderer<T> {

@Override
public void init(Cell cell) {
// Implement if needed
}

@Override
public Collection<String> getConsumedEvents() {
return null;
}

@Override
public void onBrowserEvent(CellInfo cell, NativeEvent event) {
// Implement if needed
}

@Override
public boolean onActivate() {
return false;
}
}

+ 123
- 0
client/src/com/vaadin/client/ui/grid/renderers/ComplexRenderer.java View File

@@ -0,0 +1,123 @@
/*
* Copyright 2000-2014 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.client.ui.grid.renderers;

import java.util.Collection;

import com.google.gwt.dom.client.NativeEvent;
import com.vaadin.client.ui.grid.Cell;
import com.vaadin.client.ui.grid.FlyweightCell;
import com.vaadin.client.ui.grid.Renderer;

/**
* Base class for renderers that needs initialization and destruction logic
* (override {@link #init(FlyweightCell) and #destroy(FlyweightCell) } and event
* handling (see {@link #onBrowserEvent(Cell, NativeEvent)},
* {@link #getConsumedEvents()} and {@link #onActivate()}.
*
* <p>
* Also provides a helper method for hiding the cell contents by overriding
* {@link #setContentVisible(FlyweightCell, boolean)}
*
* @since 7.4
* @author Vaadin Ltd
*/
public abstract class ComplexRenderer<T> implements Renderer<T> {

/**
* Called at initialization stage. Perform any initialization here e.g.
* attach handlers, attach widgets etc.
*
* @param cell
* The cell. Note that the cell is not to be stored outside of
* the method as the cell install will change. See
* {@link FlyweightCell}
*/
public void init(FlyweightCell cell) {
// Implement if needed
}

/**
* Called after the cell is deemed to be destroyed and no longer used by the
* Grid. Called after the cell element is detached from the DOM.
*
* @param cell
* The cell. Note that the cell is not to be stored outside of
* the method as the cell install will change. See
* {@link FlyweightCell}
*/
public void destroy(FlyweightCell cell) {
// Implement if needed
}

/**
* Returns the events that the renderer should consume. These are also the
* events that the Grid will pass to
* {@link #onBrowserEvent(Cell, NativeEvent)} when they occur.
* <code>null</code> if no events are consumed
*
* @return the consumed events, or null if no events are consumed
*
* @see com.google.gwt.dom.client.BrowserEvents
*/
public Collection<String> getConsumedEvents() {
return null;
}

/**
* Called whenever a registered event is triggered in the column the
* renderer renders.
* <p>
* The events that triggers this needs to be returned by the
* {@link #getConsumedEvents()} method.
*
* @param cell
* Object containing information about the cell the event was
* triggered on.
*
* @param event
* The original DOM event
*/
public void onBrowserEvent(Cell cell, NativeEvent event) {
// Implement if needed
}

/**
* Hides content by setting visibility: hidden to all elements inside the
* cell. Text nodes are left as is for now - renderers that add such to the
* root element need to implement explicit support hiding them
*
* @param cell
* The cell
* @param visible
* Is the cell content be visible
* @return <code>true</code> if the content should be set visible
*/
public boolean setContentVisible(FlyweightCell cell, boolean visible) {
return false;
}

/**
* Called when the cell is "activated" by pressing <code>enter</code>,
* double clicking or performing a double tap on the cell.
*
* @return <code>true</code> if event was handled and should not be
* interpreted as a generic gesture by Grid.
*/
public boolean onActivate() {
return false;
}
}

+ 4
- 3
client/src/com/vaadin/client/ui/grid/renderers/DateRenderer.java View File

@@ -19,7 +19,8 @@ import java.util.Date;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.TimeZone;
import com.vaadin.client.ui.grid.Cell;
import com.vaadin.client.ui.grid.FlyweightCell;
import com.vaadin.client.ui.grid.Renderer;

/**
* A renderer for rendering dates into cells
@@ -27,7 +28,7 @@ import com.vaadin.client.ui.grid.Cell;
* @since 7.4
* @author Vaadin Ltd
*/
public class DateRenderer extends AbstractRenderer<Date> {
public class DateRenderer implements Renderer<Date> {

private DateTimeFormat format = DateTimeFormat.getShortDateTimeFormat();

@@ -35,7 +36,7 @@ public class DateRenderer extends AbstractRenderer<Date> {
.getTimezoneOffset());

@Override
public void render(Cell cell, Date date) {
public void render(FlyweightCell cell, Date date) {
String dateStr = format.format(date, timeZone);
cell.getElement().setInnerText(dateStr);
}

+ 4
- 3
client/src/com/vaadin/client/ui/grid/renderers/HtmlRenderer.java View File

@@ -17,7 +17,8 @@ package com.vaadin.client.ui.grid.renderers;

import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.vaadin.client.ui.grid.Cell;
import com.vaadin.client.ui.grid.FlyweightCell;
import com.vaadin.client.ui.grid.Renderer;

/**
* Renders a string as HTML into a cell.
@@ -31,10 +32,10 @@ import com.vaadin.client.ui.grid.Cell;
* @author Vaadin Ltd
* @see SafeHtmlUtils#fromSafeConstant(String)
*/
public class HtmlRenderer extends AbstractRenderer<String> {
public class HtmlRenderer implements Renderer<String> {

@Override
public void render(Cell cell, String htmlString) {
public void render(FlyweightCell cell, String htmlString) {
cell.getElement().setInnerSafeHtml(
SafeHtmlUtils.fromSafeConstant(htmlString));
}

+ 4
- 3
client/src/com/vaadin/client/ui/grid/renderers/NumberRenderer.java View File

@@ -16,7 +16,8 @@
package com.vaadin.client.ui.grid.renderers;

import com.google.gwt.i18n.client.NumberFormat;
import com.vaadin.client.ui.grid.Cell;
import com.vaadin.client.ui.grid.FlyweightCell;
import com.vaadin.client.ui.grid.Renderer;

/**
* Renders a number into a cell using a specific {@link NumberFormat}. By
@@ -28,7 +29,7 @@ import com.vaadin.client.ui.grid.Cell;
* @param <T>
* The number type to render.
*/
public class NumberRenderer<T extends Number> extends AbstractRenderer<T> {
public class NumberRenderer<T extends Number> implements Renderer<T> {

private NumberFormat format = NumberFormat.getDecimalFormat();

@@ -57,7 +58,7 @@ public class NumberRenderer<T extends Number> extends AbstractRenderer<T> {
}

@Override
public void render(Cell cell, Number number) {
public void render(FlyweightCell cell, Number number) {
cell.getElement().setInnerText(format.format(number));
}
}

+ 4
- 3
client/src/com/vaadin/client/ui/grid/renderers/TextRenderer.java View File

@@ -15,7 +15,8 @@
*/
package com.vaadin.client.ui.grid.renderers;

import com.vaadin.client.ui.grid.Cell;
import com.vaadin.client.ui.grid.FlyweightCell;
import com.vaadin.client.ui.grid.Renderer;

/**
* Renderer that renders text into a cell.
@@ -23,10 +24,10 @@ import com.vaadin.client.ui.grid.Cell;
* @since 7.4
* @author Vaadin Ltd
*/
public class TextRenderer extends AbstractRenderer<String> {
public class TextRenderer implements Renderer<String> {

@Override
public void render(Cell cell, String text) {
public void render(FlyweightCell cell, String text) {
cell.getElement().setInnerText(text);
}
}

+ 8
- 8
uitest/src/com/vaadin/tests/widgetset/client/grid/VTestGrid.java View File

@@ -5,10 +5,10 @@ import java.util.List;

import com.google.gwt.user.client.Window.Location;
import com.google.gwt.user.client.ui.Composite;
import com.vaadin.client.ui.grid.Cell;
import com.vaadin.client.ui.grid.ColumnConfiguration;
import com.vaadin.client.ui.grid.Escalator;
import com.vaadin.client.ui.grid.EscalatorUpdater;
import com.vaadin.client.ui.grid.FlyweightCell;
import com.vaadin.client.ui.grid.Row;
import com.vaadin.client.ui.grid.RowContainer;
import com.vaadin.shared.ui.grid.ScrollDestination;
@@ -43,8 +43,8 @@ public class VTestGrid extends Composite {
return new EscalatorUpdater() {
@Override
public void updateCells(final Row row,
final Iterable<Cell> cellsToUpdate) {
for (final Cell cell : cellsToUpdate) {
final Iterable<FlyweightCell> cellsToUpdate) {
for (final FlyweightCell cell : cellsToUpdate) {
if (cell.getColumn() % 3 == 0) {
cell.setColSpan(2);
}
@@ -61,8 +61,8 @@ public class VTestGrid extends Composite {
return new EscalatorUpdater() {
@Override
public void updateCells(final Row row,
final Iterable<Cell> cellsToUpdate) {
for (final Cell cell : cellsToUpdate) {
final Iterable<FlyweightCell> cellsToUpdate) {
for (final FlyweightCell cell : cellsToUpdate) {
if (cell.getColumn() % 3 == 1) {
cell.setColSpan(2);
}
@@ -79,7 +79,7 @@ public class VTestGrid extends Composite {
return new EscalatorUpdater() {
private int i = 0;

public void renderCell(final Cell cell) {
public void renderCell(final FlyweightCell cell) {
final Integer columnName = columns.get(cell.getColumn());
final Integer rowName = rows.get(cell.getRow());
String cellInfo = columnName + "," + rowName;
@@ -123,8 +123,8 @@ public class VTestGrid extends Composite {

@Override
public void updateCells(final Row row,
final Iterable<Cell> cellsToUpdate) {
for (final Cell cell : cellsToUpdate) {
final Iterable<FlyweightCell> cellsToUpdate) {
for (final FlyweightCell cell : cellsToUpdate) {
renderCell(cell);
}
}

Loading…
Cancel
Save