Skip to content

Package: ObjectViewerComparator

ObjectViewerComparator

nameinstructionbranchcomplexitylinemethod
ObjectViewerComparator(TriFunction)
M: 0 C: 12
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 5
100%
M: 0 C: 1
100%
compare(Viewer, Object, Object)
M: 0 C: 11
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
getDirection()
M: 2 C: 9
82%
M: 1 C: 3
75%
M: 1 C: 3
75%
M: 1 C: 4
80%
M: 0 C: 1
100%
toggleDirection()
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2019 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Lucas Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emfforms.spi.swt.core.ui;
15:
16: import org.eclipse.emfforms.common.TriFunction;
17: import org.eclipse.jface.viewers.Viewer;
18: import org.eclipse.jface.viewers.ViewerComparator;
19: import org.eclipse.swt.SWT;
20:
21: /**
22: * The {@link ObjectViewerComparator} allows to rotate between three sorting states:
23: * <ol>
24: * <li>no sorting
25: * <li>ascending
26: * <li>descending
27: * </ol>
28: * To sort the objects, the comparator applies the configured sorting function to the given objects.
29: *
30: * @author Lucas Koehler
31: * @since 1.20
32: *
33: */
34: public class ObjectViewerComparator extends ViewerComparator {
35:
36:         private static final int NONE = 0;
37:         private int direction = NONE;
38:         private final TriFunction<Integer, Integer, Object, Object> compareFunction;
39:
40:         /**
41:          * Creates a new instance.
42:          *
43:          * @param compareFunction The function used to compare objects of the viewer. This tri-function accepts the sorting
44:          * direction as its first argument and the objects to compare as the following arguments. The sorting
45:          * directions are: 0 = none, 1 = ascending, 2 = descending
46:          */
47:         public ObjectViewerComparator(TriFunction<Integer, Integer, Object, Object> compareFunction) {
48:                 this.compareFunction = compareFunction;
49:                 direction = NONE;
50:         }
51:
52:         /** Toggles through the sorting directions: NONE -> UP (ascending) -> DOWN (descending) -> NONE. */
53:         public void toggleDirection() {
54:                 direction = (direction + 1) % 3;
55:         }
56:
57:         /**
58:          * Get the current sorting direction as an SWT constant.
59:          *
60:          * @return SWT.NONE, SWT.UP (ascending), or SWT.DOWN (descending)
61:          */
62:         public int getDirection() {
63:•                switch (direction) {
64:                 case 0:
65:                         return SWT.NONE;
66:                 case 1:
67:                         return SWT.UP;
68:                 case 2:
69:                         return SWT.DOWN;
70:                 default:
71:                         return SWT.NONE;
72:                 }
73:
74:         }
75:
76:         @Override
77:         public int compare(Viewer viewer, Object e1, Object e2) {
78:                 return compareFunction.apply(direction, e1, e2);
79:         }
80: }