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

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