Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 444b630489bc656b6b0814111e39cd1522989790 (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
/*******************************************************************************
 * Copyright (c) 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
 *******************************************************************************/

package org.eclipse.m2e.editor.dialogs;

import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.ARTIFACT_ID;
import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.DEPENDENCIES;
import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.DEPENDENCY;
import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.DEPENDENCY_MANAGEMENT;
import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.GROUP_ID;
import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.VERSION;
import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.findChild;
import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.findChilds;
import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.getChild;
import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.getTextValue;
import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.performOnDOMDocument;
import static org.eclipse.m2e.core.ui.internal.editing.PomEdits.removeChild;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import org.apache.maven.model.Dependency;
import org.apache.maven.project.MavenProject;
import org.eclipse.core.resources.IFile;
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.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.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
import org.eclipse.m2e.core.ui.internal.dialogs.AbstractMavenDialog;
import org.eclipse.m2e.core.ui.internal.editing.PomEdits.CompoundOperation;
import org.eclipse.m2e.core.ui.internal.editing.PomEdits.Operation;
import org.eclipse.m2e.core.ui.internal.editing.PomEdits.OperationTuple;
import org.eclipse.m2e.core.ui.internal.editing.PomHelper;
import org.eclipse.m2e.editor.MavenEditorPlugin;
import org.eclipse.m2e.editor.composites.DependencyLabelProvider;
import org.eclipse.m2e.editor.composites.ListEditorContentProvider;
import org.eclipse.m2e.editor.composites.PomHierarchyComposite;
import org.eclipse.m2e.editor.pom.ValueProvider;
import org.eclipse.osgi.util.NLS;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
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.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 * This dialog is used to present the user with a list of dialogs that they can move to being managed under
 * "dependencyManagement". It allows them to pick the destination POM where the dependencies will be managed.
 * 
 * @author rgould
 */
public class ManageDependenciesDialog extends AbstractMavenDialog {
  private static final Logger LOG = LoggerFactory.getLogger(ManageDependenciesDialog.class);

  private static final String DIALOG_SETTINGS = ManageDependenciesDialog.class.getName();

  private TableViewer dependenciesViewer;

  private final LinkedList<MavenProject> projectHierarchy;

  private PomHierarchyComposite pomHierarchy;

  private IStatus status;
  
  private List<Object> originalSelection;
  
  private ValueProvider<List<Dependency>> modelVProvider;

  /**
   * Hierarchy is a LinkedList representing the hierarchy relationship between POM represented by model and its parents.
   * The head of the list should be the child, while the tail should be the root parent, with the others in between.
   */
  public ManageDependenciesDialog(Shell parent, ValueProvider<List<Dependency>> modelVProvider, LinkedList<MavenProject> hierarchy) {
    this(parent, modelVProvider, hierarchy, null);
  }

  public ManageDependenciesDialog(Shell parent, ValueProvider<List<Dependency>> modelVProvider, LinkedList<MavenProject> hierarchy,
       List<Object> selection) {
    super(parent, DIALOG_SETTINGS);

    setShellStyle(getShellStyle() | SWT.RESIZE);
    setTitle(Messages.ManageDependenciesDialog_dialogTitle);

    this.projectHierarchy = hierarchy;
    this.originalSelection = selection;
    this.modelVProvider = modelVProvider;
  }

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

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

    Label infoLabel = new Label(composite, SWT.WRAP);
    infoLabel.setText(Messages.ManageDependenciesDialog_dialogInfo);

    Label horizontalBar = new Label(composite, SWT.SEPARATOR | SWT.HORIZONTAL);

    SashForm sashForm = new SashForm(composite, SWT.SMOOTH | SWT.HORIZONTAL);
    Composite dependenciesComposite = new Composite(sashForm, SWT.NONE);

    Label selectDependenciesLabel = new Label(dependenciesComposite, SWT.NONE);
    selectDependenciesLabel.setText(Messages.ManageDependenciesDialog_selectDependenciesLabel);

    final Table dependenciesTable = new Table(dependenciesComposite, SWT.FLAT | SWT.MULTI | SWT.BORDER);
    final TableColumn column = new TableColumn(dependenciesTable, SWT.NONE);
    dependenciesTable.addControlListener(new ControlAdapter() {
      @Override
      public void controlResized(ControlEvent e) {
        column.setWidth(dependenciesTable.getClientArea().width);
      }
    });

    Composite pomComposite = new Composite(sashForm, SWT.NONE);

    Label selectPomLabel = new Label(pomComposite, SWT.NONE);
    selectPomLabel.setText(Messages.ManageDependenciesDialog_selectPOMLabel);

    pomHierarchy = new PomHierarchyComposite(pomComposite, SWT.BORDER);
    pomHierarchy.setHierarchy(getProjectHierarchy());
    // pomsViewer = new TreeViewer(pomComposite, SWT.BORDER);

    /*
     * Configure layouts
     */

    GridLayout layout = new GridLayout(1, false);
    composite.setLayout(layout);

    GridData gridData = new GridData(SWT.FILL, SWT.NONE, true, false);
    gridData.widthHint = 300;
    infoLabel.setLayoutData(gridData);

    gridData = new GridData(SWT.FILL, SWT.NONE, true, false);
    horizontalBar.setLayoutData(gridData);

    gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
    sashForm.setLayoutData(gridData);

    gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
    dependenciesComposite.setLayoutData(gridData);

    layout = new GridLayout(1, false);
    dependenciesComposite.setLayout(layout);

    gridData = new GridData(SWT.FILL, SWT.NONE, true, false);
    selectDependenciesLabel.setLayoutData(gridData);

    gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
    dependenciesTable.setLayoutData(gridData);

    gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
    pomComposite.setLayoutData(gridData);

    layout = new GridLayout(1, false);
    pomComposite.setLayout(layout);

    gridData = new GridData(SWT.FILL, SWT.NONE, true, false);
    selectPomLabel.setLayoutData(gridData);

    gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
    pomHierarchy.setLayoutData(gridData);
    //pomsViewer.getTree().setLayoutData(gridData);

    /*
     * Set up list/tree viewers
     */

    dependenciesViewer = new TableViewer(dependenciesTable);
    dependenciesViewer.setLabelProvider(new DependencyLabelProvider());
    dependenciesViewer.setContentProvider(new ListEditorContentProvider<Dependency>());
    //MNGECLIPSE-2675 only show the dependencies not already managed (decide just by absence of the version element
    List<Dependency> deps = modelVProvider.getValue();
    List<Dependency> nonManaged = new ArrayList<Dependency>();
    if (deps != null) {
      for (Dependency d : deps) {
        if (d.getVersion() != null) {
          nonManaged.add(d);
        }
      }
    }
    dependenciesViewer.setInput(nonManaged);
    dependenciesViewer.addSelectionChangedListener(new DependenciesViewerSelectionListener());

    pomHierarchy.addSelectionChangedListener(new PomViewerSelectionChangedListener());
    if(getProjectHierarchy().size() > 0) {
      pomHierarchy.setSelection(new StructuredSelection(getProjectHierarchy().getLast()));
    }

    if(originalSelection != null && originalSelection.size() > 0) {
      dependenciesViewer.setSelection(new StructuredSelection(originalSelection));
    }
    
    return composite;
  }

  /* (non-Javadoc)
   * @see org.eclipse.ui.dialogs.SelectionStatusDialog#computeResult()
   */
  protected void computeResult() {
    MavenProject targetPOM = getTargetPOM();
    IMavenProjectFacade targetFacade = MavenPlugin.getDefault().getMavenProjectRegistry()
        .getMavenProject(targetPOM.getGroupId(), targetPOM.getArtifactId(), targetPOM.getVersion());
    MavenProject currentPOM = projectHierarchy.getFirst();
    IMavenProjectFacade currentFacade = MavenPlugin.getDefault().getMavenProjectRegistry()
        .getMavenProject(currentPOM.getGroupId(), currentPOM.getArtifactId(), currentPOM.getVersion());
    
    if (targetFacade == null || currentFacade == null) {
             return;
    }
    final boolean same = targetPOM.equals(currentPOM);
     
    final LinkedList<Dependency> modelDeps = getDependenciesList();

    /*
     * 1) Remove version values from the dependencies from the current POM
     * 2) Add dependencies to dependencyManagement of targetPOM
     */


    //First we remove the version from the original dependency
    final IFile current = currentFacade.getPom();
    final IFile target = targetFacade.getPom();
    Job perform = new Job("Updating POM file(s)") {
      @Override
      protected IStatus run(IProgressMonitor monitor) {
        try {
          if (same) {
            performOnDOMDocument(new OperationTuple(current, new CompoundOperation(createManageOperation(modelDeps), createRemoveVersionOperation(modelDeps))));
          } else {
            performOnDOMDocument(new OperationTuple(target, createManageOperation(modelDeps)), new OperationTuple(current, createRemoveVersionOperation(modelDeps)));
          }
        } catch(Exception e) {
          LOG.error("Error updating managed dependencies", e);
          return new Status(IStatus.ERROR, MavenEditorPlugin.PLUGIN_ID, "Error updating managed dependencies", e);
        }
        return Status.OK_STATUS;
      }
    };
    perform.setUser(false);
    perform.setSystem(true);
    perform.schedule();
  }
  
  public static Operation createRemoveVersionOperation(final List<Dependency> modelDeps) {
    return new Operation() {
      public void process(Document document) {
        List<Element> deps = findChilds(findChild(document.getDocumentElement(), DEPENDENCIES), DEPENDENCY);
        for (Element dep : deps) {
          String grid = getTextValue(findChild(dep, GROUP_ID));
          String artid = getTextValue(findChild(dep, ARTIFACT_ID));
          for(Dependency modelDep : modelDeps) {
            if (modelDep.getGroupId() != null && modelDep.getGroupId().equals(grid) &&
                modelDep.getArtifactId() != null && modelDep.getArtifactId().equals(artid)) {
              removeChild(dep, findChild(dep, VERSION));
            }
          }
        }
      }
    };
    
  }
  public static Operation createManageOperation(final List<Dependency> modelDeps) {
    return new Operation() {
      public void process(Document document) {
        List<Dependency> modelDependencies = new ArrayList<Dependency>(modelDeps);
        Element managedDepsElement = getChild(document.getDocumentElement(), DEPENDENCY_MANAGEMENT, DEPENDENCIES);
        List<Element> existing = findChilds(managedDepsElement, DEPENDENCY);
          for (Element dep : existing) {
            String artifactId = getTextValue(findChild(dep, ARTIFACT_ID));
            String groupId = getTextValue(findChild(dep, GROUP_ID));
            //cloned list, shall not modify shared resource (used by the remove operation)
            Iterator<Dependency> mdIter = modelDependencies.iterator();
            while(mdIter.hasNext()) {
              //TODO: here we iterate to find existing managed dependencies and decide not to overwrite them.
              // but this could eventually break the current project when the versions are diametrally different
              // we should have shown this information to the user in the UI in the first place (for him to decide what to do)
              Dependency md = mdIter.next();
              if(artifactId.equals(md.getArtifactId()) && groupId.equals(md.getGroupId())) {
                mdIter.remove();
                break;
              }
            }
          }
          //TODO is the version is defined by property expression, we should make sure the property is defined in the current project
          for (Dependency modelDependency : modelDependencies) {
            PomHelper.createDependency(managedDepsElement, modelDependency.getGroupId(), modelDependency.getArtifactId(), modelDependency.getVersion());
          }
      }
    };
    
  }

  protected LinkedList<Dependency> getDependenciesList() {
    IStructuredSelection selection = (IStructuredSelection) dependenciesViewer.getSelection();

    LinkedList<Dependency> dependencies = new LinkedList<Dependency>();

    for(Object obj : selection.toArray()) {
      dependencies.add((Dependency) obj);
    }

    return dependencies;
  }

  protected LinkedList<MavenProject> getProjectHierarchy() {
    return this.projectHierarchy;
  }

  protected MavenProject getTargetPOM() {
    IStructuredSelection selection = (IStructuredSelection) pomHierarchy.getSelection();
    return (MavenProject) selection.getFirstElement();
  }

  /**
   * Compare the list of selected dependencies against the selected targetPOM. If one of the dependencies is already
   * under dependencyManagement, but has a different version than the selected dependency, warn the user about this.
   * returns true if the user has been warned (but this method updates the status itself)
   * 
   * @param model
   * @param dependencies
   */
  protected boolean checkDependencies(org.apache.maven.model.Model model, LinkedList<Dependency> dependencies) {
    if(this.status != null && this.status.getCode() == IStatus.ERROR) {
      //Don't warn the user if there is already an error
      return false;
    }
    if(model == null || model.getDependencyManagement() == null
        || model.getDependencyManagement().getDependencies() == null
        || model.getDependencyManagement().getDependencies().isEmpty()) {
      return false;
    }

    for(Dependency selectedDep : dependencies) {
      for(org.apache.maven.model.Dependency targetDep : model.getDependencyManagement().getDependencies()) {
        if(selectedDep.getGroupId().equals(targetDep.getGroupId())
            && selectedDep.getArtifactId().equals(targetDep.getArtifactId())
            && !selectedDep.getVersion().equals(targetDep.getVersion())) {
          String modelID = model.getGroupId() + ":" + model.getArtifactId() + ":" + model.getVersion(); //$NON-NLS-1$ //$NON-NLS-2$
          if(targetDep.getLocation("") != null && targetDep.getLocation("").getSource() != null) { //$NON-NLS-1$ //$NON-NLS-2$
            modelID = targetDep.getLocation("").getSource().getModelId(); //$NON-NLS-1$
          }
          Object[] arguments = {selectedDep.getArtifactId() + "-" + selectedDep.getVersion(), //$NON-NLS-1$
              targetDep.getVersion(), modelID};
          String message = NLS.bind(Messages.ManageDependenciesDialog_dependencyExistsWarning, arguments);
          updateStatus(new Status(IStatus.WARNING, MavenEditorPlugin.PLUGIN_ID, message));
          return true;
        }
      }
    }
    return false;
  }

  protected void checkStatus(MavenProject targetProject, LinkedList<Dependency> selectedDependencies) {
    if(targetProject == null || selectedDependencies.isEmpty()) {
      updateStatus(new Status(IStatus.ERROR, MavenEditorPlugin.PLUGIN_ID, Messages.ManageDependenciesDialog_emptySelectionError));
      return;
    }
    boolean error = false;
    IMavenProjectFacade facade = MavenPlugin.getDefault().getMavenProjectRegistry()
        .getMavenProject(targetProject.getGroupId(), targetProject.getArtifactId(), targetProject.getVersion());
    if(facade == null) {
      error = true;
      updateStatus(new Status(IStatus.ERROR, MavenEditorPlugin.PLUGIN_ID,
          Messages.ManageDependenciesDialog_projectNotPresentError));
    } else {
      org.apache.maven.model.Model model = null;
      if(facade.getMavenProject() == null || facade.getMavenProject().getModel() == null) {
        try {
          model = MavenPlugin.getDefault().getMavenModelManager().readMavenModel(facade.getPom());
        } catch(CoreException e) {
          Object[] arguments = {facade.getPom(), e.getLocalizedMessage()};
          String message = NLS.bind(Messages.ManageDependenciesDialog_pomReadingError, arguments);
          Status status = new Status(IStatus.ERROR, MavenEditorPlugin.PLUGIN_ID, message);
          LOG.info(message, e);
          updateStatus(status);
          error = true;
        }
      } else {
        model = facade.getMavenProject().getModel();
      }
      if(model != null) {
        error = checkDependencies(model, getDependenciesList());
      }
    }

    if(!error) {
      clearStatus();
    }
  }

  protected void clearStatus() {
    updateStatus(new Status(IStatus.OK, MavenEditorPlugin.PLUGIN_ID, "")); //$NON-NLS-1$
  }

  protected class DependenciesViewerSelectionListener implements ISelectionChangedListener {
    public void selectionChanged(SelectionChangedEvent event) {
      checkStatus(getTargetPOM(), getDependenciesList());
    }
  }

  protected class PomViewerSelectionChangedListener implements ISelectionChangedListener {
    public void selectionChanged(SelectionChangedEvent event) {
      checkStatus(getTargetPOM(), getDependenciesList());
    }
  }

  @Override
  protected void updateStatus(IStatus status) {
    this.status = status;
    super.updateStatus(status);
  }

  public static class DepLabelProvider extends LabelProvider implements IColorProvider {
    /* (non-Javadoc)
     * @see org.eclipse.jface.viewers.LabelProvider#getText(java.lang.Object)
     */
    @Override
    public String getText(Object element) {
      MavenProject project = null;
      if(element instanceof MavenProject) {
        project = (MavenProject) element;
      } else if(element instanceof Object[]) {
        project = (MavenProject) ((Object[]) element)[0];
      } else {
        return ""; //$NON-NLS-1$
      }

      StringBuffer buffer = new StringBuffer();
      buffer.append(project.getGroupId() + " : " + project.getArtifactId() + " : " + project.getVersion()); //$NON-NLS-1$ //$NON-NLS-2$
      return buffer.toString();

    }

    public Color getForeground(Object element) {
      if(element instanceof MavenProject) {
        MavenProject project = (MavenProject) element;
        IMavenProjectFacade search = MavenPlugin.getDefault().getMavenProjectRegistry()
            .getMavenProject(project.getGroupId(), project.getArtifactId(), project.getVersion());
        if(search == null) {
          //This project is not in the workspace so we can't really modify it.
          return Display.getDefault().getSystemColor(SWT.COLOR_DARK_GRAY);
        }
      }
      return null;
    }

    public Color getBackground(Object element) {
      return null;
    }
  }

  public class ContentProvider implements ITreeContentProvider {
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
    }

    public void dispose() {
    }

    public boolean hasChildren(Object element) {
      Object[] children = getChildren(element);

      return children.length != 0;
    }

    public Object getParent(Object element) {
      if(element instanceof MavenProject) {
        MavenProject project = (MavenProject) element;
        return project.getParent();
      }
      return null;
    }

    /*
     * Return root element
     * (non-Javadoc)
     * @see org.eclipse.jface.viewers.ITreeContentProvider#getElements(java.lang.Object)
     */
    public Object[] getElements(Object inputElement) {

      if(inputElement instanceof LinkedList) {
        LinkedList<MavenProject> projects = (LinkedList<MavenProject>) inputElement;
        if(projects.isEmpty()) {
          return new Object[0];
        }
        return new Object[] {projects.getLast()};
      }

      return new Object[0];
    }

    public Object[] getChildren(Object parentElement) {
      if(parentElement instanceof MavenProject) {
        /*
         * Walk the hierarchy list until we find the parentElement and
         * return the previous element, which is the child.
         */
        MavenProject parent = (MavenProject) parentElement;

        if(getProjectHierarchy().size() == 1) {
          //No parent exists, only one element in the tree
          return new Object[0];
        }

        if(getProjectHierarchy().getFirst().equals(parent)) {
          //We are the final child
          return new Object[0];
        }

        ListIterator<MavenProject> iter = getProjectHierarchy().listIterator();
        while(iter.hasNext()) {
          MavenProject next = iter.next();
          if(next.equals(parent)) {
            iter.previous();
            MavenProject previous = iter.previous();
            return new Object[] {previous};
          }
        }
      }
      return new Object[0];
    }
  }
}

Back to the top