Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f649842b9316bb96d62b734fa902e5a7623cf3ad (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
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 *
 *
 * 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:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.infra.nattable.utils;

import java.util.Comparator;

import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
import org.eclipse.nebula.widgets.nattable.style.DisplayMode;
import org.eclipse.papyrus.infra.nattable.model.nattable.nattableaxis.IAxis;
import org.eclipse.papyrus.infra.services.labelprovider.service.LabelProviderService;


/**
 * The comparator used to sort IAxis
 *
 * @author Vincent Lorenzo
 *
 */
public class AxisComparator implements Comparator<IAxis> {

	/**
	 * indicates the direction of the sort
	 */
	private boolean alphabeticOrder;

	/**
	 * the config registry is used to find the label provider service
	 */
	private IConfigRegistry configRegistry;

	private LabelProviderContextElementWrapper wrapper1;
	
	private LabelProviderContextElementWrapper wrapper2;

	/**
	 *
	 * Constructor.
	 *
	 * @param alphabeticOrder
	 *            indicates the direction of the sort
	 * @param configRegistry
	 *            the config registry used by the table
	 */
	public AxisComparator(boolean alphabticOrder, final IConfigRegistry configRegistry) {
		this.alphabeticOrder = alphabticOrder;
		this.configRegistry = configRegistry;
		wrapper1 = new LabelProviderContextElementWrapper();
		wrapper2 = new LabelProviderContextElementWrapper();
		wrapper1.setConfigRegistry(configRegistry);
		wrapper2.setConfigRegistry(configRegistry);
	}

	/**
	 * Compare 2 {@link IAxis}
	 *
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
	 *
	 * @param arg0
	 * @param arg1
	 * @return
	 */
	@Override
	public int compare(IAxis arg0, IAxis arg1) {
		final LabelProviderService serv = this.configRegistry.getConfigAttribute(NattableConfigAttributes.LABEL_PROVIDER_SERVICE_CONFIG_ATTRIBUTE, DisplayMode.NORMAL, NattableConfigAttributes.LABEL_PROVIDER_SERVICE_ID);
		wrapper1.setObject(arg0);
		wrapper2.setObject(arg1);
		final ILabelProvider provider = serv.getLabelProvider(Constants.HEADER_LABEL_PROVIDER_CONTEXT);
		
		final String str1 = provider.getText(wrapper1).replaceAll(AxisUtils.REGEX, "");// we keep only words characters (letters + numbers) + whitespace //$NON-NLS-1$
		final String str2 = provider.getText(wrapper2).replaceAll(AxisUtils.REGEX, ""); //$NON-NLS-1$
		if (this.alphabeticOrder) {
			return str1.compareToIgnoreCase(str2);
		}
		return str2.compareToIgnoreCase(str1);

	}

	/**
	 *
	 * @param serv
	 *            the label provider service
	 * @param obj
	 *            the object for which we want the displayed text
	 * @return
	 * 
	 * @Deprecated since Eclipse Mars
	 */
	@Deprecated
	protected String getText(final LabelProviderService serv, final Object obj) {
		final ILabelProvider provider = serv.getLabelProvider(Constants.HEADER_LABEL_PROVIDER_CONTEXT);
		LabelProviderContextElementWrapper wrapper = new LabelProviderContextElementWrapper();
		wrapper.setConfigRegistry(this.configRegistry);
		wrapper.setObject(obj);
		return provider.getText(wrapper);
	}
}

Back to the top