Skip to main content
summaryrefslogtreecommitdiffstats
blob: 17d7b6d0a3aa8793b63055cd4375b3ff23df6ad1 (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
/*
 * Copyright (c) 2015, 2016 Eike Stepper (Berlin, Germany) and others.
 * 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:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.explorer.ui.checkouts;

import org.eclipse.emf.cdo.CDOElement;
import org.eclipse.emf.cdo.eresource.CDOResourceFolder;
import org.eclipse.emf.cdo.eresource.CDOResourceNode;

import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;

import org.eclipse.jface.viewers.Viewer;

/**
 * @author Eike Stepper
 */
@SuppressWarnings("deprecation")
public class CDOCheckoutViewerSorter extends org.eclipse.jface.viewers.ViewerSorter
{
  @Override
  public int category(Object element)
  {
    if (element instanceof CDOResourceFolder)
    {
      // Sort folders before other elements.
      return -1;
    }

    return super.category(element);
  }

  @Override
  public int compare(Viewer viewer, Object e1, Object e2)
  {
    // Don't sort normal EObjects.
    if (e1 instanceof EObject && !(e1 instanceof CDOResourceNode))
    {
      EObject child1 = (EObject)e1;
      EObject child2 = (EObject)e2;

      EObject parent = CDOElement.getParentOf(child1);
      if (parent != null)
      {
        EList<EObject> children = parent.eContents();

        int pos1 = children.indexOf(child1);
        int pos2 = children.indexOf(child2);
        return pos1 - pos2;
      }
    }

    return super.compare(viewer, e1, e2);
  }
}

Back to the top