Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e9e6b570f151f84fc95890950847e0f7da8992de (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*******************************************************************************
 * Copyright (c) 2008-2010 Sonatype, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *      Sonatype, Inc. - initial API and implementation
 *******************************************************************************/

package org.eclipse.m2e.editor.xml;

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

import java.io.IOException;

import org.apache.maven.project.MavenProject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.ICompletionProposalExtension4;
import org.eclipse.jface.text.contentassist.ICompletionProposalExtension5;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;

import org.eclipse.m2e.core.internal.index.IIndex;
import org.eclipse.m2e.core.internal.index.IndexedArtifactFile;
import org.eclipse.m2e.core.ui.internal.dialogs.MavenRepositorySearchDialog;
import org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation;
import org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple;
import org.eclipse.m2e.editor.xml.internal.Messages;
import org.eclipse.m2e.editor.xml.internal.XmlUtils;

public class InsertArtifactProposal implements ICompletionProposal, ICompletionProposalExtension4, ICompletionProposalExtension5 {
  private static final Logger log = LoggerFactory.getLogger(InsertArtifactProposal.class);

  private ISourceViewer sourceViewer;
  private Region region;
  private int generatedLength = 0;
  private int generatedOffset;
  private Configuration config;

  public InsertArtifactProposal(ISourceViewer sourceViewer, Region region, Configuration config) {
    this.sourceViewer = sourceViewer;
    this.region = region;
    generatedOffset = region.getOffset();
    this.config = config;
    assert config.getType() != null;
  }

  public void apply(IDocument document) {
    MavenProject prj = XmlUtils.extractMavenProject(sourceViewer);
    IProject eclPrj = XmlUtils.extractProject(sourceViewer);
    MavenRepositorySearchDialog dialog = null;
    if (config.getType() == SearchType.PLUGIN) {
      String path = XmlUtils.pathUp(config.getCurrentNode(), 2);
      boolean inPM = path.contains("pluginManagement");   //$NON-NLS-1$
      dialog = MavenRepositorySearchDialog.createSearchPluginDialog(sourceViewer.getTextWidget().getShell(),
          config.getType().getWindowTitle(), prj, eclPrj, inPM);
    }
    if (config.getType() == SearchType.PARENT) {
      dialog = MavenRepositorySearchDialog.createSearchParentDialog(sourceViewer.getTextWidget().getShell(),
          config.getType().getWindowTitle(), prj, eclPrj);
    }
    if (config.getType() == SearchType.DEPENDENCY) {
      //only populate the lists when in dependency search..
      // and when in dependency management or plugin section use the different set than elsewhere to get different visual effect.
      String path = XmlUtils.pathUp(config.getCurrentNode(), 2);
      boolean inDM = path.contains(DEPENDENCY_MANAGEMENT);
      dialog = MavenRepositorySearchDialog.createSearchDependencyDialog(sourceViewer.getTextWidget().getShell(),
          config.getType().getWindowTitle(), prj, eclPrj, inDM);
    }
    if (dialog == null) {
      throw new IllegalStateException("Wrong search type: " + config.getType());
    }
    if (config.getInitiaSearchString() != null) {
      dialog.setQuery(config.getInitiaSearchString());
    }
    final MavenRepositorySearchDialog fDialog = dialog;
    if(dialog.open() == Window.OK) {
      final IndexedArtifactFile af = (IndexedArtifactFile) dialog.getFirstResult();
      int offset = region.getOffset();
      if(af != null) {
        if (config.getType() == SearchType.PARENT) {
          try {
              final int fOffset = offset;
              performOnDOMDocument(new OperationTuple(document, new Operation() {
                public void process(Document doc) {
                  Element parent = insertAt(doc.createElement(PARENT), fOffset);
                  setText(getChild(parent, GROUP_ID), af.group);
                  setText(getChild(parent, ARTIFACT_ID), af.artifact);
                  setText(getChild(parent, VERSION), af.version);
                  String relativePath = PomContentAssistProcessor.findRelativePath(sourceViewer, af.group, af.artifact, af.version);
                  if (relativePath != null) {
                    setText(getChild(parent, RELATIVE_PATH), relativePath);
                  }
                  format(parent);
                  generatedOffset = ((IndexedRegion)parent).getStartOffset();
                  generatedLength = ((IndexedRegion)parent).getEndOffset() - generatedOffset;
                }
              }));
          } catch(IOException e) {
            log.error("Failed inserting parent element", e); //$NON-NLS-1$
          } catch(CoreException e) {
            log.error("Failed inserting parent element", e); //$NON-NLS-1$
          }
        }

        // plugin type
        
        if(config.getType() == SearchType.PLUGIN) {
          try {
            final int fOffset = offset;
            performOnDOMDocument(new OperationTuple(document, new Operation() {
              public void process(Document doc) {
                Element currentNode = elementAtOffset(doc, fOffset);
                if (currentNode == null) {
                  return;
                }
                String currentName = currentNode.getNodeName();
                Element plugin = null;
                Element toFormat = null;
                if("project".equals(currentName)) { //$NON-NLS-1$
                  Element build = findChild(currentNode, BUILD);
                  if(build == null) {
                    build = insertAt(doc.createElement(BUILD), fOffset);
                    toFormat = build;
                  }
                  plugin = createElement(getChild(build, PLUGINS), PLUGIN);
                }
                if(BUILD.equals(currentName) || PLUGIN_MANAGEMENT.equals(currentName)) { //$NON-NLS-1$ //$NON-NLS-2$
                  Element plugins = findChild(currentNode, PLUGINS);
                  if(plugins == null) {
                    plugins = insertAt(doc.createElement(PLUGINS), fOffset);
                    toFormat = plugins;
                  }
                  plugin = createElement(plugins, PLUGIN);
                }
                if(PLUGINS.equals(currentName)) {
                  plugin = insertAt(doc.createElement(PLUGIN), fOffset);
                }
                if (toFormat == null) {
                  toFormat = plugin;
                }
                setText(getChild(plugin, GROUP_ID), af.group);
                setText(getChild(plugin, ARTIFACT_ID), af.artifact);
                if (af.version != null) {
                  setText(getChild(plugin, VERSION), af.version);
                }
                format(toFormat);
                generatedOffset = ((IndexedRegion)toFormat).getStartOffset();
                generatedLength = ((IndexedRegion)toFormat).getEndOffset() - generatedOffset;
              }
            }));
          } catch(IOException e) {
            log.error("Failed inserting plugin element", e); //$NON-NLS-1$
          } catch(CoreException e) {
            log.error("Failed inserting plugin element", e); //$NON-NLS-1$
          }
        }
          // dependency type
          
          if (config.getType() == SearchType.DEPENDENCY) {
            try {
              final int fOffset = offset;
              performOnDOMDocument(new OperationTuple(document, new Operation() {
                public void process(Document doc) {
                  Element currentNode = elementAtOffset(doc, fOffset);
                  if (currentNode == null) {
                    return;
                  }
                  String currentName = currentNode.getNodeName();
                  Element dependency = null;
                  Element toFormat = null;
                  if("project".equals(currentName) || DEPENDENCY_MANAGEMENT.equals(currentName) || PROFILE.equals(currentName)) { //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                    Element deps = findChild(currentNode, DEPENDENCIES);
                    if(deps == null) {
                      deps = insertAt(doc.createElement(DEPENDENCIES), fOffset);
                      toFormat = deps;
                    }
                    dependency = doc.createElement(DEPENDENCY);
                    deps.appendChild(dependency);
                  }
                  if(DEPENDENCIES.equals(currentName)) {
                    dependency = insertAt(doc.createElement(DEPENDENCY), fOffset);
                  }
                  if (toFormat == null) {
                    toFormat = dependency;
                  }
                  setText(getChild(dependency, GROUP_ID), af.group);
                  setText(getChild(dependency, ARTIFACT_ID), af.artifact);
                  if (af.version != null) {
                    setText(getChild(dependency, VERSION), af.version);
                  }
                  if (fDialog.getSelectedScope() != null && !"compile".equals(fDialog.getSelectedScope())) {
                    setText(getChild(dependency, SCOPE), fDialog.getSelectedScope());
                  }
                  format(toFormat);
                  generatedOffset = ((IndexedRegion)toFormat).getStartOffset();
                  generatedLength = ((IndexedRegion)toFormat).getEndOffset() - generatedOffset;
                }
              }));
            } catch(IOException e) {
              log.error("Failed inserting dependency element", e); //$NON-NLS-1$
            } catch(CoreException e) {
              log.error("Failed inserting dependency element", e); //$NON-NLS-1$
            }            
        }
      }
    }
  }
  
  public Point getSelection(IDocument document) {
    return new Point(generatedOffset, generatedLength);
  }

  public String getAdditionalProposalInfo() {
    return null; //not to be used anymore
  }

  public String getDisplayString() {
    return config.getType().getDisplayName(); 
  }

  public Image getImage() {
    return config.getType().getImage();
  }

  public IContextInformation getContextInformation() {
    // TODO Auto-generated method stub
    return null;
  }

  public boolean isAutoInsertable() {
    return false;
  }

  public Object getAdditionalProposalInfo(IProgressMonitor monitor) {
    return config.getType().getAdditionalInfo();
  }
  
  /**
   * supported search types
   * @author mkleint
   *
   */
  public static enum SearchType {
    
    PARENT(IIndex.SEARCH_PARENTS, Messages.InsertArtifactProposal_searchDialog_title, Messages.InsertArtifactProposal_display_name, MvnImages.IMG_OPEN_POM, Messages.InsertArtifactProposal_additionals), 
    PLUGIN(IIndex.SEARCH_PLUGIN, Messages.InsertArtifactProposal_insert_plugin_title, Messages.InsertArtifactProposal_insert_plugin_display_name, MvnImages.IMG_OPEN_POM, Messages.InsertArtifactProposal_insert_plugin_description),
    DEPENDENCY(IIndex.SEARCH_ARTIFACT, Messages.InsertArtifactProposal_insert_dep_title, Messages.InsertArtifactProposal_insert_dep_display_name, MvnImages.IMG_OPEN_POM, Messages.InsertArtifactProposal_insert_dep_desc);
    
    private final String type;
    private final String windowTitle;
    private final String displayName;
    private final Image image;
    private final String additionalInfo;
    private SearchType(String type, String windowTitle, String dn, Image img, String addInfo) {
      this.type = type;
      this.windowTitle = windowTitle;
      this.displayName = dn;
      this.image = img;
      this.additionalInfo = addInfo;
    }
    
    String getIIndexType() {
      return type;
    }

    public String getWindowTitle() {
      return windowTitle;
    }

    public String getDisplayName() {
      return displayName;
    }

    public Image getImage() {
      return image;
    }

    public String getAdditionalInfo() {
      return additionalInfo;
    }
    
  }
  
  public static class Configuration {
    private final SearchType type;
    private String initiaSearchString;
    private Node node;
    
    public Configuration(SearchType type) {
      this.type = type;
    }
    
    public void setInitiaSearchString(String initiaSearchString) {
      this.initiaSearchString = initiaSearchString;
    }
    public String getInitiaSearchString() {
      return initiaSearchString;
    }
    public SearchType getType() {
      return type;
    }

    public void setCurrentNode(Node node) {
      this.node = node;
    }

    public Node getCurrentNode() {
      return node;
    }
  }

}

Back to the top