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.client.ui.grid.selection.MultiSelectionRenderer;
import com.vaadin.shared.ui.grid.GridConstants;
import com.vaadin.shared.ui.grid.HeightMode;
import com.vaadin.shared.ui.grid.Range;
*/
public class Grid<T> extends Composite implements SubPartAware {
+ private class SelectionColumn extends GridColumn<Boolean, T> {
+ private boolean initDone = false;
+
+ public SelectionColumn(final Renderer<Boolean> selectColumnRenderer) {
+ super(selectColumnRenderer);
+
+ setHeaderRenderer(new Renderer<String>() {
+ @Override
+ public void render(FlyweightCell cell, String data) {
+ if (cell.getRow() == escalator.getHeader().getRowCount() - 1) {
+ selectColumnRenderer.render(cell, Boolean.FALSE);
+ }
+ }
+ });
+ }
+
+ public void initDone() {
+ initDone = true;
+ }
+
+ @Override
+ public void setFooterCaption(String caption) {
+ if (initDone) {
+ throw new UnsupportedOperationException(
+ "The selection column is read only");
+ } else {
+ super.setFooterCaption(caption);
+ }
+ }
+
+ @Override
+ public void setFooterRenderer(Renderer<String> renderer) {
+ if (initDone) {
+ throw new UnsupportedOperationException(
+ "The selection column is read only");
+ } else {
+ super.setFooterRenderer(renderer);
+ }
+ }
+
+ @Override
+ public void setHeaderCaption(String caption) {
+ if (initDone) {
+ throw new UnsupportedOperationException(
+ "The selection column is read only");
+ } else {
+ super.setHeaderCaption(caption);
+ }
+ }
+
+ @Override
+ public void setHeaderRenderer(Renderer<String> renderer) {
+ if (initDone) {
+ throw new UnsupportedOperationException(
+ "The selection column is read only");
+ } else {
+ super.setHeaderRenderer(renderer);
+ }
+ }
+
+ @Override
+ public void setVisible(boolean visible) {
+ if (initDone) {
+ throw new UnsupportedOperationException(
+ "The selection column is read only");
+ } else {
+ super.setVisible(visible);
+ }
+ }
+
+ @Override
+ public void setWidth(int pixels) {
+ if (initDone) {
+ throw new UnsupportedOperationException(
+ "The selection column is read only");
+ } else {
+ super.setWidth(pixels);
+ }
+ }
+
+ @Override
+ public Boolean getValue(T row) {
+ return Boolean.valueOf(isSelected(row));
+ }
+ }
+
/**
* Escalator used internally by grid to render the rows
*/
*/
private GridColumn<?, T> lastFrozenColumn;
+ private Renderer<Boolean> selectColumnRenderer = null;
+
+ private SelectionColumn selectionColumn;
+
/**
* Base class for grid columns internally used by the Grid. The user should
* use {@link GridColumn} when creating new columns.
this.grid = grid;
- setVisible(this.visible);
- setWidth(this.width);
- setHeaderCaption(this.header);
- setFooterCaption(this.footer);
+ if (grid != null) {
+ setVisible(this.visible);
+ setWidth(this.width);
+ setHeaderCaption(this.header);
+ setFooterCaption(this.footer);
+ }
}
/**
* the column to add
*/
public void addColumn(GridColumn<?, T> column) {
- ColumnConfiguration conf = escalator.getColumnConfiguration();
- addColumn(column, conf.getColumnCount());
+ addColumn(column, getColumnCount());
}
/**
* the index where the column should be inserted into
* @param column
* the column to add
+ * @throws IllegalStateException
+ * if Grid's current selection model renders a selection column,
+ * and {@code index} is 0.
*/
public void addColumn(GridColumn<?, T> column, int index) {
+ if (column == selectionColumn) {
+ throw new IllegalArgumentException("The selection column many "
+ + "not be added manually");
+ } else if (selectionColumn != null && index == 0) {
+ throw new IllegalStateException("A column cannot be inserted "
+ + "before the selection column");
+ }
+ addColumnSkipSelectionColumnCheck(column, index);
+ }
+
+ private void addColumnSkipSelectionColumnCheck(GridColumn<?, T> column,
+ int index) {
// Register column with grid
columns.add(index, column);
* the column to remove
*/
public void removeColumn(GridColumn<?, T> column) {
+ if (column != null && column.equals(selectionColumn)) {
+ throw new IllegalArgumentException(
+ "The selection column may not be removed manually.");
+ }
+
+ removeColumnSkipSelectionColumnCheck(column);
+ }
+ private void removeColumnSkipSelectionColumnCheck(GridColumn<?, T> column) {
int columnIndex = columns.indexOf(column);
int visibleIndex = findVisibleColumnIndex(column);
columns.remove(columnIndex);
}
return null;
}
+
+ private void setSelectColumnRenderer(
+ final Renderer<Boolean> selectColumnRenderer) {
+ if (this.selectColumnRenderer == selectColumnRenderer) {
+ return;
+ }
+
+ if (this.selectColumnRenderer != null) {
+ removeColumnSkipSelectionColumnCheck(selectionColumn);
+ }
+
+ this.selectColumnRenderer = selectColumnRenderer;
+
+ if (selectColumnRenderer != null) {
+ selectionColumn = new SelectionColumn(selectColumnRenderer);
+
+ // FIXME: this needs to be done elsewhere, requires design...
+ selectionColumn.setWidth(25);
+ addColumnSkipSelectionColumnCheck(selectionColumn, 0);
+ selectionColumn.initDone();
+ } else {
+ selectionColumn = null;
+ }
+ }
+
+ /* TODO remove before final */
+ public void setSelectionCheckboxes(boolean set) {
+ if (set) {
+ setSelectColumnRenderer(new MultiSelectionRenderer());
+ } else {
+ setSelectColumnRenderer(null);
+ }
+ }
+
+ /*
+ * This is the same client-side Grid "isSelected" method as in the selection
+ * model design.
+ */
+ private boolean isSelected(T row) {
+ return false;
+ }
}
--- /dev/null
+/*
+ * 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.selection;
+
+import com.google.gwt.dom.client.Element;
+import com.google.gwt.user.client.DOM;
+import com.vaadin.client.ui.grid.FlyweightCell;
+import com.vaadin.client.ui.grid.Renderer;
+
+/* This class will probably not survive the final merge of all selection functionality. */
+public class MultiSelectionRenderer implements Renderer<Boolean> {
+ @Override
+ public void render(FlyweightCell cell, Boolean data) {
+ Element checkbox = Element.as(DOM.createInputCheck());
+ if (Boolean.TRUE.equals(data)) {
+ checkbox.setAttribute("checked", "checked");
+ }
+ cell.getElement().removeAllChildren();
+ cell.getElement().appendChild(checkbox);
+ }
+}