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());
}
}
}));
}
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);
}
}
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);
}
}
}
}
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);
}
}
}
}
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)));
}
}
Aggregations