tmf: Simple warning fixes in tmf.core and tests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / dialogs / FilterCriteria.java
1 /**********************************************************************
2 * Copyright (c) 2005, 2008 IBM Corporation and others.
3 * Copyright (c) 2011, 2012 Ericsson.
4 *
5 * All rights reserved. This program and the accompanying materials
6 * are made available under the terms of the Eclipse Public License v1.0
7 * which accompanies this distribution, and is available at
8 * http://www.eclipse.org/legal/epl-v10.html
9 *
10 * Contributors:
11 * IBM - Initial API and implementation
12 * Bernd Hufmann - Updated for TMF
13 **********************************************************************/
14 package org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs;
15
16 import java.util.Iterator;
17 import java.util.List;
18
19 import org.eclipse.jface.dialogs.DialogSettings;
20
21 /**
22 * A filter criteria is a criteria that can be activated or not, positive or not.
23 *
24 * @version 1.0
25 * @author sveyrier
26 *
27 */
28 public class FilterCriteria {
29
30 // ------------------------------------------------------------------------
31 // Constants
32 // ------------------------------------------------------------------------
33 /**
34 * The filter state value for 'active'.
35 */
36 protected static final String ACTIVE = "active"; //$NON-NLS-1$
37 /**
38 * The property value for positive filter.
39 */
40 protected static final String POSITIVE = "positive"; //$NON-NLS-1$
41 /**
42 * The filter loader class name property.
43 */
44 protected static final String LOADERCLASSNAME = "loaderClassName"; //$NON-NLS-1$
45
46 // ------------------------------------------------------------------------
47 // Attributes
48 // ------------------------------------------------------------------------
49 /**
50 * The criteria reference.
51 */
52 protected Criteria fCriteria;
53 /**
54 * Flag whether this criteria is active or not
55 */
56 protected boolean fIsActive;
57 /**
58 * Flag whether this criteria is for positive filter or not
59 */
60 protected boolean fIsPositive;
61 /**
62 * The loader class name.
63 */
64 protected String fLoaderClassName;
65
66 // ------------------------------------------------------------------------
67 // Constructor
68 // ------------------------------------------------------------------------
69 /**
70 * Standard constructor
71 *
72 * @param criteria A criteria reference
73 * @param isActive <code>true</code> if filter criteria is active else <code>false</code>
74 * @param isPositive <code>true</code> for positive filter else <code>false</code>
75 */
76 public FilterCriteria(Criteria criteria, boolean isActive, boolean isPositive) {
77 this(criteria, isActive, isPositive, null);
78 }
79
80 /**
81 * Constructor
82 *
83 * @param criteria A criteria reference
84 * @param isActive <code>true</code> if filter criteria is active else <code>false</code>
85 * @param isPositive <code>true</code> for positive filter else <code>false</code>
86 * @param loaderClassName A loader class name
87 */
88 public FilterCriteria(Criteria criteria, boolean isActive, boolean isPositive, String loaderClassName) {
89 fCriteria = criteria;
90 fIsActive = isActive;
91 fIsPositive = isPositive;
92 fLoaderClassName = loaderClassName;
93 }
94
95 /**
96 * Copy Constructor
97 * @param other FilterCriteria
98 */
99 public FilterCriteria (FilterCriteria other) {
100 fCriteria = new Criteria(other.fCriteria);
101 fIsActive = other.fIsActive;
102 fIsPositive = other.fIsPositive;
103 fLoaderClassName = other.fLoaderClassName;
104 }
105
106 /**
107 * Default constructor
108 */
109 protected FilterCriteria() {
110 }
111
112 // ------------------------------------------------------------------------
113 // Methods
114 // ------------------------------------------------------------------------
115 /*
116 * (non-Javadoc)
117 * @see java.lang.Object#toString()
118 */
119 @Override
120 public String toString() {
121 StringBuffer sb = new StringBuffer(super.toString());
122 sb.append(':');
123 if (fCriteria != null) {
124 sb.append(" expression=");sb.append(fCriteria.getExpression()); //$NON-NLS-1$
125 sb.append(" active=");sb.append(fIsActive); //$NON-NLS-1$
126 sb.append(" positive=");sb.append(fIsPositive); //$NON-NLS-1$
127 } else {
128 sb.append("empty criteria"); //$NON-NLS-1$
129 }
130 return sb.toString();
131 }
132
133 /**
134 * Sets a criteria reference.
135 * @param criteria A criteria reference
136 */
137 public void setCriteria(Criteria criteria) {
138 fCriteria = criteria;
139 }
140
141 /**
142 * Returns the criteria reference.
143 *
144 * @return the criteria reference
145 */
146 public Criteria getCriteria() {
147 return fCriteria;
148 }
149
150 /**
151 * Sets the active flag.
152 *
153 * @param isActive A active value.
154 */
155 public void setActive(boolean isActive) {
156 fIsActive = isActive;
157 }
158
159 /**
160 * Returns whether filter criteria is active or not.
161 *
162 * @return whether filter criteria is active or not.
163 */
164 public boolean isActive() {
165 return fIsActive;
166 }
167
168 /**
169 * Sets filter is for positive filtering or not.
170 *
171 * @param isPositive The value to set.
172 */
173 public void setPositive(boolean isPositive) {
174 fIsPositive = isPositive;
175 }
176
177 /**
178 * Returns whether the filter si for positive filtering or not.
179 *
180 * @return Returns the positive.
181 */
182 public boolean isPositive() {
183 return fIsPositive;
184 }
185
186 /**
187 * Sets the loader class name for this filter.
188 *
189 * @param loaderClassName The loader class name to set
190 */
191 public void setLoaderClassName(String loaderClassName) {
192 fLoaderClassName = loaderClassName;
193 }
194
195 /**
196 * Returns the class loader name.
197 *
198 * @return the class loader name.
199 */
200 public String getLoaderClassName() {
201 return fLoaderClassName;
202 }
203
204 /**
205 * Finds a filter criteria within a list of criteria.
206 *
207 * @param what The filter to find
208 * @param list A list of filter criteria
209 * @return The found filter criteria or null
210 */
211 public static FilterCriteria find(FilterCriteria what, List<FilterCriteria> list) {
212 if (what != null && list != null) {
213 try {
214 for (Iterator<FilterCriteria> i = list.iterator(); i.hasNext();) {
215 FilterCriteria fc = (FilterCriteria) i.next();
216 if (what.compareTo(fc)) {
217 return fc;
218 }
219 }
220 } catch (Exception e) {
221 // Silence
222 }
223 }
224 return null;
225 }
226
227 /**
228 * Compares this filter criteria with a given criteria.
229 *
230 * @param to The filter criteria to compare.
231 * @return usual comparison result (< 0, 0, > 0)
232 */
233 public boolean compareTo(FilterCriteria to) {
234 if (isPositive() == to.isPositive() && getCriteria().compareTo(to.getCriteria())) {
235 if (getLoaderClassName() == null && to.getLoaderClassName() == null) {
236 return true;
237 }
238 if ((getLoaderClassName() != null && to.getLoaderClassName() != null) && getLoaderClassName().equals(to.getLoaderClassName())) {
239 return true;
240 }
241 }
242 return false;
243 }
244
245 /**
246 * Saves current criteria attributes in the dialog settings.
247 *
248 * @param settings The dialog settings
249 */
250 public void save(DialogSettings settings) {
251 settings.put(ACTIVE, isActive());
252 settings.put(POSITIVE, isPositive());
253 if (getLoaderClassName() != null) {
254 settings.put(LOADERCLASSNAME, getLoaderClassName());
255 } else {
256 settings.put(LOADERCLASSNAME, ""); //$NON-NLS-1$
257 }
258 if (fCriteria != null) {
259 fCriteria.save(settings);
260 }
261 }
262
263 /**
264 * Loads the criteria with values of the dialog settings.
265 *
266 * @param settings The dialog settings
267 */
268 public void load(DialogSettings settings) {
269 setActive(settings.getBoolean(ACTIVE));
270 setPositive(settings.getBoolean(POSITIVE));
271 String loaderClassName = settings.get(LOADERCLASSNAME);
272 setLoaderClassName(loaderClassName != null && loaderClassName.length() > 0 ? loaderClassName : null);
273 if (fCriteria != null) {
274 fCriteria.load(settings);
275 }
276 }
277 }
This page took 0.03863 seconds and 5 git commands to generate.