/*
 * Copyright 2014 Codenvy, S.A.
 *
 * 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.codenvy.ide.ext.datasource.client.editdatasource.celllist;

import com.codenvy.ide.ext.datasource.client.DatasourceUiResources;
import com.codenvy.ide.ext.datasource.client.DatasourceUiResources.DatasourceUiStyle;
import com.codenvy.ide.ext.datasource.shared.DatabaseConfigurationDTO;
import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.inject.Inject;

/**
 * Display of a datasource in the edit/delete dialog list.
 * 
 * @author "Mickaël Leduque"
 */
public class DatasourceCell extends AbstractCell<DatabaseConfigurationDTO> {

    private final DatasourceUiStyle style;
    private final CellTemplate      template;

    @Inject
    public DatasourceCell(final DatasourceUiResources resource,
                          final CellTemplate template) {
        this.style = resource.datasourceUiCSS();
        this.template = template;
    }

    @Override
    public void render(final Context context, final DatabaseConfigurationDTO value, final SafeHtmlBuilder sb) {
        if (value == null) {
            return;
        }

        final SafeHtml id = SafeHtmlUtils.fromString(value.getDatasourceId());
        final SafeHtml type = SafeHtmlUtils.fromString(value.getDatabaseType().toString());

        sb.append(this.template.datasourceItem(id,
                                               this.style.datasourceIdStyle(),
                                               type,
                                               this.style.datasourceTypeStyle(),
                                               this.style.datasourceIdCellStyle(),
                                               this.style.datasourceTypeCellStyle()));
    }

    /**
     * {@link SafeHtml} template for the datasource cell.
     * 
     * @author "Mickaël Leduque"
     */
    interface CellTemplate extends SafeHtmlTemplates {

        @Template("<table><tr><td class='{4}'> <span class='{1}'>{0}</span> </td><td class='{5}'> <span class='{3}'>{2}</span> </td></tr></table>")
        SafeHtml datasourceItem(SafeHtml datasourceId,
                                String idStyle,
                                SafeHtml datasourceType,
                                String typeStyle,
                                String idCellStyle,
                                String typeCellStyle);
    }
}

@@ -663,16 +663,62 @@
 llist;%0A%0A
+import javax.validation.constraints.NotNull;%0A%0A
 import c
@@ -863,16 +863,207 @@
 iStyle;%0A
+import com.codenvy.ide.ext.datasource.client.newdatasource.connector.NewDatasourceConnector;%0Aimport com.codenvy.ide.ext.datasource.client.newdatasource.connector.NewDatasourceConnectorAgent;%0A
 import c
@@ -1634,16 +1634,26 @@
 UiStyle 
+          
 style;%0A 
@@ -1687,24 +1687,96 @@
 te      
-template
+          template;%0A    private final NewDatasourceConnectorAgent connectorAgent
 ;%0A%0A    @
@@ -1810,24 +1810,33 @@
 eCell(final 
+@NotNull 
 DatasourceUi
@@ -1883,24 +1883,33 @@
       final 
+@NotNull 
 CellTemplate
@@ -1917,16 +1917,101 @@
 template
+,%0A                          final @NotNull NewDatasourceConnectorAgent connectorAgent
 ) %7B%0A    
@@ -2085,24 +2085,70 @@
 = template;%0A
+        this.connectorAgent = connectorAgent;%0A
     %7D%0A%0A    @
@@ -2426,82 +2426,468 @@
 al S
-afeHtml type = SafeHtmlUtils.fromString(value.getDatabaseType().toString()
+tring connectorId = value.getConfigurationConnectorId();%0A%0A        // for old datasources, the connector id was not recorded%0A%0A        NewDatasourceConnector connector = this.connectorAgent.getConnector(connectorId);%0A        String dbType;%0A        if (connector != null) %7B%0A            dbType = connector.getTitle();%0A        %7D else %7B%0A            dbType = value.getDatabaseType().toString();%0A        %7D%0A%0A        final SafeHtml type = SafeHtmlUtils.fromString(dbType
 );%0A%0A

Edit datasources : show the same database type as in the wizard - #123