package org.ovirt.engine.ui.common.widget.table.cell;

import java.util.List;

import org.ovirt.engine.core.common.businessentities.VM;
import org.ovirt.engine.ui.common.utils.ElementIdUtils;
import org.ovirt.engine.ui.common.widget.table.HasStyleClass;

import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.HasCell;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.CssResource;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;

public class StatusCompositeCellWithElementId extends CompositeCellWithElementId<VM> implements CellWithElementId<VM> {
    public interface StatusCompositeCellResources extends ClientBundle {
        @ClientBundle.Source("org/ovirt/engine/ui/common/css/StatusCompositeCell.css")
        StatusCompositeCellCss statusCompositeCellCss();
    }

    public interface StatusCompositeCellCss extends CssResource {
        String divInlineBlock();
    }

    public interface ContentTemplate extends SafeHtmlTemplates {
        @Template("<div id=\"{0}\">")
        SafeHtml id(String id);
    }

    private static final StatusCompositeCellResources RESOURCES = GWT.create(StatusCompositeCellResources.class);
    private final StatusCompositeCellCss style;
    private final List<HasCell<VM, ?>> hasCells;

    private ContentTemplate template;

    public StatusCompositeCellWithElementId(List<HasCell<VM, ?>> hasCells) {
        super(hasCells);
        this.hasCells = hasCells;
        style = RESOURCES.statusCompositeCellCss();
        style.ensureInjected();
    }

    ContentTemplate getTemplate() {
        if (template == null) {
            template = GWT.create(ContentTemplate.class);
        }
        return template;
    }

    @Override
    public void render(Cell.Context context, VM value, SafeHtmlBuilder sb) {
        sb.append(getTemplate().id(ElementIdUtils.createTableCellElementId(
                getElementIdPrefix(), getColumnId(), context)));

        for (HasCell<VM, ?> hasCell : hasCells) {
            render(context, value, sb, hasCell);
        }

        sb.appendHtmlConstant("</div>"); //$NON-NLS-1$
    }

    @Override
    protected <T> void render(Cell.Context context, VM value,
            SafeHtmlBuilder sb, HasCell<VM, T> hasCell) {
        Cell<T> cell = hasCell.getCell();
        if (cell instanceof HasStyleClass) {
            ((HasStyleClass) cell).setStyleClass(style.divInlineBlock());
        }
        cell.render(context, hasCell.getValue(value), sb);
    }

    @Override
    public boolean isEditing(Context context, Element parent, VM value) {
        return false;
    }

    @Override
    public boolean resetFocus(Context context, Element parent, VM value) {
        return false;
    }
}

@@ -76,65 +76,8 @@
 t;%0A%0A
-import org.ovirt.engine.core.common.businessentities.VM;%0A
 impo
@@ -666,16 +666,19 @@
 lementId
+%3CC%3E
  extends
@@ -705,18 +705,17 @@
 ementId%3C
-VM
+C
 %3E implem
@@ -737,18 +737,17 @@
 ementId%3C
-VM
+C
 %3E %7B%0A    
@@ -1399,34 +1399,33 @@
 al List%3CHasCell%3C
-VM
+C
 , ?%3E%3E hasCells;%0A
@@ -1517,26 +1517,25 @@
 ist%3CHasCell%3C
-VM
+C
 , ?%3E%3E hasCel
@@ -1913,26 +1913,25 @@
 xt context, 
-VM
+C
  value, Safe
@@ -2111,18 +2111,17 @@
 HasCell%3C
-VM
+C
 , ?%3E has
@@ -2327,18 +2327,17 @@
 ontext, 
-VM
+C
  value,%0A
@@ -2376,18 +2376,17 @@
 HasCell%3C
-VM
+C
 , T%3E has
@@ -2698,34 +2698,33 @@
 Element parent, 
-VM
+C
  value) %7B%0A      
@@ -2827,10 +2827,9 @@
 nt, 
-VM
+C
  val

modify StatusCompositeCellWithElementId to support any cell