/*
 * Copyright (c) 2008 Haulmont Technology Ltd. All Rights Reserved.
 * Haulmont Technology proprietary and confidential.
 * Use is subject to license terms.

 * Author: Eugeniy Degtyarjov
 * Created: 26.05.2010 16:52:04
 *
 * $Id$
 */
package cuba.client.web.ui.report.browse;

import com.haulmont.chile.core.model.MetaClass;
import com.haulmont.chile.core.model.MetaProperty;
import com.haulmont.chile.core.model.MetaPropertyPath;
import com.haulmont.cuba.core.global.MessageProvider;
import com.haulmont.cuba.gui.components.AbstractLookup;
import com.haulmont.cuba.gui.components.IFrame;
import com.haulmont.cuba.gui.components.Table;
import com.haulmont.cuba.gui.data.impl.CollectionDatasourceImpl;
import com.haulmont.cuba.web.gui.components.WebTable;
import com.haulmont.cuba.web.gui.components.WebVBoxLayout;

import java.util.Map;

public class CommonLookupController extends AbstractLookup {
    public CommonLookupController(IFrame frame) {
        super(frame);
    }

    @Override
    protected void init(Map<String, Object> params) {
        super.init(params);
        WebVBoxLayout vbox = getComponent("vbox");
        final MetaClass metaClass = (MetaClass) params.get("param$class");
        final Class javaClass = metaClass.getJavaClass();
        setCaption(MessageProvider.getMessage(javaClass, javaClass.getSimpleName()));
        CollectionDatasourceImpl cds = new CollectionDatasourceImpl(getDsContext(), getDsContext().getDataService(), "mainDs", metaClass, "_minimal");
        WebTable table = new WebTable() {
            @Override
            protected void initComponent(com.haulmont.cuba.web.toolkit.ui.Table component) {
                super.initComponent(component);
                for (MetaProperty prop : metaClass.getOwnProperties()) {//todo: reimplement - not all properties
                    MetaPropertyPath mpp = new MetaPropertyPath(metaClass, prop);
                    Table.Column column = new Table.Column(mpp);
                    column.setCaption(MessageProvider.getMessage(javaClass, javaClass.getSimpleName() + "." + prop.getName()));
                    addColumn(column);
                }
            }
        };
        table.setId("table");
        table.setDatasource(cds);
        vbox.add(table);
        vbox.expand(table, "100%", "100%");
        table.setMultiSelect(true);
        table.refresh();
        this.setLookupComponent(table);
    }
}

@@ -282,24 +282,72 @@
 .browse;%0D%0A%0D%0A
+import com.haulmont.chile.core.model.Instance;%0D%0A
 import com.h
@@ -656,56 +656,8 @@
 e;%0D%0A
-import com.haulmont.cuba.gui.components.Table;%0D%0A
 impo
@@ -718,16 +718,137 @@
 eImpl;%0D%0A
+import com.haulmont.cuba.web.gui.components.WebComponentsHelper;%0D%0Aimport com.haulmont.cuba.web.gui.components.WebLabel;%0D%0A
 import c
@@ -954,16 +954,78 @@
 ayout;%0D%0A
+import com.vaadin.ui.Component;%0D%0A%0D%0Aimport java.util.ArrayList;
 %0D%0Aimport
@@ -1569,16 +1569,22 @@
         
+final 
 Collecti
@@ -1727,16 +1727,22 @@
         
+final 
 WebTable
@@ -1954,113 +1954,553 @@
     
-for (MetaProperty prop : metaClass.getOwnProperties()) %7B//todo: reimplement - not all properties%0D%0A       
+com.vaadin.ui.Table.ColumnGenerator generator = new com.vaadin.ui.Table.ColumnGenerator() %7B%0D%0A                    @Override%0D%0A                    public Component generateCell(com.vaadin.ui.Table source, Object itemId, Object columnId) %7B%0D%0A                        Object inst = cds.getItem(itemId);%0D%0A                        WebLabel label = new WebLabel();%0D%0A                        label.setValue(((Instance) inst).getInstanceName());%0D%0A                        return WebComponentsHelper.unwrap(label);%0D%0A                    %7D%0D%0A                %7D;%0D%0A%0D%0A
     
@@ -2508,16 +2508,19 @@
         
+   
 MetaProp
@@ -2528,19 +2528,28 @@
 rtyPath 
-mpp
+nameProperty
  = new M
@@ -2575,20 +2575,80 @@
 aClass, 
-prop
+new ArrayList%3CMetaProperty%3E(metaClass.getOwnProperties()).get(0)
 );%0D%0A    
@@ -2663,18 +2663,8 @@
     
-  
-  Table.
 Colu
@@ -2683,24 +2683,27 @@
 new 
-Table.
 Column(
-mpp
+nameProperty
 );%0D%0A
@@ -2718,20 +2718,16 @@
         
- 
-   
 column.s
@@ -2740,103 +2740,18 @@
 ion(
-MessageProvider.getMessage(javaClass, javaClass.getSimpleName() + %22.%22 + prop.get
+%22
 Name
-())
+%22
 );%0D%0A
-    
     
@@ -2790,33 +2790,119 @@
                 
-%7D
+addGeneratedColumn(nameProperty, generator); //todo: govnokod. write ticket to gorodnov
 %0D%0A            %7D%0D

update common lookup component