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

 * Author: Dmitry Abramov
 * Created: 16.02.2009 17:18:03
 * $Id$
 */
package com.haulmont.cuba.gui.components;

import com.haulmont.chile.core.model.Instance;
import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.core.global.MessageProvider;
import com.haulmont.cuba.gui.AppConfig;
import com.haulmont.cuba.gui.WindowManager;
import com.haulmont.cuba.gui.export.ExcelExporter;
import com.haulmont.cuba.gui.export.ExportDisplay;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.DataService;

import java.util.Collection;
import java.util.Map;

public class TableActionsHelper extends ListActionsHelper<Table>{
    public TableActionsHelper(IFrame frame, Table table) {
        super(frame, table);
    }

    public Action createCreateAction(final ValueProvider valueProvider, final WindowManager.OpenType openType) {
        final AbstractAction action = new AbstractAction("create") {
            public String getCaption() {
                final String messagesPackage = AppConfig.getInstance().getMessagesPack();
                return MessageProvider.getMessage(messagesPackage, "actions.Create");
            }

            public boolean isEnabled() {
                return true;
            }

            public void actionPerform(Component component) {
                final CollectionDatasource datasource = TableActionsHelper.this.component.getDatasource();
                final DataService dataservice = datasource.getDataService();
                final String windowID = datasource.getMetaClass().getName() + ".edit";

                final Entity item = dataservice.<Entity>newInstance(datasource.getMetaClass());
                for (Map.Entry<String, Object> entry : valueProvider.getValues().entrySet()) {
                    final Object value = entry.getValue();
                    if (value instanceof Collection) {
                        final Collection collection = (Collection) value;
                        if (collection.size() != 1) {
                            throw new UnsupportedOperationException();
                        } else {
                            ((Instance) item).setValue(entry.getKey(), collection.iterator().next());
                        }
                    } else {
                        ((Instance) item).setValue(entry.getKey(), value);
                    }
                }
                final Window window = frame.openEditor(windowID, item, openType);
                window.addListener(new Window.CloseListener() {
                    public void windowClosed(String actionId) {
                        if (window instanceof Window.Editor) {
                            Object item = ((Window.Editor) window).getItem();
                            if (item instanceof Entity)
                                datasource.addItem((Entity) item);
                        }
                    }
                });
            }
        };
        TableActionsHelper.this.component.addAction(action);

        return action;
    }

    public Action createExcelAction(final ExportDisplay display) {
        AbstractAction action = new AbstractAction("excel") {
            public String getCaption() {
                final String messagesPackage = AppConfig.getInstance().getMessagesPack();
                return MessageProvider.getMessage(messagesPackage, "actions.Excel");
            }

            public void actionPerform(Component component) {
                ExcelExporter exporter = new ExcelExporter();
                exporter.exportTable(TableActionsHelper.this.component, display);
            }
        };
        TableActionsHelper.this.component.addAction(action);
        return action;
    }
}

@@ -721,16 +721,82 @@
 ervice;%0D
+%0Aimport com.haulmont.cuba.gui.data.impl.DatasourceImplementation;%0D
 %0A%0D%0Aimpor
@@ -3097,76 +3097,282 @@
 ity)
-%0D%0A                                datasource.addItem((Entity) item);
+ %7B%0D%0A                                boolean modified = datasource.isModified();%0D%0A                                datasource.addItem((Entity) item);%0D%0A                                ((DatasourceImplementation) datasource).setModified(modified);%0D%0A                            %7D
 %0D%0A  

Set browser datasource modified = false after Create operation