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