Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 669a10e7ab6469dac70687ba5ad3e7aafe17c7de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*******************************************************************************
 * Copyright (c) 2012 Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.editor.xml;

import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.performOnDOMDocument;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;

import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple;
import org.eclipse.m2e.core.ui.internal.util.XmlUtils;
import org.eclipse.m2e.editor.pom.PomTemplateContext;
import org.eclipse.m2e.editor.xml.internal.Messages;
import org.eclipse.m2e.editor.xml.internal.dialogs.SPDXLicense;
import org.eclipse.m2e.editor.xml.internal.dialogs.SelectSPDXLicenseDialog;


public class InsertSPDXLicenseProposal implements ICompletionProposal {
  private static final Logger log = LoggerFactory.getLogger(InsertSPDXLicenseProposal.class);

  private final ITextViewer sourceViewer;

  private final Region region;

  private final PomTemplateContext context;

  private Point selection;

  public InsertSPDXLicenseProposal(ITextViewer sourceViewer, PomTemplateContext context, Region region) {
    this.sourceViewer = sourceViewer;
    this.context = context;
    this.region = region;
  }

  public void apply(IDocument document) {
    IProject project = XmlUtils.extractProject(sourceViewer);
    IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().getProject(project);
    SelectSPDXLicenseDialog dialog = new SelectSPDXLicenseDialog(sourceViewer.getTextWidget().getShell(), facade);

    if(dialog.open() == Window.OK) {
      final SPDXLicense license = dialog.getLicense();
      try {

        IMavenProjectFacade targetProject = dialog.getTargetProject();
        if(!targetProject.getPom().equals(facade.getPom())) {
          // add license to a parent
          AddLicensePomOperation operation = new AddLicensePomOperation(license, PomTemplateContext.PROJECT, null);
          performOnDOMDocument(new OperationTuple(targetProject.getPom(), operation));
        } else {
          AddLicensePomOperation operation = new AddLicensePomOperation(license, context, region);
          performOnDOMDocument(new OperationTuple(document, operation));
          this.selection = operation.getSelection();
        }

      } catch(CoreException e) {
        log.error("Failed inserting parent element", e); //$NON-NLS-1$
      } catch(IOException e) {
        log.error("Failed inserting parent element", e); //$NON-NLS-1$
      }
    }
  }

  public Point getSelection(IDocument document) {
    return selection;
  }

  public String getAdditionalProposalInfo() {
    return null;
  }

  public String getDisplayString() {
    return Messages.InsertSPDXLicenseProposal_0;
  }

  public Image getImage() {
    return MvnImages.IMG_LICENSE;
  }

  public IContextInformation getContextInformation() {
    return null;
  }

}

Back to the top