/*******************************************************************************
 * Copyright (c) 2014 Codenvy, S.A.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Codenvy, S.A. - initial API and implementation
 *******************************************************************************/
package com.codenvy.eclipse.ui.wizard.exporter.pages;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.eclipse.jface.dialogs.IPageChangedListener;
import org.eclipse.jface.dialogs.PageChangedEvent;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;

import com.codenvy.eclipse.client.model.Workspace.WorkspaceRef;
import com.codenvy.eclipse.ui.CodenvyUIPlugin;
import com.codenvy.eclipse.ui.Images;
import com.codenvy.eclipse.ui.wizard.common.CredentialsProviderWizard;
import com.codenvy.eclipse.ui.wizard.common.jobs.LoadWorkspacesJob;

/**
 * @author Stéphane Daviet
 */
public class WorkspaceWizardPage extends WizardPage implements IPageChangedListener {
    private ListViewer workspaceListViewer;

    public WorkspaceWizardPage() {
        super(WorkspaceWizardPage.class.getSimpleName());

        setTitle("Codenvy Workspaces");
        setDescription("Select Codenvy workspace where project should be imported.");
        setImageDescriptor(CodenvyUIPlugin.getDefault().getImageRegistry().getDescriptor(Images.WIZARD_LOGO));
        setPageComplete(false);
    }

    @Override
    public void createControl(Composite parent) {
        final Composite wizardContainer = new Composite(parent, SWT.NONE);
        wizardContainer.setLayout(new GridLayout(2, false));
        wizardContainer.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        final Label workspaceTableLabel = new Label(wizardContainer, SWT.NONE);
        workspaceTableLabel.setText("Remote Codenvy Workspaces:");
        workspaceTableLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 2, 1));

        workspaceListViewer = new ListViewer(wizardContainer, SWT.BORDER | SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL);
        workspaceListViewer.setLabelProvider(new LabelProvider() {
            @Override
            public String getText(Object element) {
                return element instanceof WorkspaceRef ? ((WorkspaceRef)element).name : super.getText(element);
            }
        });
        workspaceListViewer.setContentProvider(ArrayContentProvider.getInstance());
        workspaceListViewer.getControl().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        workspaceListViewer.addSelectionChangedListener(new ISelectionChangedListener() {
            @Override
            public void selectionChanged(SelectionChangedEvent event) {
                setPageComplete(!workspaceListViewer.getSelection().isEmpty());
            }
        });

        setControl(wizardContainer);
    }

    @Override
    public void pageChanged(PageChangedEvent event) {
        if (isCurrentPage()) {
            workspaceListViewer.setInput(null);
            loadWorkspaces();
            setPageComplete(!workspaceListViewer.getSelection().isEmpty());
        }
    }

    public WorkspaceRef getSelectedWorkspace() {
        return (WorkspaceRef)((IStructuredSelection)workspaceListViewer.getSelection()).getFirstElement();
    }

    /**
     * Method used to load the workspaces asynchronously when the wizard page is displayed.
     */
    private void loadWorkspaces() {
        try {

            getContainer().run(true, false, new LoadWorkspacesJob(getWizard()) {
                @Override
                public void postLoadCallback(List<WorkspaceRef> workspaceRefs) {
                    workspaceListViewer.setInput(workspaceRefs.toArray());
                    if (!workspaceRefs.isEmpty()) {
                        workspaceListViewer.setSelection(new StructuredSelection(workspaceRefs.get(0)));
                    }
                    workspaceListViewer.refresh();
                }
            });

        } catch (InvocationTargetException | InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public CredentialsProviderWizard getWizard() {
        return (CredentialsProviderWizard)super.getWizard();
    }
}

@@ -969,53 +969,8 @@
 er;%0A
-import org.eclipse.jface.viewers.ListViewer;%0A
 impo
@@ -1067,32 +1067,78 @@
 turedSelection;%0A
+import org.eclipse.jface.viewers.TableViewer;%0A
 import org.eclip
@@ -1778,20 +1778,21 @@
 private 
-List
+Table
 Viewer w
@@ -1795,28 +1795,29 @@
 er workspace
-List
+Table
 Viewer;%0A%0A   
@@ -2728,28 +2728,29 @@
    workspace
-List
+Table
 Viewer = new
@@ -2750,20 +2750,21 @@
 r = new 
-List
+Table
 Viewer(w
@@ -2832,16 +2832,37 @@
 V_SCROLL
+ %7C SWT.FULL_SELECTION
 );%0A     
@@ -2865,36 +2865,37 @@
        workspace
-List
+Table
 Viewer.setLabelP
@@ -3145,36 +3145,37 @@
        workspace
-List
+Table
 Viewer.setConten
@@ -3230,36 +3230,37 @@
        workspace
-List
+Table
 Viewer.getContro
@@ -3316,33 +3316,32 @@
 , true, true));%0A
-%0A
         workspac
@@ -3337,28 +3337,29 @@
    workspace
-List
+Table
 Viewer.addSe
@@ -3543,36 +3543,37 @@
 plete(!workspace
-List
+Table
 Viewer.getSelect
@@ -3773,36 +3773,37 @@
        workspace
-List
+Table
 Viewer.setInput(
@@ -3869,36 +3869,37 @@
 plete(!workspace
-List
+Table
 Viewer.getSelect
@@ -4039,28 +4039,29 @@
 on)workspace
-List
+Table
 Viewer.getSe
@@ -4465,36 +4465,37 @@
        workspace
-List
+Table
 Viewer.setInput(
@@ -4601,28 +4601,29 @@
    workspace
-List
+Table
 Viewer.setSe
@@ -4729,20 +4729,21 @@
 orkspace
-List
+Table
 Viewer.r

Modify WorkspaceWizardPage table selection style