Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 47d8d465fecb173331f5bcc62e5018767c52cf8c (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
/*****************************************************************************
 * Copyright (c) 2015, 2017 CEA LIST 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:
 *   CEA LIST - Initial API and implementation
 *   Thanh Liem PHAN (ALL4TEC) thanhliem.phan@all4tec.net - Bug 515806
 *****************************************************************************/

package org.eclipse.papyrus.infra.nattable.filter;

import java.util.Collection;

import org.eclipse.core.runtime.Assert;
import org.eclipse.nebula.widgets.nattable.config.IConfigRegistry;
import org.eclipse.nebula.widgets.nattable.data.IColumnAccessor;
import org.eclipse.papyrus.infra.nattable.utils.CellHelper;

import ca.odell.glazedlists.matchers.Matcher;

/**
 * Matcher Editor for Boolean. It manages Boolean and the string N/A
 *
 */
public class BooleanMatcherEditor extends AbstractPapyrusMatcherEditor {



	/**
	 * Constructor.
	 *
	 * @param columnAccesor
	 * @param columnIndex
	 * @param matchOn
	 * @param configRegistry
	 */
	public BooleanMatcherEditor(IColumnAccessor<Object> columnAccesor, int columnIndex, Object matchOn, IConfigRegistry configRegistry) {
		super(columnAccesor, columnIndex, matchOn, configRegistry);
	}

	/**
	 * @see org.eclipse.papyrus.infra.nattable.filter.AbstractPapyrusMatcherEditor#createMatcher(org.eclipse.nebula.widgets.nattable.data.IColumnAccessor, int, java.lang.Object, org.eclipse.nebula.widgets.nattable.config.IConfigRegistry)
	 *
	 * @param columnAccesor
	 * @param columnIndex
	 * @param matchOn
	 * @param configRegistry
	 * @return
	 */
	@Override
	protected Matcher<Object> createMatcher(IColumnAccessor<Object> columnAccesor, int columnIndex, Object matchOn, IConfigRegistry configRegistry) {
		return new BooleanMatcher(columnAccesor, matchOn, columnIndex);
	}

	/**
	 * the boolean matcher it can matches with Boolean or N/A
	 *
	 */
	public static class BooleanMatcher extends AbstractSinglePapyrusMatcher<Object> {

		/**
		 * Constructor.
		 *
		 * @param accessor
		 *            the accessor to use to get cell value
		 * @param wantedObject
		 *            the wanted value, must not be a collection
		 * @param columnIndex
		 *            the index of the column
		 */
		public BooleanMatcher(IColumnAccessor<Object> accessor, Object wantedObject, int columnIndex) {
			super(accessor, columnIndex, wantedObject);
			Assert.isTrue(!(wantedObject instanceof Collection<?>));
			Assert.isTrue(wantedObject instanceof Boolean || wantedObject.equals(CellHelper.getUnsupportedCellContentsText()));
		}

		/**
		 * @see ca.odell.glazedlists.matchers.Matcher#matches(java.lang.Object)
		 *
		 * @param item
		 * @return
		 */
		@Override
		public boolean matches(Object item) {
			Object wantedValue = getObjectToMatch();
			Object value = getCellValueFor(item);
			if (value instanceof Collection<?>) {
				Collection<?> coll = (Collection<?>) value;
				return coll.contains(wantedValue);
			} else {
				return getObjectToMatch().equals(value);
			}
		}
	}
}

Back to the top