Package org.jbpm.ui.wizard

Source Code of org.jbpm.ui.wizard.ImportParWizardPage

package org.jbpm.ui.wizard;

import java.io.File;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.gef.EditPart;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jface.preference.FileFieldEditor;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.ListViewer;
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.jbpm.ui.PluginConstants;
import org.jbpm.ui.ProcessCache;
import org.jbpm.ui.resource.Messages;
import org.jbpm.ui.util.ProjectFinder;

public class ImportParWizardPage extends WizardPage {
    private final IStructuredSelection selection;

    private FileFieldEditor editor;

    private ListViewer projectsListViewer;

    public ImportParWizardPage(String pageName, IStructuredSelection selection) {
        super(pageName);
        this.selection = selection;
        setTitle(Messages.getString("ImportParWizardPage.page.title"));
        setDescription(Messages.getString("ImportParWizardPage.page.description"));
    }

    public void createControl(Composite parent) {
        Composite wholePageComposite = new Composite(parent, SWT.NONE);
        wholePageComposite.setLayout(new GridLayout(1, false));
        wholePageComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        Composite fileSelectionArea = new Composite(wholePageComposite, SWT.NONE);
        GridData fileSelectionData = new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL);
        fileSelectionArea.setLayoutData(fileSelectionData);

        GridLayout fileSelectionLayout = new GridLayout();
        fileSelectionLayout.numColumns = 3;
        fileSelectionLayout.makeColumnsEqualWidth = false;
        fileSelectionLayout.marginWidth = 0;
        fileSelectionLayout.marginHeight = 0;
        fileSelectionArea.setLayout(fileSelectionLayout);

        editor = new FileFieldEditor("fileSelect", Messages.getString("ImportParWizardPage.page.parFile"), fileSelectionArea);
        editor.setFileExtensions(new String[] { "*.par" });
        fileSelectionArea.moveAbove(null);

        createListGroup(wholePageComposite);

        setControl(wholePageComposite);
    }

    private void createListGroup(Composite parent) {
        // process selection
        projectsListViewer = new ListViewer(parent, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
        GridData gridData = new GridData(300, 300);
        gridData.horizontalAlignment = SWT.FILL;
        projectsListViewer.getControl().setLayoutData(gridData);
        projectsListViewer.setLabelProvider(new LabelProvider() {
            @Override
            public String getText(Object element) {
                return ((IProject) element).getName();
            }
        });
        projectsListViewer.setContentProvider(new ArrayContentProvider());
        projectsListViewer.setInput(ProjectFinder.getAllProjects());
        IProject project = getInitialJavaElement(selection);
        if (project != null) {
            projectsListViewer.setSelection(new StructuredSelection(project));
        }
    }

    private IProject getInitialJavaElement(IStructuredSelection selection) {
        if (selection != null && !selection.isEmpty()) {
            Object selectedElement = selection.getFirstElement();
            if (selectedElement instanceof EditPart) {
                return ProjectFinder.getCurrentProject();
            }
            if (selectedElement instanceof IAdaptable) {
                IAdaptable adaptable = (IAdaptable) selectedElement;
                IResource resource = (IResource) adaptable.getAdapter(IResource.class);
                if (resource != null) {
                    return resource.getProject();
                }
                IJavaElement javaElement = (IJavaElement) adaptable.getAdapter(IJavaElement.class);
                if (javaElement != null) {
                    return javaElement.getJavaProject().getProject();
                }
            }
        }
        return null;
    }

    public boolean performFinish() {
        try {
            File parFile = new File(editor.getStringValue());
            IStructuredSelection selectedProject = (IStructuredSelection) projectsListViewer.getSelection();
            IProject project = (IProject) selectedProject.getFirstElement();
            if (project == null) {
                throw new Exception(Messages.getString("ImportParWizardPage.error.selectTargetProject"));
            }
            if (!parFile.exists()) {
                throw new Exception(Messages.getString("ImportParWizardPage.error.selectValidPar"));
            }

            String parFileName = parFile.getName();
            String processName = parFileName.substring(0, parFileName.length() - 4);

            IFolder processFolder = project.getFolder("src/process/" + processName);
            if (processFolder.exists()) {
                throw new Exception(Messages.getString("ImportParWizardPage.error.processWithSameNameExists"));
            }
            processFolder.create(true, true, null);

            ZipFile zip = new ZipFile(parFile, ZipFile.OPEN_READ);
            Enumeration<? extends ZipEntry> entries = zip.entries();
            while (entries.hasMoreElements()) {
                ZipEntry entry = entries.nextElement();
                if (!entry.getName().contains("META-INF")) {
                    IFile file = processFolder.getFile(entry.getName());
                    InputStream entryStream = zip.getInputStream(entry);
                    file.create(entryStream, true, null);
                    file.setCharset(PluginConstants.UTF_ENCODING, null);
                    entryStream.close();
                }
            }
            IFile definitionFile = ProjectFinder.getProcessDefinitionFile(processFolder);
            ProcessCache.newProcessDefinitionWasCreated(definitionFile);
        } catch (Exception exception) {
            setErrorMessage(exception.getMessage());
            return false;
        }

        return true;
    }
}
TOP

Related Classes of org.jbpm.ui.wizard.ImportParWizardPage

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.