com.siteview.mde.internal.ui.wizards.tools.ConvertedProjectsPage.java Source code

Java tutorial

Introduction

Here is the source code for com.siteview.mde.internal.ui.wizards.tools.ConvertedProjectsPage.java

Source

/*******************************************************************************
 *  Copyright (c) 2000, 2008 IBM Corporation and others.
 *  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:
 *     IBM Corporation - initial API and implementation
 *     Les Jones <lesojones@gamil.com> - bug 205361
 *******************************************************************************/
package com.siteview.mde.internal.ui.wizards.tools;

import java.lang.reflect.InvocationTargetException;
import java.util.Vector;
import org.eclipse.core.resources.IProject;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.*;
import org.eclipse.jface.wizard.WizardPage;
import com.siteview.mde.internal.ui.*;
import com.siteview.mde.internal.ui.elements.DefaultContentProvider;
import com.siteview.mde.internal.ui.parts.WizardCheckboxTablePart;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;

public class ConvertedProjectsPage extends WizardPage {
    private CheckboxTableViewer projectViewer;
    private TablePart tablePart;
    private IProject[] fSelected;
    private IProject[] fUnconverted;

    public class ProjectContentProvider extends DefaultContentProvider implements IStructuredContentProvider {
        public Object[] getElements(Object parent) {
            if (fUnconverted != null)
                return fUnconverted;
            return new Object[0];
        }
    }

    public class ProjectLabelProvider extends LabelProvider implements ITableLabelProvider {
        public String getColumnText(Object obj, int index) {
            if (index == 0)
                return ((IProject) obj).getName();
            return ""; //$NON-NLS-1$
        }

        public Image getColumnImage(Object obj, int index) {
            return PlatformUI.getWorkbench().getSharedImages().getImage(IDE.SharedImages.IMG_OBJ_PROJECT);
        }
    }

    class TablePart extends WizardCheckboxTablePart {
        public TablePart(String mainLabel) {
            super(mainLabel);
        }

        public void updateCounter(int count) {
            super.updateCounter(count);
            setPageComplete(count > 0);
        }
    }

    public ConvertedProjectsPage(IProject[] projects, Vector initialSelection) {
        super("convertedProjects"); //$NON-NLS-1$
        setTitle(MDEUIMessages.ConvertedProjectWizard_title);
        setDescription(MDEUIMessages.ConvertedProjectWizard_desc);
        tablePart = new TablePart(MDEUIMessages.ConvertedProjectWizard_projectList);
        this.fSelected = (IProject[]) initialSelection.toArray(new IProject[initialSelection.size()]);
        this.fUnconverted = projects;
    }

    public void createControl(Composite parent) {
        Composite container = new Composite(parent, SWT.NONE);
        GridLayout layout = new GridLayout();
        layout.numColumns = 2;
        layout.marginHeight = 0;
        layout.marginWidth = 5;
        container.setLayout(layout);

        tablePart.createControl(container);

        projectViewer = tablePart.getTableViewer();
        projectViewer.setContentProvider(new ProjectContentProvider());
        projectViewer.setLabelProvider(new ProjectLabelProvider());
        projectViewer.setInput(MDEPlugin.getWorkspace());

        tablePart.setSelection(fSelected);
        tablePart.updateCounter(fSelected.length);

        setControl(container);
        Dialog.applyDialogFont(container);
        PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IHelpContextIds.CONVERTED_PROJECTS);
    }

    public boolean finish() {
        final Object[] selected = tablePart.getSelection();

        IProject[] projectsToConvert = castSelectionToProjects(selected);

        try {
            IRunnableWithProgress convertOperation;
            convertOperation = new ConvertProjectToPluginOperation(projectsToConvert);
            getContainer().run(false, true, convertOperation);
        } catch (InvocationTargetException e) {
            MDEPlugin.logException(e);
            return false;
        } catch (InterruptedException e) {
            MDEPlugin.logException(e);
            return false;
        }
        return true;
    }

    /**
     * Convert the selected Object[] into an IProject[]
     * @param selected Selected projects in an Object []
     * @return the same selected projects in an IProject []
     */
    private IProject[] castSelectionToProjects(Object[] selected) {

        if (selected instanceof IProject[]) {
            return (IProject[]) selected;
        }

        IProject[] projects = new IProject[selected.length];

        for (int i = 0; i < selected.length; i++) {
            projects[i] = (IProject) selected[i];
        }

        return projects;
    }

}