Kaynağa Gözat

Fix declarative support for Grid basic properties (#16596)

Change-Id: I8b15ecb2eaff2dc5c8986473a7badbbd29432dda
tags/7.5.0.beta1
Johannes Dahlström 9 yıl önce
ebeveyn
işleme
24a192101b

+ 88
- 2
server/src/com/vaadin/ui/Grid.java Dosyayı Görüntüle

import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;


import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Element;

import com.google.gwt.thirdparty.guava.common.collect.Sets; import com.google.gwt.thirdparty.guava.common.collect.Sets;
import com.google.gwt.thirdparty.guava.common.collect.Sets.SetView; import com.google.gwt.thirdparty.guava.common.collect.Sets.SetView;
import com.vaadin.data.Container; import com.vaadin.data.Container;
import com.vaadin.shared.ui.grid.HeightMode; import com.vaadin.shared.ui.grid.HeightMode;
import com.vaadin.shared.ui.grid.ScrollDestination; import com.vaadin.shared.ui.grid.ScrollDestination;
import com.vaadin.shared.util.SharedUtil; import com.vaadin.shared.util.SharedUtil;
import com.vaadin.ui.declarative.DesignAttributeHandler;
import com.vaadin.ui.declarative.DesignContext;
import com.vaadin.ui.renderers.Renderer; import com.vaadin.ui.renderers.Renderer;
import com.vaadin.ui.renderers.TextRenderer; import com.vaadin.ui.renderers.TextRenderer;
import com.vaadin.util.ReflectTools; import com.vaadin.util.ReflectTools;
* Grid initial setup * Grid initial setup
*/ */
private void initGrid() { private void initGrid() {
setSelectionMode(SelectionMode.SINGLE);
setSelectionMode(getDefaultSelectionMode());
addSelectionListener(new SelectionListener() { addSelectionListener(new SelectionListener() {
@Override @Override
public void select(SelectionEvent event) { public void select(SelectionEvent event) {
if (numberOfColumns < -1 || numberOfColumns > columns.size()) { if (numberOfColumns < -1 || numberOfColumns > columns.size()) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"count must be between -1 and the current number of columns (" "count must be between -1 and the current number of columns ("
+ columns + ")");
+ columns.size() + "): " + numberOfColumns);
} }


getState().frozenColumnCount = numberOfColumns; getState().frozenColumnCount = numberOfColumns;
public void recalculateColumnWidths() { public void recalculateColumnWidths() {
getRpcProxy(GridClientRpc.class).recalculateColumnWidths(); getRpcProxy(GridClientRpc.class).recalculateColumnWidths();
} }

protected SelectionMode getDefaultSelectionMode() {
return SelectionMode.SINGLE;
}

@Override
public void readDesign(Element design, DesignContext context) {
super.readDesign(design, context);

Attributes attrs = design.attributes();
if (attrs.hasKey("editable")) {
setEditorEnabled(DesignAttributeHandler.readAttribute("editable",
attrs, boolean.class));
}
if (attrs.hasKey("frozen-columns")) {
setFrozenColumnCount(DesignAttributeHandler.readAttribute(
"frozen-columns", attrs, int.class));
}
if (attrs.hasKey("rows")) {
setHeightByRows(DesignAttributeHandler.readAttribute("rows", attrs,
double.class));
setHeightMode(HeightMode.ROW);
}
if (attrs.hasKey("selection-mode")) {
setSelectionMode(DesignAttributeHandler.readAttribute(
"selection-mode", attrs, SelectionMode.class));
}
}

@Override
public void writeDesign(Element design, DesignContext context) {
super.writeDesign(design, context);

Attributes attrs = design.attributes();
Grid def = context.getDefaultInstance(this);

DesignAttributeHandler.writeAttribute("editable", attrs,
isEditorEnabled(), def.isEditorEnabled(), boolean.class);

DesignAttributeHandler.writeAttribute("frozen-columns", attrs,
getFrozenColumnCount(), def.getFrozenColumnCount(), int.class);

if (getHeightMode() == HeightMode.ROW) {
DesignAttributeHandler.writeAttribute("rows", attrs,
getHeightByRows(), def.getHeightByRows(), double.class);
}

SelectionMode selectionMode = null;

if (selectionModel.getClass().equals(SingleSelectionModel.class)) {
selectionMode = SelectionMode.SINGLE;
} else if (selectionModel.getClass().equals(MultiSelectionModel.class)) {
selectionMode = SelectionMode.MULTI;
} else if (selectionModel.getClass().equals(NoSelectionModel.class)) {
selectionMode = SelectionMode.NONE;
}

assert selectionMode != null : "Unexpected selection model "
+ selectionModel.getClass().getName();

DesignAttributeHandler.writeAttribute("selection-mode", attrs,
selectionMode, getDefaultSelectionMode(), SelectionMode.class);

}

@Override
protected Collection<String> getCustomAttributes() {
Collection<String> result = super.getCustomAttributes();
result.add("editor-enabled");
result.add("editable");
result.add("frozen-column-count");
result.add("frozen-columns");
result.add("height-by-rows");
result.add("rows");
result.add("selection-mode");
result.add("header-visible");
result.add("footer-visible");
result.add("editor-error-handler");
result.add("height-mode");
return result;
}
} }

+ 69
- 0
server/tests/src/com/vaadin/tests/server/component/grid/declarative/GridDeclarativeAttributeTest.java Dosyayı Görüntüle

/*
* 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.tests.server.component.grid.declarative;

import static org.junit.Assert.assertSame;

import org.junit.Test;

import com.vaadin.shared.ui.grid.HeightMode;
import com.vaadin.tests.design.DeclarativeTestBase;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.MultiSelectionModel;
import com.vaadin.ui.Grid.NoSelectionModel;
import com.vaadin.ui.Grid.SingleSelectionModel;

/**
* Tests declarative support for {@link Grid} properties.
*
* @since
* @author Vaadin Ltd
*/
public class GridDeclarativeAttributeTest extends DeclarativeTestBase<Grid> {

@Test
public void testBasicAttributes() {

String design = "<v-grid editable='true' rows=20 frozen-columns=-1 "
+ "editor-save-caption='Tallenna' editor-cancel-caption='Peruuta'>";

Grid grid = new Grid();
grid.setEditorEnabled(true);
grid.setHeightMode(HeightMode.ROW);
grid.setHeightByRows(20);
grid.setFrozenColumnCount(-1);
grid.setEditorSaveCaption("Tallenna");
grid.setEditorCancelCaption("Peruuta");

testRead(design, grid);
testWrite(design, grid);
}

@Test
public void testSelectionMode() {
String design = "<v-grid selection-mode='none'>";
assertSame(NoSelectionModel.class, read(design).getSelectionModel()
.getClass());

design = "<v-grid selection-mode='single'>";
assertSame(SingleSelectionModel.class, read(design).getSelectionModel()
.getClass());

design = "<v-grid selection-mode='multi'>";
assertSame(MultiSelectionModel.class, read(design).getSelectionModel()
.getClass());
}
}

Loading…
İptal
Kaydet