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

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