/*
 * Copyright (c) 2015 - Tyl Consulting s.a.s.
 *
 *   Authors: Edoardo Vacchi
 *   Contributors: Marco Pancotti, Daniele Zonca
 *
 * 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 org.tylproject.vaadin.addon.fields.zoom;

import com.vaadin.data.Item;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Grid;
import com.vaadin.ui.VerticalLayout;

/**
 * Created by evacchi on 20/01/15.
 */
public class GridZoomDialog<T> extends VerticalLayout implements ZoomDialog<T> {

    private final Grid grid;
    private Object propertyId;


    public GridZoomDialog(Grid grid) {
        this.grid = grid;

        if (!(grid.getSelectionModel() instanceof Grid.SingleSelectionModel)) {
            throw new AssertionError(
                    "Selection mode must be "+Grid.SelectionMode.SINGLE.getClass()+", "+
                            grid.getSelectionModel().getClass() +" was given");
        }

        addComponent(grid);
        setExpandRatio(getGrid(), 1f);
        setSizeFull();

    }


    public GridZoomDialog(Grid grid, Object propertyId) {
        this(grid);
        this.propertyId = propertyId;
        setCaption(DefaultFieldFactory.createCaptionByPropertyId(propertyId));

    }

    public GridZoomDialog(Grid grid, Object propertyId, String caption) {
        this(grid, propertyId);
        this.setCaption(caption);
    }

    public GridZoomDialog withPropertyId(Object propertyId) {
        this.propertyId = propertyId;
        return this;
    }


    public Grid getGrid() {
        return grid;
    }


    /**
     * Assumes the given value to be an itemId; tries to select it
     * in the table. A null value clears the selection
     *
     */
    @Override
    public void show(T value) {

    }

    /**
     * Returns the current selected value
     */
    @Override
    public T dismiss() {
        Object itemId = getGrid().getSelectedRow();
        if (itemId == null) return null;
        Item item = getGrid().getContainerDataSource().getItem(itemId);
        return (T) item.getItemProperty(propertyId).getValue();
    }
}

@@ -732,16 +732,50 @@
 .zoom;%0A%0A
+import com.vaadin.data.Container;%0A
 import c
@@ -938,16 +938,74 @@
 lLayout;
+%0Aimport org.tylproject.vaadin.addon.fields.FilterableGrid;
 %0A%0A/**%0A *
@@ -1250,16 +1250,91 @@
 = grid;%0A
+        setupDialog(grid);%0A    %7D%0A%0A    private void setupDialog(Grid grid) %7B
 %0A       
@@ -1714,17 +1714,16 @@
 Full();%0A
-%0A
     %7D%0A%0A%0A
@@ -1916,16 +1916,463 @@
 rtyId));
+%0A    %7D%0A%0A    public %3CC extends Container.Indexed & Container.Filterable%3E%0A        GridZoomDialog(C container) %7B%0A%0A        FilterableGrid grid = new FilterableGrid(container);%0A%0A        grid.setSelectionMode(Grid.SelectionMode.SINGLE);%0A        grid.setSizeFull();%0A        grid.setWidth(%22100%25%22);%0A        grid.setHeight(%22100%25%22);%0A        grid.setVisibileColumns(%22firstName%22, %22lastName%22, %22birthDate%22);%0A%0A        this.grid = grid;%0A%0A        setupDialog(grid);
 %0A%0A    %7D%0A
@@ -2364,32 +2364,35 @@
 (grid);%0A%0A    %7D%0A%0A
+%0A%0A%0A
     public GridZ
@@ -2547,16 +2547,19 @@
 omDialog
+%3CT%3E
  withPro
@@ -2715,153 +2715,8 @@
 %7D%0A%0A%0A
-    /**%0A     * Assumes the given value to be an itemId; tries to select it%0A     * in the table. A null value clears the selection%0A     *%0A     */%0A
     

Add default when Grid parameter is omitted