Search in sources :

Example 11 with OperationTuple

use of org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple in project grails-ide by spring-attic.

the class GrailsMavenTests method addDependency.

// borrowed from org.eclipse.m2e.core.ui.internal.actions.AddDependencyAction
private void addDependency(final Dependency dependency, IFile file) throws IOException, CoreException {
    PomEdits.performOnDOMDocument(new OperationTuple(file, new Operation() {

        public void process(Document document) {
            Element depsEl = PomEdits.getChild(document.getDocumentElement(), PomEdits.DEPENDENCIES);
            Element dep = PomEdits.findChild(depsEl, PomEdits.DEPENDENCY, PomEdits.childEquals(PomEdits.GROUP_ID, dependency.getGroupId()), PomEdits.childEquals(PomEdits.ARTIFACT_ID, dependency.getArtifactId()));
            if (dep == null) {
                dep = PomHelper.createDependency(depsEl, dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion());
            } else {
                // only set version if already exists
                if (dependency.getVersion() != null) {
                    PomEdits.setText(PomEdits.getChild(dep, PomEdits.VERSION), dependency.getVersion());
                }
            }
            if (// 
            dependency.getType() != null && // //$NON-NLS-1$
            !"jar".equals(dependency.getType()) && !"null".equals(dependency.getType())) {
                // guard against MNGECLIPSE-622 //$NON-NLS-1$
                PomEdits.setText(PomEdits.getChild(dep, PomEdits.TYPE), dependency.getType());
            }
            if (dependency.getClassifier() != null) {
                PomEdits.setText(PomEdits.getChild(dep, PomEdits.CLASSIFIER), dependency.getClassifier());
            }
            if (dependency.getScope() != null && !"compile".equals(dependency.getScope())) {
                // $NON-NLS-1$
                PomEdits.setText(PomEdits.getChild(dep, PomEdits.SCOPE), dependency.getScope());
            }
        }
    }));
}
Also used : OperationTuple(org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple) Element(org.w3c.dom.Element) Operation(org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation) Document(org.w3c.dom.Document)

Example 12 with OperationTuple

use of org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple in project m2e-core by eclipse-m2e.

the class OverviewPage method addSelectedModules.

private void addSelectedModules(Object[] result, boolean updateParentSection) {
    final String[] vals = new String[3];
    try {
        performOnDOMDocument(new OperationTuple(getPomEditor().getDocument(), document -> {
            Element root = document.getDocumentElement();
            String grid = getTextValue(findChild(root, GROUP_ID));
            Element parent = findChild(root, PARENT);
            if (grid == null) {
                grid = getTextValue(findChild(parent, GROUP_ID));
            }
            String artifactId = getTextValue(findChild(root, ARTIFACT_ID));
            String version = getTextValue(findChild(root, VERSION));
            if (version == null) {
                version = getTextValue(findChild(parent, VERSION));
            }
            vals[0] = grid;
            vals[1] = artifactId;
            vals[2] = version;
        }, true));
    } catch (Exception ex) {
        LOG.error("Error getting values from document", ex);
    }
    final String parentGroupId = vals[0];
    final String parentArtifactId = vals[1];
    final String parentVersion = vals[2];
    final IPath projectPath = getProject().getLocation();
    for (Object selection : result) {
        IContainer container = null;
        IFile pomFile = null;
        if (selection instanceof IFile) {
            pomFile = (IFile) selection;
            if (!IMavenConstants.POM_FILE_NAME.equals(pomFile.getName())) {
                continue;
            }
            container = pomFile.getParent();
        } else if (selection instanceof IContainer && !selection.equals(getProject())) {
            container = (IContainer) selection;
            pomFile = container.getFile(new Path(IMavenConstants.POM_FILE_NAME));
        }
        if (pomFile == null || !pomFile.exists() || container == null) {
            continue;
        }
        IPath resultPath = container.getLocation();
        String path = resultPath.makeRelativeTo(projectPath).toString();
        if (updateParentSection) {
            final String relativePath = projectPath.makeRelativeTo(resultPath).toString();
            try {
                performOnDOMDocument(new OperationTuple(pomFile, (Operation) document -> {
                    Element root = document.getDocumentElement();
                    Element parent = getChild(root, PARENT);
                    setText(getChild(parent, GROUP_ID), parentGroupId);
                    setText(getChild(parent, ARTIFACT_ID), parentArtifactId);
                    setText(getChild(parent, VERSION), parentVersion);
                    setText(getChild(parent, RELATIVE_PATH), relativePath);
                    Element grId = findChild(root, GROUP_ID);
                    String grIdText = getTextValue(grId);
                    if (grIdText != null && grIdText.equals(parentGroupId)) {
                        removeChild(root, grId);
                    }
                    Element ver = findChild(root, VERSION);
                    String verText = getTextValue(ver);
                    if (verText != null && verText.equals(parentVersion)) {
                        removeChild(root, ver);
                    }
                }));
            } catch (Exception e) {
                LOG.error("Error updating parent reference in file:" + pomFile, e);
            }
        }
        createNewModule(path);
    }
}
Also used : Notification(org.eclipse.emf.common.notify.Notification) MavenModuleWizard(org.eclipse.m2e.core.ui.internal.wizards.MavenModuleWizard) DND(org.eclipse.swt.dnd.DND) CoreException(org.eclipse.core.runtime.CoreException) DropTargetEvent(org.eclipse.swt.dnd.DropTargetEvent) RELATIVE_PATH(org.eclipse.m2e.core.ui.internal.editing.PomEdits.RELATIVE_PATH) Document(org.w3c.dom.Document) Composite(org.eclipse.swt.widgets.Composite) SCM(org.eclipse.m2e.core.ui.internal.editing.PomEdits.SCM) PomEdits.removeChild(org.eclipse.m2e.core.ui.internal.editing.PomEdits.removeChild) Text(org.eclipse.swt.widgets.Text) M2EUIUtils(org.eclipse.m2e.core.ui.internal.util.M2EUIUtils) IEditorInput(org.eclipse.ui.IEditorInput) PlatformUI(org.eclipse.ui.PlatformUI) Set(java.util.Set) Status(org.eclipse.core.runtime.Status) ResourceTransfer(org.eclipse.ui.part.ResourceTransfer) Transfer(org.eclipse.swt.dnd.Transfer) Window(org.eclipse.jface.window.Window) NAME(org.eclipse.m2e.core.ui.internal.editing.PomEdits.NAME) PomEdits.createElementWithText(org.eclipse.m2e.core.ui.internal.editing.PomEdits.createElementWithText) MavenImages(org.eclipse.m2e.core.ui.internal.MavenImages) PomEdits.removeIfNoChildElement(org.eclipse.m2e.core.ui.internal.editing.PomEdits.removeIfNoChildElement) Path(org.eclipse.core.runtime.Path) SWT(org.eclipse.swt.SWT) CI_MANAGEMENT(org.eclipse.m2e.core.ui.internal.editing.PomEdits.CI_MANAGEMENT) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Operation(org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation) SelectionListener(org.eclipse.swt.events.SelectionListener) ISSUE_MANAGEMENT(org.eclipse.m2e.core.ui.internal.editing.PomEdits.ISSUE_MANAGEMENT) URL(org.eclipse.m2e.core.ui.internal.editing.PomEdits.URL) PARENT(org.eclipse.m2e.core.ui.internal.editing.PomEdits.PARENT) StringLabelProvider(org.eclipse.m2e.editor.composites.StringLabelProvider) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) PomEdits(org.eclipse.m2e.core.ui.internal.editing.PomEdits) PomEdits.getChild(org.eclipse.m2e.core.ui.internal.editing.PomEdits.getChild) ArrayList(java.util.ArrayList) IContainer(org.eclipse.core.resources.IContainer) IMavenConstants(org.eclipse.m2e.core.internal.IMavenConstants) ExpansionEvent(org.eclipse.ui.forms.events.ExpansionEvent) INCEPTION_YEAR(org.eclipse.m2e.core.ui.internal.editing.PomEdits.INCEPTION_YEAR) VERSION(org.eclipse.m2e.core.ui.internal.editing.PomEdits.VERSION) MavenRepositorySearchDialog(org.eclipse.m2e.core.ui.internal.dialogs.MavenRepositorySearchDialog) IFile(org.eclipse.core.resources.IFile) GridData(org.eclipse.swt.layout.GridData) OpenPomAction(org.eclipse.m2e.core.ui.internal.actions.OpenPomAction) LinkedHashSet(java.util.LinkedHashSet) Section(org.eclipse.ui.forms.widgets.Section) DefaultArtifact(org.apache.maven.artifact.DefaultArtifact) DefaultArtifactHandler(org.apache.maven.artifact.handler.DefaultArtifactHandler) FileEditorInput(org.eclipse.ui.part.FileEditorInput) StackLayout(org.eclipse.swt.custom.StackLayout) ListEditorContentProvider(org.eclipse.m2e.editor.composites.ListEditorContentProvider) FormUtils.setText(org.eclipse.m2e.editor.pom.FormUtils.setText) PROPERTIES(org.eclipse.m2e.core.ui.internal.editing.PomEdits.PROPERTIES) Plugin(org.apache.maven.model.Plugin) ModifyListener(org.eclipse.swt.events.ModifyListener) DocumentBuilder(javax.xml.parsers.DocumentBuilder) MODULES(org.eclipse.m2e.core.ui.internal.editing.PomEdits.MODULES) MavenEditorImages(org.eclipse.m2e.editor.MavenEditorImages) GridLayout(org.eclipse.swt.layout.GridLayout) ArtifactKey(org.eclipse.m2e.core.embedder.ArtifactKey) LoggerFactory(org.slf4j.LoggerFactory) Hyperlink(org.eclipse.ui.forms.widgets.Hyperlink) JarFile(java.util.jar.JarFile) HyperlinkEvent(org.eclipse.ui.forms.events.HyperlinkEvent) MavenModuleSelectionDialog(org.eclipse.m2e.editor.dialogs.MavenModuleSelectionDialog) PomEdits.getTextValue(org.eclipse.m2e.core.ui.internal.editing.PomEdits.getTextValue) IStatus(org.eclipse.core.runtime.IStatus) PomEdits.performOnDOMDocument(org.eclipse.m2e.core.ui.internal.editing.PomEdits.performOnDOMDocument) DropTargetAdapter(org.eclipse.swt.dnd.DropTargetAdapter) MavenPluginActivator(org.eclipse.m2e.core.internal.MavenPluginActivator) IPath(org.eclipse.core.runtime.IPath) ARTIFACT_ID(org.eclipse.m2e.core.ui.internal.editing.PomEdits.ARTIFACT_ID) OperationTuple(org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple) TAG(org.eclipse.m2e.core.ui.internal.editing.PomEdits.TAG) Artifact(org.apache.maven.artifact.Artifact) ListEditorComposite(org.eclipse.m2e.editor.composites.ListEditorComposite) PomEdits.setText(org.eclipse.m2e.core.ui.internal.editing.PomEdits.setText) CCombo(org.eclipse.swt.custom.CCombo) Messages(org.eclipse.m2e.editor.internal.Messages) PomEdits.format(org.eclipse.m2e.core.ui.internal.editing.PomEdits.format) NLS(org.eclipse.osgi.util.NLS) WidthGroup(org.eclipse.m2e.core.ui.internal.wizards.WidthGroup) Display(org.eclipse.swt.widgets.Display) IManagedForm(org.eclipse.ui.forms.IManagedForm) MODULE(org.eclipse.m2e.core.ui.internal.editing.PomEdits.MODULE) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) List(java.util.List) ExpansionAdapter(org.eclipse.ui.forms.events.ExpansionAdapter) PomEdits.textEquals(org.eclipse.m2e.core.ui.internal.editing.PomEdits.textEquals) WizardDialog(org.eclipse.jface.wizard.WizardDialog) SYSTEM(org.eclipse.m2e.core.ui.internal.editing.PomEdits.SYSTEM) Label(org.eclipse.swt.widgets.Label) ExpandableComposite(org.eclipse.ui.forms.widgets.ExpandableComposite) PomEdits.findChilds(org.eclipse.m2e.core.ui.internal.editing.PomEdits.findChilds) ArtifactHandler(org.apache.maven.artifact.handler.ArtifactHandler) CONNECTION(org.eclipse.m2e.core.ui.internal.editing.PomEdits.CONNECTION) ORGANIZATION(org.eclipse.m2e.core.ui.internal.editing.PomEdits.ORGANIZATION) Image(org.eclipse.swt.graphics.Image) PACKAGING(org.eclipse.m2e.core.ui.internal.editing.PomEdits.PACKAGING) HashSet(java.util.HashSet) ProposalUtil(org.eclipse.m2e.core.ui.internal.util.ProposalUtil) JarEntry(java.util.jar.JarEntry) DESCRIPTION(org.eclipse.m2e.core.ui.internal.editing.PomEdits.DESCRIPTION) GROUP_ID(org.eclipse.m2e.core.ui.internal.editing.PomEdits.GROUP_ID) Node(org.w3c.dom.Node) ArtifactRepository(org.apache.maven.artifact.repository.ArtifactRepository) PomEdits.findChild(org.eclipse.m2e.core.ui.internal.editing.PomEdits.findChild) FormUtils.nvl(org.eclipse.m2e.editor.pom.FormUtils.nvl) ICellModifier(org.eclipse.jface.viewers.ICellModifier) Packaging(org.eclipse.m2e.core.ui.internal.search.util.Packaging) Logger(org.slf4j.Logger) NodeList(org.w3c.dom.NodeList) Job(org.eclipse.core.runtime.jobs.Job) IMavenProjectFacade(org.eclipse.m2e.core.project.IMavenProjectFacade) FormToolkit(org.eclipse.ui.forms.widgets.FormToolkit) Action(org.eclipse.jface.action.Action) PomEdits.childAt(org.eclipse.m2e.core.ui.internal.editing.PomEdits.childAt) ScrolledForm(org.eclipse.ui.forms.widgets.ScrolledForm) Element(org.w3c.dom.Element) ToolBarManager(org.eclipse.jface.action.ToolBarManager) IndexedArtifactFile(org.eclipse.m2e.core.internal.index.IndexedArtifactFile) HyperlinkAdapter(org.eclipse.ui.forms.events.HyperlinkAdapter) DEV_CONNECTION(org.eclipse.m2e.core.ui.internal.editing.PomEdits.DEV_CONNECTION) Control(org.eclipse.swt.widgets.Control) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) OperationTuple(org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) PomEdits.removeIfNoChildElement(org.eclipse.m2e.core.ui.internal.editing.PomEdits.removeIfNoChildElement) Element(org.w3c.dom.Element) Operation(org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation) IContainer(org.eclipse.core.resources.IContainer) CoreException(org.eclipse.core.runtime.CoreException)

Example 13 with OperationTuple

use of org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple in project m2e-core by eclipse-m2e.

the class AddDependencyAction method run.

@Override
public void run(IAction action) {
    IFile file = getPomFileFromPomEditorOrViewSelection();
    if (file == null) {
        return;
    }
    MavenProject mp = null;
    IProject prj = file.getProject();
    if (prj != null && IMavenConstants.POM_FILE_NAME.equals(file.getProjectRelativePath().toString())) {
        IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().getProject(prj);
        if (facade != null) {
            mp = facade.getMavenProject();
        }
    }
    MavenRepositorySearchDialog dialog = MavenRepositorySearchDialog.createSearchDependencyDialog(getShell(), Messages.AddDependencyAction_searchDialog_title, mp, prj, false);
    if (dialog.open() == Window.OK) {
        IndexedArtifactFile indexedArtifactFile = (IndexedArtifactFile) dialog.getFirstResult();
        if (indexedArtifactFile != null) {
            try {
                final Dependency dependency = indexedArtifactFile.getDependency();
                String selectedScope = dialog.getSelectedScope();
                dependency.setScope(selectedScope);
                if (indexedArtifactFile.version == null) {
                    dependency.setVersion(null);
                }
                performOnDOMDocument(new OperationTuple(file, (Operation) document -> {
                    Element depsEl = getChild(document.getDocumentElement(), DEPENDENCIES);
                    Element dep = findChild(depsEl, DEPENDENCY, childEquals(GROUP_ID, dependency.getGroupId()), childEquals(ARTIFACT_ID, dependency.getArtifactId()));
                    if (dep == null) {
                        dep = PomHelper.createDependency(depsEl, dependency.getGroupId(), dependency.getArtifactId(), dependency.getVersion());
                    } else {
                        if (dependency.getVersion() != null) {
                            setText(getChild(dep, VERSION), dependency.getVersion());
                        }
                    }
                    if (dependency.getType() != null && !"jar".equals(dependency.getType()) && !"null".equals(dependency.getType())) {
                        setText(getChild(dep, TYPE), dependency.getType());
                    }
                    if (dependency.getClassifier() != null) {
                        setText(getChild(dep, CLASSIFIER), dependency.getClassifier());
                    }
                    if (dependency.getScope() != null && !"compile".equals(dependency.getScope())) {
                        setText(getChild(dep, SCOPE), dependency.getScope());
                    }
                }));
            } catch (Exception ex) {
                String msg = NLS.bind(Messages.AddDependencyAction_error_msg, file);
                log.error(msg, ex);
                MessageDialog.openError(Display.getCurrent().getActiveShell(), Messages.AddDependencyAction_error_title, msg);
            }
        }
    }
}
Also used : MavenRepositorySearchDialog(org.eclipse.m2e.core.ui.internal.dialogs.MavenRepositorySearchDialog) IFile(org.eclipse.core.resources.IFile) OperationTuple(org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple) MavenProject(org.apache.maven.project.MavenProject) IndexedArtifactFile(org.eclipse.m2e.core.internal.index.IndexedArtifactFile) Element(org.w3c.dom.Element) Dependency(org.apache.maven.model.Dependency) Operation(org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation) IMavenProjectFacade(org.eclipse.m2e.core.project.IMavenProjectFacade) IProject(org.eclipse.core.resources.IProject)

Example 14 with OperationTuple

use of org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple in project m2e-core by eclipse-m2e.

the class AddPluginAction method run.

@Override
public void run(IAction action) {
    IFile file = getPomFileFromPomEditorOrViewSelection();
    if (file == null) {
        return;
    }
    MavenProject mp = null;
    IProject prj = file.getProject();
    if (prj != null && IMavenConstants.POM_FILE_NAME.equals(file.getProjectRelativePath().toString())) {
        IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().getProject(prj);
        if (facade != null) {
            mp = facade.getMavenProject();
        }
    }
    MavenRepositorySearchDialog dialog = MavenRepositorySearchDialog.createSearchPluginDialog(getShell(), Messages.AddPluginAction_searchDialog_title, mp, prj, false);
    if (dialog.open() == Window.OK) {
        final IndexedArtifactFile indexedArtifactFile = (IndexedArtifactFile) dialog.getFirstResult();
        if (indexedArtifactFile != null) {
            try {
                performOnDOMDocument(new OperationTuple(file, (Operation) document -> {
                    Element pluginsEl = getChild(document.getDocumentElement(), BUILD, PLUGINS);
                    PomHelper.createPlugin(pluginsEl, indexedArtifactFile.group, indexedArtifactFile.artifact, indexedArtifactFile.version);
                }));
            } catch (Exception ex) {
                // $NON-NLS-1$
                log.error("Can't add plugin to " + file, ex);
            }
        }
    }
}
Also used : MavenRepositorySearchDialog(org.eclipse.m2e.core.ui.internal.dialogs.MavenRepositorySearchDialog) IFile(org.eclipse.core.resources.IFile) OperationTuple(org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple) MavenProject(org.apache.maven.project.MavenProject) IndexedArtifactFile(org.eclipse.m2e.core.internal.index.IndexedArtifactFile) Element(org.w3c.dom.Element) Operation(org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation) IMavenProjectFacade(org.eclipse.m2e.core.project.IMavenProjectFacade) IProject(org.eclipse.core.resources.IProject)

Example 15 with OperationTuple

use of org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple in project m2e-core by eclipse-m2e.

the class LifecycleMappingResolution method performIgnore.

private void performIgnore(List<IMarker> markers) throws IOException, CoreException {
    final IFile[] pomFile = new IFile[1];
    PlatformUI.getWorkbench().getDisplay().syncExec(() -> {
        LifecycleMappingDialog dialog = new LifecycleMappingDialog(Display.getCurrent().getActiveShell(), (IFile) getMarker().getResource(), getMarker().getAttribute(IMavenConstants.MARKER_ATTR_GROUP_ID, ""), getMarker().getAttribute(IMavenConstants.MARKER_ATTR_ARTIFACT_ID, ""), getMarker().getAttribute(IMavenConstants.MARKER_ATTR_GOAL, ""));
        dialog.setBlockOnOpen(true);
        if (dialog.open() == Window.OK) {
            pomFile[0] = dialog.getPomFile();
        }
    });
    if (pomFile[0] != null) {
        performOnDOMDocument(new OperationTuple(pomFile[0], createOperation(markers)));
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) OperationTuple(org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple)

Aggregations

OperationTuple (org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple)30 Test (org.junit.Test)16 IFile (org.eclipse.core.resources.IFile)11 Element (org.w3c.dom.Element)11 CoreException (org.eclipse.core.runtime.CoreException)9 IStatus (org.eclipse.core.runtime.IStatus)8 IMavenProjectFacade (org.eclipse.m2e.core.project.IMavenProjectFacade)8 Operation (org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation)8 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)7 Status (org.eclipse.core.runtime.Status)7 Job (org.eclipse.core.runtime.jobs.Job)7 IProject (org.eclipse.core.resources.IProject)6 AddExclusionOperation (org.eclipse.m2e.core.ui.internal.editing.AddExclusionOperation)6 PomEdits.findChild (org.eclipse.m2e.core.ui.internal.editing.PomEdits.findChild)6 PomEdits.performOnDOMDocument (org.eclipse.m2e.core.ui.internal.editing.PomEdits.performOnDOMDocument)6 NLS (org.eclipse.osgi.util.NLS)6 List (java.util.List)5 ArtifactKey (org.eclipse.m2e.core.embedder.ArtifactKey)5 ARTIFACT_ID (org.eclipse.m2e.core.ui.internal.editing.PomEdits.ARTIFACT_ID)5 GROUP_ID (org.eclipse.m2e.core.ui.internal.editing.PomEdits.GROUP_ID)5