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

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