Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: eb34b89f8bac9149f8fb6aaba45de72fdb4d3175 (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
/*******************************************************************************
 * 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.wizards;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.project.MavenProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdapterManager;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ComboBoxCellEditor;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.m2e.core.internal.lifecyclemapping.discovery.ILifecycleMappingElementKey;
import org.eclipse.m2e.core.internal.lifecyclemapping.discovery.IMavenDiscoveryProposal;
import org.eclipse.m2e.core.internal.lifecyclemapping.discovery.IMavenDisovery;
import org.eclipse.m2e.core.internal.lifecyclemapping.discovery.MojoExecutionMappingConfiguration;
import org.eclipse.m2e.core.internal.lifecyclemapping.discovery.PackagingTypeMappingConfiguration;
import org.eclipse.m2e.core.internal.lifecyclemapping.discovery.ProjectLifecycleMappingConfiguration;
import org.eclipse.m2e.core.project.MavenProjectInfo;
import org.eclipse.m2e.core.project.ProjectImportConfiguration;
import org.eclipse.m2e.core.ui.internal.MavenImages;
import org.eclipse.m2e.core.ui.internal.lifecyclemapping.AggregateMappingLabelProvider;
import org.eclipse.m2e.core.ui.internal.lifecyclemapping.ILifecycleMappingLabelProvider;
import org.eclipse.m2e.core.ui.internal.lifecyclemapping.LifecycleMappingConfiguration;
import org.eclipse.m2e.core.ui.internal.lifecyclemapping.MojoExecutionMappingLabelProvider;
import org.eclipse.m2e.core.ui.internal.lifecyclemapping.PackagingTypeMappingLabelProvider;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;


/**
 * LifecycleMappingPage
 * 
 * @author igor
 */
@SuppressWarnings("restriction")
public class LifecycleMappingPage extends WizardPage {

  private static final int MAVEN_INFO_IDX = 0;

  private static final int ECLIPSE_INFO_IDX = 1;

  private LifecycleMappingConfiguration mappingConfiguration;

  private TreeViewer treeViewer;

  private IAdapterManager adapterManager;

  private Button btnNewButton;

  private boolean loading = true;

  /**
   * Create the wizard.
   */
  public LifecycleMappingPage() {
    super("LifecycleMappingPage");
    setTitle("Setup Maven plugin connectors");
    setDescription("Discover and map Eclipse plugins to Maven plugin goal executions.");
    adapterManager = Platform.getAdapterManager();
    setPageComplete(true);

  }

  /**
   * Create contents of the wizard.
   * 
   * @param parent
   */
  public void createControl(Composite parent) {
    Composite container = new Composite(parent, SWT.NULL);

    setControl(container);
    container.setLayout(new GridLayout(1, false));

    treeViewer = new TreeViewer(container, SWT.BORDER | SWT.FULL_SELECTION);
//    treeViewer.setUseHashlookup(true);

    Tree tree = treeViewer.getTree();
    tree.setLinesVisible(true);
    tree.setHeaderVisible(true);
    tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));

    TreeViewerColumn treeViewerColumn = new TreeViewerColumn(treeViewer, SWT.NONE);
    TreeColumn trclmnNewColumn = treeViewerColumn.getColumn();
    trclmnNewColumn.setText("Maven build");
    
    TreeViewerColumn columnViewerMapping = new TreeViewerColumn(treeViewer, SWT.NONE);
    TreeColumn columnMapping = columnViewerMapping.getColumn();
    columnMapping.setText("Eclipse build");

    TreeViewerColumn columnViewerAction = new TreeViewerColumn(treeViewer, SWT.NONE);
    TreeColumn columnAction = columnViewerAction.getColumn();
    columnAction.setText("Action");
//    columnViewerAction.setEditingSupport(new EditingSupport(treeViewer) {
//      
//      @Override
//      protected void setValue(Object element, Object value) {
//        if (element instanceof ILifecycleMappingLabelProvider) { 
//          ILifecycleMappingLabelProvider prov = (ILifecycleMappingLabelProvider)element;
//          Integer val = (Integer)value;
//          List<IMavenDiscoveryProposal> all = mappingConfiguration.getProposals(prov.getKey());
//          IMavenDiscoveryProposal sel = all.get(val.intValue());
//          IMavenDiscoveryProposal prop = mappingConfiguration.getSelectedProposal(prov.getKey());
//          if (prop != null) {
//            mappingConfiguration.removeSelectedProposal(prop);
//          }
//          if (sel != null) {
//            mappingConfiguration.addSelectedProposal(sel);
//          }
//        }
//      }
//      
//      @Override
//      protected Object getValue(Object element) {
//        if (element instanceof ILifecycleMappingLabelProvider) { 
//          ILifecycleMappingLabelProvider prov = (ILifecycleMappingLabelProvider)element;
//          IMavenDiscoveryProposal prop = mappingConfiguration.getSelectedProposal(prov.getKey());
//          List<IMavenDiscoveryProposal> all = mappingConfiguration.getProposals(prov.getKey());
//          return new Integer(all.indexOf(prop));
//        }
//        return new Integer(0);
//      }
//      
//      @Override
//      protected CellEditor getCellEditor(Object element) {
//        if (element instanceof ILifecycleMappingLabelProvider) { 
//          ILifecycleMappingLabelProvider prov = (ILifecycleMappingLabelProvider)element;
//          List<IMavenDiscoveryProposal> all = mappingConfiguration.getProposals(prov.getKey());
//          List<String> values = new ArrayList<String>();
//          for (IMavenDiscoveryProposal prop : all) {
//            values.add(NLS.bind("Install {0}", prop.toString()));
//          }
//          ComboBoxCellEditor edit = new ComboBoxCellEditor(treeViewer.getTree(), values.toArray(new String[0]));
//          return edit;
//        }
//        throw new IllegalStateException();
//      }
//      
//      @Override
//      protected boolean canEdit(Object element) {
//        if (element instanceof ILifecycleMappingLabelProvider) { 
//          ILifecycleMappingLabelProvider prov = (ILifecycleMappingLabelProvider)element;
//          List<IMavenDiscoveryProposal> all = mappingConfiguration.getProposals(prov.getKey());
//          return all != null && all.size() > 1;
//        }
//        return false;
//      }
//    });
    
    treeViewer.setContentProvider(new ITreeContentProvider() {

      public void dispose() {
      }

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

      public Object[] getElements(Object inputElement) {
        if(inputElement instanceof LifecycleMappingConfiguration) {
          Map<ILifecycleMappingElementKey, List<ILifecycleMappingLabelProvider>> packagings = new HashMap<ILifecycleMappingElementKey, List<ILifecycleMappingLabelProvider>>();
          Map<ILifecycleMappingElementKey, List<ILifecycleMappingLabelProvider>> mojos = new HashMap<ILifecycleMappingElementKey, List<ILifecycleMappingLabelProvider>>();
          Collection<ProjectLifecycleMappingConfiguration> projects = ((LifecycleMappingConfiguration) inputElement).getProjects();
          for (ProjectLifecycleMappingConfiguration prjconf : projects) {
            PackagingTypeMappingConfiguration pack = prjconf.getPackagingTypeMappingConfiguration();
            if (pack != null && !pack.isOK()) {
              List<ILifecycleMappingLabelProvider> val = packagings.get(pack.getLifecycleMappingElementKey());
              if (val == null) {
                val = new ArrayList<ILifecycleMappingLabelProvider>();
                packagings.put(pack.getLifecycleMappingElementKey(), val);
              }
              val.add(new PackagingTypeMappingLabelProvider(prjconf, pack));
            }
            List<MojoExecutionMappingConfiguration> mojoExecs = prjconf.getMojoExecutionConfigurations();
            if (mojoExecs != null) {
              for (MojoExecutionMappingConfiguration mojoMap : mojoExecs) {
                if (!mojoMap.isOK()) {
                  List<ILifecycleMappingLabelProvider> val = mojos.get(mojoMap.getLifecycleMappingElementKey());
                  if (val == null) {
                    val = new ArrayList<ILifecycleMappingLabelProvider>();
                    mojos.put(mojoMap.getLifecycleMappingElementKey(), val);
                  }
                  val.add(new MojoExecutionMappingLabelProvider(prjconf, mojoMap));
                }
              }
            }
          }
          List<ILifecycleMappingLabelProvider> toRet = new ArrayList<ILifecycleMappingLabelProvider>();
          for (Map.Entry<ILifecycleMappingElementKey, List<ILifecycleMappingLabelProvider>> ent : packagings.entrySet()) {
            toRet.add(new AggregateMappingLabelProvider(ent.getKey(), ent.getValue()));
          }
          for (Map.Entry<ILifecycleMappingElementKey, List<ILifecycleMappingLabelProvider>> ent : mojos.entrySet()) {
            toRet.add(new AggregateMappingLabelProvider(ent.getKey(), ent.getValue()));
          }
          return toRet.toArray();
        }
        return null;
      }

      public Object[] getChildren(Object parentElement) {
        if (parentElement instanceof AggregateMappingLabelProvider) {
          return ((AggregateMappingLabelProvider)parentElement).getChildren();
        }
        return new Object[0];
      }

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

      public boolean hasChildren(Object element) {
        Object[] children = getChildren(element);
        return children != null && children.length > 0;
      }

    });
    treeViewer.setLabelProvider(new ITableLabelProvider() {

      public void removeListener(ILabelProviderListener listener) {
      }

      public boolean isLabelProperty(Object element, String property) {
        return false;
      }

      public void dispose() {
      }

      public void addListener(ILabelProviderListener listener) {
      }

      public String getColumnText(Object element, int columnIndex) {
        if(element instanceof ILifecycleMappingLabelProvider) {
          ILifecycleMappingLabelProvider prov = (ILifecycleMappingLabelProvider) element;
          if(columnIndex == 0) {
            return prov.getMavenText();
          }
          if (columnIndex == 1) {
            return prov.getEclipseMappingText(mappingConfiguration);
          }
          if (columnIndex == 2) {
            IMavenDiscoveryProposal proposal = mappingConfiguration.getSelectedProposal(prov.getKey());
            if (proposal != null) {
              return NLS.bind("Install {0}", proposal.toString()); //not really feeling well here.
            }
            if (loading) {
              return "";
            } else {
              return "";//"Nothing discovered";
            }
//            List<IMavenDiscoveryProposal> all = mappingConfiguration.getProposals(prov.getKey());
//            if (all.size() > 0) {
//              return "<Please select>";
//            } else {
//              return "Nothing discovered";
//            }
          }
        }
        return null;
      }

      public Image getColumnImage(Object element, int columnIndex) {
        if(element instanceof ILifecycleMappingLabelProvider) {
          ILifecycleMappingLabelProvider prov = (ILifecycleMappingLabelProvider)element;
          if (columnIndex == 0 && prov.isError()) {
            if (mappingConfiguration.getSelectedProposal(prov.getKey()) == null) {
              return MavenImages.IMG_ERROR;
            } else {
              return MavenImages.IMG_MSG_INFO;
            }
          }
        }
        return null;
      }
    });

//    treeViewer.setCheckStateProvider(new ICheckStateProvider() {
//
//      public boolean isGrayed(Object element) {
//        return false;
//      }
//
//      public boolean isChecked(Object element) {
//        if(element instanceof IMavenDiscoveryProposal) {
//          return mappingConfiguration.getSelectedProposals().contains(element);
//        }
//        return false;
//      }
//    });
//
//    treeViewer.addCheckStateListener(new ICheckStateListener() {
//      public void checkStateChanged(CheckStateChangedEvent event) {
//        if(!(event.getElement() instanceof IMavenDiscoveryProposal)) {
//          event.getCheckable().setChecked(event.getElement(), false);
//          return;
//        }
//        IMavenDiscoveryProposal proposal = (IMavenDiscoveryProposal) event.getElement();
//        if(event.getChecked()) {
//          mappingConfiguration.addSelectedProposal(proposal);
//        } else {
//          mappingConfiguration.removeSelectedProposal(proposal);
//        }
//
//        discoverProposals();
//      }
//    });

    btnNewButton = new Button(container, SWT.NONE);
    btnNewButton.addSelectionListener(new SelectionAdapter() {
      @Override
      public void widgetSelected(SelectionEvent e) {
        mappingConfiguration.clearSelectedProposals();
        discoverProposals();
      }
    });
    btnNewButton.setLayoutData(new GridData(SWT.RIGHT, SWT.TOP, false, false, 1, 1));
    btnNewButton.setText("Check m2e marketplace");
  }

  protected void discoverProposals() {
    loading = true;
    treeViewer.refresh();
    final IMavenDisovery discovery = ((AbstractMavenProjectWizard) getWizard()).getDiscovery();
    try {
      getContainer().run(true, true, new IRunnableWithProgress() {
        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
          Collection<ProjectLifecycleMappingConfiguration> projects = mappingConfiguration.getProjects();
          monitor.beginTask("Searching m2e marketplace", projects.size());

          Map<ILifecycleMappingElementKey, List<IMavenDiscoveryProposal>> proposals = new LinkedHashMap<ILifecycleMappingElementKey, List<IMavenDiscoveryProposal>>();

          for(ProjectLifecycleMappingConfiguration project : projects) {
            if(monitor.isCanceled()) {
              throw new OperationCanceledException();
            }
            MavenProject mavenProject = project.getMavenProject();
            List<MojoExecution> mojoExecutions = project.getMojoExecutions();
            try {
              proposals.putAll(discovery.discover(mavenProject, mojoExecutions,
                  mappingConfiguration.getSelectedProposals(),
                  SubMonitor.convert(monitor, NLS.bind("Analysing {0}", project.getRelpath()), 1)));
            } catch(CoreException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
            monitor.worked(1);
          }

          mappingConfiguration.setProposals(proposals);
          mappingConfiguration.autoCompleteMapping();
        }
      });
    } catch(InvocationTargetException e) {
      setErrorMessage(e.getMessage());
    } catch(InterruptedException e) {
      setErrorMessage(e.getMessage());
    }
    loading  = false;
    treeViewer.refresh();

  }

//  protected String getMojoExecutionColumnText(MojoExecutionMappingConfiguration execution, int columnIndex) {
//    MojoExecutionMappingLabelProvider provider = new MojoExecutionMappingLabelProvider(execution);
//    switch(columnIndex) {
//      case MAVEN_INFO_IDX:
//        return provider.getMavenText();
//      case ECLIPSE_INFO_IDX:
//        String text = provider.getEclipseMappingText();
//        if(!execution.isOK()
//            && mappingConfiguration.getSelectedProposal(execution.getLifecycleMappingElementKey()) != null) {
//          text = "OK (WAS: " + text + ")";
//        }
//        return text;
//    }
//    return null;
//  }
//
//  protected String getPackagingTypeMappingColumnText(PackagingTypeMappingConfiguration packagingType, int columnIndex) {
//    PackagingTypeMappingLabelProvider provider = new PackagingTypeMappingLabelProvider(packagingType);
//    switch(columnIndex) {
//      case MAVEN_INFO_IDX:
//        return provider.getMavenText();
//      case ECLIPSE_INFO_IDX:
//        String text = provider.getEclipseMappingText();
//        if(!packagingType.isOK()
//            && mappingConfiguration.getSelectedProposal(packagingType.getLifecycleMappingElementKey()) != null) {
//          text = "OK (WAS: " + text + ")";
//        }
//        return text;
//    }
//    return null;
//  }
//
//  protected String getProjectColumnText(ProjectLifecycleMappingConfiguration configuration, int columnIndex) {
//    switch(columnIndex) {
//      case MAVEN_INFO_IDX:
//        return configuration.getMavenText();
//    }
//    return null;
//  }

//  protected String getProposalColumnText(IMavenDiscoveryProposal proposal, int columnIndex) {
//    ILifecycleMappingLabelProvider provider = (ILifecycleMappingLabelProvider) adapterManager.getAdapter(proposal,
//        ILifecycleMappingLabelProvider.class);
//    if(provider == null) {
//      return null;
//    }
//    switch(columnIndex) {
//      case MAVEN_INFO_IDX:
//        return provider.getMavenText();
//      case ECLIPSE_INFO_IDX:
//        return provider.getEclipseMappingText();
//    }
//    return null;
//  }

  @Override
  public void setVisible(boolean visible) {
    super.setVisible(visible);
    if(visible) {
      mappingConfiguration = getMappingConfiguration();
      treeViewer.setInput(mappingConfiguration);
      
      //set initial column sizes
      TreeColumn[] columns = treeViewer.getTree().getColumns();
      for (int i = 0; i < columns.length; i++) {
        int ratio = i == 0 ? 5 :  i == 1 ? 3 : 2;
        columns[i].setWidth(treeViewer.getTree().getClientArea().width / 10 * ratio);        
      }
      Display.getDefault().asyncExec(new Runnable() {
        public void run() {
          discoverProposals();
        }
      });
    }
  }


  protected Collection<MavenProjectInfo> getProjects() {
    return ((MavenImportWizard) getWizard()).getProjects();
  }

  protected ProjectImportConfiguration getProjectImportConfiguration() {
    return ((MavenImportWizard) getWizard()).getProjectImportConfiguration();
  }
  
  protected LifecycleMappingConfiguration getMappingConfiguration() {
    return ((MavenImportWizard) getWizard()).getMappingConfiguration();
  }

  public List<IMavenDiscoveryProposal> getSelectedDiscoveryProposals() {
    if(mappingConfiguration == null) {
      return Collections.emptyList();
    }
    return mappingConfiguration.getSelectedProposals();
  }
  

}

Back to the top