Commit | Line | Data |
---|---|---|
73005152 | 1 | /********************************************************************** |
df0b8ff4 BH |
2 | * Copyright (c) 2005, 2008 IBM Corporation and others. |
3 | * Copyright (c) 2011, 2012 Ericsson. | |
abbdd66a | 4 | * |
73005152 BH |
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 | |
abbdd66a AM |
9 | * |
10 | * Contributors: | |
73005152 BH |
11 | * IBM - Initial API and implementation |
12 | * Bernd Hufmann - Updated for TMF | |
13 | **********************************************************************/ | |
df0b8ff4 | 14 | package org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs; |
73005152 BH |
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. | |
abbdd66a | 23 | * |
df0b8ff4 BH |
24 | * @version 1.0 |
25 | * @author sveyrier | |
abbdd66a | 26 | * |
73005152 BH |
27 | */ |
28 | public class FilterCriteria { | |
29 | ||
df0b8ff4 BH |
30 | // ------------------------------------------------------------------------ |
31 | // Constants | |
32 | // ------------------------------------------------------------------------ | |
33 | /** | |
34 | * The filter state value for 'active'. | |
35 | */ | |
73005152 | 36 | protected static final String ACTIVE = "active"; //$NON-NLS-1$ |
df0b8ff4 | 37 | /** |
abbdd66a | 38 | * The property value for positive filter. |
df0b8ff4 | 39 | */ |
73005152 | 40 | protected static final String POSITIVE = "positive"; //$NON-NLS-1$ |
df0b8ff4 | 41 | /** |
abbdd66a | 42 | * The filter loader class name property. |
df0b8ff4 | 43 | */ |
73005152 BH |
44 | protected static final String LOADERCLASSNAME = "loaderClassName"; //$NON-NLS-1$ |
45 | ||
df0b8ff4 BH |
46 | // ------------------------------------------------------------------------ |
47 | // Attributes | |
48 | // ------------------------------------------------------------------------ | |
49 | /** | |
abbdd66a | 50 | * The criteria reference. |
df0b8ff4 | 51 | */ |
eb63f5ff | 52 | protected Criteria fCriteria; |
df0b8ff4 BH |
53 | /** |
54 | * Flag whether this criteria is active or not | |
55 | */ | |
eb63f5ff | 56 | protected boolean fIsActive; |
df0b8ff4 BH |
57 | /** |
58 | * Flag whether this criteria is for positive filter or not | |
59 | */ | |
eb63f5ff | 60 | protected boolean fIsPositive; |
df0b8ff4 BH |
61 | /** |
62 | * The loader class name. | |
63 | */ | |
eb63f5ff | 64 | protected String fLoaderClassName; |
73005152 | 65 | |
df0b8ff4 BH |
66 | // ------------------------------------------------------------------------ |
67 | // Constructor | |
68 | // ------------------------------------------------------------------------ | |
73005152 | 69 | /** |
df0b8ff4 | 70 | * Standard constructor |
abbdd66a | 71 | * |
eb63f5ff | 72 | * @param criteria A criteria reference |
abbdd66a | 73 | * @param isActive <code>true</code> if filter criteria is active else <code>false</code> |
eb63f5ff | 74 | * @param isPositive <code>true</code> for positive filter else <code>false</code> |
73005152 | 75 | */ |
eb63f5ff BH |
76 | public FilterCriteria(Criteria criteria, boolean isActive, boolean isPositive) { |
77 | this(criteria, isActive, isPositive, null); | |
73005152 BH |
78 | } |
79 | ||
80 | /** | |
df0b8ff4 | 81 | * Constructor |
abbdd66a | 82 | * |
eb63f5ff | 83 | * @param criteria A criteria reference |
abbdd66a | 84 | * @param isActive <code>true</code> if filter criteria is active else <code>false</code> |
eb63f5ff BH |
85 | * @param isPositive <code>true</code> for positive filter else <code>false</code> |
86 | * @param loaderClassName A loader class name | |
73005152 | 87 | */ |
eb63f5ff BH |
88 | public FilterCriteria(Criteria criteria, boolean isActive, boolean isPositive, String loaderClassName) { |
89 | fCriteria = criteria; | |
90 | fIsActive = isActive; | |
91 | fIsPositive = isPositive; | |
92 | fLoaderClassName = loaderClassName; | |
73005152 BH |
93 | } |
94 | ||
95 | /** | |
96 | * Copy Constructor | |
97 | * @param other FilterCriteria | |
98 | */ | |
99 | public FilterCriteria (FilterCriteria other) { | |
eb63f5ff BH |
100 | fCriteria = new Criteria(other.fCriteria); |
101 | fIsActive = other.fIsActive; | |
102 | fIsPositive = other.fIsPositive; | |
103 | fLoaderClassName = other.fLoaderClassName; | |
73005152 BH |
104 | } |
105 | ||
df0b8ff4 BH |
106 | /** |
107 | * Default constructor | |
108 | */ | |
73005152 BH |
109 | protected FilterCriteria() { |
110 | } | |
111 | ||
df0b8ff4 BH |
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()); | |
eb63f5ff BH |
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$ | |
df0b8ff4 BH |
127 | } else { |
128 | sb.append("empty criteria"); //$NON-NLS-1$ | |
129 | } | |
130 | return sb.toString(); | |
131 | } | |
132 | ||
73005152 | 133 | /** |
df0b8ff4 | 134 | * Sets a criteria reference. |
eb63f5ff | 135 | * @param criteria A criteria reference |
73005152 | 136 | */ |
eb63f5ff BH |
137 | public void setCriteria(Criteria criteria) { |
138 | fCriteria = criteria; | |
73005152 BH |
139 | } |
140 | ||
df0b8ff4 BH |
141 | /** |
142 | * Returns the criteria reference. | |
abbdd66a | 143 | * |
df0b8ff4 BH |
144 | * @return the criteria reference |
145 | */ | |
73005152 | 146 | public Criteria getCriteria() { |
eb63f5ff | 147 | return fCriteria; |
73005152 BH |
148 | } |
149 | ||
150 | /** | |
df0b8ff4 | 151 | * Sets the active flag. |
abbdd66a | 152 | * |
eb63f5ff | 153 | * @param isActive A active value. |
73005152 | 154 | */ |
eb63f5ff BH |
155 | public void setActive(boolean isActive) { |
156 | fIsActive = isActive; | |
73005152 BH |
157 | } |
158 | ||
df0b8ff4 BH |
159 | /** |
160 | * Returns whether filter criteria is active or not. | |
abbdd66a | 161 | * |
df0b8ff4 BH |
162 | * @return whether filter criteria is active or not. |
163 | */ | |
73005152 | 164 | public boolean isActive() { |
eb63f5ff | 165 | return fIsActive; |
73005152 BH |
166 | } |
167 | ||
168 | /** | |
df0b8ff4 | 169 | * Sets filter is for positive filtering or not. |
abbdd66a | 170 | * |
eb63f5ff | 171 | * @param isPositive The value to set. |
73005152 | 172 | */ |
eb63f5ff BH |
173 | public void setPositive(boolean isPositive) { |
174 | fIsPositive = isPositive; | |
73005152 BH |
175 | } |
176 | ||
177 | /** | |
df0b8ff4 | 178 | * Returns whether the filter si for positive filtering or not. |
abbdd66a | 179 | * |
73005152 BH |
180 | * @return Returns the positive. |
181 | */ | |
182 | public boolean isPositive() { | |
eb63f5ff | 183 | return fIsPositive; |
73005152 BH |
184 | } |
185 | ||
186 | /** | |
df0b8ff4 | 187 | * Sets the loader class name for this filter. |
abbdd66a | 188 | * |
eb63f5ff | 189 | * @param loaderClassName The loader class name to set |
73005152 | 190 | */ |
eb63f5ff BH |
191 | public void setLoaderClassName(String loaderClassName) { |
192 | fLoaderClassName = loaderClassName; | |
73005152 BH |
193 | } |
194 | ||
195 | /** | |
df0b8ff4 | 196 | * Returns the class loader name. |
abbdd66a | 197 | * |
df0b8ff4 | 198 | * @return the class loader name. |
73005152 BH |
199 | */ |
200 | public String getLoaderClassName() { | |
eb63f5ff | 201 | return fLoaderClassName; |
73005152 BH |
202 | } |
203 | ||
df0b8ff4 BH |
204 | /** |
205 | * Finds a filter criteria within a list of criteria. | |
abbdd66a | 206 | * |
df0b8ff4 BH |
207 | * @param what The filter to find |
208 | * @param list A list of filter criteria | |
209 | * @return The found filter criteria or null | |
210 | */ | |
73005152 BH |
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();) { | |
abbdd66a | 215 | FilterCriteria fc = i.next(); |
73005152 BH |
216 | if (what.compareTo(fc)) { |
217 | return fc; | |
218 | } | |
219 | } | |
220 | } catch (Exception e) { | |
221 | // Silence | |
222 | } | |
223 | } | |
224 | return null; | |
225 | } | |
226 | ||
df0b8ff4 BH |
227 | /** |
228 | * Compares this filter criteria with a given criteria. | |
abbdd66a | 229 | * |
df0b8ff4 BH |
230 | * @param to The filter criteria to compare. |
231 | * @return usual comparison result (< 0, 0, > 0) | |
232 | */ | |
73005152 BH |
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 | ||
df0b8ff4 BH |
245 | /** |
246 | * Saves current criteria attributes in the dialog settings. | |
abbdd66a | 247 | * |
df0b8ff4 BH |
248 | * @param settings The dialog settings |
249 | */ | |
73005152 BH |
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 | } | |
eb63f5ff BH |
258 | if (fCriteria != null) { |
259 | fCriteria.save(settings); | |
df0b8ff4 | 260 | } |
73005152 BH |
261 | } |
262 | ||
263 | /** | |
df0b8ff4 | 264 | * Loads the criteria with values of the dialog settings. |
abbdd66a | 265 | * |
df0b8ff4 | 266 | * @param settings The dialog settings |
73005152 BH |
267 | */ |
268 | public void load(DialogSettings settings) { | |
269 | setActive(settings.getBoolean(ACTIVE)); | |
270 | setPositive(settings.getBoolean(POSITIVE)); | |
eb63f5ff BH |
271 | String loaderClassName = settings.get(LOADERCLASSNAME); |
272 | setLoaderClassName(loaderClassName != null && loaderClassName.length() > 0 ? loaderClassName : null); | |
273 | if (fCriteria != null) { | |
274 | fCriteria.load(settings); | |
df0b8ff4 | 275 | } |
73005152 BH |
276 | } |
277 | } |