Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d67a88e4a96d1a305804d3ecec63a182b0dabc6c (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**********************************************************************
 * Copyright (c) 2005, 2013 IBM Corporation, Ericsson
 * 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:
 *     IBM - Initial API and implementation
 *     Bernd Hufmann - Updated for TMF
 **********************************************************************/

package org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs;

import java.util.Iterator;
import java.util.List;

import org.eclipse.jface.dialogs.DialogSettings;

/**
 * A filter criteria is a criteria that can be activated or not, positive or not.
 *
 * @version 1.0
 * @author sveyrier
 *
 */
public class FilterCriteria {

    // ------------------------------------------------------------------------
    // Constants
    // ------------------------------------------------------------------------
    /**
     * The filter state value for 'active'.
     */
    protected static final String ACTIVE = "active"; //$NON-NLS-1$
    /**
     * The property value for positive filter.
     */
    protected static final String POSITIVE = "positive"; //$NON-NLS-1$
    /**
     * The filter loader class name property.
     */
    protected static final String LOADERCLASSNAME = "loaderClassName"; //$NON-NLS-1$

    // ------------------------------------------------------------------------
    // Attributes
    // ------------------------------------------------------------------------
    /**
     * The criteria reference.
     */
    private Criteria fCriteria;
    /**
     * Flag whether this criteria is active or not
     */
    private boolean fIsActive;
    /**
     * Flag whether this criteria is for positive filter or not
     */
    private boolean fIsPositive;
    /**
     * The loader class name.
     */
    private String fLoaderClassName;

    // ------------------------------------------------------------------------
    // Constructor
    // ------------------------------------------------------------------------
    /**
     * Standard constructor
     *
     * @param criteria A criteria reference
     * @param isActive <code>true</code> if filter criteria is active else <code>false</code>
     * @param isPositive  <code>true</code> for positive filter else <code>false</code>
     */
    public FilterCriteria(Criteria criteria, boolean isActive, boolean isPositive) {
        this(criteria, isActive, isPositive, null);
    }

    /**
     * Constructor
     *
     * @param criteria A criteria reference
     * @param isActive <code>true</code> if filter criteria is active else <code>false</code>
     * @param isPositive  <code>true</code> for positive filter else <code>false</code>
     * @param loaderClassName A loader class name
     */
    public FilterCriteria(Criteria criteria, boolean isActive, boolean isPositive, String loaderClassName) {
        fCriteria = criteria;
        fIsActive = isActive;
        fIsPositive = isPositive;
        fLoaderClassName = loaderClassName;
    }

    /**
     * Copy Constructor
     * @param other FilterCriteria
     */
    public FilterCriteria (FilterCriteria other) {
        fCriteria = new Criteria(other.fCriteria);
        fIsActive = other.fIsActive;
        fIsPositive = other.fIsPositive;
        fLoaderClassName = other.fLoaderClassName;
    }

    /**
     * Default constructor
     */
    protected FilterCriteria() {
    }

    // ------------------------------------------------------------------------
    // Methods
    // ------------------------------------------------------------------------

    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer(super.toString());
        sb.append(':');
        if (fCriteria != null) {
            sb.append(" expression=");sb.append(fCriteria.getExpression()); //$NON-NLS-1$
            sb.append(" active=");sb.append(fIsActive); //$NON-NLS-1$
            sb.append(" positive=");sb.append(fIsPositive); //$NON-NLS-1$
        } else {
            sb.append("empty criteria"); //$NON-NLS-1$
        }
        return sb.toString();
    }

    /**
     * Sets a criteria reference.
     * @param criteria A criteria reference
     */
    public void setCriteria(Criteria criteria) {
        fCriteria = criteria;
    }

    /**
     * Returns the criteria reference.
     *
     * @return the criteria reference
     */
    public Criteria getCriteria() {
        return fCriteria;
    }

    /**
     * Sets the active flag.
     *
     * @param isActive A active value.
     */
    public void setActive(boolean isActive) {
        fIsActive = isActive;
    }

    /**
     * Returns whether filter criteria is active or not.
     *
     * @return whether filter criteria is active or not.
     */
    public boolean isActive() {
        return fIsActive;
    }

    /**
     * Sets filter is for positive filtering or not.
     *
     * @param isPositive The value to set.
     */
    public void setPositive(boolean isPositive) {
        fIsPositive = isPositive;
    }

    /**
     * Returns whether the filter si for positive filtering or not.
     *
     * @return Returns the positive.
     */
    public boolean isPositive() {
        return fIsPositive;
    }

    /**
     * Sets the loader class name for this filter.
     *
     * @param loaderClassName The loader class name to set
     */
    public void setLoaderClassName(String loaderClassName) {
        fLoaderClassName = loaderClassName;
    }

    /**
     * Returns the class loader name.
     *
     * @return the class loader name.
     */
    public String getLoaderClassName() {
        return fLoaderClassName;
    }

    /**
     * Finds a filter criteria within a  list of criteria.
     *
     * @param what The filter to find
     * @param list A list of filter criteria
     * @return The found filter criteria or null
     */
    public static FilterCriteria find(FilterCriteria what, List<FilterCriteria> list) {
        if (what != null && list != null) {
            try {
                for (Iterator<FilterCriteria> i = list.iterator(); i.hasNext();) {
                    FilterCriteria fc = i.next();
                    if (what.compareTo(fc)) {
                        return fc;
                    }
                }
            } catch (Exception e) {
                // Silence
            }
        }
        return null;
    }

    /**
     * Compares this filter criteria with a given criteria.
     *
     * @param to The filter criteria to compare.
     * @return usual comparison result (< 0, 0, > 0)
     */
    public boolean compareTo(FilterCriteria to) {
        if (isPositive() == to.isPositive() && getCriteria().compareTo(to.getCriteria())) {
            if (getLoaderClassName() == null && to.getLoaderClassName() == null) {
                return true;
            }
            if ((getLoaderClassName() != null && to.getLoaderClassName() != null) && getLoaderClassName().equals(to.getLoaderClassName())) {
                return true;
            }
        }
        return false;
    }

    /**
     * Saves current criteria attributes in the dialog settings.
     *
     * @param settings The dialog settings
     */
    public void save(DialogSettings settings) {
        settings.put(ACTIVE, isActive());
        settings.put(POSITIVE, isPositive());
        if (getLoaderClassName() != null) {
            settings.put(LOADERCLASSNAME, getLoaderClassName());
        } else {
            settings.put(LOADERCLASSNAME, ""); //$NON-NLS-1$
        }
        if (fCriteria != null) {
            fCriteria.save(settings);
        }
    }

    /**
     * Loads the criteria with values of the dialog settings.
     *
     * @param settings The dialog settings
     */
    public void load(DialogSettings settings) {
        setActive(settings.getBoolean(ACTIVE));
        setPositive(settings.getBoolean(POSITIVE));
        String loaderClassName = settings.get(LOADERCLASSNAME);
        setLoaderClassName(loaderClassName != null && loaderClassName.length() > 0 ? loaderClassName : null);
        if (fCriteria != null) {
            fCriteria.load(settings);
        }
    }
}

Back to the top