Commit | Line | Data |
---|---|---|
73005152 | 1 | /********************************************************************** |
11252342 | 2 | * Copyright (c) 2005, 2013 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 | */ |
cab6c8ff | 51 | private Criteria fCriteria; |
df0b8ff4 BH |
52 | /** |
53 | * Flag whether this criteria is active or not | |
54 | */ | |
cab6c8ff | 55 | private boolean fIsActive; |
df0b8ff4 BH |
56 | /** |
57 | * Flag whether this criteria is for positive filter or not | |
58 | */ | |
cab6c8ff | 59 | private boolean fIsPositive; |
df0b8ff4 BH |
60 | /** |
61 | * The loader class name. | |
62 | */ | |
cab6c8ff | 63 | private 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 | // ------------------------------------------------------------------------ | |
11252342 | 114 | |
df0b8ff4 BH |
115 | @Override |
116 | public String toString() { | |
117 | StringBuffer sb = new StringBuffer(super.toString()); | |
eb63f5ff BH |
118 | sb.append(':'); |
119 | if (fCriteria != null) { | |
120 | sb.append(" expression=");sb.append(fCriteria.getExpression()); //$NON-NLS-1$ | |
121 | sb.append(" active=");sb.append(fIsActive); //$NON-NLS-1$ | |
122 | sb.append(" positive=");sb.append(fIsPositive); //$NON-NLS-1$ | |
df0b8ff4 BH |
123 | } else { |
124 | sb.append("empty criteria"); //$NON-NLS-1$ | |
125 | } | |
126 | return sb.toString(); | |
127 | } | |
128 | ||
73005152 | 129 | /** |
df0b8ff4 | 130 | * Sets a criteria reference. |
eb63f5ff | 131 | * @param criteria A criteria reference |
73005152 | 132 | */ |
eb63f5ff BH |
133 | public void setCriteria(Criteria criteria) { |
134 | fCriteria = criteria; | |
73005152 BH |
135 | } |
136 | ||
df0b8ff4 BH |
137 | /** |
138 | * Returns the criteria reference. | |
abbdd66a | 139 | * |
df0b8ff4 BH |
140 | * @return the criteria reference |
141 | */ | |
73005152 | 142 | public Criteria getCriteria() { |
eb63f5ff | 143 | return fCriteria; |
73005152 BH |
144 | } |
145 | ||
146 | /** | |
df0b8ff4 | 147 | * Sets the active flag. |
abbdd66a | 148 | * |
eb63f5ff | 149 | * @param isActive A active value. |
73005152 | 150 | */ |
eb63f5ff BH |
151 | public void setActive(boolean isActive) { |
152 | fIsActive = isActive; | |
73005152 BH |
153 | } |
154 | ||
df0b8ff4 BH |
155 | /** |
156 | * Returns whether filter criteria is active or not. | |
abbdd66a | 157 | * |
df0b8ff4 BH |
158 | * @return whether filter criteria is active or not. |
159 | */ | |
73005152 | 160 | public boolean isActive() { |
eb63f5ff | 161 | return fIsActive; |
73005152 BH |
162 | } |
163 | ||
164 | /** | |
df0b8ff4 | 165 | * Sets filter is for positive filtering or not. |
abbdd66a | 166 | * |
eb63f5ff | 167 | * @param isPositive The value to set. |
73005152 | 168 | */ |
eb63f5ff BH |
169 | public void setPositive(boolean isPositive) { |
170 | fIsPositive = isPositive; | |
73005152 BH |
171 | } |
172 | ||
173 | /** | |
df0b8ff4 | 174 | * Returns whether the filter si for positive filtering or not. |
abbdd66a | 175 | * |
73005152 BH |
176 | * @return Returns the positive. |
177 | */ | |
178 | public boolean isPositive() { | |
eb63f5ff | 179 | return fIsPositive; |
73005152 BH |
180 | } |
181 | ||
182 | /** | |
df0b8ff4 | 183 | * Sets the loader class name for this filter. |
abbdd66a | 184 | * |
eb63f5ff | 185 | * @param loaderClassName The loader class name to set |
73005152 | 186 | */ |
eb63f5ff BH |
187 | public void setLoaderClassName(String loaderClassName) { |
188 | fLoaderClassName = loaderClassName; | |
73005152 BH |
189 | } |
190 | ||
191 | /** | |
df0b8ff4 | 192 | * Returns the class loader name. |
abbdd66a | 193 | * |
df0b8ff4 | 194 | * @return the class loader name. |
73005152 BH |
195 | */ |
196 | public String getLoaderClassName() { | |
eb63f5ff | 197 | return fLoaderClassName; |
73005152 BH |
198 | } |
199 | ||
df0b8ff4 BH |
200 | /** |
201 | * Finds a filter criteria within a list of criteria. | |
abbdd66a | 202 | * |
df0b8ff4 BH |
203 | * @param what The filter to find |
204 | * @param list A list of filter criteria | |
205 | * @return The found filter criteria or null | |
206 | */ | |
73005152 BH |
207 | public static FilterCriteria find(FilterCriteria what, List<FilterCriteria> list) { |
208 | if (what != null && list != null) { | |
209 | try { | |
210 | for (Iterator<FilterCriteria> i = list.iterator(); i.hasNext();) { | |
abbdd66a | 211 | FilterCriteria fc = i.next(); |
73005152 BH |
212 | if (what.compareTo(fc)) { |
213 | return fc; | |
214 | } | |
215 | } | |
216 | } catch (Exception e) { | |
217 | // Silence | |
218 | } | |
219 | } | |
220 | return null; | |
221 | } | |
222 | ||
df0b8ff4 BH |
223 | /** |
224 | * Compares this filter criteria with a given criteria. | |
abbdd66a | 225 | * |
df0b8ff4 BH |
226 | * @param to The filter criteria to compare. |
227 | * @return usual comparison result (< 0, 0, > 0) | |
228 | */ | |
73005152 BH |
229 | public boolean compareTo(FilterCriteria to) { |
230 | if (isPositive() == to.isPositive() && getCriteria().compareTo(to.getCriteria())) { | |
231 | if (getLoaderClassName() == null && to.getLoaderClassName() == null) { | |
232 | return true; | |
233 | } | |
234 | if ((getLoaderClassName() != null && to.getLoaderClassName() != null) && getLoaderClassName().equals(to.getLoaderClassName())) { | |
235 | return true; | |
236 | } | |
237 | } | |
238 | return false; | |
239 | } | |
240 | ||
df0b8ff4 BH |
241 | /** |
242 | * Saves current criteria attributes in the dialog settings. | |
abbdd66a | 243 | * |
df0b8ff4 BH |
244 | * @param settings The dialog settings |
245 | */ | |
73005152 BH |
246 | public void save(DialogSettings settings) { |
247 | settings.put(ACTIVE, isActive()); | |
248 | settings.put(POSITIVE, isPositive()); | |
249 | if (getLoaderClassName() != null) { | |
250 | settings.put(LOADERCLASSNAME, getLoaderClassName()); | |
251 | } else { | |
252 | settings.put(LOADERCLASSNAME, ""); //$NON-NLS-1$ | |
253 | } | |
eb63f5ff BH |
254 | if (fCriteria != null) { |
255 | fCriteria.save(settings); | |
df0b8ff4 | 256 | } |
73005152 BH |
257 | } |
258 | ||
259 | /** | |
df0b8ff4 | 260 | * Loads the criteria with values of the dialog settings. |
abbdd66a | 261 | * |
df0b8ff4 | 262 | * @param settings The dialog settings |
73005152 BH |
263 | */ |
264 | public void load(DialogSettings settings) { | |
265 | setActive(settings.getBoolean(ACTIVE)); | |
266 | setPositive(settings.getBoolean(POSITIVE)); | |
eb63f5ff BH |
267 | String loaderClassName = settings.get(LOADERCLASSNAME); |
268 | setLoaderClassName(loaderClassName != null && loaderClassName.length() > 0 ? loaderClassName : null); | |
269 | if (fCriteria != null) { | |
270 | fCriteria.load(settings); | |
df0b8ff4 | 271 | } |
73005152 BH |
272 | } |
273 | } |