Skip to main content
summaryrefslogtreecommitdiffstats
blob: 30235387e4d273b64abc5c3e0b413be5c378978e (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
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * 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:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.ui.skynet.widgets.xchange;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.logging.Level;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.skynet.core.artifact.Artifact;
import org.eclipse.osee.framework.ui.skynet.internal.Activator;

/**
 * @author Jeff C. Phillips
 */
public class DefaultHierarchySorter implements Comparator<Artifact> {

   private List<Artifact> populateParent(Artifact artifact, List<Artifact> parentList) throws OseeCoreException {
      if (artifact != null) {
         parentList.add(artifact);
         parentList = populateParent(artifact.getParent(), parentList);
      }
      return parentList;
   }

   @Override
   public int compare(Artifact firstArtifact, Artifact secondArtifact) {
      List<Artifact> firstArtifactsParentList = new ArrayList<Artifact>();
      List<Artifact> secondArtifactParentList = new ArrayList<Artifact>();

      try {
         firstArtifactsParentList = populateParent(firstArtifact, firstArtifactsParentList);
         secondArtifactParentList = populateParent(secondArtifact, secondArtifactParentList);

         Artifact commonParent = getCommonParent(firstArtifactsParentList, secondArtifactParentList);

         if (commonParent != null) {

            if (commonParent.equals(firstArtifact)) {
               return -1;
            } else if (commonParent.equals(secondArtifact)) {
               return 1;
            }

            for (Artifact possibleChild : commonParent.getChildren()) {
               int firstIndex = firstArtifactsParentList.indexOf(commonParent);
               if (firstIndex > 0) {
                  firstArtifact = firstArtifactsParentList.get(firstIndex - 1);
               }
               int secondIndex = secondArtifactParentList.indexOf(commonParent);
               if (secondIndex > 0) {
                  secondArtifact = secondArtifactParentList.get(secondIndex - 1);
               }

               if (possibleChild.equals(firstArtifact)) {
                  return -1;
               } else if (possibleChild.equals(secondArtifact)) {
                  return 1;
               }
            }
         }
      } catch (OseeCoreException ex) {
         OseeLog.log(Activator.class, Level.SEVERE, ex);
      }
      return 0;
   }

   /**
    * @return Returns the common parent for both lists.
    */
   private Artifact getCommonParent(List<Artifact> firstArtifactsParentList, List<Artifact> secondArtifactParentList) {
      Artifact toReturn = null;
      for (Artifact commonParent : firstArtifactsParentList) {
         if (secondArtifactParentList.contains(commonParent)) {
            toReturn = commonParent;
            break;
         }
      }
      return toReturn;
   }
}

Back to the top