source: trunk/autoquest-ui-swt/src/main/java/de/ugoe/cs/autoquest/ui/swt/ShowTaskTreeDialog.java @ 2046

Last change on this file since 2046 was 2033, checked in by pharms, 9 years ago
  • added support for plotting action instances covered by sequences
File size: 32.9 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.ui.swt;
16
17import java.awt.Frame;
18import java.awt.GraphicsDevice;
19import java.awt.GraphicsEnvironment;
20import java.lang.reflect.Method;
21import java.util.Collection;
22import java.util.Collections;
23import java.util.Comparator;
24import java.util.LinkedList;
25import java.util.List;
26
27import org.eclipse.swt.SWT;
28import org.eclipse.swt.awt.SWT_AWT;
29import org.eclipse.swt.custom.SashForm;
30import org.eclipse.swt.custom.StackLayout;
31import org.eclipse.swt.events.SelectionAdapter;
32import org.eclipse.swt.events.SelectionEvent;
33import org.eclipse.swt.events.ShellAdapter;
34import org.eclipse.swt.events.ShellEvent;
35import org.eclipse.swt.events.ShellListener;
36import org.eclipse.swt.graphics.Color;
37import org.eclipse.swt.layout.GridData;
38import org.eclipse.swt.layout.GridLayout;
39import org.eclipse.swt.widgets.Button;
40import org.eclipse.swt.widgets.Composite;
41import org.eclipse.swt.widgets.Control;
42import org.eclipse.swt.widgets.Dialog;
43import org.eclipse.swt.widgets.Display;
44import org.eclipse.swt.widgets.Event;
45import org.eclipse.swt.widgets.FileDialog;
46import org.eclipse.swt.widgets.Label;
47import org.eclipse.swt.widgets.Listener;
48import org.eclipse.swt.widgets.Menu;
49import org.eclipse.swt.widgets.MenuItem;
50import org.eclipse.swt.widgets.Shell;
51import org.eclipse.swt.widgets.TabFolder;
52import org.eclipse.swt.widgets.TabItem;
53import org.eclipse.swt.widgets.Tree;
54import org.eclipse.swt.widgets.TreeColumn;
55import org.eclipse.swt.widgets.TreeItem;
56import org.jfree.chart.ChartPanel;
57import org.jfree.chart.JFreeChart;
58
59import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
60import de.ugoe.cs.autoquest.tasktrees.treeifc.IMarkingTemporalRelationship;
61import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional;
62import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance;
63import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
64import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance;
65import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
66import de.ugoe.cs.autoquest.tasktrees.treeifc.IStructuringTemporalRelationship;
67import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
68import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
69import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList;
70import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskModel;
71import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession;
72import de.ugoe.cs.autoquest.tasktrees.treeifc.TaskMetric;
73import de.ugoe.cs.util.console.Console;
74
75/**
76 * <p>
77 * a dialog to inspect the tasks and task instances of a task model
78 * </p>
79 *
80 * @author Patrick Harms
81 */
82public class ShowTaskTreeDialog extends Dialog {
83
84    /** the main shell */
85    private Shell shell;
86   
87    /** the listener for the main shell to prevent disposing required */
88    private ShellListener shellListener;
89   
90    /** the tab folder containing the instance tree, the model tree, and the visualizations list */
91    private TabFolder tabFolder;
92
93    /** the tree of task instances on the left */
94    private Tree instanceTree;
95   
96    /** the tree of tasks on the left */
97    private Tree modelTree;
98   
99    /** the tree of possible visualizations on the left */
100    private Tree modelVisualizationTree;
101   
102    /** the right hand composite containing either task details or a visualization */
103    private Composite rightHandComposite;
104   
105    /** the stack layout displaying either the task details or a visualization */
106    private StackLayout rightHandLayout;
107   
108    /** the task details composite containing the task details tree, the execution variants, etc.*/
109    private Composite detailsComposite;
110   
111    /** the tree of a specific task on the right */
112    private Tree taskDetailsTree;
113
114    /** the tree of execution variants of a specific task on the right */
115    private Tree executionVariantsTree;
116
117    /** the tree of involved GUI elements of a specific task on the right bottom */
118    private Tree involvedTargetsTree;
119
120    /** the table containing the parents tasks of a displayed task */
121    private Tree parentTasks;
122   
123    /** the visualization composite on the right hand side toggled with the details composite */
124    private Composite visualizationsComposite;
125
126    /** the displayed task model */
127    private ITaskModel taskModel;
128   
129    /** a boolean indicating if the CTTE export of a task is possible */
130    private boolean taskExportAvailable = false;
131   
132    /** default expansion listener */
133    private Listener defaultExpansionListener = new Listener() {
134        public void handleEvent(final Event event) {
135            ensureChildren((TreeItem) event.item);
136            ((TreeItem) event.item).setExpanded(true);
137        }
138    };
139
140    /**
141     * creates the dialog
142     */
143    public ShowTaskTreeDialog(Shell parent, int style, ITaskModel taskModel, String taskTreeName) {
144        super(parent, style);
145        setText("Task Model " + taskTreeName);
146        this.taskModel = taskModel;
147    }
148
149    /**
150     * displays the dialog
151     * @param task
152     */
153    public void open() {
154        // check, if export to CTTE is possible
155        try {
156            Class<?> exportClass = this.getClass().getClassLoader().loadClass
157                ("de.ugoe.cs.autoquest.plugin.exports.tasktree.CTTExportUtils");
158           
159            if (exportClass != null) {
160                taskExportAvailable = true;
161            }
162        }
163        catch (ClassNotFoundException e) {
164            // ignore. No export available
165        }
166       
167        open(null);
168    }
169
170    /**
171     * displays the dialog with a specific task opened
172     * @param task
173     */
174    public void open(ITask task) {
175        if (shell == null) {
176            createContents();
177            shell.open();
178            shell.layout();
179
180            VisualizationUtils.updateColumnWidths(taskDetailsTree);
181        }
182        else {
183            shell.setVisible(true);
184        }
185       
186        if (task != null) {
187            shellListener = new ShellAdapter() {
188                @Override
189                public void shellClosed(ShellEvent e) {
190                    e.doit = false;
191                    shell.setVisible(false);
192                }
193            };
194           
195            shell.addShellListener(shellListener);
196        }
197
198        if (task != null) {
199            navigateTo(task);
200        }
201       
202        Display display = getParent().getDisplay();
203        while (!shell.isDisposed() && shell.isVisible()) {
204            if (!display.readAndDispatch()) {
205                display.sleep();
206            }
207        }
208       
209        if (task == null) {
210            dispose();
211        }
212    }
213
214    /**
215     * disposes the dialog if it is not used anymore
216     */
217    public void dispose() {
218        if ((shell != null) && (!shell.isDisposed())) {
219            if (shellListener != null) {
220                shell.removeShellListener(shellListener);
221            }
222           
223            shell.dispose();
224        }
225    }
226
227    /**
228     * creates the two views, one on the task instances on the left, on on the task models on the
229     * right. Also adds a selection adapter to the task instances so that for a selected task
230     * instance always the respective model is presented.
231     */
232    private void createContents() {
233        shell = new Shell(getParent(), SWT.SHELL_TRIM | SWT.BORDER);
234
235        GraphicsDevice gd =
236            GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
237       
238        shell.setSize(gd.getDisplayMode().getWidth(), gd.getDisplayMode().getHeight());
239        shell.setText(getText());
240
241        shell.setLayout(new GridLayout(4, false));
242       
243        SashForm mainSashForm = new SashForm(shell, SWT.HORIZONTAL);
244        mainSashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 4, 1));
245       
246        createLeftHandSideTabs(mainSashForm);
247        createRightHandSideView(mainSashForm);
248
249        mainSashForm.setWeights(new int[] { 1, 3 });
250
251        Button btnExpandAll = new Button(shell, SWT.NONE);
252        btnExpandAll.addSelectionListener(new SelectionAdapter() {
253            @Override
254            public void widgetSelected(SelectionEvent e) {
255                if (tabFolder.getSelectionIndex() == 0) {
256                    VisualizationUtils.expandAll(instanceTree, true);
257                }
258                else {
259                    VisualizationUtils.expandAll(modelTree, true);
260                }
261            }
262        });
263        btnExpandAll.setText("Expand all");
264
265        Button btnCollapseAll = new Button(shell, SWT.NONE);
266        btnCollapseAll.addSelectionListener(new SelectionAdapter() {
267            @Override
268            public void widgetSelected(SelectionEvent e) {
269                if (tabFolder.getSelectionIndex() == 0) {
270                    VisualizationUtils.expandAll(instanceTree, false);
271                }
272                else {
273                    VisualizationUtils.expandAll(modelTree, false);
274                }
275            }
276        });
277        btnCollapseAll.setText("Collapse all");
278       
279        new Label(shell, SWT.NONE);
280        new Label(shell, SWT.NONE);
281    }
282
283    /**
284     *
285     */
286    private void createLeftHandSideTabs(final SashForm mainSashForm) {
287        tabFolder = new TabFolder(mainSashForm, SWT.NONE);
288        tabFolder.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
289        tabFolder.addSelectionListener(new SelectionAdapter() {
290            @Override
291            public void widgetSelected(SelectionEvent e) {
292                if (tabFolder.getSelectionIndex() == 0) {
293                    // task instances tab selected
294                    rightHandLayout.topControl = detailsComposite;
295                }
296                else if (tabFolder.getSelectionIndex() == 1) {
297                    // task model tab selected
298                    rightHandLayout.topControl = detailsComposite;
299                    VisualizationUtils.updateColumnWidths(modelTree);
300                }
301                else {
302                    rightHandLayout.topControl = visualizationsComposite;
303                }
304               
305                rightHandComposite.layout();
306                rightHandComposite.redraw();
307                rightHandComposite.update();
308            }
309        });
310       
311        TabItem instanceTreeTab = new TabItem(tabFolder, SWT.NONE);
312        instanceTreeTab.setText("Task Instances");
313
314        instanceTree = new Tree(tabFolder, SWT.BORDER | SWT.SINGLE | SWT.VIRTUAL);
315        instanceTree.addSelectionListener(new SelectionAdapter() {
316            @Override
317            public void widgetSelected(SelectionEvent e) {
318                TreeItem[] selectedItems = instanceTree.getSelection();
319                if ((selectedItems.length == 1) &&
320                    (selectedItems[0].getData() instanceof ITaskInstance))
321                {
322                    displayTaskDetails(((ITaskInstance) selectedItems[0].getData()).getTask());
323                }
324                else {
325                    clearTaskDetails();
326                }
327            }
328        });
329
330        VisualizationUtils.addItemSpecificContextMenu
331            (instanceTree, ITaskInstance.class, "show in task list", new SelectionAdapter()
332        {
333            @Override
334            public void widgetSelected(SelectionEvent e) {
335                navigateTo(((ITaskInstance) instanceTree.getSelection()[0].getData()).getTask());
336            }
337        });
338       
339        if (taskExportAvailable) {
340            VisualizationUtils.addItemSpecificContextMenu
341                (instanceTree, ITask.class, "export to ConcurTaskTree", new SelectionAdapter()
342            {
343                @Override
344                public void widgetSelected(SelectionEvent e) {
345                    export((ITask) taskDetailsTree.getSelection()[0].getData());
346                }
347            });
348        }
349
350        buildInstanceTree();
351        instanceTreeTab.setControl(instanceTree);
352
353        TabItem modelTreeTab = new TabItem(tabFolder, SWT.NONE);
354        modelTreeTab.setText("Tasks");
355       
356        modelTree =
357            VisualizationUtils.createTaskDetailsTree(tabFolder, "tasks in model", taskModel);
358       
359        // show task details if requested
360        modelTree.addSelectionListener(new SelectionAdapter() {
361            @Override
362            public void widgetSelected(SelectionEvent e) {
363                TreeItem[] selectedItems = modelTree.getSelection();
364                if ((selectedItems.length == 1) && (selectedItems[0].getData() instanceof ITask)) {
365                    displayTaskDetails((ITask) selectedItems[0].getData());
366                }
367                else {
368                    clearTaskDetails();
369                }
370            }
371        });
372       
373        buildModelTree(taskModel);
374       
375        modelTreeTab.setControl(modelTree);
376
377        TabItem modelVisualizationTab = new TabItem(tabFolder, SWT.NONE);
378        modelVisualizationTab.setText("Visualizations");
379       
380        modelVisualizationTree = new Tree(tabFolder, SWT.BORDER | SWT.SINGLE | SWT.VIRTUAL);
381       
382        TreeItem item1 = new TreeItem(modelVisualizationTree, SWT.NULL);
383        item1.setText(TaskTreePlotUtils.TASK_COUNT__EVENT_COVERAGE_PLOT);
384       
385        item1 = new TreeItem(modelVisualizationTree, SWT.NULL);
386        item1.setText(TaskTreePlotUtils.ORDERED_TASK_COUNT_PLOT);
387       
388        item1 = new TreeItem(modelVisualizationTree, SWT.NULL);
389        item1.setText(TaskTreePlotUtils.CUMULATIVE_TASK_COVERAGE_PLOT);
390       
391        Menu menu = modelVisualizationTree.getMenu();
392       
393        if (menu == null) {
394            menu = new Menu(modelVisualizationTree);
395            modelVisualizationTree.setMenu(menu);
396        }
397       
398        // show visualization if requested
399        modelVisualizationTree.addSelectionListener(new SelectionAdapter() {
400            @Override
401            public void widgetSelected(SelectionEvent e) {
402                clearVisualization();
403                TreeItem[] selectedItems = modelVisualizationTree.getSelection();
404                if (selectedItems.length == 1) {
405                    showVisualization(selectedItems[0].getText());
406                }
407            }
408        });
409       
410        modelVisualizationTab.setControl(modelVisualizationTree);
411       
412        // add a context menu for PDF export
413        final Menu finalMenu = menu;
414        MenuItem newItem = new MenuItem(finalMenu, SWT.NONE);
415        newItem.setText("save as PDF");
416        newItem.addSelectionListener(new SelectionAdapter() {
417            @Override
418            public void widgetSelected(SelectionEvent e) {
419                try {
420                    FileDialog dialog = new FileDialog(shell, SWT.SAVE);
421                    dialog.setFilterExtensions(new String [] {"*.pdf"});
422                    TaskTreePlotUtils.saveChartToPDF
423                        ((JFreeChart) modelVisualizationTree.getSelection()[0].getData(),
424                         dialog.open(), 500, 500);
425                }
426                catch (Exception e1) {
427                    // TODO Auto-generated catch block
428                    e1.printStackTrace();
429                }
430            }
431        });
432    }
433   
434    /**
435     *
436     */
437    private void createRightHandSideView(SashForm mainSashForm) {
438        rightHandComposite = new Composite(mainSashForm, SWT.NO_BACKGROUND);
439        rightHandLayout = new StackLayout();
440        rightHandComposite.setLayout(rightHandLayout);
441       
442        detailsComposite = new Composite(rightHandComposite, SWT.NO_BACKGROUND);
443        detailsComposite.setLayout(new GridLayout(1, false));
444        detailsComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
445       
446        new Label(detailsComposite, SWT.NONE).setText("Task Details:");
447       
448        SashForm detailsSashForm = new SashForm(detailsComposite, SWT.VERTICAL);
449        detailsSashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
450       
451        SashForm detailsTopSashForm = new SashForm(detailsSashForm, SWT.HORIZONTAL);
452        detailsTopSashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
453       
454        taskDetailsTree =
455            VisualizationUtils.createTaskDetailsTree(detailsTopSashForm, "task details", taskModel);
456       
457        VisualizationUtils.addExpansionListener(taskDetailsTree, defaultExpansionListener);
458       
459        VisualizationUtils.addItemSpecificContextMenu
460            (taskDetailsTree, ITask.class, "show in task list", new SelectionAdapter()
461        {
462            @Override
463            public void widgetSelected(SelectionEvent e) {
464                navigateTo((ITask) taskDetailsTree.getSelection()[0].getData());
465            }
466        });
467       
468        if (taskExportAvailable) {
469            VisualizationUtils.addItemSpecificContextMenu
470                (taskDetailsTree, ITask.class, "export to ConcurTaskTree", new SelectionAdapter()
471            {
472                @Override
473                public void widgetSelected(SelectionEvent e) {
474                    export((ITask) taskDetailsTree.getSelection()[0].getData());
475                }
476            });
477        }
478       
479        executionVariantsTree = new Tree(detailsTopSashForm, SWT.BORDER | SWT.SINGLE | SWT.WRAP);
480        executionVariantsTree.setHeaderVisible(true);
481       
482        TreeColumn taskColumn = new TreeColumn(executionVariantsTree, SWT.NONE);
483        taskColumn.setText("instance variants");
484        taskColumn.setAlignment(SWT.LEFT);
485
486        // load the correct children on expansion
487        VisualizationUtils.addExpansionListener(executionVariantsTree, defaultExpansionListener);
488       
489        detailsTopSashForm.setWeights(new int[] { 3, 2 });
490
491        SashForm detailsBottomSashForm = new SashForm(detailsSashForm, SWT.HORIZONTAL);
492        detailsBottomSashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
493       
494        parentTasks = VisualizationUtils.createTaskDetailsTree
495            (detailsBottomSashForm, "parent tasks", taskModel);
496       
497        VisualizationUtils.addItemSpecificContextMenu
498            (parentTasks, ITask.class, "show in task list", new SelectionAdapter()
499        {
500            @Override
501            public void widgetSelected(SelectionEvent e) {
502                navigateTo((ITask) parentTasks.getSelection()[0].getData());
503            }
504        });
505       
506        if (taskExportAvailable) {
507            VisualizationUtils.addItemSpecificContextMenu
508                (parentTasks, ITask.class, "export to ConcurTaskTree", new SelectionAdapter()
509            {
510                @Override
511                public void widgetSelected(SelectionEvent e) {
512                    export((ITask) taskDetailsTree.getSelection()[0].getData());
513                }
514            });
515        }
516       
517        involvedTargetsTree =
518            VisualizationUtils.createTargetsTree(detailsBottomSashForm, "involved GUI elements");
519       
520        visualizationsComposite = new Composite(rightHandComposite, SWT.NO_BACKGROUND);
521        visualizationsComposite.setLayout(new GridLayout(1, false));
522        visualizationsComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
523       
524        new Label(visualizationsComposite, SWT.NONE).setText("Visualization:");
525       
526        rightHandLayout.topControl = detailsComposite;
527       
528        detailsBottomSashForm.setWeights(new int[] { 2, 3 });
529        detailsSashForm.setWeights(new int[] { 1, 1 });
530    }
531
532    /**
533     * convenience method for creating the display of the instances
534     */
535    private void buildInstanceTree() {
536        List<IUserSession> sessions = taskModel.getUserSessions();
537
538        // load the correct children on expansion
539        VisualizationUtils.addExpansionListener(instanceTree, defaultExpansionListener);
540
541        TreeItem root = new TreeItem(instanceTree, SWT.NULL);
542        root.setText(sessions.size() + " sessions");
543        root.setData(taskModel);
544       
545        for (IUserSession session : sessions) {
546            buildInstanceTree(root, session);
547        }
548       
549        root.setExpanded(true);
550    }
551
552    /**
553     * convenience method for creating the display of the instances
554     */
555    private void buildInstanceTree(TreeItem currentParent, IUserSession session) {
556        TreeItem item = new TreeItem(currentParent, SWT.NULL);
557        item.setText(session.toString());
558        item.setData(session);
559       
560        // simulate a child
561        new TreeItem(item, SWT.NULL);
562    }
563
564    /**
565     * convenience method for creating the display of the task model
566     */
567    private void buildModelTree(ITaskModel taskModel) {
568        Collection<ITask> allTasks = taskModel.getTasks();
569       
570        List<ITask> sequences = new LinkedList<ITask>();
571        List<ITask> iterations = new LinkedList<ITask>();
572        List<ITask> selections = new LinkedList<ITask>();
573        List<ITask> optionals = new LinkedList<ITask>();
574        List<ITask> others = new LinkedList<ITask>();
575       
576        TreeItem root = new TreeItem(modelTree, SWT.NULL);
577        root.setText(allTasks.size() + " tasks in model");
578        root.setData(taskModel);
579
580        createSortedTaskLists(allTasks, sequences, iterations, selections, optionals, others);
581       
582        //allTasks = createSortedTaskList(allTasks);
583       
584        createModelTreeItemFor(sequences, sequences.size() + " Sequences", root);
585        createModelTreeItemFor(iterations, iterations.size() + " Iterations", root);
586        createModelTreeItemFor(selections, selections.size() + " Selections", root);
587        createModelTreeItemFor(optionals, optionals.size() + " Optionals", root);
588        createModelTreeItemFor(others, others.size() + " other Tasks", root);
589       
590        root.setExpanded(true);
591    }
592
593    /**
594     *
595     */
596    private void createSortedTaskLists(Collection<ITask> allTasks,
597                                       List<ITask>       sequences,
598                                       List<ITask>       iterations,
599                                       List<ITask>       selections,
600                                       List<ITask>       optionals,
601                                       List<ITask>       others)
602    {
603        List<ITask> toAdd;
604       
605        for (ITask task : allTasks) {
606            if (task instanceof ISequence) {
607                toAdd = sequences;
608            }
609            else if (task instanceof IIteration) {
610                toAdd = iterations;
611            }
612            else if (task instanceof ISelection) {
613                toAdd = selections;
614            }
615            else if (task instanceof IOptional) {
616                toAdd = optionals;
617            }
618            else {
619                toAdd = others;
620            }
621           
622            int taskValue = taskModel.getTaskInfo(task).getMeasureValue(TaskMetric.EVENT_COVERAGE);
623            int start = 0;
624            int end = toAdd.size();
625            int center = 0;
626            int centerValue;
627           
628            while (start != end) {
629                center = start + ((end - start) / 2);
630               
631                if ((center != start) || (center != end)) {
632                    //centerValue = toAdd.get(center).getInstances().size();
633                    centerValue = taskModel.getTaskInfo(toAdd.get(center)).getMeasureValue
634                        (TaskMetric.EVENT_COVERAGE);
635               
636                    if (centerValue > taskValue) {
637                        start = Math.max(center, start + 1);
638                    }
639                    else if (centerValue < taskValue) {
640                        end = Math.min(center, end - 1);
641                    }
642                    else {
643                        // add the event directly where the center is, as the count of the center
644                        // and the new task are equal
645                        end = center;
646                        start = end;
647                        break;
648                    }
649                }
650                else {
651                    // add the event to the position denoted by the add index
652                    break;
653                }
654            }
655           
656            toAdd.add(start, task);
657        }
658    }
659
660    /**
661     *
662     */
663    private void clearTaskDetails() {
664        taskDetailsTree.removeAll();
665        executionVariantsTree.removeAll();
666        involvedTargetsTree.removeAll();
667        parentTasks.removeAll();
668    }
669
670    /**
671     *
672     */
673    private void displayTaskDetails(ITask task) {
674        clearTaskDetails();
675       
676        VisualizationUtils.createTreeItemFor(task, taskDetailsTree, taskModel, true);
677        VisualizationUtils.expandAll(taskDetailsTree, true);
678       
679        // do it twice. Otherwise, it doesn't work
680        VisualizationUtils.updateColumnWidths(taskDetailsTree);
681       
682        Collection<Collection<ITaskInstance>> executionVariants = task.getExecutionVariants();
683        List<Collection<ITaskInstance>> sortedExecutionVariants =
684            new LinkedList<Collection<ITaskInstance>>(executionVariants);
685       
686        Collections.sort(sortedExecutionVariants, new Comparator<Collection<ITaskInstance>>() {
687            @Override
688            public int compare(Collection<ITaskInstance> list1, Collection<ITaskInstance> list2) {
689                return list2.size() - list1.size();
690            }
691        });
692       
693        int counter = 1;
694        for (Collection<ITaskInstance> variant : sortedExecutionVariants) {
695            TreeItem item = new TreeItem(executionVariantsTree, SWT.NULL);
696            if (variant.size() > 1) {
697                item.setText("variant " + counter++ + " (executed " + variant.size() + " times)");
698            }
699            else {
700                item.setText("variant " + counter++ + " (executed once)");
701            }
702            item.setData(variant);
703           
704            createTreeItemFor(variant.iterator().next(), item);
705        }
706
707        VisualizationUtils.expandAll(executionVariantsTree, true);
708        VisualizationUtils.updateColumnWidths(executionVariantsTree);
709       
710        addParentTasks(task);
711        VisualizationUtils.updateColumnWidths(parentTasks);
712       
713        VisualizationUtils.addInvolvedTargets(involvedTargetsTree, task);
714    }
715
716    /**
717     *
718     */
719    private void addParentTasks(ITask task) {
720        for (ITask candidate : taskModel.getTasks()) {
721            if (((candidate instanceof IStructuringTemporalRelationship) &&
722                 (((IStructuringTemporalRelationship) candidate).getChildren().contains(task))) ||
723                ((candidate instanceof IMarkingTemporalRelationship) &&
724                 (((IMarkingTemporalRelationship) candidate).getMarkedTask().equals(task))))
725            {
726                VisualizationUtils.createTreeItemFor(candidate, parentTasks, taskModel, false);
727            }
728        }
729    }
730
731    /**
732     *
733     */
734    private void navigateTo(ITask task) {
735        tabFolder.setSelection(1);
736       
737        OUTER:
738        for (TreeItem sublist : modelTree.getItem(0).getItems()) {
739            for (TreeItem taskItem : sublist.getItems()) {
740                if (task.equals(taskItem.getData())) {
741                    modelTree.setSelection(taskItem);
742                    sublist.setExpanded(true);
743                    VisualizationUtils.updateColumnWidths(modelTree);
744                    break OUTER;
745                }
746            }
747        }
748       
749        displayTaskDetails(task);
750    }
751
752    /**
753     *
754     */
755    private void export(ITask task) {
756        try {
757            Class<?> exportClass = this.getClass().getClassLoader().loadClass
758                ("de.ugoe.cs.autoquest.plugin.exports.tasktree.CTTExportUtils");
759           
760            Method method = exportClass.getDeclaredMethod("exportToCTT", ITask.class, String.class);
761           
762            method.invoke(null, task, task.getId() + "_ctte.xml");
763        }
764        catch (Exception e) {
765            Console.logException(e);
766        }
767    }
768   
769    /**
770     * ensures, that the children of a specific node are loaded
771     */
772    private void ensureChildren(TreeItem parent) {
773        TreeItem[] items = parent.getItems();
774        if ((items == null) || (items.length == 0) || (items[0].getData() == null)) {
775            if (items != null) {
776                for (int i = 0; i < items.length; i++) {
777                    items[i].dispose();
778                }
779            }
780
781            if (parent.getData() instanceof ITask) {
782                ITask task = (ITask) parent.getData();
783
784                if (task instanceof IStructuringTemporalRelationship) {
785                    for (ITask subTask : ((IStructuringTemporalRelationship) task).getChildren()) {
786                        VisualizationUtils.createTreeItemFor(subTask, parent, taskModel, true);
787                    }
788                }
789                else if (task instanceof IMarkingTemporalRelationship) {
790                    VisualizationUtils.createTreeItemFor
791                        (((IMarkingTemporalRelationship) task).getMarkedTask(), parent,
792                         taskModel, true);
793                }
794            }
795            else if (parent.getData() instanceof List<?>) {
796                @SuppressWarnings("unchecked")
797                List<ITask> tasks = (List<ITask>) parent.getData();
798                for (ITask task : tasks) {
799                    VisualizationUtils.createTreeItemFor(task, parent, taskModel, false);
800                }
801            }
802            else if (parent.getData() instanceof ITaskInstanceList) {
803                for (ITaskInstance subInstance : (ITaskInstanceList) parent.getData()) {
804                    createTreeItemFor(subInstance, parent);
805                }
806            }
807            else if (parent.getData() instanceof ITaskInstance) {
808                ITaskInstance instance = (ITaskInstance) parent.getData();
809
810                if (instance instanceof ISelectionInstance) {
811                    if (((ISelectionInstance) instance).getChild() != null) {
812                        createTreeItemFor(((ISelectionInstance) instance).getChild(), parent);
813                    }
814                }
815                else if (instance instanceof IOptionalInstance) {
816                    if (((IOptionalInstance) instance).getChild() != null) {
817                        createTreeItemFor(((IOptionalInstance) instance).getChild(), parent);
818                    }
819                }
820            }
821        }
822    }
823
824    /**
825     *
826     */
827    private void createModelTreeItemFor(List<ITask> taskList, String name, TreeItem parent) {
828        TreeItem item = new TreeItem(parent, SWT.NULL);
829        item.setText(name);
830        item.setData(taskList);
831       
832        // simulate a child
833        if (taskList.size() > 0) {
834            for (ITask task : taskList) {
835                VisualizationUtils.createTreeItemFor(task, item, taskModel, false);
836            }
837        }
838    }
839
840    /**
841     * convenience method to create a tree item for a task
842     */
843    private void createTreeItemFor(ITaskInstance taskInstance,
844                                   TreeItem      parent)
845    {
846        TreeItem item = new TreeItem(parent, SWT.NULL);
847        item.setText(taskInstance.toString());
848        item.setData(taskInstance);
849       
850        // simulate a child
851        if ((taskInstance instanceof ITaskInstanceList) ||
852            (taskInstance instanceof ISelectionInstance) ||
853            (taskInstance instanceof IOptionalInstance))
854        {
855            new TreeItem(item, SWT.NULL);
856        }
857    }
858
859    /**
860     *
861     */
862    private void showVisualization(String name) {
863        Color backgroundColor = visualizationsComposite.getBackground();
864        Composite parent = new Composite(visualizationsComposite, SWT.EMBEDDED);
865        parent.setLayout(new GridLayout(1, false));
866        parent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
867       
868        Frame chartFrame = SWT_AWT.new_Frame(parent);
869        chartFrame.setBackground(new java.awt.Color(backgroundColor.getRed(),
870                                                    backgroundColor.getGreen(),
871                                                    backgroundColor.getBlue()));
872       
873        chartFrame.setLayout(new java.awt.GridLayout());
874       
875        List<ITaskModel> models = new LinkedList<>();
876        models.add(taskModel);
877       
878        List<String> names = new LinkedList<>();
879        names.add(name);
880       
881        List<Integer> groups = new LinkedList<>();
882        groups.add(0);
883
884        JFreeChart chart = TaskTreePlotUtils.createPlot(name, models, names, groups);
885        chartFrame.add(new ChartPanel(chart));
886       
887        modelVisualizationTree.getSelection()[0].setData(chart);
888       
889        rightHandComposite.layout();
890        rightHandComposite.redraw();
891        rightHandComposite.update();
892    }
893
894    /**
895     *
896     */
897    private void clearVisualization() {
898        for (Control child : visualizationsComposite.getChildren()) {
899            child.dispose();
900        }
901       
902        visualizationsComposite.pack();
903    }
904}
Note: See TracBrowser for help on using the repository browser.