1 /**********************************************************************
2 * Copyright (c) 2005, 2006 IBM Corporation and others.
3 * Copyright (c) 2011, 2012 Ericsson.
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
11 * IBM - Initial API and implementation
12 * Bernd Hufmann - Updated for TMF
13 **********************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.uml2sd
.dialogs
;
16 import java
.util
.ArrayList
;
17 import java
.util
.Iterator
;
18 import java
.util
.regex
.Pattern
;
19 import java
.util
.regex
.PatternSyntaxException
;
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
;
27 * This class describes the find or filter criteria selected by the user in the find or filter dialog box
31 * @author Bernd Hufmann
33 public class Criteria
{
35 // ------------------------------------------------------------------------
37 // ------------------------------------------------------------------------
39 * Flag whether lifeline is selected or not.
41 protected boolean lifeLineSelected
= false;
43 * Flag whether synchronous message is selected or not.
45 protected boolean syncMessageSelected
= false;
47 * Flag whether synchronous message return is selected or not.
49 protected boolean syncMessageReturnSelected
= false;
51 * Flag whether asynchronous message is selected or not.
53 protected boolean asyncMessageSelected
= false;
55 * Flag whether asynchronous message return is selected or not.
57 protected boolean asyncMessageReturnSelected
= false;
59 * Flag whether case sensitive find is required or not.
61 protected boolean caseSenstiveSelected
= false;
63 * Flag whether stop graph node is selected or not.
65 protected boolean stopSelected
= false;
67 * The find expression.
69 protected String expression
= null;
71 * The find pattern as regular expression.
73 protected Pattern pattern
= null;
75 // ------------------------------------------------------------------------
77 // ------------------------------------------------------------------------
88 * @param other Criteria to create new criteria
90 public Criteria (Criteria other
) {
91 this.lifeLineSelected
= other
.lifeLineSelected
;
92 this.syncMessageSelected
= other
.syncMessageSelected
;
93 this.syncMessageReturnSelected
= other
.syncMessageReturnSelected
;
94 this.asyncMessageSelected
= other
.asyncMessageSelected
;
95 this.asyncMessageReturnSelected
= other
.asyncMessageReturnSelected
;
96 this.caseSenstiveSelected
= other
.caseSenstiveSelected
;
97 this.stopSelected
= other
.stopSelected
;
98 setExpression(other
.expression
);
101 // ------------------------------------------------------------------------
103 // ------------------------------------------------------------------------
106 * Returns true if the AsyncMessageReturn is selected, false otherwise.
108 * @return true if the AsyncMessageReturn is selected, false otherwise
110 public boolean isAsyncMessageReturnSelected() {
111 return asyncMessageReturnSelected
;
115 * Returns true if the AsyncMessage is selected, false otherwise.
117 * @return true if the AsyncMessage is selected, false otherwise
119 public boolean isAsyncMessageSelected() {
120 return asyncMessageSelected
;
124 * Returns the text enter by the user.
126 * @return the expression text
128 public String
getExpression() {
133 * Returns the regular expression pattern.
135 * @return the regular expression pattern
137 public Pattern
getPattern() {
142 * Sets the regular expression pattern.
144 * @param the pattern to set
146 public void setPattern(Pattern pattern
) {
147 this.pattern
= pattern
;
151 * Returns true if the LifeLine is selected, false otherwise.
153 * @return true if the LifeLine is selected, false otherwise
155 public boolean isLifeLineSelected() {
156 return lifeLineSelected
;
160 * Returns true if the Stop is selected, false otherwise.
162 * @return true if the Stop is selected, false otherwise
164 public boolean isStopSelected() {
169 * Returns true if the SyncMessageReturn is selected, false otherwise.
171 * @return true if the SyncMessageReturn is selected, false otherwise
173 public boolean isSyncMessageReturnSelected() {
174 return syncMessageReturnSelected
;
178 * Returns true if the SyncMessage is selected, false otherwise.
180 * @return true if the SyncMessage is selected, false otherwise
182 public boolean isSyncMessageSelected() {
183 return syncMessageSelected
;
187 * Sets the AsyncMessageReturn selection state.
189 * @param b true if selected, false otherwise
191 public void setAsyncMessageReturnSelected(boolean b
) {
192 asyncMessageReturnSelected
= b
;
196 * Sets the AsyncMessage selection state.
198 * @param b true if selected, false otherwise
200 public void setAsyncMessageSelected(boolean b
) {
201 asyncMessageSelected
= b
;
205 * Sets the text entered by the user and compiles the regular expression.
207 * @param string the text
209 public void setExpression(String string
) {
211 if (expression
!= null) {
213 if (caseSenstiveSelected
) {
214 pattern
= Pattern
.compile(string
);
217 pattern
= Pattern
.compile(string
, Pattern
.CASE_INSENSITIVE
);
219 } catch (PatternSyntaxException e
) {
229 * Sets the Stop selection state.
231 * @param b true if selected, false otherwise
233 public void setLifeLineSelected(boolean b
) {
234 lifeLineSelected
= b
;
238 * Set Stop selection state.
240 * @param b true if selected, false otherwise
242 public void setStopSelected(boolean b
) {
247 * Sets the SyncMessageReturn selection state.
249 * @param b true if selected, false otherwise
251 public void setSyncMessageReturnSelected(boolean b
) {
252 syncMessageReturnSelected
= b
;
256 * Sets the SyncMessage selection state.
258 * @param b true if selected, false otherwise
260 public void setSyncMessageSelected(boolean b
) {
261 syncMessageSelected
= b
;
265 * Returns true if the case sensitive is selected, false otherwise.
267 * @return true if the case sensitive is selected, false otherwise
269 public boolean isCaseSenstiveSelected() {
270 return caseSenstiveSelected
;
274 * Set case sensitive selection state.
276 * @param b true if selected, false otherwise
278 public void setCaseSenstiveSelected(boolean b
) {
279 caseSenstiveSelected
= b
;
280 // Make sure that pattern is set
281 setExpression(expression
);
285 * Compares this criteria with a given criteria.
287 * @param to The criteria to compare
288 * @return usual comparison result (< 0, 0, > 0)
290 public boolean compareTo(Criteria to
) {
291 boolean retVal
= true;
292 if (getExpression() != null) {
293 retVal
= getExpression().equals(to
.getExpression());
294 } else if (to
.getExpression() != null) {
295 retVal
= to
.getExpression().equals(getExpression());
297 return retVal
&& isCaseSenstiveSelected() == to
.isCaseSenstiveSelected() && isAsyncMessageReturnSelected() == to
.isAsyncMessageReturnSelected() && isAsyncMessageSelected() == to
.isAsyncMessageSelected()
298 && isLifeLineSelected() == to
.isLifeLineSelected() && isStopSelected() == to
.isStopSelected() && isSyncMessageReturnSelected() == to
.isSyncMessageReturnSelected() && isSyncMessageSelected() == to
.isSyncMessageSelected();
302 * Saves current criteria attributes in the dialog settings.
304 * @param settings The dialog settings
306 public void save(DialogSettings settings
) {
307 settings
.put("expression", getExpression()); //$NON-NLS-1$
308 settings
.put("isCaseSenstiveSelected", isCaseSenstiveSelected()); //$NON-NLS-1$
309 settings
.put("isAsyncMessageReturnSelected", isAsyncMessageReturnSelected()); //$NON-NLS-1$
310 settings
.put("isAsyncMessageSelected", isAsyncMessageSelected()); //$NON-NLS-1$
311 settings
.put("isLifeLineSelected", isLifeLineSelected()); //$NON-NLS-1$
312 settings
.put("isStopSelected", isStopSelected()); //$NON-NLS-1$
313 settings
.put("isSyncMessageReturnSelected", isSyncMessageReturnSelected()); //$NON-NLS-1$
314 settings
.put("isSyncMessageSelected", isSyncMessageSelected()); //$NON-NLS-1$
318 * Loads the criteria with values of the dialog settings.
320 * @param settings The dialog settings
322 public void load(DialogSettings settings
) {
323 setExpression(settings
.get("expression")); //$NON-NLS-1$
324 setCaseSenstiveSelected(settings
.getBoolean("isCaseSenstiveSelected")); //$NON-NLS-1$
325 setAsyncMessageReturnSelected(settings
.getBoolean("isAsyncMessageReturnSelected")); //$NON-NLS-1$
326 setAsyncMessageSelected(settings
.getBoolean("isAsyncMessageSelected")); //$NON-NLS-1$
327 setLifeLineSelected(settings
.getBoolean("isLifeLineSelected")); //$NON-NLS-1$
328 setStopSelected(settings
.getBoolean("isStopSelected")); //$NON-NLS-1$
329 setSyncMessageReturnSelected(settings
.getBoolean("isSyncMessageReturnSelected")); //$NON-NLS-1$
330 setSyncMessageSelected(settings
.getBoolean("isSyncMessageSelected")); //$NON-NLS-1$
334 * Gets the summary of supported graph nodes.
336 * @param provider A filter provider
337 * @param loaderClassName A class loader
338 * @return graph node summary
340 public String
getGraphNodeSummary(ISDFilterProvider provider
, String loaderClassName
) {
341 ArrayList
<String
> list
= new ArrayList
<String
>();
343 if (provider
!= null) {
344 if (isLifeLineSelected()) {
345 list
.add(provider
.getNodeName(ISDGraphNodeSupporter
.LIFELINE
, loaderClassName
));
347 if (isSyncMessageSelected()) {
348 list
.add(provider
.getNodeName(ISDGraphNodeSupporter
.SYNCMESSAGE
, loaderClassName
));
350 if (isSyncMessageReturnSelected()) {
351 list
.add(provider
.getNodeName(ISDGraphNodeSupporter
.SYNCMESSAGERETURN
, loaderClassName
));
353 if (isAsyncMessageSelected()) {
354 list
.add(provider
.getNodeName(ISDGraphNodeSupporter
.ASYNCMESSAGE
, loaderClassName
));
356 if (isAsyncMessageReturnSelected()) {
357 list
.add(provider
.getNodeName(ISDGraphNodeSupporter
.ASYNCMESSAGERETURN
, loaderClassName
));
359 if (isStopSelected()) {
360 list
.add(provider
.getNodeName(ISDGraphNodeSupporter
.STOP
, loaderClassName
));
363 if (isLifeLineSelected()) {
364 list
.add(SDMessages
._28
);
366 if (isSyncMessageSelected()) {
367 list
.add(SDMessages
._30
);
369 if (isSyncMessageReturnSelected()) {
370 list
.add(SDMessages
._31
);
372 if (isAsyncMessageSelected()) {
373 list
.add(SDMessages
._32
);
375 if (isAsyncMessageReturnSelected()) {
376 list
.add(SDMessages
._33
);
378 if (isStopSelected()) {
379 list
.add(SDMessages
._29
);
383 String ret
= "", prefix
= "["; //$NON-NLS-1$ //$NON-NLS-2$
384 for (Iterator
<String
> i
= list
.iterator(); i
.hasNext();) {
385 String s
= (String
) i
.next();
387 prefix
= " " + SDMessages
._34
+ " "; //$NON-NLS-1$ //$NON-NLS-2$
389 ret
+= "]"; //$NON-NLS-1$
394 * Matches given string using compiled pattern based on user expression.
396 * @param stringToMatch A string to match
397 * @return true if string matches expression
399 public boolean matches(String stringToMatch
) {
400 if (pattern
== null) {
403 return pattern
.matcher(stringToMatch
).matches();