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