Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 052988e53cb5c5dc464731506014814ecc11f481 (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
/*******************************************************************************
 * 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.core.ui.internal.wizards;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.maven.model.Model;
import org.apache.maven.model.Parent;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.CheckStateChangedEvent;
import org.eclipse.jface.viewers.CheckboxTreeViewer;
import org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider;
import org.eclipse.jface.viewers.ICheckStateListener;
import org.eclipse.jface.viewers.IColorProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.wizard.IWizardPage;
import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.embedder.MavenModelManager;
import org.eclipse.m2e.core.internal.lifecyclemapping.discovery.LifecycleMappingConfiguration;
import org.eclipse.m2e.core.project.AbstractProjectScanner;
import org.eclipse.m2e.core.project.LocalProjectScanner;
import org.eclipse.m2e.core.project.MavenProjectInfo;
import org.eclipse.m2e.core.project.ProjectImportConfiguration;
import org.eclipse.m2e.core.ui.internal.Messages;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.IWorkingSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Maven Import Wizard Page
 * 
 * @author Eugene Kuleshov
 */
public class MavenImportWizardPage extends AbstractMavenWizardPage {
  private static final Logger log = LoggerFactory.getLogger(MavenImportWizardPage.class);

  static final Object[] EMPTY = new Object[0];

  protected Combo rootDirectoryCombo;

  protected CheckboxTreeViewer projectTreeViewer;

  private List<String> locations;

  private IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();

  private WorkingSetGroup workingSetGroup;

  private boolean showLocation = true;

  private final List<IWorkingSet> workingSets;

  private String rootDirectory;

  private String loadingErrorMessage;

  protected MavenImportWizardPage(ProjectImportConfiguration importConfiguration, List<IWorkingSet> workingSets) {
    super("MavenProjectImportWizardPage", importConfiguration); //$NON-NLS-1$
    this.workingSets = workingSets;
    setTitle(org.eclipse.m2e.core.ui.internal.Messages.MavenImportWizardPage_title);
    setDescription(org.eclipse.m2e.core.ui.internal.Messages.MavenImportWizardPage_desc);
    setPageComplete(false);
  }

  public void setShowLocation(boolean showLocation) {
    this.showLocation = showLocation;
  }

  public void setLocations(List<String> locations) {
    this.locations = locations;
  }
  
  public void createControl(Composite parent) {
    Composite composite = new Composite(parent, SWT.NONE);
    composite.setLayout(new GridLayout(3, false));
    setControl(composite);

    if(showLocation || locations==null || locations.isEmpty()) {
      final Label selectRootDirectoryLabel = new Label(composite, SWT.NONE);
      selectRootDirectoryLabel.setLayoutData(new GridData());
      selectRootDirectoryLabel.setText(Messages.wizardImportPageRoot);

      rootDirectoryCombo = new Combo(composite, SWT.NONE);
      rootDirectoryCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
      rootDirectoryCombo.setFocus();
      addFieldWithHistory("rootDirectory", rootDirectoryCombo); //$NON-NLS-1$
      
      if(locations!=null && locations.size()==1) {
        rootDirectoryCombo.setText(locations.get(0));
        rootDirectory = locations.get(0);
      }

      final Button browseButton = new Button(composite, SWT.NONE);
      browseButton.setText(Messages.wizardImportPageBrowse);
      browseButton.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false));
      browseButton.addSelectionListener(new SelectionAdapter() {
        public void widgetSelected(SelectionEvent e) {
          DirectoryDialog dialog = new DirectoryDialog(getShell(), SWT.NONE);
          dialog.setText(Messages.wizardImportPageSelectRootFolder);
          String path = rootDirectoryCombo.getText();
          if(path.length()==0) {
            path = ResourcesPlugin.getWorkspace().getRoot().getLocation().toPortableString();
          }
          dialog.setFilterPath(path);

          String result = dialog.open();
          if(result != null) {
            rootDirectoryCombo.setText(result);
            if (rootDirectoryChanged()) {
              scanProjects();
            }
          }
        }
      });

      rootDirectoryCombo.addModifyListener(new ModifyListener() {
        public void modifyText(ModifyEvent e) {
//          if (rootDirectoryChanged()) {
//            scanProjects();
//          }
        }
      });
      rootDirectoryCombo.addFocusListener(new FocusAdapter() {
        public void focusLost(FocusEvent e) {
          if(rootDirectoryChanged()) {
            scanProjects();
          }
        }
      });
      rootDirectoryCombo.addSelectionListener(new SelectionAdapter() {
        public void widgetDefaultSelected(SelectionEvent e) {
          if(rootDirectoryChanged()) {
            scanProjects();
          }
        }

        public void widgetSelected(SelectionEvent e) {
          if(rootDirectoryChanged()) {
            //in runnable to have the combo popup collapse before disabling controls.
            Display.getDefault().asyncExec(new Runnable() {
              public void run() {
                scanProjects();
              }
            });
          }
        }
      });
    }

    final Label projectsLabel = new Label(composite, SWT.NONE);
    projectsLabel.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1));
    projectsLabel.setText(Messages.wizardImportPageProjects);

    projectTreeViewer = new CheckboxTreeViewer(composite, SWT.BORDER);

    projectTreeViewer.addCheckStateListener(new ICheckStateListener() {
      public void checkStateChanged(CheckStateChangedEvent event) {
        projectTreeViewer.setSubtreeChecked(event.getElement(), event.getChecked());
        updateCheckedState();
        getMappingConfiguration().setSelectedProjects(getProjects());
        setPageComplete();
      }
    });
    
    projectTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() {

      public void selectionChanged(SelectionChangedEvent event) {
        IStructuredSelection selection = (IStructuredSelection) event.getSelection();
        if (selection.getFirstElement() != null) {
          String  errorMsg = validateProjectInfo((MavenProjectInfo) selection.getFirstElement());
          if (errorMsg != null) {
            setMessage(errorMsg, IMessageProvider.WARNING);
          } else {
          //TODO if no error on current, shall show any existing general errors if found..
            setMessage(loadingErrorMessage, IMessageProvider.WARNING);
          }
        } else {
          //TODO if on current selection, shall show any existing general errors if existing..
          setMessage(loadingErrorMessage, IMessageProvider.WARNING);
        }
      }});

    projectTreeViewer.setContentProvider(new ITreeContentProvider() {

      public Object[] getElements(Object element) {
        if(element instanceof List) {
          @SuppressWarnings("unchecked")
          List<MavenProjectInfo> projects = (List<MavenProjectInfo>) element;
          return projects.toArray(new MavenProjectInfo[projects.size()]);
        }
        return EMPTY;
      }

      public Object[] getChildren(Object parentElement) {
        if(parentElement instanceof List) {
          @SuppressWarnings("unchecked")
          List<MavenProjectInfo> projects = (List<MavenProjectInfo>) parentElement;
          return projects.toArray(new MavenProjectInfo[projects.size()]);
        } else if(parentElement instanceof MavenProjectInfo) {
          MavenProjectInfo mavenProjectInfo = (MavenProjectInfo) parentElement;
          Collection<MavenProjectInfo> projects = mavenProjectInfo.getProjects();
          return projects.toArray(new MavenProjectInfo[projects.size()]);
        }
        return EMPTY;
      }

      public Object getParent(Object element) {
        return null;
      }

      public boolean hasChildren(Object parentElement) {
        if(parentElement instanceof List) {
          List<?> projects = (List<?>) parentElement;
          return !projects.isEmpty();
        } else if(parentElement instanceof MavenProjectInfo) {
          MavenProjectInfo mavenProjectInfo = (MavenProjectInfo) parentElement;
          return !mavenProjectInfo.getProjects().isEmpty();
        }
        return false;
      }

      public void dispose() {
      }

      public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
      }
    });

    projectTreeViewer.setLabelProvider(new DelegatingStyledCellLabelProvider(new ProjectLabelProvider()));

    final Tree projectTree = projectTreeViewer.getTree();
    GridData projectTreeData = new GridData(SWT.FILL, SWT.FILL, true, true, 2, 3);
    projectTreeData.heightHint = 250;
    projectTreeData.widthHint = 500;
    projectTree.setLayoutData(projectTreeData);

    final Button selectAllButton = new Button(composite, SWT.NONE);
    selectAllButton.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false));
    selectAllButton.setText(Messages.wizardImportPageSelectAll);
    selectAllButton.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        projectTreeViewer.expandAll();
        setAllChecked(true);
        // projectTreeViewer.setSubtreeChecked(projectTreeViewer.getInput(), true);
        validate();
      }
    });

    final Button deselectAllButton = new Button(composite, SWT.NONE);
    deselectAllButton.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, false));
    deselectAllButton.setText(Messages.wizardImportPageDeselectAll);
    deselectAllButton.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        setAllChecked(false);
        // projectTreeViewer.setSubtreeChecked(projectTreeViewer.getInput(), false);
        setPageComplete(false);
      }
    });

    final Button refreshButton = new Button(composite, SWT.NONE);
    refreshButton.setLayoutData(new GridData(SWT.FILL, SWT.TOP, false, true));
    refreshButton.setText(Messages.wizardImportPageRefresh);
    refreshButton.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        scanProjects();
      }
    });

    this.workingSetGroup = new WorkingSetGroup(composite, workingSets, getShell());
    
    createAdvancedSettings(composite, new GridData(SWT.FILL, SWT.TOP, false, false, 3, 1));
    resolverConfigurationComponent.template.addModifyListener(new ModifyListener(){
      public void modifyText(ModifyEvent arg0) {
        Display.getDefault().asyncExec(new Runnable() {
          public void run() {
            validate();
          }
        });
      }
    });
    
    if(locations!=null && !locations.isEmpty()) {
      scanProjects();
    }
  
  }

  protected boolean rootDirectoryChanged() {
    String _rootDirectory = rootDirectory;
    rootDirectory = rootDirectoryCombo.getText().trim();
    return _rootDirectory == null || !_rootDirectory.equals(rootDirectory);
  }

  public void dispose() {
    super.dispose();
    workingSetGroup.dispose();
  }
  
  protected void scanProjects() {
    final AbstractProjectScanner<MavenProjectInfo> projectScanner = getProjectScanner();
    try {
      getWizard().getContainer().run(true, true, new IRunnableWithProgress() {
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
          projectScanner.run(monitor);
          ((MavenImportWizard) getWizard()).scanProjects(getProjects(projectScanner.getProjects()), monitor);
        }
        
        //this collects all projects for analyzing..
        List<MavenProjectInfo> getProjects(Collection<MavenProjectInfo> input) {
          List<MavenProjectInfo> toRet = new ArrayList<MavenProjectInfo>();
          for (MavenProjectInfo info : input) {
            toRet.add(info);
            toRet.addAll(getProjects(info.getProjects()));
          }
          return toRet;
        }
        
      });

      projectTreeViewer.setInput(projectScanner.getProjects());
      projectTreeViewer.expandAll();
      // projectTreeViewer.setAllChecked(true);
      setAllChecked(true);
      setPageComplete();
      setErrorMessage(null);
      setMessage(null);
      loadingErrorMessage = null;
      LifecycleMappingConfiguration config = ((MavenImportWizard)getWizard()).getMappingConfiguration();
      
      //mkleint: XXX this sort of error handling is rather unfortunate
      
      List<Throwable> errors = new ArrayList<Throwable>(projectScanner.getErrors());
      if (config != null) {
        errors.addAll(config.getErrors().values());
      }
      if(!errors.isEmpty()) {
        StringBuffer sb = new StringBuffer(NLS.bind(Messages.wizardImportPageScanningErrors, errors.size()));
        int n = 1;
        for(Throwable ex : errors) {
          if(ex instanceof CoreException) {
            String msg = ((CoreException) ex).getStatus().getMessage();
            sb.append("\n  ").append(n).append(" ").append(msg.trim()); //$NON-NLS-1$ //$NON-NLS-2$
            
          } else {
            String msg = ex.getMessage()==null ? ex.toString() : ex.getMessage();
            sb.append("\n  ").append(n).append(" ").append(msg.trim()); //$NON-NLS-1$ //$NON-NLS-2$
          }
          n++;
        }
        loadingErrorMessage = sb.toString();
        setMessage(sb.toString(), IMessageProvider.WARNING);
      }
      
    } catch(InterruptedException ex) {
      // canceled

    } catch(InvocationTargetException ex) {
      Throwable e = ex.getCause() == null ? ex : ex.getCause();
      String msg;
      if(e instanceof CoreException) {
        msg = e.getMessage();
        log.error(msg, e);
      } else {
        msg = "Scanning error " + projectScanner.getDescription() + "; " + e.toString(); //$NON-NLS-2$
        log.error(msg, e);
      }
      projectTreeViewer.setInput(null);
      setPageComplete(false);
      setErrorMessage(msg);
    }
  }

  void setAllChecked(boolean state) {
    @SuppressWarnings("unchecked")
    List<MavenProjectInfo> input = (List<MavenProjectInfo>) projectTreeViewer.getInput();
    if(input!=null) {
      for(MavenProjectInfo mavenProjectInfo : input) {
        projectTreeViewer.setSubtreeChecked(mavenProjectInfo, state);
      }
      updateCheckedState();
    }
  }
  
  void updateCheckedState() {
    Object[] elements = projectTreeViewer.getCheckedElements();
    for(int i = 0; i < elements.length; i++ ) {
      Object element = elements[i];
      if(element instanceof MavenProjectInfo) {
        MavenProjectInfo info = (MavenProjectInfo) element;
        if(isWorkspaceFolder(info) || isAlreadyExists(info)) {
          projectTreeViewer.setChecked(info, false);
        }
      }
    }
  }

  boolean isWorkspaceFolder(MavenProjectInfo info) {
    if(info!=null) {
      File pomFile = info.getPomFile();
      if(pomFile != null) {
        File parentFile = pomFile.getParentFile();
        if(parentFile.getAbsolutePath().equals(workspaceRoot.getLocation().toFile().getAbsolutePath())) {
          return true;
        }
      }
    }
    return false;
  }  
  
  boolean isAlreadyExists(MavenProjectInfo info) {
    if(info!=null) {
      IWorkspace workspace = ResourcesPlugin.getWorkspace();
      String name = getImportConfiguration().getProjectName(info.getModel());
      if (name != null && name.length() > 0) {
        IProject project = workspace.getRoot().getProject(name);
        return project.exists();
      }
    }
    return false;
  }
  
  /**
   * this will iterate all existing projects and return true 
   * if the absolute location URI of the old (imported) and new (to-be-imported) projects match
   * @param info
   * @return
   */
  boolean isAlreadyImported(MavenProjectInfo info) {
    if(info!=null) {
      IWorkspace workspace = ResourcesPlugin.getWorkspace();
      for (IProject project : workspace.getRoot().getProjects()) {
        URI mavenuri = info.getPomFile().getParentFile().toURI();
        //mkleint: this is sort of heuristic blah blah code. unfortunately for some reason the 
        // URI returned by the eclipse code in project.getLocationURI() differs by the ending / character from the 
        // java.io.File code. That results in failing match of the URIs. I've blah it by removing the ending slash.
        // please tell me there is a more sane solution!
        if (mavenuri.toString().endsWith("/")) { //$NON-NLS-1$
          try {
            mavenuri = new URI(mavenuri.toString().substring(0, mavenuri.toString().length() - 1));
          } catch(URISyntaxException ex) {
            log.error(ex.getMessage(), ex);
          }
        }
        boolean ok =  project.exists() && project.getLocationURI().equals(mavenuri);
        if (ok) {
          return true;
        }
      }
    }
    return false;
  }  

  protected AbstractProjectScanner<MavenProjectInfo> getProjectScanner() {
    File root = workspaceRoot.getLocation().toFile();
    MavenModelManager modelManager = MavenPlugin.getMavenModelManager();
    if(showLocation) {
      String location = rootDirectoryCombo.getText().trim();
      if (location.length() > 0) {
        return new LocalProjectScanner(root, location, false, modelManager);
      }
    } else if(locations != null && !locations.isEmpty()) {
      return new LocalProjectScanner(root, locations, false, modelManager);
    }

    // empty scanner
    return new AbstractProjectScanner<MavenProjectInfo>() {
      @Override
      public String getDescription() {
        return ""; //$NON-NLS-1$
      }

      @Override
      public void run(IProgressMonitor monitor) throws InterruptedException {
      }
    };
  }

  /**
   * @return collection of <code>MavenProjectInfo</code>
   */
  public Collection<MavenProjectInfo> getProjects() {
    Collection<MavenProjectInfo> checkedProjects = new ArrayList<MavenProjectInfo>();
    for(Object o : projectTreeViewer.getCheckedElements()) {
      checkedProjects.add((MavenProjectInfo) o);
    }

    return checkedProjects;
  }

  /**
   * 
   * @param info
   * @return
   */
  protected String validateProjectInfo(MavenProjectInfo info) {
    if(info!=null) {
      if(isWorkspaceFolder(info)) {
        String projectName = getImportConfiguration().getProjectName(info.getModel());
        return NLS.bind(Messages.wizardImportValidatorWorkspaceFolder, projectName); 
      } else if(isAlreadyImported(info)) {
        String projectName = getImportConfiguration().getProjectName(info.getModel());
        return NLS.bind(Messages.wizardImportValidatorProjectImported, projectName); 
      } else if(isAlreadyExists(info)) {
        String projectName = getImportConfiguration().getProjectName(info.getModel());
        return NLS.bind(Messages.wizardImportValidatorProjectExists, projectName); 
      }
    }
    return null;
  }

  protected void validate() {
    Object[] elements = projectTreeViewer.getCheckedElements();
    for(int i = 0; i < elements.length; i++ ) {
      Object element = elements[i];
      if(element instanceof MavenProjectInfo) {
        String errorMsg = validateProjectInfo((MavenProjectInfo) element); 
        if (errorMsg != null) {
          setPageComplete(false);
          return;
        }
      }
    }
    setMessage(null);
    setPageComplete();
    projectTreeViewer.refresh();
  }

  @Override
  public IWizardPage getNextPage() {
    IWizardPage next = super.getNextPage();
    LifecycleMappingConfiguration config = getMappingConfiguration();
    if (config == null || (config.isMappingComplete(true) && config.getAllProposals().isEmpty())) {
      next = null;
    }
    return next;
  }

  void setPageComplete() {
    Object[] checkedElements = projectTreeViewer.getCheckedElements();
    setPageComplete(checkedElements != null && checkedElements.length > 0);
  }

  LifecycleMappingConfiguration getMappingConfiguration() {
    return ((MavenImportWizard) getWizard()).getMappingConfiguration();
  }

  /**
   * ProjectLabelProvider
   */
  class ProjectLabelProvider extends LabelProvider implements IColorProvider, DelegatingStyledCellLabelProvider.IStyledLabelProvider {

    public String getText(Object element) {
      if(element instanceof MavenProjectInfo) {
        MavenProjectInfo info = (MavenProjectInfo) element;
        
        if(info.getProfiles().isEmpty()) {
          return info.getLabel() + " - " + getId(info); //$NON-NLS-1$
        }
        
        return info.getLabel() + " - " + getId(info) + "  " + info.getProfiles(); //$NON-NLS-1$ //$NON-NLS-2$
      }
      return super.getText(element);
    }

    private String getId(MavenProjectInfo info) {
      Model model = info.getModel();
      
      String groupId = model.getGroupId();
      String artifactId = model.getArtifactId();
      String version = model.getVersion();
      String packaging = model.getPackaging();

      Parent parent = model.getParent();

      if(groupId==null && parent!=null) {
        groupId = parent.getGroupId();
      }
      if(groupId==null) {
        groupId = org.eclipse.m2e.core.ui.internal.Messages.MavenImportWizardPage_inherited;
      }
      
      if(version==null && parent!=null) {
        version = parent.getVersion();
      }
      if(version==null) {
        version = org.eclipse.m2e.core.ui.internal.Messages.MavenImportWizardPage_inherited;
      }

      return groupId + ":" + artifactId + ":" + version + ":" + packaging; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.viewers.IColorProvider#getBackground(java.lang.Object)
     */
    public Color getForeground(Object element) {
      if(element instanceof MavenProjectInfo) {
        MavenProjectInfo info = (MavenProjectInfo) element;
        if(isWorkspaceFolder(info)) {
          return Display.getDefault().getSystemColor(SWT.COLOR_RED);
        } else if(isAlreadyExists(info)) {
          return Display.getDefault().getSystemColor(SWT.COLOR_GRAY);
        }
      }
      return null;
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.viewers.IColorProvider#getForeground(java.lang.Object)
     */
    public Color getBackground(Object element) {
      return null;
    }

    /* (non-Javadoc)
     * @see org.eclipse.jface.viewers.DelegatingStyledCellLabelProvider.IStyledLabelProvider#getStyledText(java.lang.Object)
     */
    public StyledString getStyledText(Object element) {
      if(element instanceof MavenProjectInfo) {
        MavenProjectInfo info = (MavenProjectInfo) element;
        StyledString ss = new StyledString();
        ss.append(info.getLabel() + "  ");
        ss.append(getId(info), StyledString.DECORATIONS_STYLER);
        if (! info.getProfiles().isEmpty()) {
          ss.append(" - " + info.getProfiles(), StyledString.QUALIFIER_STYLER );
        }
        return ss;
      }
      return null;
    }

  }

}

Back to the top