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