Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 308b87f7c16bcbf50f9a679889dc092e7196e58c (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
/*******************************************************************************
 * Copyright (c) 2009, 2010 THALES GLOBAL SERVICES.
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.sirius.diagram.ui.tools.internal.dialogs;

import org.eclipse.gmf.runtime.common.core.util.StringMatcher;
import org.eclipse.sirius.diagram.DDiagramElement;
import org.eclipse.sirius.diagram.ui.business.api.provider.AbstractDDiagramElementLabelItemProvider;

import com.google.common.base.Predicate;

/**
 * <p>
 * A {@link StringMatcher} used to detect elements that match a regular
 * expression.
 * </p>
 * 
 * This Matcher creates a MatchPredicate, that can be used to determine if a
 * given Object matches a regular expression.
 * 
 * @author <a href="mailto:alex.lagarde@obeo.fr">Alex Lagarde</a>
 */
public class DiagramElementsSelectionDialogPatternMatcher {

    private Predicate<Object> matchPredicate;

    private StringMatcher stringMatcher;

    /**
     * Creates a new PatternMatcher.
     * 
     * 
     * @param expreg
     *            the regular expression (for example '?a?' or 'abc' or '*c') ;
     *            <code>null</code> or empty regular expression will be replaced
     *            by '*'
     * 
     */
    public DiagramElementsSelectionDialogPatternMatcher(String expreg) {
        String computedExpreg = expreg;
        if (expreg == null) {
            computedExpreg = ""; //$NON-NLS-1$
        }
        // If the regular expression ends with a space, we have to use the exact
        // value of the given expreg
        if (computedExpreg.endsWith(" ")) { //$NON-NLS-1$
            computedExpreg = computedExpreg.substring(0, computedExpreg.lastIndexOf(' '));
        } else {
            // Otherwise, we add a star to make 'XYZ' recognized by the 'X'
            // expreg (as in quick outline for example)
            computedExpreg = computedExpreg + "*"; //$NON-NLS-1$
        }
        this.stringMatcher = new StringMatcher(computedExpreg, true, false);

    }

    /**
     * Creates a {@link Predicate} that can be applied on any Object. This
     * predicates will return true if the tested element is a
     * {@link DDiagramElement} and that its name is matching the regular
     * expression used to construct this Matcher.
     * 
     * @return a {@link Predicate} that can be applied on any Object to
     *         determine if whether it's matching the regular expression used to
     *         construct this Matcher
     */
    public Predicate<Object> getMatchPredicate() {
        if (matchPredicate == null) {
            matchPredicate = new Predicate<Object>() {

                public boolean apply(Object input) {
                    String elementName = null;
                    if (input instanceof DDiagramElement) {
                        DDiagramElement element = (DDiagramElement) input;
                        elementName = element.getName();
                    } else if (input instanceof AbstractDDiagramElementLabelItemProvider) {
                        AbstractDDiagramElementLabelItemProvider element = (AbstractDDiagramElementLabelItemProvider) input;
                        elementName = element.getText(element.getTarget());
                    }
                    return elementName != null && stringMatcher.match(elementName);
                }
            };
        }
        return matchPredicate;
    }
}

Back to the top