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