tmf: Continue upating the Javadoc in tmf.ui
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / uml2sd / dialogs / Criteria.java
1 /**********************************************************************
2 * Copyright (c) 2005, 2006 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.ArrayList;
17 import java.util.Iterator;
18 import java.util.regex.Pattern;
19 import java.util.regex.PatternSyntaxException;
20
21 import org.eclipse.jface.dialogs.DialogSettings;
22 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.provider.ISDFilterProvider;
23 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.provider.ISDGraphNodeSupporter;
24 import org.eclipse.linuxtools.tmf.ui.views.uml2sd.util.SDMessages;
25
26 /**
27 * This class describes the find or filter criteria selected by the user in the find or filter dialog box
28 *
29 * @version 1.0
30 * @author sveyrier
31 * @author Bernd Hufmann
32 */
33 public class Criteria {
34
35 // ------------------------------------------------------------------------
36 // Attributes
37 // ------------------------------------------------------------------------
38 /**
39 * Flag whether lifeline is selected or not.
40 */
41 protected boolean fLifeLineSelected = false;
42 /**
43 * Flag whether synchronous message is selected or not.
44 */
45 protected boolean fSyncMessageSelected = false;
46 /**
47 * Flag whether synchronous message return is selected or not.
48 */
49 protected boolean fSyncMessageReturnSelected = false;
50 /**
51 * Flag whether asynchronous message is selected or not.
52 */
53 protected boolean fAsyncMessageSelected = false;
54 /**
55 * Flag whether asynchronous message return is selected or not.
56 */
57 protected boolean fAsyncMessageReturnSelected = false;
58 /**
59 * Flag whether case sensitive find is required or not.
60 */
61 protected boolean fCaseSenstiveSelected = false;
62 /**
63 * Flag whether stop graph node is selected or not.
64 */
65 protected boolean fStopSelected = false;
66 /**
67 * The find expression.
68 */
69 protected String fExpression = null;
70 /**
71 * The find pattern as regular expression.
72 */
73 protected Pattern pattern = null;
74
75 // ------------------------------------------------------------------------
76 // Constructors
77 // ------------------------------------------------------------------------
78
79 /**
80 * Default constructor
81 */
82 public Criteria () {
83 }
84
85 /**
86 * Copy constructor
87 *
88 * @param other Criteria to create new criteria
89 */
90 public Criteria (Criteria other) {
91 this.fLifeLineSelected = other.fLifeLineSelected;
92 this.fSyncMessageSelected = other.fSyncMessageSelected;
93 this.fSyncMessageReturnSelected = other.fSyncMessageReturnSelected;
94 this.fAsyncMessageSelected = other.fAsyncMessageSelected;
95 this.fAsyncMessageReturnSelected = other.fAsyncMessageReturnSelected;
96 this.fCaseSenstiveSelected = other.fCaseSenstiveSelected;
97 this.fStopSelected = other.fStopSelected;
98 fExpression = other.fExpression;
99 updatePattern();
100 }
101
102 // ------------------------------------------------------------------------
103 // Methods
104 // ------------------------------------------------------------------------
105
106 /**
107 * Returns true if the AsyncMessageReturn is selected, false otherwise.
108 *
109 * @return true if the AsyncMessageReturn is selected, false otherwise
110 */
111 public boolean isAsyncMessageReturnSelected() {
112 return fAsyncMessageReturnSelected;
113 }
114
115 /**
116 * Returns true if the AsyncMessage is selected, false otherwise.
117 *
118 * @return true if the AsyncMessage is selected, false otherwise
119 */
120 public boolean isAsyncMessageSelected() {
121 return fAsyncMessageSelected;
122 }
123
124 /**
125 * Returns the text enter by the user.
126 *
127 * @return the expression text
128 */
129 public String getExpression() {
130 return fExpression;
131 }
132
133 /**
134 * Returns the regular expression pattern.
135 *
136 * @return the regular expression pattern
137 */
138 public Pattern getPattern() {
139 return pattern;
140 }
141
142 /**
143 * Sets the regular expression pattern.
144 *
145 * @param pattern
146 * The pattern to set
147 */
148 public void setPattern(Pattern pattern) {
149 this.pattern = pattern;
150 }
151
152 /**
153 * Returns true if the LifeLine is selected, false otherwise.
154 *
155 * @return true if the LifeLine is selected, false otherwise
156 */
157 public boolean isLifeLineSelected() {
158 return fLifeLineSelected;
159 }
160
161 /**
162 * Returns true if the Stop is selected, false otherwise.
163 *
164 * @return true if the Stop is selected, false otherwise
165 */
166 public boolean isStopSelected() {
167 return fStopSelected;
168 }
169
170 /**
171 * Returns true if the SyncMessageReturn is selected, false otherwise.
172 *
173 * @return true if the SyncMessageReturn is selected, false otherwise
174 */
175 public boolean isSyncMessageReturnSelected() {
176 return fSyncMessageReturnSelected;
177 }
178
179 /**
180 * Returns true if the SyncMessage is selected, false otherwise.
181 *
182 * @return true if the SyncMessage is selected, false otherwise
183 */
184 public boolean isSyncMessageSelected() {
185 return fSyncMessageSelected;
186 }
187
188 /**
189 * Sets the AsyncMessageReturn selection state.
190 *
191 * @param b true if selected, false otherwise
192 */
193 public void setAsyncMessageReturnSelected(boolean b) {
194 fAsyncMessageReturnSelected = b;
195 }
196
197 /**
198 * Sets the AsyncMessage selection state.
199 *
200 * @param b true if selected, false otherwise
201 */
202 public void setAsyncMessageSelected(boolean b) {
203 fAsyncMessageSelected = b;
204 }
205
206 /**
207 * Sets the text entered by the user and compiles the regular expression.
208 *
209 * @param string the text
210 */
211 public void setExpression(String string) {
212 fExpression = string;
213 updatePattern();
214 }
215
216 /**
217 * Sets the Stop selection state.
218 *
219 * @param b true if selected, false otherwise
220 */
221 public void setLifeLineSelected(boolean b) {
222 fLifeLineSelected = b;
223 }
224
225 /**
226 * Set Stop selection state.
227 *
228 * @param b true if selected, false otherwise
229 */
230 public void setStopSelected(boolean b) {
231 fStopSelected = b;
232 }
233
234 /**
235 * Sets the SyncMessageReturn selection state.
236 *
237 * @param b true if selected, false otherwise
238 */
239 public void setSyncMessageReturnSelected(boolean b) {
240 fSyncMessageReturnSelected = b;
241 }
242
243 /**
244 * Sets the SyncMessage selection state.
245 *
246 * @param b true if selected, false otherwise
247 */
248 public void setSyncMessageSelected(boolean b) {
249 fSyncMessageSelected = b;
250 }
251
252 /**
253 * Returns true if the case sensitive is selected, false otherwise.
254 *
255 * @return true if the case sensitive is selected, false otherwise
256 */
257 public boolean isCaseSenstiveSelected() {
258 return fCaseSenstiveSelected;
259 }
260
261 /**
262 * Set case sensitive selection state.
263 *
264 * @param b true if selected, false otherwise
265 */
266 public void setCaseSenstiveSelected(boolean b) {
267 fCaseSenstiveSelected = b;
268 // Make sure that pattern is set
269 setExpression(fExpression);
270 }
271
272 /**
273 * Compares this criteria with a given criteria.
274 *
275 * @param to The criteria to compare
276 * @return usual comparison result (< 0, 0, > 0)
277 */
278 public boolean compareTo(Criteria to) {
279 boolean retVal = true;
280 if (getExpression() != null) {
281 retVal = getExpression().equals(to.getExpression());
282 } else if (to.getExpression() != null) {
283 retVal = to.getExpression().equals(getExpression());
284 }
285 return retVal && isCaseSenstiveSelected() == to.isCaseSenstiveSelected() && isAsyncMessageReturnSelected() == to.isAsyncMessageReturnSelected() && isAsyncMessageSelected() == to.isAsyncMessageSelected()
286 && isLifeLineSelected() == to.isLifeLineSelected() && isStopSelected() == to.isStopSelected() && isSyncMessageReturnSelected() == to.isSyncMessageReturnSelected() && isSyncMessageSelected() == to.isSyncMessageSelected();
287 }
288
289 /**
290 * Saves current criteria attributes in the dialog settings.
291 *
292 * @param settings The dialog settings
293 */
294 public void save(DialogSettings settings) {
295 settings.put("expression", getExpression()); //$NON-NLS-1$
296 settings.put("isCaseSenstiveSelected", isCaseSenstiveSelected()); //$NON-NLS-1$
297 settings.put("isAsyncMessageReturnSelected", isAsyncMessageReturnSelected()); //$NON-NLS-1$
298 settings.put("isAsyncMessageSelected", isAsyncMessageSelected()); //$NON-NLS-1$
299 settings.put("isLifeLineSelected", isLifeLineSelected()); //$NON-NLS-1$
300 settings.put("isStopSelected", isStopSelected()); //$NON-NLS-1$
301 settings.put("isSyncMessageReturnSelected", isSyncMessageReturnSelected()); //$NON-NLS-1$
302 settings.put("isSyncMessageSelected", isSyncMessageSelected()); //$NON-NLS-1$
303 }
304
305 /**
306 * Loads the criteria with values of the dialog settings.
307 *
308 * @param settings The dialog settings
309 */
310 public void load(DialogSettings settings) {
311 setExpression(settings.get("expression")); //$NON-NLS-1$
312 setCaseSenstiveSelected(settings.getBoolean("isCaseSenstiveSelected")); //$NON-NLS-1$
313 setAsyncMessageReturnSelected(settings.getBoolean("isAsyncMessageReturnSelected")); //$NON-NLS-1$
314 setAsyncMessageSelected(settings.getBoolean("isAsyncMessageSelected")); //$NON-NLS-1$
315 setLifeLineSelected(settings.getBoolean("isLifeLineSelected")); //$NON-NLS-1$
316 setStopSelected(settings.getBoolean("isStopSelected")); //$NON-NLS-1$
317 setSyncMessageReturnSelected(settings.getBoolean("isSyncMessageReturnSelected")); //$NON-NLS-1$
318 setSyncMessageSelected(settings.getBoolean("isSyncMessageSelected")); //$NON-NLS-1$
319 }
320
321 /**
322 * Gets the summary of supported graph nodes.
323 *
324 * @param provider A filter provider
325 * @param loaderClassName A class loader
326 * @return graph node summary
327 */
328 public String getGraphNodeSummary(ISDFilterProvider provider, String loaderClassName) {
329 ArrayList<String> list = new ArrayList<String>();
330
331 if (provider != null) {
332 if (isLifeLineSelected()) {
333 list.add(provider.getNodeName(ISDGraphNodeSupporter.LIFELINE, loaderClassName));
334 }
335 if (isSyncMessageSelected()) {
336 list.add(provider.getNodeName(ISDGraphNodeSupporter.SYNCMESSAGE, loaderClassName));
337 }
338 if (isSyncMessageReturnSelected()) {
339 list.add(provider.getNodeName(ISDGraphNodeSupporter.SYNCMESSAGERETURN, loaderClassName));
340 }
341 if (isAsyncMessageSelected()) {
342 list.add(provider.getNodeName(ISDGraphNodeSupporter.ASYNCMESSAGE, loaderClassName));
343 }
344 if (isAsyncMessageReturnSelected()) {
345 list.add(provider.getNodeName(ISDGraphNodeSupporter.ASYNCMESSAGERETURN, loaderClassName));
346 }
347 if (isStopSelected()) {
348 list.add(provider.getNodeName(ISDGraphNodeSupporter.STOP, loaderClassName));
349 }
350 } else {
351 if (isLifeLineSelected()) {
352 list.add(SDMessages._28);
353 }
354 if (isSyncMessageSelected()) {
355 list.add(SDMessages._30);
356 }
357 if (isSyncMessageReturnSelected()) {
358 list.add(SDMessages._31);
359 }
360 if (isAsyncMessageSelected()) {
361 list.add(SDMessages._32);
362 }
363 if (isAsyncMessageReturnSelected()) {
364 list.add(SDMessages._33);
365 }
366 if (isStopSelected()) {
367 list.add(SDMessages._29);
368 }
369 }
370 StringBuffer ret = new StringBuffer();
371 String prefix = "["; //$NON-NLS-1$
372 for (Iterator<String> i = list.iterator(); i.hasNext();) {
373 String s = (String) i.next();
374 ret.append(prefix);
375 ret.append(s);
376 prefix = " " + SDMessages._34 + " "; //$NON-NLS-1$ //$NON-NLS-2$
377 }
378 ret.append("]"); //$NON-NLS-1$
379 return ret.toString();
380 }
381
382 /**
383 * Matches given string using compiled pattern based on user expression.
384 *
385 * @param stringToMatch A string to match
386 * @return true if string matches expression
387 */
388 public boolean matches(String stringToMatch) {
389 if (pattern == null) {
390 return false;
391 }
392 return pattern.matcher(stringToMatch).matches();
393 }
394
395 /**
396 * Updates the regular expression pattern based on the expression.
397 */
398 private void updatePattern() {
399 if (fExpression != null) {
400 try {
401 if (fCaseSenstiveSelected) {
402 pattern = Pattern.compile(fExpression);
403 }
404 else {
405 pattern = Pattern.compile(fExpression, Pattern.CASE_INSENSITIVE);
406 }
407 } catch (PatternSyntaxException e) {
408 pattern = null;
409 }
410 }
411 else {
412 pattern = null;
413 }
414 }
415
416 }
This page took 0.040361 seconds and 6 git commands to generate.