Package org.jbpm.ui.editor.ltk

Source Code of org.jbpm.ui.editor.ltk.SubprocessPresentation$SubprocessChange

package org.jbpm.ui.editor.ltk;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.IRegion;
import org.eclipse.ltk.core.refactoring.Change;
import org.eclipse.ltk.core.refactoring.NullChange;
import org.eclipse.ltk.core.refactoring.RefactoringStatus;
import org.eclipse.ltk.core.refactoring.TextEditBasedChange;
import org.eclipse.ltk.core.refactoring.TextEditBasedChangeGroup;
import org.eclipse.ltk.ui.refactoring.TextEditChangeNode;
import org.jbpm.ui.common.model.Subprocess;
import org.jbpm.ui.util.VariableMapping;

public class SubprocessPresentation extends Presentation {
    private final Subprocess subprocess;

    public SubprocessPresentation(final Subprocess subprocess, String variableName, String replacement) {
        super(variableName, replacement);
        this.subprocess = subprocess;
    }

    @Override
    public List<Change> getChanges() throws Exception {
        List<VariableMapping> mappingsToChange = new ArrayList<VariableMapping>();
        for (VariableMapping mapping : subprocess.getVariablesList()) {
            if (mapping.getProcessVariable().equals(variableName)) {
                mappingsToChange.add(mapping);
            }
        }
       
        List<Change> changes = new ArrayList<Change>();
        if (mappingsToChange.size() > 0) {
            changes.add(new SubprocessChange(mappingsToChange));
        }
        return changes;
    }
   
    private class SubprocessChange extends TextEditBasedChange {
        private final List<VariableMapping> mappingsToChange;

        protected SubprocessChange(List<VariableMapping> mappingsToChange) {
            super(subprocess.getName());
            this.mappingsToChange = mappingsToChange;
        }

        @Override
        public Object getAdapter(Class adapter) {
            if (adapter == TextEditChangeNode.class) {
                return new GPDChangeNode(this, Subprocess.class);
            }
            return super.getAdapter(adapter);
        }

        @Override
        public String getCurrentContent(IProgressMonitor pm) throws CoreException {
            return toXmlPreviewContent(variableName);
        }

        @Override
        public String getCurrentContent(IRegion region, boolean arg1, int arg2, IProgressMonitor pm) throws CoreException {
            throw new UnsupportedOperationException();
        }

        @Override
        public String getPreviewContent(IProgressMonitor pm) throws CoreException {
            return toXmlPreviewContent(replacement);
        }

        @Override
        public String getPreviewContent(TextEditBasedChangeGroup[] groups, IRegion region, boolean arg2, int arg3, IProgressMonitor pm) throws CoreException {
            throw new UnsupportedOperationException();
        }

        @Override
        public Object getModifiedElement() {
            return subprocess;
        }

        @Override
        public void initializeValidationData(IProgressMonitor pm) {
        }

        @Override
        public RefactoringStatus isValid(IProgressMonitor pm) {
            return RefactoringStatus.createInfoStatus("Ok");
        }

        @Override
        public Change perform(IProgressMonitor pm) throws CoreException {
            for (VariableMapping mapping : mappingsToChange) {
                mapping.setProcessVariable(replacement);
            }
            return new NullChange("Subprocess");
        }
       
        private String toXmlPreviewContent(String varName) {
            StringBuffer buffer = new StringBuffer();
            for (VariableMapping mapping : mappingsToChange) {
                buffer.append("<variable access=\"").append(mapping.getUsage()).append("\" mapped-name=\"").append(varName);
                buffer.append("\" name=\"").append(mapping.getSubprocessVariable()).append("\" />").append("\n");
            }
            return buffer.toString();
        }
    }
}
TOP

Related Classes of org.jbpm.ui.editor.ltk.SubprocessPresentation$SubprocessChange

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.