root = rowContainerElement;
}
+ @Override
+ public Element getElement() {
+ return root;
+ }
+
/**
* Gets the tag name of an element to represent a cell in a row.
* <p>
return cellElem;
}
+ @Override
+ public Element getRowElement(int index) {
+ return getTrByVisualIndex(index);
+ }
+
/**
* Gets the child element that is visually at a certain index
*
* @throws IndexOutOfBoundsException
* if {@code index} is not valid within {@link #root}
*/
- abstract protected Element getTrByVisualIndex(int index)
+ protected abstract Element getTrByVisualIndex(int index)
throws IndexOutOfBoundsException;
protected void paintRemoveColumns(final int offset,
}
}
+ @Override
+ public Element getRowElement(int index) {
+ if (index < 0 || index >= getRowCount()) {
+ throw new IndexOutOfBoundsException("No such logical index: "
+ + index);
+ }
+ int visualIndex = index
+ - getLogicalRowIndex(visualRowOrder.getFirst());
+ if (visualIndex >= 0 && visualIndex < visualRowOrder.size()) {
+ return super.getRowElement(visualIndex);
+ } else {
+ throw new IllegalStateException("Row with logical index "
+ + index + " is currently not available in the DOM");
+ }
+ }
+
private void setBodyScrollPosition(final double scrollLeft,
final double scrollTop) {
tBodyScrollLeft = scrollLeft;
.getLast()) + 1;
int visibleRowCount = visibleRangeEnd - visibleRangeStart;
-
fireEvent(new RowVisibilityChangeEvent(visibleRangeStart,
visibleRowCount));
} else {
}
}
+ /**
+ * Gets the range of currently visible rows
+ *
+ * @return range of visible rows
+ */
+ public Range getVisibleRowRange() {
+ return Range.between(
+ body.getLogicalRowIndex(body.visualRowOrder.getFirst()),
+ body.getLogicalRowIndex(body.visualRowOrder.getLast()) + 1);
+ }
+
/**
* Accesses the package private method Widget#setParent()
*
package com.vaadin.client.ui.grid;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
+import java.util.logging.Level;
import java.util.logging.Logger;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.EventTarget;
import com.google.gwt.event.shared.HandlerRegistration;
+import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasVisibility;
import com.vaadin.client.data.DataChangeHandler;
import com.vaadin.client.data.DataSource;
+import com.vaadin.client.ui.SubPartAware;
import com.vaadin.client.ui.grid.renderers.ComplexRenderer;
import com.vaadin.client.ui.grid.renderers.TextRenderer;
import com.vaadin.shared.ui.grid.GridConstants;
import com.vaadin.shared.ui.grid.HeightMode;
+import com.vaadin.shared.ui.grid.Range;
import com.vaadin.shared.ui.grid.ScrollDestination;
import com.vaadin.shared.util.SharedUtil;
* @since 7.4
* @author Vaadin Ltd
*/
-public class Grid<T> extends Composite {
+public class Grid<T> extends Composite implements SubPartAware {
/**
* Escalator used internally by grid to render the rows
}
}
}
+
+ @Override
+ public com.google.gwt.user.client.Element getSubPartElement(String subPart) {
+ // Parse SubPart string to type and indices
+ String[] splitArgs = subPart.split("\\[");
+
+ String type = splitArgs[0];
+ int[] indices = new int[splitArgs.length - 1];
+ for (int i = 0; i < indices.length; ++i) {
+ String tmp = splitArgs[i + 1];
+ indices[i] = Integer.parseInt(tmp.substring(0, tmp.length() - 1));
+ }
+
+ // Get correct RowContainer for type from Escalator
+ RowContainer container = null;
+ if (type.equalsIgnoreCase("header")) {
+ container = escalator.getHeader();
+ } else if (type.equalsIgnoreCase("cell")) {
+ // If wanted row is not visible, we need to scroll there.
+ Range visibleRowRange = escalator.getVisibleRowRange();
+ if (!visibleRowRange.contains(indices[0])) {
+ try {
+ scrollToRow(indices[0]);
+ } catch (IllegalArgumentException e) {
+ getLogger().log(Level.SEVERE, e.getMessage());
+ }
+ // Scrolling causes a lazy loading event. No element can
+ // currently be retrieved.
+ return null;
+ }
+ container = escalator.getBody();
+ } else if (type.equalsIgnoreCase("footer")) {
+ container = escalator.getFooter();
+ }
+
+ if (null != container) {
+ if (indices.length == 0) {
+ // No indexing. Just return the wanted container element
+ return DOM.asOld(container.getElement());
+ } else {
+ try {
+ return DOM.asOld(getSubPart(container, indices));
+ } catch (Exception e) {
+ getLogger().log(Level.SEVERE, e.getMessage());
+ }
+ }
+ }
+ return null;
+ }
+
+ private Element getSubPart(RowContainer container, int[] indices) {
+ // Scroll wanted column to view if able
+ if (indices.length > 1
+ && escalator.getColumnConfiguration().getFrozenColumnCount() <= indices[1]) {
+ escalator.scrollToColumn(indices[1], ScrollDestination.ANY, 0);
+ }
+
+ Element targetElement = container.getRowElement(indices[0]);
+ for (int i = 1; i < indices.length && targetElement != null; ++i) {
+ targetElement = (Element) targetElement.getChild(indices[i]);
+ }
+ return targetElement;
+ }
+
+ @Override
+ public String getSubPartName(com.google.gwt.user.client.Element subElement) {
+ // Containers and matching SubPart types
+ List<RowContainer> containers = Arrays.asList(escalator.getHeader(),
+ escalator.getBody(), escalator.getFooter());
+ List<String> containerType = Arrays.asList("header", "cell", "footer");
+
+ for (int i = 0; i < containers.size(); ++i) {
+ RowContainer container = containers.get(i);
+ boolean containerRow = (subElement.getTagName().equalsIgnoreCase(
+ "tr") && subElement.getParentElement() == container
+ .getElement());
+ if (containerRow) {
+ // Wanted SubPart is row that is a child of containers root
+ // To get indices, we use a cell that is a child of this row
+ subElement = DOM.asOld(subElement.getFirstChildElement());
+ }
+
+ Cell cell = container.getCell(subElement);
+ if (cell != null) {
+ // Skip the column index if subElement was a child of root
+ return containerType.get(i) + "[" + cell.getRow()
+ + (containerRow ? "]" : "][" + cell.getColumn() + "]");
+ }
+ }
+ return null;
+ }
}