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
RevLine 
[1094]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
[2033]17import java.awt.Frame;
[1424]18import java.awt.GraphicsDevice;
19import java.awt.GraphicsEnvironment;
[2127]20import java.awt.Toolkit;
21import java.awt.datatransfer.Clipboard;
22import java.awt.datatransfer.StringSelection;
[2033]23import java.lang.reflect.Method;
[2127]24import java.util.ArrayList;
[1153]25import java.util.Collection;
[1262]26import java.util.Collections;
27import java.util.Comparator;
28import java.util.LinkedList;
[1146]29import java.util.List;
30
[1094]31import org.eclipse.swt.SWT;
[2033]32import org.eclipse.swt.awt.SWT_AWT;
[1146]33import org.eclipse.swt.custom.SashForm;
[2033]34import org.eclipse.swt.custom.StackLayout;
[1094]35import org.eclipse.swt.events.SelectionAdapter;
36import org.eclipse.swt.events.SelectionEvent;
[1424]37import org.eclipse.swt.events.ShellAdapter;
38import org.eclipse.swt.events.ShellEvent;
39import org.eclipse.swt.events.ShellListener;
[2033]40import org.eclipse.swt.graphics.Color;
[1094]41import org.eclipse.swt.layout.GridData;
42import org.eclipse.swt.layout.GridLayout;
43import org.eclipse.swt.widgets.Button;
[1424]44import org.eclipse.swt.widgets.Composite;
[2033]45import org.eclipse.swt.widgets.Control;
[1094]46import org.eclipse.swt.widgets.Dialog;
47import org.eclipse.swt.widgets.Display;
[1153]48import org.eclipse.swt.widgets.Event;
[2033]49import org.eclipse.swt.widgets.FileDialog;
[1424]50import org.eclipse.swt.widgets.Label;
[1153]51import org.eclipse.swt.widgets.Listener;
[2033]52import org.eclipse.swt.widgets.Menu;
53import org.eclipse.swt.widgets.MenuItem;
[1094]54import org.eclipse.swt.widgets.Shell;
[1424]55import org.eclipse.swt.widgets.TabFolder;
56import org.eclipse.swt.widgets.TabItem;
[1094]57import org.eclipse.swt.widgets.Tree;
[1424]58import org.eclipse.swt.widgets.TreeColumn;
[1094]59import org.eclipse.swt.widgets.TreeItem;
[2033]60import org.jfree.chart.ChartPanel;
61import org.jfree.chart.JFreeChart;
[1094]62
[1424]63import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
[1146]64import de.ugoe.cs.autoquest.tasktrees.treeifc.IMarkingTemporalRelationship;
[1424]65import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional;
[1294]66import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance;
[1424]67import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
[1294]68import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance;
[1424]69import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
[1146]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;
[1636]76import de.ugoe.cs.autoquest.tasktrees.treeifc.TaskMetric;
[2033]77import de.ugoe.cs.util.console.Console;
[1094]78
79/**
80 * <p>
[1275]81 * a dialog to inspect the tasks and task instances of a task model
[1094]82 * </p>
83 *
84 * @author Patrick Harms
85 */
86public class ShowTaskTreeDialog extends Dialog {
87
[1275]88    /** the main shell */
[1424]89    private Shell shell;
[1094]90   
[1424]91    /** the listener for the main shell to prevent disposing required */
92    private ShellListener shellListener;
93   
[2033]94    /** the tab folder containing the instance tree, the model tree, and the visualizations list */
[1424]95    private TabFolder tabFolder;
96
[1275]97    /** the tree of task instances on the left */
[1146]98    private Tree instanceTree;
99   
[1424]100    /** the tree of tasks on the left */
[1146]101    private Tree modelTree;
[1424]102   
[2033]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   
[1424]115    /** the tree of a specific task on the right */
116    private Tree taskDetailsTree;
[1094]117
[1424]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 */
[1495]122    private Tree involvedTargetsTree;
[1424]123
124    /** the table containing the parents tasks of a displayed task */
125    private Tree parentTasks;
[2033]126   
127    /** the visualization composite on the right hand side toggled with the details composite */
128    private Composite visualizationsComposite;
[1424]129
[1275]130    /** the displayed task model */
[1424]131    private ITaskModel taskModel;
132   
[2033]133    /** a boolean indicating if the CTTE export of a task is possible */
134    private boolean taskExportAvailable = false;
135   
[1424]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    };
[1094]143
144    /**
[1275]145     * creates the dialog
[1094]146     */
[1146]147    public ShowTaskTreeDialog(Shell parent, int style, ITaskModel taskModel, String taskTreeName) {
[1094]148        super(parent, style);
[1146]149        setText("Task Model " + taskTreeName);
150        this.taskModel = taskModel;
[1094]151    }
152
153    /**
[1275]154     * displays the dialog
[1424]155     * @param task
[1094]156     */
157    public void open() {
[2033]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       
[1424]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       
[1094]206        Display display = getParent().getDisplay();
[1424]207        while (!shell.isDisposed() && shell.isVisible()) {
[1094]208            if (!display.readAndDispatch()) {
209                display.sleep();
210            }
211        }
[1424]212       
213        if (task == null) {
214            dispose();
215        }
[1094]216    }
217
218    /**
[1424]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    /**
[1275]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.
[1094]235     */
236    private void createContents() {
[1424]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());
[1094]243        shell.setText(getText());
244
245        shell.setLayout(new GridLayout(4, false));
[1146]246       
[1424]247        SashForm mainSashForm = new SashForm(shell, SWT.HORIZONTAL);
248        mainSashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 4, 1));
[1146]249       
[2033]250        createLeftHandSideTabs(mainSashForm);
251        createRightHandSideView(mainSashForm);
[1424]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     */
[2033]290    private void createLeftHandSideTabs(final SashForm mainSashForm) {
[1424]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) {
[2183]296                if (rightHandLayout == null) {
297                        return;
298                }
299               
[2033]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();
[1424]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);
[1146]323        instanceTree.addSelectionListener(new SelectionAdapter() {
324            @Override
325            public void widgetSelected(SelectionEvent e) {
326                TreeItem[] selectedItems = instanceTree.getSelection();
[1424]327                if ((selectedItems.length == 1) &&
328                    (selectedItems[0].getData() instanceof ITaskInstance))
329                {
330                    displayTaskDetails(((ITaskInstance) selectedItems[0].getData()).getTask());
[1146]331                }
[1424]332                else {
333                    clearTaskDetails();
334                }
[1146]335            }
336        });
[1094]337
[1424]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        });
[2033]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        }
[1424]357
[2127]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
[1146]370        buildInstanceTree();
[1424]371        instanceTreeTab.setControl(instanceTree);
[1094]372
[1424]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       
[1153]393        buildModelTree(taskModel);
[1424]394       
395        modelTreeTab.setControl(modelTree);
[2033]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        });
[1424]452    }
[2033]453   
[1424]454    /**
455     *
456     */
[2033]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);
[1424]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       
[2033]479        VisualizationUtils.addItemSpecificContextMenu
480            (taskDetailsTree, ITask.class, "show in task list", new SelectionAdapter()
[1424]481        {
[1094]482            @Override
483            public void widgetSelected(SelectionEvent e) {
[1424]484                navigateTo((ITask) taskDetailsTree.getSelection()[0].getData());
[1094]485            }
486        });
[1424]487       
[2033]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        }
[2127]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        });
[2033]510       
[1424]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);
[1094]517
[1424]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       
[2033]529        VisualizationUtils.addItemSpecificContextMenu
530            (parentTasks, ITask.class, "show in task list", new SelectionAdapter()
[1424]531        {
[1094]532            @Override
533            public void widgetSelected(SelectionEvent e) {
[1424]534                navigateTo((ITask) parentTasks.getSelection()[0].getData());
[1094]535            }
536        });
537       
[2033]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        }
[2127]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        });
[2033]560       
[1495]561        involvedTargetsTree =
562            VisualizationUtils.createTargetsTree(detailsBottomSashForm, "involved GUI elements");
[1424]563       
[2033]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       
[1424]572        detailsBottomSashForm.setWeights(new int[] { 2, 3 });
573        detailsSashForm.setWeights(new int[] { 1, 1 });
[1094]574    }
575
576    /**
[1275]577     * convenience method for creating the display of the instances
[1094]578     */
[1146]579    private void buildInstanceTree() {
580        List<IUserSession> sessions = taskModel.getUserSessions();
[1424]581
582        // load the correct children on expansion
583        VisualizationUtils.addExpansionListener(instanceTree, defaultExpansionListener);
584
[1146]585        TreeItem root = new TreeItem(instanceTree, SWT.NULL);
586        root.setText(sessions.size() + " sessions");
[1153]587        root.setData(taskModel);
[1146]588       
[2127]589        // simulate a child
590        new TreeItem(root, SWT.NULL);
591       
592        /*for (IUserSession session : sessions) {
[1146]593            buildInstanceTree(root, session);
[2127]594        }*/
[1424]595       
[2127]596        root.setExpanded(false);
[1094]597    }
598
599    /**
[1275]600     * convenience method for creating the display of the instances
[1094]601     */
[1294]602    private void buildInstanceTree(TreeItem currentParent, IUserSession session) {
[1424]603        TreeItem item = new TreeItem(currentParent, SWT.NULL);
604        item.setText(session.toString());
605        item.setData(session);
[1146]606       
[1424]607        // simulate a child
608        new TreeItem(item, SWT.NULL);
[1146]609    }
610
611    /**
[1275]612     * convenience method for creating the display of the task model
[1146]613     */
[1153]614    private void buildModelTree(ITaskModel taskModel) {
[2127]615        VisualizationUtils.addExpansionListener(modelTree, defaultExpansionListener);
616       
[1424]617        Collection<ITask> allTasks = taskModel.getTasks();
[1153]618       
[2160]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>();
[1153]624       
625        TreeItem root = new TreeItem(modelTree, SWT.NULL);
[1424]626        root.setText(allTasks.size() + " tasks in model");
[1153]627        root.setData(taskModel);
628
[2160]629        createSortedTaskLists(allTasks, sequences, iterations, selections, optionals, others);
[1153]630       
[1424]631        //allTasks = createSortedTaskList(allTasks);
[1153]632       
[1424]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       
[1153]639        root.setExpanded(true);
640    }
641
642    /**
[1424]643     *
[1153]644     */
[2160]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)
[1424]651    {
[2160]652        List<ITask> toAdd;
[1153]653       
[1424]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           
[1636]671            int taskValue = taskModel.getTaskInfo(task).getMeasureValue(TaskMetric.EVENT_COVERAGE);
[2160]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);
[1424]706        }
[1146]707    }
708
709    /**
[1424]710     *
[1146]711     */
[1424]712    private void clearTaskDetails() {
713        taskDetailsTree.removeAll();
714        executionVariantsTree.removeAll();
[1495]715        involvedTargetsTree.removeAll();
[1424]716        parentTasks.removeAll();
717    }
718
719    /**
720     *
721     */
722    private void displayTaskDetails(ITask task) {
723        clearTaskDetails();
[1146]724       
[1424]725        VisualizationUtils.createTreeItemFor(task, taskDetailsTree, taskModel, true);
726        VisualizationUtils.expandAll(taskDetailsTree, true);
[1153]727       
[1424]728        // do it twice. Otherwise, it doesn't work
729        VisualizationUtils.updateColumnWidths(taskDetailsTree);
[1262]730       
[1424]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>>() {
[1262]736            @Override
[1424]737            public int compare(Collection<ITaskInstance> list1, Collection<ITaskInstance> list2) {
738                return list2.size() - list1.size();
[1262]739            }
740        });
741       
[1424]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       
[1495]762        VisualizationUtils.addInvolvedTargets(involvedTargetsTree, task);
[1153]763    }
764
765    /**
[1424]766     *
[1153]767     */
[1424]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);
[1094]776            }
777        }
778    }
779
780    /**
[1424]781     *
[1094]782     */
[1424]783    private void navigateTo(ITask task) {
784        tabFolder.setSelection(1);
[1153]785       
[1424]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                }
[1153]795            }
796        }
[1424]797       
798        displayTaskDetails(task);
[1094]799    }
[2033]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    }
[1094]817   
[1153]818    /**
[1275]819     * ensures, that the children of a specific node are loaded
[1153]820     */
821    private void ensureChildren(TreeItem parent) {
[1424]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();
[1153]827                }
[1424]828            }
[1153]829
[1424]830            if (parent.getData() instanceof ITask) {
[1153]831                ITask task = (ITask) parent.getData();
832
833                if (task instanceof IStructuringTemporalRelationship) {
834                    for (ITask subTask : ((IStructuringTemporalRelationship) task).getChildren()) {
[1424]835                        VisualizationUtils.createTreeItemFor(subTask, parent, taskModel, true);
[1153]836                    }
837                }
838                else if (task instanceof IMarkingTemporalRelationship) {
[1424]839                    VisualizationUtils.createTreeItemFor
840                        (((IMarkingTemporalRelationship) task).getMarkedTask(), parent,
841                         taskModel, true);
[1153]842                }
843            }
[1424]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            }
[2127]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            }
[1153]877        }
878    }
879
880    /**
[1424]881     *
882     */
[2160]883    private void createModelTreeItemFor(List<ITask> taskList, String name, TreeItem parent) {
[1424]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) {
[2127]890            new TreeItem(item, SWT.NULL);
891            /*for (ITask task : taskList) {
[1424]892                VisualizationUtils.createTreeItemFor(task, item, taskModel, false);
[2127]893            }*/
[1424]894        }
895    }
896
897    /**
[1275]898     * convenience method to create a tree item for a task
[1153]899     */
[1424]900    private void createTreeItemFor(ITaskInstance taskInstance,
901                                   TreeItem      parent)
902    {
[1153]903        TreeItem item = new TreeItem(parent, SWT.NULL);
[1424]904        item.setText(taskInstance.toString());
905        item.setData(taskInstance);
[1153]906       
907        // simulate a child
[1424]908        if ((taskInstance instanceof ITaskInstanceList) ||
909            (taskInstance instanceof ISelectionInstance) ||
910            (taskInstance instanceof IOptionalInstance))
[1153]911        {
912            new TreeItem(item, SWT.NULL);
913        }
914    }
[2033]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    }
[1094]961}
Note: See TracBrowser for help on using the repository browser.