Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cc1a83fdfde2b04255b97199f7ed95e480294385 (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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/*******************************************************************************
 * Copyright (c) 2008 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
 *******************************************************************************/

package org.eclipse.m2e.core.ui.internal.dialogs;

import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

import org.apache.lucene.search.BooleanQuery;
import org.apache.maven.project.MavenProject;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.core.IMavenConstants;
import org.eclipse.m2e.core.core.MavenLogger;
import org.eclipse.m2e.core.index.IIndex;
import org.eclipse.m2e.core.index.IndexManager;
import org.eclipse.m2e.core.index.IndexedArtifact;
import org.eclipse.m2e.core.index.IndexedArtifactFile;
import org.eclipse.m2e.core.index.UserInputSearchExpression;
import org.eclipse.m2e.core.ui.internal.Messages;
import org.eclipse.m2e.core.ui.internal.search.util.Packaging;
import org.eclipse.m2e.core.ui.internal.util.M2EUIUtils;
import org.eclipse.m2e.core.ui.internal.util.ProposalUtil;
import org.eclipse.m2e.core.ui.internal.wizards.MavenPomSelectionComponent;
import org.eclipse.m2e.core.ui.internal.wizards.WidthGroup;
import org.eclipse.m2e.model.edit.pom.Dependency;
import org.eclipse.m2e.model.edit.pom.PomFactory;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;

import com.ibm.icu.text.DateFormat;


/**
 * A Dialog whose primary goal is to allow the user to select a dependency, either by entering the GAV coordinates
 * manually, or by search through a repository index.
 * 
 * @author rgould
 */
public class AddDependencyDialog extends AbstractMavenDialog {

  public static final String[] SCOPES = new String[] {"compile", "provided", "runtime", "test", "system"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$

  /*
   * dependencies under dependencyManagement are permitted to use an the extra "import" scope
   */
  public static final String[] DEP_MANAGEMENT_SCOPES = new String[] {"compile", "provided", "runtime", "test", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
      "system", "import"}; //$NON-NLS-1$ //$NON-NLS-2$

  protected static final String DIALOG_SETTINGS = AddDependencyDialog.class.getName();

  protected static final long SEARCH_DELAY = 500L; //in milliseconds

  protected String[] scopes;

  protected TreeViewer resultsViewer;

  protected Text queryText;

  protected Text groupIDtext;

  protected Text artifactIDtext;

  protected Text versionText;

  protected Text infoTextarea;

  protected Combo scopeCombo;

  protected java.util.List<Dependency> dependencies;

  protected WidthGroup widthGroup;

  /*
   * Stores selected files from the results viewer. These are later
   * converted into the above dependencies when OK is pressed.
   */
  protected java.util.List<IndexedArtifactFile> artifactFiles;

  protected SearchJob currentSearch;

  protected IProject project;

  protected IStatus lastStatus;

  protected SelectionListener resultsListener;

  protected boolean updating;

  private final MavenProject mavenProject;

  private final boolean isForDependencyManagement;

  private Set<String> managedKeys;

  private Set<String> existingKeys;

  /**
   * The AddDependencyDialog differs slightly in behaviour depending on context. If it is being used to apply a
   * dependency under the "dependencyManagement" context, the extra "import" scope is available. Set @param
   * isForDependencyManagement to true if this is case.
   * 
   * @param parent
   * @param isForDependencyManagement
   * @param project the project which contains this POM. Used for looking up indices
   */
  public AddDependencyDialog(Shell parent, boolean isForDependencyManagement, IProject project, MavenProject mavenProject) {
    super(parent, DIALOG_SETTINGS);
    this.project = project;
    this.mavenProject = mavenProject;
    
    this.isForDependencyManagement = isForDependencyManagement;

    setShellStyle(getShellStyle() | SWT.RESIZE);
    setTitle(Messages.AddDependencyDialog_title);
//    setStatusLineAboveButtons(true);

    if(!isForDependencyManagement) {
      this.scopes = SCOPES;
    } else {
      this.scopes = DEP_MANAGEMENT_SCOPES;
    }
  }

  /* (non-Javadoc)
   * @see org.eclipse.jface.dialogs.Dialog#createDialogArea()
   */
  protected Control createDialogArea(Composite parent) {
    readSettings();

    Composite composite = (Composite) super.createDialogArea(parent);

    widthGroup = new WidthGroup();
    composite.addControlListener(widthGroup);

    Composite gavControls = createGAVControls(composite);
    gavControls.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

    new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL).setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

    Composite searchControls = createSearchControls(composite);
    searchControls.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    updateStatus();
    return composite;
  }

  /**
   * Sets the up group-artifact-version controls
   */
  private Composite createGAVControls(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);

    GridLayout gridLayout = new GridLayout(4, false);
    gridLayout.marginWidth = 0;
    gridLayout.horizontalSpacing = 10;
    composite.setLayout(gridLayout);

    Label groupIDlabel = new Label(composite, SWT.NONE);
    groupIDlabel.setText(Messages.AddDependencyDialog_groupId_label);
    widthGroup.addControl(groupIDlabel);

    groupIDtext = new Text(composite, SWT.BORDER);
    groupIDtext.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    M2EUIUtils.addRequiredDecoration(groupIDtext);

    new Label(composite, SWT.NONE);
    new Label(composite, SWT.NONE);
    

    Label artifactIDlabel = new Label(composite, SWT.NONE);
    artifactIDlabel.setText(Messages.AddDependencyDialog_artifactId_label);
    widthGroup.addControl(artifactIDlabel);

    artifactIDtext = new Text(composite, SWT.BORDER);
    artifactIDtext.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    M2EUIUtils.addRequiredDecoration(artifactIDtext);

    new Label(composite, SWT.NONE);
    new Label(composite, SWT.NONE);

    Label versionLabel = new Label(composite, SWT.NONE);
    versionLabel.setText(Messages.AddDependencyDialog_version_label);
    widthGroup.addControl(versionLabel);

    versionText = new Text(composite, SWT.BORDER);
    versionText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    
    Label scopeLabel = new Label(composite, SWT.NONE);
    scopeLabel.setText(Messages.AddDependencyDialog_scope_label);

    scopeCombo = new Combo(composite, SWT.DROP_DOWN | SWT.READ_ONLY);
    scopeCombo.setItems(scopes);
    GridData scopeListData = new GridData(SWT.LEFT, SWT.CENTER, false, false);
    scopeCombo.setLayoutData(scopeListData);
    scopeCombo.setText(scopes[0]);

    /*
     * Fix the tab order (group -> artifact -> version -> scope)
     */
    composite.setTabList(new Control[] {groupIDtext, artifactIDtext, versionText, scopeCombo});

    ProposalUtil.addGroupIdProposal(project, groupIDtext, Packaging.ALL);
    ProposalUtil.addArtifactIdProposal(project, groupIDtext, artifactIDtext, Packaging.ALL);
    ProposalUtil.addVersionProposal(project, mavenProject, groupIDtext, artifactIDtext, versionText, Packaging.ALL);

    artifactIDtext.addModifyListener(new ModifyListener() {

      public void modifyText(ModifyEvent e) {
        updateInfo();
      }
    });

    groupIDtext.addModifyListener(new ModifyListener() {

      public void modifyText(ModifyEvent e) {
        updateInfo();
      }
    });

    return composite;
  }

  void updateInfo() {
//    infoTextarea.setText(""); //$NON-NLS-1$
    if(!updating) {
      resultsViewer.setSelection(StructuredSelection.EMPTY);
      updateStatus();
    }
 // TODO mkleint: for now just ignore.. 
//  this snippet is supposed to tell people that they selected dependency already in the
//  project    
    
//    if(dependencyNode == null) {
//      return;
//    }
//    dependencyNode.accept(new DependencyVisitor() {
//
//      public boolean visitLeave(DependencyNode node) {
//        if(node.getDependency() != null && node.getDependency().getArtifact() != null) {
//          Artifact artifact = node.getDependency().getArtifact();
//          if(artifact.getGroupId().equalsIgnoreCase(groupIDtext.getText().trim())
//              && artifact.getArtifactId().equalsIgnoreCase(artifactIDtext.getText().trim())) {
//            infoTextarea.setText(NLS.bind(Messages.AddDependencyDialog_info_transitive,
//                new String[] {artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion()}));
//          }
//          return false;
//        }
//        return true;
//      }
//
//      public boolean visitEnter(DependencyNode node) {
//        return true;
//      }
//    });

  }

  private Composite createSearchControls(Composite parent) {
    SashForm sashForm = new SashForm(parent, SWT.VERTICAL | SWT.SMOOTH);
    sashForm.setLayout(new FillLayout());

    Composite resultsComposite = new Composite(sashForm, SWT.NONE);
    GridLayout resultsLayout = new GridLayout(1, false);
    resultsLayout.marginWidth = 0;
    resultsComposite.setLayout(resultsLayout);

    Label queryLabel = new Label(resultsComposite, SWT.NONE);
    queryLabel.setText(Messages.AddDependencyDialog_search_label);
//    widthGroup.addControl(queryLabel);

    queryText = new Text(resultsComposite, SWT.BORDER | SWT.SEARCH);
    queryText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    queryText.setFocus();

//    queryText.setMessage(Messages.AddDependencyDialog_search_message);

    Label resultsLabel = new Label(resultsComposite, SWT.NONE);
    resultsLabel.setText(Messages.AddDependencyDialog_results_label);
    resultsLabel.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false));
//    widthGroup.addControl(resultsLabel);

    Tree resultsTree = new Tree(resultsComposite, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
    GridData treeData = new GridData(SWT.FILL, SWT.FILL, true, true);
    treeData.heightHint = 140;
    treeData.widthHint = 100;
    resultsTree.setLayoutData(treeData);

    Composite infoComposite = new Composite(sashForm, SWT.NONE);
    GridLayout infoLayout = new GridLayout(1, false);
    infoLayout.marginWidth = 0;
    infoComposite.setLayout(infoLayout);

    Label infoLabel = new Label(infoComposite, SWT.NONE);
    infoLabel.setText(Messages.AddDependencyDialog_info_label);
    infoLabel.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false));
//    widthGroup.addControl(infoLabel);

    infoTextarea = new Text(infoComposite, SWT.MULTI | SWT.READ_ONLY | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
    GridData infoData = new GridData(SWT.FILL, SWT.FILL, true, true);
    infoData.heightHint = 60;
    infoData.widthHint = 100;
    infoTextarea.setLayoutData(infoData);

    sashForm.setWeights(new int[] {70, 30});

    /*
     * Set up TreeViewer for search results
     */

    resultsViewer = new TreeViewer(resultsTree);
    resultsViewer.setContentProvider(new MavenPomSelectionComponent.SearchResultContentProvider());
    //TODO we want to have the artifacts marked for presence and management..
    managedKeys = new HashSet<String>();
    existingKeys = new HashSet<String>();
    if (mavenProject != null && mavenProject.getDependencyManagement() != null) {
      for (org.apache.maven.model.Dependency d : mavenProject.getDependencyManagement().getDependencies()) {
        managedKeys.add(d.getGroupId() + ":" + d.getArtifactId());
        managedKeys.add(d.getGroupId() + ":" + d.getArtifactId() + ":" + d.getVersion());
      }
    }
    if (isForDependencyManagement) {
      existingKeys = managedKeys;
      managedKeys = Collections.<String>emptySet();
    }
    resultsViewer.setLabelProvider(new DelegatingStyledCellLabelProvider(
        new MavenPomSelectionComponent.SearchResultLabelProvider(existingKeys, managedKeys,
            IIndex.SEARCH_ARTIFACT)));

    /*
     * Hook up events
     */

    resultsListener = new SelectionListener();
    resultsViewer.addSelectionChangedListener(resultsListener);

    queryText.addKeyListener(new KeyAdapter() {
      public void keyPressed(KeyEvent e) {
        if(e.keyCode == SWT.ARROW_DOWN) {
          resultsViewer.getTree().setFocus();
        }
      }
    });

    queryText.addModifyListener(new ModifyListener() {

      public void modifyText(ModifyEvent e) {
        search(queryText.getText());
      }
    });

    return sashForm;
  }

  /**
   * Just a short helper method to determine what to display in the text widgets when the user selects multiple objects
   * in the tree viewer. If the objects have the same value, then we should show that to them, otherwise we show
   * something like "(multiple selected)"
   * 
   * @param current
   * @param newValue
   * @return
   */
  String chooseWidgetText(String current, String newValue) {
    if(current == null) {
      return newValue;
    } else if(!current.equals(newValue)) {
      return Messages.AddDependencyDialog_multipleValuesSelected;
    }
    return current;
  }

  void appendFileInfo(final StringBuffer buffer, final IndexedArtifactFile file) {
    buffer.append(" * " + file.fname);
    if(file.size != -1) {
      buffer.append(", size: ");
      if((file.size / 1024 / 1024) > 0) {
        buffer.append((file.size / 1024 / 1024) + "MB");
      } else {
        buffer.append(Math.max(1, file.size / 1024) + "KB");
      }
    }
    buffer.append(", date: " + DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT).format(file.date));
    buffer.append("\n");

// TODO mkleint: for now just ignore.. 
//    this snippet is supposed to tell people that they selected dependency already in the
//    project    
    
//    if(dependencyNode != null) {
//      dependencyNode.accept(new DependencyVisitor() {
//
//        public boolean visitEnter(DependencyNode node) {
//          return true;
//        }
//
//        public boolean visitLeave(DependencyNode node) {
//          if(node.getDependency() == null || node.getDependency().getArtifact() == null) {
//            return true;
//          }
//          Artifact artifact = node.getDependency().getArtifact();
//          if(artifact.getGroupId().equalsIgnoreCase(file.group)
//              && artifact.getArtifactId().equalsIgnoreCase(file.artifact)) {
//            buffer.append(NLS.bind(Messages.AddDependencyDialog_transitive_dependency,
//                new String[] {artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion()}));
//            /*
//             * DependencyNodes don't know their parents. Determining which transitive dependency 
//             * is using the selected dependency is non trivial :(
//             */
//            return false;
//          }
//          return true;
//        }
//      });
//    }
  }

  protected void search(String query) {
    if(query == null || query.length() <= 2) {
      if(this.currentSearch != null) {
        this.currentSearch.cancel();
      }
    } else {
      IndexManager indexManager = MavenPlugin.getDefault().getIndexManager();

      if(this.currentSearch != null) {
        this.currentSearch.cancel();
      }

      this.currentSearch = new SearchJob(query.toLowerCase(), indexManager);
      this.currentSearch.schedule(SEARCH_DELAY);
    }
  }

  protected boolean isGroupAndArtifactPresent() {
    return groupIDtext.getText().trim().length() > 0 && artifactIDtext.getText().trim().length() > 0;
  }

  protected void updateStatus() {
    boolean enableOK = isGroupAndArtifactPresent() || (artifactFiles != null && artifactFiles.size() > 0);

    int severity = enableOK ? IStatus.OK : IStatus.ERROR;
    String message = enableOK ? "" : Messages.AddDependencyDialog_groupAndArtifactRequired; //$NON-NLS-1$

    if(lastStatus == null || lastStatus.getSeverity() != severity) {
      setInfo(severity, message);
    }
  }

  protected void updateStatus(IStatus status) {
    lastStatus = status;
    super.updateStatus(status);
  }

  /* (non-Javadoc)
   * @see org.eclipse.ui.dialogs.SelectionStatusDialog#computeResult()
   * This is called when OK is pressed. There's no obligation to do anything.
   */
  protected void computeResult() {
    String scope = scopeCombo.getText(); //$NON-NLS-1$

    if(artifactFiles == null || artifactFiles.size() == 1) {
      String type = "";
      String classifier = "";
      if (artifactFiles != null && artifactFiles.size() == 1) {
        // use the selected artifact props if available..
        IndexedArtifactFile file = artifactFiles.iterator().next();
        classifier = file.classifier;
        type = file.type;
      }
      Dependency dependency = createDependency(groupIDtext.getText().trim(), artifactIDtext.getText().trim(),
          versionText.getText().trim(), scope, type, classifier); //$NON-NLS-1$
      this.dependencies = Collections.singletonList(dependency);
    } else {
      this.dependencies = new LinkedList<Dependency>();
      for(IndexedArtifactFile file : artifactFiles) {
        Dependency dep = createDependency(file.group, file.artifact, managedKeys.contains(MavenPomSelectionComponent.getKey(file)) ? null : file.version, scope, file.type, file.classifier);
        this.dependencies.add(dep);
      }
    }
  }

  private Dependency createDependency(String groupID, String artifactID, String version, String scope, String type, String classifier) {
    Dependency dependency = PomFactory.eINSTANCE.createDependency();
    dependency.setGroupId(groupID);
    dependency.setArtifactId(artifactID);
    if (version != null) {
      dependency.setVersion(version);
    }
    dependency.setClassifier(classifier);

    /*
     * For scope and type, if the values are the default, don't save them.
     * This reduces clutter in the XML file (although forces people who don't
     * know what the defaults are to look them up).
     */
    dependency.setScope("compile".equals(scope) ? "" : scope); //$NON-NLS-1$ //$NON-NLS-2$
    dependency.setType("jar".equals(type) ? "" : type); //$NON-NLS-1$ //$NON-NLS-2$

    return dependency;
  }

  public java.util.List<Dependency> getDependencies() {
    return this.dependencies;
  }

  void setInfo(int status, String message) {
    updateStatus(new Status(status, IMavenConstants.PLUGIN_ID, message));
  }

  public final class SelectionListener implements ISelectionChangedListener {
    public void selectionChanged(SelectionChangedEvent event) {
      IStructuredSelection selection = (IStructuredSelection) event.getSelection();
      if(selection.isEmpty()) {
        infoTextarea.setText(""); //$NON-NLS-1$
        artifactFiles = null;
        updateStatus();
      } else {
        String artifact = null;
        String group = null;
        String version = null;

        artifactFiles = new LinkedList<IndexedArtifactFile>();
        StringBuffer buffer = new StringBuffer();
        Iterator iter = selection.iterator();
        while(iter.hasNext()) {
          Object obj = iter.next();
          IndexedArtifactFile file = null;
          boolean managed = false;
          if(obj instanceof IndexedArtifact) {
            //the idea here is that if we have a managed version for something, then the IndexedArtifact shall
            //represent that value..
            IndexedArtifact ia = (IndexedArtifact)obj;
            if (managedKeys.contains(MavenPomSelectionComponent.getKey(ia))) {
              for (IndexedArtifactFile f : ia.getFiles()) {
                if (managedKeys.contains(MavenPomSelectionComponent.getKey(f))) {
                  file = f;
                  managed = true;
                  break;
                }
              }
            }
            if (file == null) {
              file = ((IndexedArtifact) obj).getFiles().iterator().next();
            }
          } else {
            file = (IndexedArtifactFile) obj;
            if (managedKeys.contains(MavenPomSelectionComponent.getKey(file))) {
              managed = true;
            }
          }

          appendFileInfo(buffer, file);
          artifactFiles.add(file);

          artifact = chooseWidgetText(artifact, file.artifact);
          group = chooseWidgetText(group, file.group);
          version = chooseWidgetText(version, managed ? "" : file.version);
        }
        setInfo(OK, NLS.bind(artifactFiles.size() == 1 ? Messages.AddDependencyDialog_itemSelected
            : Messages.AddDependencyDialog_itemsSelected, artifactFiles.size()));
        infoTextarea.setText(buffer.toString());

        updating = true;
        artifactIDtext.setText(artifact);
        groupIDtext.setText(group);
        versionText.setText(version);
        updating = false;

        boolean enabled = !(artifactFiles.size() > 1);
        artifactIDtext.setEnabled(enabled);
        groupIDtext.setEnabled(enabled);
        versionText.setEnabled(enabled);
      }
    }
  }

  private class SearchJob extends Job {

    private String query;

    private IndexManager indexManager;

    private boolean cancelled = false;

    public SearchJob(String query, IndexManager indexManager) {
      super(NLS.bind(Messages.AddDependencyDialog_searchingFor, query));
      this.query = query;
      this.indexManager = indexManager;
    }

    /* (non-Javadoc)
     * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
     */
    protected IStatus run(IProgressMonitor monitor) {
      if(this.cancelled || resultsViewer == null || resultsViewer.getControl() == null
          || resultsViewer.getControl().isDisposed()) {
        return Status.CANCEL_STATUS;
      }

      try {
        setResults(IStatus.OK, Messages.AddDependencyDialog_searching, Collections.<String, IndexedArtifact> emptyMap());
        // TODO: before it was searching all indexes, but it should current project? (cstamas)
        // If not, the change getIndex(project) to getAllIndexes() and done
        // TODO: cstamas identified this as "user input", true?
        Map<String, IndexedArtifact> results = indexManager.getIndex(project).search(
            new UserInputSearchExpression(query), IIndex.SEARCH_ARTIFACT, IIndex.SEARCH_JARS + IIndex.SEARCH_TESTS);
        setResults(IStatus.OK, NLS.bind(Messages.AddDependencyDialog_searchDone, results.size()), results);
      } catch(BooleanQuery.TooManyClauses exception) {
        setResults(IStatus.ERROR, Messages.AddDependencyDialog_tooManyResults,
            Collections.<String, IndexedArtifact> emptyMap());
      } catch(RuntimeException exception) {
        setResults(IStatus.ERROR, NLS.bind(Messages.AddDependencyDialog_searchError, exception.toString()),
            Collections.<String, IndexedArtifact> emptyMap());
      } catch(CoreException ex) {
        setResults(IStatus.ERROR, NLS.bind(Messages.AddDependencyDialog_searchError, ex.getMessage()),
            Collections.<String, IndexedArtifact> emptyMap());
        MavenLogger.log(ex);
      }

      return Status.OK_STATUS;
    }

    /* (non-Javadoc)
     * @see org.eclipse.core.runtime.jobs.Job#canceling()
     */
    protected void canceling() {
      this.cancelled = true;
      super.canceling();
    }

    private void setResults(final int status, final String infoMessage, final Map<String, IndexedArtifact> results) {
      if(cancelled) {
        return;
      }

      Display.getDefault().syncExec(new Runnable() {

        public void run() {
          if(status == IStatus.OK) {
            infoTextarea.setText(infoMessage);
          } else {
            setInfo(status, infoMessage);
          }
          if(results != null && resultsViewer != null && resultsViewer.getControl() != null
              && !resultsViewer.getControl().isDisposed()) {
            resultsViewer.setInput(results);
          }
        }
      });
    }

  }
}

Back to the top