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

Last change on this file since 1294 was 1294, checked in by pharms, 11 years ago
  • rework of task model to move event instance stuff to task instances
  • introduction of sequence, selection, iteration and optional instances
File size: 12.7 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.util.Collection;
18import java.util.Collections;
19import java.util.Comparator;
20import java.util.LinkedList;
21import java.util.List;
22
23import org.eclipse.swt.SWT;
24import org.eclipse.swt.custom.SashForm;
25import org.eclipse.swt.events.SelectionAdapter;
26import org.eclipse.swt.events.SelectionEvent;
27import org.eclipse.swt.layout.GridData;
28import org.eclipse.swt.layout.GridLayout;
29import org.eclipse.swt.widgets.Button;
30import org.eclipse.swt.widgets.Dialog;
31import org.eclipse.swt.widgets.Display;
32import org.eclipse.swt.widgets.Event;
33import org.eclipse.swt.widgets.Listener;
34import org.eclipse.swt.widgets.Shell;
35import org.eclipse.swt.widgets.Tree;
36import org.eclipse.swt.widgets.TreeItem;
37
38import de.ugoe.cs.autoquest.tasktrees.treeifc.IMarkingTemporalRelationship;
39import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance;
40import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance;
41import de.ugoe.cs.autoquest.tasktrees.treeifc.IStructuringTemporalRelationship;
42import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
43import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
44import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList;
45import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskModel;
46import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession;
47
48import org.eclipse.swt.widgets.Label;
49
50/**
51 * <p>
52 * a dialog to inspect the tasks and task instances of a task model
53 * </p>
54 *
55 * @author Patrick Harms
56 */
57public class ShowTaskTreeDialog extends Dialog {
58
59    /** the main shell */
60    protected Shell shell;
61   
62    /** the tree of task instances on the left */
63    private Tree instanceTree;
64   
65    /** the tree of tasks (model) on the right*/
66    private Tree modelTree;
67
68    /** the displayed task model */
69    protected ITaskModel taskModel;
70
71    /**
72     * creates the dialog
73     */
74    public ShowTaskTreeDialog(Shell parent, int style, ITaskModel taskModel, String taskTreeName) {
75        super(parent, style);
76        setText("Task Model " + taskTreeName);
77        this.taskModel = taskModel;
78    }
79
80    /**
81     * displays the dialog
82     */
83    public void open() {
84        createContents();
85        shell.open();
86        shell.layout();
87        Display display = getParent().getDisplay();
88        while (!shell.isDisposed()) {
89            if (!display.readAndDispatch()) {
90                display.sleep();
91            }
92        }
93    }
94
95    /**
96     * creates the two views, one on the task instances on the left, on on the task models on the
97     * right. Also adds a selection adapter to the task instances so that for a selected task
98     * instance always the respective model is presented.
99     */
100    private void createContents() {
101        shell = new Shell(getParent(), SWT.SHELL_TRIM | SWT.BORDER | SWT.APPLICATION_MODAL);
102        shell.setSize(750, 800);
103        shell.setText(getText());
104
105        shell.setLayout(new GridLayout(4, false));
106       
107        SashForm sashForm = new SashForm(shell, SWT.HORIZONTAL);
108        sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 4, 1));
109       
110        instanceTree = new Tree(sashForm, SWT.BORDER | SWT.MULTI);
111        instanceTree.addSelectionListener(new SelectionAdapter() {
112            @Override
113            public void widgetSelected(SelectionEvent e) {
114                modelTree.removeAll();
115                TreeItem[] selectedItems = instanceTree.getSelection();
116                if (selectedItems.length == 1) {
117                    if (selectedItems[0].getData() instanceof ITaskInstance) {
118                        buildModelTree((ITaskInstance) selectedItems[0].getData());
119                    }
120                    else if (selectedItems[0].getData() instanceof ITaskModel) {
121                        buildModelTree((ITaskModel) selectedItems[0].getData());
122                    }
123                }
124            }
125        });
126
127        buildInstanceTree();
128
129        modelTree = new Tree(sashForm, SWT.BORDER | SWT.MULTI);
130        buildModelTree(taskModel);
131
132        Button btnExpandAll = new Button(shell, SWT.NONE);
133        btnExpandAll.addSelectionListener(new SelectionAdapter() {
134            @Override
135            public void widgetSelected(SelectionEvent e) {
136                expandAll(instanceTree, true);
137            }
138        });
139        btnExpandAll.setText("Expand all");
140
141        Button btnCollapseAll = new Button(shell, SWT.NONE);
142        btnCollapseAll.addSelectionListener(new SelectionAdapter() {
143            @Override
144            public void widgetSelected(SelectionEvent e) {
145                expandAll(instanceTree, false);
146            }
147        });
148        btnCollapseAll.setText("Collapse all");
149       
150        //new Label(shell, SWT.NONE);
151        new Label(shell, SWT.NONE);
152        new Label(shell, SWT.NONE);
153        new Label(shell, SWT.NONE);
154
155    }
156
157    /**
158     * convenience method for creating the display of the instances
159     */
160    private void buildInstanceTree() {
161        List<IUserSession> sessions = taskModel.getUserSessions();
162       
163        TreeItem root = new TreeItem(instanceTree, SWT.NULL);
164        root.setText(sessions.size() + " sessions");
165        root.setData(taskModel);
166       
167        for (IUserSession session : sessions) {
168            buildInstanceTree(root, session);
169        }
170    }
171
172    /**
173     * convenience method for creating the display of the instances
174     */
175    private void buildInstanceTree(TreeItem currentParent, IUserSession session) {
176        TreeItem child = new TreeItem(currentParent, SWT.NULL);
177        child.setText(session.toString());
178        child.setData(session);
179       
180        for (ITaskInstance childInstance : session) {
181            buildInstanceTree(child, childInstance);
182        }
183    }
184
185    /**
186     * convenience method for creating the display of the instances
187     */
188    private void buildInstanceTree(TreeItem currentParent, ITaskInstance taskInstance) {
189        TreeItem child = new TreeItem(currentParent, SWT.NULL);
190        child.setText(taskInstance.toString());
191        child.setData(taskInstance);
192       
193        if (taskInstance instanceof ITaskInstanceList) {
194            for (ITaskInstance childInstance : (ITaskInstanceList) taskInstance) {
195                buildInstanceTree(child, childInstance);
196            }
197        }
198        else if (taskInstance instanceof ISelectionInstance) {
199            buildInstanceTree(child, ((ISelectionInstance) taskInstance).getChild());
200        }
201        else if (taskInstance instanceof IOptionalInstance) {
202            buildInstanceTree(child, ((IOptionalInstance) taskInstance).getChild());
203        }
204    }
205
206    /**
207     * convenience method for creating the display of the task model
208     */
209    private void buildModelTree(ITaskModel taskModel) {
210        modelTree.removeAll();
211       
212        // load the correct children on expansion
213        if (modelTree.getListeners(SWT.Expand).length == 0) {
214           modelTree.addListener(SWT.Expand, new Listener() {
215               public void handleEvent(final Event event) {
216                   ensureChildren((TreeItem) event.item);
217                   ((TreeItem) event.item).setExpanded(true);
218               }
219           });
220        }
221       
222        Collection<ITask> tasks = taskModel.getTasks();
223       
224        TreeItem root = new TreeItem(modelTree, SWT.NULL);
225        root.setText(tasks.size() + " tasks in model (showing only root tasks)");
226        root.setData(taskModel);
227
228        tasks = createSortedTaskList(tasks);
229       
230        for (ITask task : tasks) {
231            createTreeItemFor(task, root);
232        }
233       
234        root.setExpanded(true);
235    }
236
237    /**
238     * convenience method for creating the display of the task model
239     */
240    private void buildModelTree(ITaskInstance taskInstance) {
241        modelTree.removeAll();
242        TreeItem root = new TreeItem(modelTree, SWT.NULL);
243        root.setText("model of instance " + taskInstance);
244        root.setData(taskInstance);
245
246        createTreeItemFor(taskInstance.getTask(), root);
247       
248        expandAll(root, true, 20);
249    }
250
251    /**
252     * sort the list of tasks so that it contain those occurring most often first
253     */
254    private List<ITask> createSortedTaskList(Collection<ITask> tasks) {
255        /*Set<ITask> resultSet = new HashSet<ITask>(tasks);
256       
257        for (ITask task : tasks) {
258            removeChildren(task, resultSet);
259        }*/
260       
261        List<ITask> result = new LinkedList<ITask>(tasks);
262       
263        Collections.sort(result, new Comparator<ITask>() {
264            @Override
265            public int compare(ITask task1, ITask task2) {
266                return taskModel.getTaskInfo(task2).getCount() -
267                    taskModel.getTaskInfo(task1).getCount();
268            }
269
270            /*private int depth(ITask task) {
271                int maxChildDepth = 0;
272                if (task instanceof IStructuringTemporalRelationship) {
273                    for (ITask child : ((IStructuringTemporalRelationship) task).getChildren()) {
274                        maxChildDepth = Math.max(maxChildDepth, depth(child));
275                    }
276                }
277                else if (task instanceof IMarkingTemporalRelationship) {
278                    maxChildDepth = Math.max
279                        (maxChildDepth, depth(((IMarkingTemporalRelationship) task).getMarkedTask()));
280                }
281                return maxChildDepth + 1;
282            }*/
283        });
284       
285        return result;
286    }
287
288    /**
289     * convenience method for removed all non root tasks of the provided list
290     */
291    /*private void removeChildren(ITask task, Set<ITask> result) {
292        if (task instanceof IStructuringTemporalRelationship) {
293            for (ITask child : ((IStructuringTemporalRelationship) task).getChildren()) {
294                result.remove(child);
295            }
296        }
297        else if (task instanceof IMarkingTemporalRelationship) {
298            result.remove(((IMarkingTemporalRelationship) task).getMarkedTask());
299        }
300    }*/
301
302    /**
303     * expands all nodes in the tree
304     */
305    private void expandAll(Tree tree, boolean expanded) {
306        for (TreeItem item : tree.getItems()) {
307            expandAll(item, expanded, Integer.MAX_VALUE);
308        }
309    }
310
311    /**
312     * expands all nodes in the tree
313     */
314    private void expandAll(TreeItem item, boolean expanded, int maxChildrenToExpand) {
315        if (expanded) {
316            ensureChildren(item);
317        }
318       
319        if (item.getItems().length < maxChildrenToExpand) {
320            item.setExpanded(expanded);
321
322            for (TreeItem childItem : item.getItems()) {
323                expandAll(childItem, expanded, maxChildrenToExpand);
324            }
325        }
326    }
327   
328    /**
329     * ensures, that the children of a specific node are loaded
330     */
331    private void ensureChildren(TreeItem parent) {
332        if (parent.getData() instanceof ITask) {
333            TreeItem[] items = parent.getItems();
334            if ((items == null) || (items.length == 0) || (items[0].getData() == null)) {
335                if (items != null) {
336                    for (int i = 0; i < items.length; i++) {
337                        items[i].dispose();
338                    }
339                }
340
341                ITask task = (ITask) parent.getData();
342
343                if (task instanceof IStructuringTemporalRelationship) {
344                    for (ITask subTask : ((IStructuringTemporalRelationship) task).getChildren()) {
345                        createTreeItemFor(subTask, parent);
346                    }
347                }
348                else if (task instanceof IMarkingTemporalRelationship) {
349                    createTreeItemFor
350                        (((IMarkingTemporalRelationship) task).getMarkedTask(), parent);
351                }
352            }
353        }
354    }
355
356    /**
357     * convenience method to create a tree item for a task
358     */
359    private void createTreeItemFor(ITask task, TreeItem parent) {
360        TreeItem item = new TreeItem(parent, SWT.NULL);
361        item.setText(task.toString() + " (" + taskModel.getTaskInfo(task).getCount() + ")");
362        item.setData(task);
363       
364        // simulate a child
365        if ((task instanceof IStructuringTemporalRelationship) ||
366            (task instanceof IMarkingTemporalRelationship))
367        {
368            new TreeItem(item, SWT.NULL);
369        }
370    }
371
372}
Note: See TracBrowser for help on using the repository browser.