123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- /*
- * Copyright 2000-2016 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.connectors.grid;
-
- import com.vaadin.client.ServerConnector;
- import com.vaadin.client.annotations.OnStateChange;
- import com.vaadin.client.connectors.AbstractRendererConnector;
- import com.vaadin.client.extensions.AbstractExtensionConnector;
- import com.vaadin.client.widgets.Grid.Column;
- import com.vaadin.client.widgets.Grid.HeaderCell;
- import com.vaadin.shared.data.DataCommunicatorConstants;
- import com.vaadin.shared.ui.Connect;
- import com.vaadin.shared.ui.grid.ColumnState;
-
- import elemental.json.JsonObject;
- import elemental.json.JsonValue;
-
- /**
- * A connector class for columns of the Grid component.
- *
- * @author Vaadin Ltd
- * @since 8.0
- */
- @Connect(com.vaadin.ui.Grid.Column.class)
- public class ColumnConnector extends AbstractExtensionConnector {
-
- public abstract static class CustomColumn
- extends Column<Object, JsonObject> {
-
- private final String connectorId;
-
- CustomColumn(String connectorId) {
- this.connectorId = connectorId;
- }
-
- public String getConnectorId() {
- return connectorId;
- }
-
- @Override
- protected void setDefaultHeaderContent(HeaderCell cell) {
- // NO-OP, Server takes care of header contents.
- }
- }
-
- private CustomColumn column;
-
- /* This parent is needed because it's no longer available in onUnregister */
- private GridConnector parent;
-
- @Override
- protected void extend(ServerConnector target) {
- parent = getParent();
- column = new CustomColumn(getConnectorId()) {
-
- @Override
- public Object getValue(JsonObject row) {
- final JsonObject rowData = row
- .getObject(DataCommunicatorConstants.DATA);
-
- if (rowData.hasKey(getConnectorId())) {
- final JsonValue columnValue = rowData.get(getConnectorId());
-
- return getRendererConnector().decode(columnValue);
- }
-
- return null;
- }
- };
-
- // Initially set a renderer
- updateRenderer();
-
- getParent().addColumn(column, getState().internalId);
-
- }
-
- @SuppressWarnings("unchecked")
- private AbstractRendererConnector<Object> getRendererConnector() {
- return (AbstractRendererConnector<Object>) getState().renderer;
- }
-
- @OnStateChange("caption")
- void updateCaption() {
- column.setHeaderCaption(getState().caption);
- }
-
- @OnStateChange("sortable")
- void updateSortable() {
- column.setSortable(getState().sortable);
- }
-
- @OnStateChange("renderer")
- void updateRenderer() {
- column.setRenderer(getRendererConnector().getRenderer());
- getParent().onColumnRendererChanged(column);
- }
-
- @OnStateChange("hidingToggleCaption")
- void updateHidingToggleCaption() {
- column.setHidingToggleCaption(getState().hidingToggleCaption);
- }
-
- @OnStateChange("hidden")
- void updateHidden() {
- column.setHidden(getState().hidden);
- }
-
- @OnStateChange("hidable")
- void updateHidable() {
- column.setHidable(getState().hidable);
- }
-
- @OnStateChange("resizable")
- void updateResizable() {
- column.setResizable(getState().resizable);
- }
-
- @OnStateChange("width")
- void updateWidth() {
- column.setWidth(getState().width);
- }
-
- @OnStateChange("minWidth")
- void updateMinWidth() {
- column.setMinimumWidth(getState().minWidth);
- }
-
- @OnStateChange("minimumWidthFromContent")
- void updateMinimumWidthFromContent() {
- column.setMinimumWidthFromContent(getState().minimumWidthFromContent);
- }
-
- @OnStateChange("maxWidth")
- void updateMaxWidth() {
- column.setMaximumWidth(getState().maxWidth);
- }
-
- @OnStateChange("expandRatio")
- void updateExpandRatio() {
- column.setExpandRatio(getState().expandRatio);
- }
-
- @OnStateChange("editable")
- void updateEditable() {
- column.setEditable(getState().editable);
- }
-
- @Override
- public void onUnregister() {
- super.onUnregister();
- if (parent.getParent() != null) {
- // If the grid itself was unregistered there is no point in spending
- // time to remove columns (and have problems with frozen columns)
- // before throwing everything away
- parent.removeColumn(column);
- parent = null;
- }
- column = null;
- }
-
- @Override
- public GridConnector getParent() {
- return (GridConnector) super.getParent();
- }
-
- @Override
- public ColumnState getState() {
- return (ColumnState) super.getState();
- }
-
- }
|