| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2013 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 | * Bernd Hufmann - Initial API and implementation |
| 10 | *******************************************************************************/ |
| 11 | |
| 12 | package org.eclipse.linuxtools.tmf.ui.tests.uml2sd.load; |
| 13 | |
| 14 | import java.net.URL; |
| 15 | import java.util.ArrayList; |
| 16 | import java.util.List; |
| 17 | |
| 18 | import org.eclipse.jface.action.Action; |
| 19 | import org.eclipse.jface.resource.ImageDescriptor; |
| 20 | import org.eclipse.jface.viewers.ISelection; |
| 21 | import org.eclipse.jface.viewers.StructuredSelection; |
| 22 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; |
| 23 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.SDView; |
| 24 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.AsyncMessageReturn; |
| 25 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.BasicExecutionOccurrence; |
| 26 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.EllipsisMessage; |
| 27 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.ExecutionOccurrence; |
| 28 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Frame; |
| 29 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.GraphNode; |
| 30 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.HotSpot; |
| 31 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Lifeline; |
| 32 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.LifelineCategories; |
| 33 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Stop; |
| 34 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.SyncMessage; |
| 35 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.SyncMessageReturn; |
| 36 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs.Criteria; |
| 37 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.dialogs.FilterCriteria; |
| 38 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IImage; |
| 39 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.provider.ISDFilterProvider; |
| 40 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.provider.ISDFindProvider; |
| 41 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.provider.ISDGraphNodeSupporter; |
| 42 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.handlers.provider.ISDPagingProvider; |
| 43 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.load.IUml2SDLoader; |
| 44 | import org.eclipse.swt.graphics.Image; |
| 45 | import org.eclipse.ui.ISelectionListener; |
| 46 | import org.eclipse.ui.IWorkbenchPart; |
| 47 | import org.eclipse.ui.PlatformUI; |
| 48 | |
| 49 | /** |
| 50 | * Test loader class. |
| 51 | */ |
| 52 | @SuppressWarnings("javadoc") |
| 53 | public class TestLoaders implements IUml2SDLoader, ISDFindProvider, ISDFilterProvider, ISDPagingProvider, ISelectionListener { |
| 54 | |
| 55 | public SDView v; |
| 56 | public int page; |
| 57 | private List<GraphNode> findResults = new ArrayList<GraphNode>(); |
| 58 | private Criteria findCriteria; |
| 59 | private int currentFindIndex = 0; |
| 60 | |
| 61 | private Frame savedFrame = null; |
| 62 | |
| 63 | public TestLoaders() { |
| 64 | this(""); //$NON-NLS-1$ |
| 65 | } |
| 66 | |
| 67 | @SuppressWarnings("unused") |
| 68 | public TestLoaders(String name) { |
| 69 | page = 1; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public void setViewer(SDView j) { |
| 74 | PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().addPostSelectionListener(this); |
| 75 | v = j; |
| 76 | v.setSDPagingProvider(this); |
| 77 | v.setSDFindProvider(this); |
| 78 | v.setSDFilterProvider(this); |
| 79 | page = 1; |
| 80 | createFrame(); |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public boolean hasNextPage() { |
| 85 | return page == 1; |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public boolean hasPrevPage() { |
| 90 | return page == 2; |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public void prevPage() { |
| 95 | page--; |
| 96 | createFrame(); |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public void nextPage() { |
| 101 | page++; |
| 102 | createFrame(); |
| 103 | } |
| 104 | |
| 105 | @SuppressWarnings("nls") |
| 106 | private void createFrame() { |
| 107 | Frame testFrame = new Frame(); |
| 108 | if (page == 1) { |
| 109 | testFrame.setName("Sequence Diagram - First Page"); |
| 110 | LifelineCategories tt[] = new LifelineCategories[2]; |
| 111 | tt[0] = new LifelineCategories(); |
| 112 | tt[1] = new LifelineCategories(); |
| 113 | tt[1].setName("Categorie 1"); |
| 114 | tt[1].setImage(new LocalImageImpl("obj16/node_obj.gif")); |
| 115 | tt[0].setImage(new LocalImageImpl("obj16/class.gif")); |
| 116 | tt[0].setName("Categorie 0"); |
| 117 | testFrame.setLifelineCategories(tt); |
| 118 | Lifeline lifeline = new Lifeline(); |
| 119 | lifeline.setName("LifeLine 0"); |
| 120 | testFrame.addLifeLine(lifeline); |
| 121 | EllipsisMessage mn = new EllipsisMessage(); |
| 122 | lifeline.getNewEventOccurrence(); |
| 123 | mn.setStartLifeline(lifeline); |
| 124 | mn.setName("******************* EllipsisisMessage TEST ****************"); |
| 125 | testFrame.addMessage(mn); |
| 126 | SyncMessage mn3 = new SyncMessage(); |
| 127 | mn3.setStartLifeline(lifeline); |
| 128 | testFrame.addMessage(mn3); |
| 129 | SyncMessage mn2 = new SyncMessage(); |
| 130 | lifeline.getNewEventOccurrence(); |
| 131 | lifeline.setCategory(0); |
| 132 | mn2.setEndLifeline(lifeline); |
| 133 | mn2.setName("*******************Sync TEST ****************"); |
| 134 | testFrame.addMessage(mn2); |
| 135 | for (int i = 1; i < 300; i++) { |
| 136 | lifeline = new Lifeline(); |
| 137 | lifeline.setName((new StringBuilder("LifeLine ")).append(i).toString()); |
| 138 | lifeline.setCategory(1); |
| 139 | testFrame.addLifeLine(lifeline); |
| 140 | SyncMessage m3 = new SyncMessage(); |
| 141 | testFrame.getLifeline(i - 1).getNewEventOccurrence(); |
| 142 | m3.setStartLifeline(testFrame.getLifeline(i - 1)); |
| 143 | m3.setEndLifeline(testFrame.getLifeline(i)); |
| 144 | m3.setName((new StringBuilder("Sync Message ")).append(i).toString()); |
| 145 | testFrame.addMessage(m3); |
| 146 | // if (i == 11) |
| 147 | // m3.setTime(new TmfTimestamp(i - 400)); |
| 148 | // else if (i == 6) |
| 149 | // m3.setTime(new TmfTimestamp(i)); |
| 150 | // else |
| 151 | m3.setTime(new TmfTimestamp(i + 1)); |
| 152 | } |
| 153 | |
| 154 | for (int i = testFrame.lifeLinesCount() - 1; i > 0; i--) { |
| 155 | SyncMessageReturn m = new SyncMessageReturn(); |
| 156 | testFrame.getLifeline(i).getNewEventOccurrence(); |
| 157 | m.setStartLifeline(testFrame.getLifeline(i)); |
| 158 | m.setEndLifeline(testFrame.getLifeline(i - 1)); |
| 159 | testFrame.addMessage(m); |
| 160 | m.setName((new StringBuilder("Sync Message return ")).append(i).toString()); |
| 161 | if (i + 1 < testFrame.lifeLinesCount()) { |
| 162 | SyncMessage h = testFrame.getSyncMessage(i + 1); |
| 163 | m.setMessage(h); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | for (int i = 0; i < testFrame.lifeLinesCount(); i++) { |
| 168 | if (i > 0) { |
| 169 | ExecutionOccurrence occ = new ExecutionOccurrence(); |
| 170 | occ.setStartOccurrence(testFrame.getSyncMessage(i).getEventOccurrence() + 1); |
| 171 | occ.setEndOccurrence(testFrame.getSyncMessageReturn(testFrame.syncMessageReturnCount() - i).getEventOccurrence()); |
| 172 | testFrame.getLifeline(i).addExecution(occ); |
| 173 | occ.setName("******************* Execution Occurance TEST ****************"); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | Stop s = new Stop(); |
| 178 | s.setLifeline(testFrame.getLifeline(1)); |
| 179 | s.setEventOccurrence(testFrame.getLifeline(1).getNewEventOccurrence()); |
| 180 | testFrame.getLifeline(1).addNode(s); |
| 181 | HotSpot gg = new HotSpot(); |
| 182 | gg.setImage(new LocalImageImpl("obj16/plus_obj.gif")); |
| 183 | gg.setExecution((BasicExecutionOccurrence) testFrame.getLifeline(1).getExecutions().get(0)); |
| 184 | AsyncMessageReturn m = new AsyncMessageReturn(); |
| 185 | m.setStartLifeline(testFrame.getLifeline(1)); |
| 186 | m.setEndLifeline(testFrame.getLifeline(3)); |
| 187 | m.setStartOccurrence(2); |
| 188 | m.setEndOccurrence(6); |
| 189 | m.setStartTime(new TmfTimestamp(2)); |
| 190 | m.setEndTime(new TmfTimestamp(6)); |
| 191 | m.setName("*******************Async TEST ****************"); |
| 192 | testFrame.addMessage(m); |
| 193 | v.setFrame(testFrame); |
| 194 | v.getSDWidget().setReorderMode(true); |
| 195 | } else { |
| 196 | |
| 197 | // if (page == 2) { |
| 198 | testFrame.setName("Sequence Diagram"); |
| 199 | Lifeline lifeline = new Lifeline(); |
| 200 | lifeline.setName("LifeLine 0"); |
| 201 | testFrame.addLifeLine(lifeline); |
| 202 | lifeline = new Lifeline(); |
| 203 | lifeline.setName("LifeLine 1"); |
| 204 | testFrame.addLifeLine(lifeline); |
| 205 | for (int i = 1; i < 30; i++) { |
| 206 | SyncMessage m3 = new SyncMessage(); |
| 207 | m3.autoSetStartLifeline(testFrame.getLifeline(0)); |
| 208 | m3.autoSetEndLifeline(testFrame.getLifeline(0)); |
| 209 | m3.setName((new StringBuilder("Message ")).append(i).toString()); |
| 210 | testFrame.addMessage(m3); |
| 211 | SyncMessageReturn m = new SyncMessageReturn(); |
| 212 | m.autoSetStartLifeline(testFrame.getLifeline(0)); |
| 213 | m.autoSetEndLifeline(testFrame.getLifeline(0)); |
| 214 | testFrame.addMessage(m); |
| 215 | m.setName((new StringBuilder("Message return ")).append(i).toString()); |
| 216 | ExecutionOccurrence occ = new ExecutionOccurrence(); |
| 217 | occ.setStartOccurrence(testFrame.getSyncMessage(i - 1).getEventOccurrence()); |
| 218 | occ.setEndOccurrence(testFrame.getSyncMessageReturn(i - 1).getEventOccurrence()); |
| 219 | testFrame.getLifeline(0).addExecution(occ); |
| 220 | } |
| 221 | } |
| 222 | v.setFrame(testFrame); |
| 223 | } |
| 224 | |
| 225 | /* |
| 226 | * (non-Javadoc) |
| 227 | * @see org.eclipse.hyades.uml2sd.ui.actions.provider.ISDFindProvider#find(org.eclipse.hyades.uml2sd.ui.actions.widgets.Criteria) |
| 228 | */ |
| 229 | @Override |
| 230 | public boolean find(Criteria toSearch) { |
| 231 | Frame frame = v.getFrame(); |
| 232 | |
| 233 | if (frame == null) { |
| 234 | return false; |
| 235 | } |
| 236 | if (findResults == null || findCriteria == null || !findCriteria.compareTo(toSearch)) { |
| 237 | findResults = new ArrayList<GraphNode>(); |
| 238 | findCriteria = toSearch; |
| 239 | if (findCriteria.isLifeLineSelected()) { |
| 240 | for (int i = 0; i < frame.lifeLinesCount(); i++) { |
| 241 | if (findCriteria.matches(frame.getLifeline(i).getName())) { |
| 242 | findResults.add(frame.getLifeline(i)); |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | } |
| 247 | ArrayList<GraphNode> msgs = new ArrayList<GraphNode>(); |
| 248 | if (findCriteria.isSyncMessageSelected()) { |
| 249 | for (int i = 0; i < frame.syncMessageCount(); i++) { |
| 250 | if (findCriteria.matches(frame.getSyncMessage(i).getName())) { |
| 251 | msgs.add(frame.getSyncMessage(i)); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | for (int i = 0; i < frame.syncMessageReturnCount(); i++) { |
| 256 | if (findCriteria.matches(frame.getSyncMessageReturn(i).getName())) { |
| 257 | msgs.add(frame.getSyncMessageReturn(i)); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | } |
| 262 | // if(msgs.size() > 0) { |
| 263 | // GraphNode temp[] = msgs.toArray(new GraphNode[0]); |
| 264 | // Arrays.sort(temp, new DateComparator()); |
| 265 | // findResults.addAll(Arrays.asList(temp)); |
| 266 | // } |
| 267 | |
| 268 | msgs = new ArrayList<GraphNode>(); |
| 269 | if (findCriteria.isAsyncMessageSelected()) { |
| 270 | for (int i = 0; i < frame.asyncMessageCount(); i++) { |
| 271 | if (findCriteria.matches(frame.getAsyncMessage(i).getName())) { |
| 272 | msgs.add(frame.getAsyncMessage(i)); |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | for (int i = 0; i < frame.asyncMessageReturnCount(); i++) { |
| 277 | if (findCriteria.matches(frame.getAsyncMessageReturn(i).getName())) { |
| 278 | msgs.add(frame.getAsyncMessageReturn(i)); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | } |
| 283 | // if(msgs.size() > 0) { |
| 284 | // GraphNode temp[] = msgs.toArray(new GraphNode[0]); |
| 285 | // Arrays.sort(temp, new DateComparator()); |
| 286 | // findResults.addAll(Arrays.asList(temp)); |
| 287 | // } |
| 288 | |
| 289 | @SuppressWarnings("rawtypes") |
| 290 | List selection = v.getSDWidget().getSelection(); |
| 291 | if (selection != null && selection.size() == 1) { |
| 292 | currentFindIndex = findResults.indexOf(selection.get(0)) + 1; |
| 293 | } else { |
| 294 | currentFindIndex = 0; |
| 295 | } |
| 296 | } else { |
| 297 | currentFindIndex++; |
| 298 | } |
| 299 | if (findResults.size() > currentFindIndex) { |
| 300 | GraphNode current = findResults.get(currentFindIndex); |
| 301 | v.getSDWidget().moveTo(current); |
| 302 | return true; |
| 303 | } |
| 304 | // return notFoundYet(findCriteria); // search in other page |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | @Override |
| 309 | public void cancel() { |
| 310 | findResults = null; |
| 311 | findCriteria = null; |
| 312 | currentFindIndex = 0; |
| 313 | } |
| 314 | |
| 315 | public boolean isLifelineSupported() { |
| 316 | return false; |
| 317 | } |
| 318 | |
| 319 | public boolean isSyncMessageSupported() { |
| 320 | return false; |
| 321 | } |
| 322 | |
| 323 | public boolean isSyncMessageReturnSupported() { |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | public boolean isAsyncMessageSupported() { |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | public boolean isAsyncMessageReturnSupported() { |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | public boolean isStopSupported() { |
| 336 | return false; |
| 337 | } |
| 338 | |
| 339 | public Action getFindAction() { |
| 340 | return null; |
| 341 | } |
| 342 | |
| 343 | @Override |
| 344 | public boolean filter(List<?> filters) { |
| 345 | |
| 346 | if (savedFrame != null) { |
| 347 | savedFrame = v.getFrame(); |
| 348 | } |
| 349 | |
| 350 | Frame frame = v.getFrame(); |
| 351 | |
| 352 | if (frame == null) { |
| 353 | return false; |
| 354 | } |
| 355 | |
| 356 | if (filters.size() != 1) { |
| 357 | return false; |
| 358 | } |
| 359 | |
| 360 | FilterCriteria filterCriteria = (FilterCriteria) filters.get(0); |
| 361 | |
| 362 | // One way is to set visiblity of the item, but this only works for messages and not |
| 363 | // for lifelines! It's better to create a new frame without the filtered messages. |
| 364 | boolean found = false; |
| 365 | if (filterCriteria.getCriteria().isSyncMessageSelected()) { |
| 366 | for (int i = 0; i < frame.syncMessageCount(); i++) { |
| 367 | if (filterCriteria.getCriteria().matches(frame.getSyncMessage(i).getName())) { |
| 368 | frame.getSyncMessage(i).setVisible(false); |
| 369 | found = true; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | for (int i = 0; i < frame.syncMessageReturnCount(); i++) { |
| 374 | if (filterCriteria.getCriteria().matches(frame.getSyncMessageReturn(i).getName())) { |
| 375 | frame.getSyncMessageReturn(i).setVisible(false); |
| 376 | found = true; |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | v.getSDWidget().redraw(); |
| 382 | return found; |
| 383 | } |
| 384 | |
| 385 | public ArrayList<?> getCurrentFilters() { |
| 386 | return null; |
| 387 | } |
| 388 | |
| 389 | @SuppressWarnings("nls") |
| 390 | @Override |
| 391 | public String getTitleString() { |
| 392 | return "Test Loader"; |
| 393 | } |
| 394 | |
| 395 | @Override |
| 396 | public void dispose() { |
| 397 | } |
| 398 | |
| 399 | |
| 400 | @Override |
| 401 | public void selectionChanged(IWorkbenchPart part, ISelection selection) { |
| 402 | ISelection sel = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService().getSelection(); |
| 403 | if (sel != null && (sel instanceof StructuredSelection)) { |
| 404 | StructuredSelection stSel = (StructuredSelection) sel; |
| 405 | if (stSel.getFirstElement() instanceof HotSpot) { |
| 406 | // OpenToolBox gg = new OpenToolBox(v); |
| 407 | // gg.run(); |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * (non-Javadoc) |
| 414 | * @see org.eclipse.hyades.uml2sd.ui.actions.provider.ISDGraphNodeSupporter#isNodeSupported(int) |
| 415 | */ |
| 416 | @Override |
| 417 | public boolean isNodeSupported(int nodeType) { |
| 418 | switch (nodeType) { |
| 419 | case ISDGraphNodeSupporter.LIFELINE: |
| 420 | case ISDGraphNodeSupporter.SYNCMESSAGE: |
| 421 | case ISDGraphNodeSupporter.SYNCMESSAGERETURN: |
| 422 | case ISDGraphNodeSupporter.ASYNCMESSAGE: |
| 423 | case ISDGraphNodeSupporter.ASYNCMESSAGERETURN: |
| 424 | case ISDGraphNodeSupporter.STOP: |
| 425 | return true; |
| 426 | |
| 427 | default: |
| 428 | break; |
| 429 | } |
| 430 | return false; |
| 431 | } |
| 432 | |
| 433 | @Override |
| 434 | public String getNodeName(int nodeType, String loaderClassName) { |
| 435 | return null; |
| 436 | } |
| 437 | |
| 438 | public static class LocalImageImpl implements IImage { |
| 439 | protected Image img; |
| 440 | |
| 441 | public LocalImageImpl(String file) { |
| 442 | img = null; |
| 443 | img = getResourceImage(file); |
| 444 | } |
| 445 | |
| 446 | public LocalImageImpl(Image img_) { |
| 447 | img = null; |
| 448 | img = img_; |
| 449 | } |
| 450 | |
| 451 | @SuppressWarnings("nls") |
| 452 | public Image getResourceImage(String _name) { |
| 453 | ImageDescriptor imgage; |
| 454 | try { |
| 455 | URL BASIC_URL = new URL("platform", "localhost", "plugin"); |
| 456 | URL url = new URL(BASIC_URL, (new StringBuilder("plugin/org.eclipse.linuxtools.tmf.ui/icons/")).append(_name).toString()); |
| 457 | imgage = ImageDescriptor.createFromURL(url); |
| 458 | return imgage.createImage(); |
| 459 | } catch (Exception e) { |
| 460 | System.err.println(e); |
| 461 | } |
| 462 | return null; |
| 463 | } |
| 464 | |
| 465 | @Override |
| 466 | public Object getImage() { |
| 467 | return img; |
| 468 | } |
| 469 | |
| 470 | @Override |
| 471 | public void dispose() { |
| 472 | if (img != null) { |
| 473 | img.dispose(); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | } |
| 478 | |
| 479 | @Override |
| 480 | public void firstPage() { |
| 481 | page = 0; |
| 482 | createFrame(); |
| 483 | |
| 484 | } |
| 485 | |
| 486 | @Override |
| 487 | public void lastPage() { |
| 488 | page = 2; |
| 489 | createFrame(); |
| 490 | } |
| 491 | } |