[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 | |
---|
| 15 | package de.ugoe.cs.autoquest.ui.swt; |
---|
| 16 | |
---|
[1153] | 17 | import java.util.Collection; |
---|
[1262] | 18 | import java.util.Collections; |
---|
| 19 | import java.util.Comparator; |
---|
[1153] | 20 | import java.util.HashSet; |
---|
[1262] | 21 | import java.util.LinkedList; |
---|
[1146] | 22 | import java.util.List; |
---|
[1153] | 23 | import java.util.Set; |
---|
[1146] | 24 | |
---|
[1094] | 25 | import org.eclipse.swt.SWT; |
---|
[1146] | 26 | import org.eclipse.swt.custom.SashForm; |
---|
[1094] | 27 | import org.eclipse.swt.events.SelectionAdapter; |
---|
| 28 | import org.eclipse.swt.events.SelectionEvent; |
---|
| 29 | import org.eclipse.swt.layout.GridData; |
---|
| 30 | import org.eclipse.swt.layout.GridLayout; |
---|
| 31 | import org.eclipse.swt.widgets.Button; |
---|
| 32 | import org.eclipse.swt.widgets.Dialog; |
---|
| 33 | import org.eclipse.swt.widgets.Display; |
---|
[1153] | 34 | import org.eclipse.swt.widgets.Event; |
---|
| 35 | import org.eclipse.swt.widgets.Listener; |
---|
[1094] | 36 | import org.eclipse.swt.widgets.Shell; |
---|
| 37 | import org.eclipse.swt.widgets.Tree; |
---|
| 38 | import org.eclipse.swt.widgets.TreeItem; |
---|
| 39 | |
---|
[1146] | 40 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IMarkingTemporalRelationship; |
---|
| 41 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IStructuringTemporalRelationship; |
---|
| 42 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
| 43 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
| 44 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; |
---|
| 45 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskModel; |
---|
| 46 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
[1094] | 47 | |
---|
| 48 | import org.eclipse.swt.widgets.Label; |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * <p> |
---|
[1275] | 52 | * a dialog to inspect the tasks and task instances of a task model |
---|
[1094] | 53 | * </p> |
---|
| 54 | * |
---|
| 55 | * @author Patrick Harms |
---|
| 56 | */ |
---|
| 57 | public class ShowTaskTreeDialog extends Dialog { |
---|
| 58 | |
---|
[1275] | 59 | /** the main shell */ |
---|
[1094] | 60 | protected Shell shell; |
---|
| 61 | |
---|
[1275] | 62 | /** the tree of task instances on the left */ |
---|
[1146] | 63 | private Tree instanceTree; |
---|
| 64 | |
---|
[1275] | 65 | /** the tree of tasks (model) on the right*/ |
---|
[1146] | 66 | private Tree modelTree; |
---|
[1094] | 67 | |
---|
[1275] | 68 | /** the displayed task model */ |
---|
[1146] | 69 | protected ITaskModel taskModel; |
---|
[1094] | 70 | |
---|
| 71 | /** |
---|
[1275] | 72 | * creates the dialog |
---|
[1094] | 73 | */ |
---|
[1146] | 74 | public ShowTaskTreeDialog(Shell parent, int style, ITaskModel taskModel, String taskTreeName) { |
---|
[1094] | 75 | super(parent, style); |
---|
[1146] | 76 | setText("Task Model " + taskTreeName); |
---|
| 77 | this.taskModel = taskModel; |
---|
[1094] | 78 | } |
---|
| 79 | |
---|
| 80 | /** |
---|
[1275] | 81 | * displays the dialog |
---|
[1094] | 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 | /** |
---|
[1275] | 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. |
---|
[1094] | 99 | */ |
---|
| 100 | private void createContents() { |
---|
| 101 | shell = new Shell(getParent(), SWT.SHELL_TRIM | SWT.BORDER | SWT.APPLICATION_MODAL); |
---|
[1153] | 102 | shell.setSize(750, 800); |
---|
[1094] | 103 | shell.setText(getText()); |
---|
| 104 | |
---|
| 105 | shell.setLayout(new GridLayout(4, false)); |
---|
[1146] | 106 | |
---|
[1153] | 107 | SashForm sashForm = new SashForm(shell, SWT.HORIZONTAL); |
---|
[1146] | 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(); |
---|
[1153] | 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 | } |
---|
[1146] | 123 | } |
---|
| 124 | } |
---|
| 125 | }); |
---|
[1094] | 126 | |
---|
[1146] | 127 | buildInstanceTree(); |
---|
[1094] | 128 | |
---|
[1146] | 129 | modelTree = new Tree(sashForm, SWT.BORDER | SWT.MULTI); |
---|
[1153] | 130 | buildModelTree(taskModel); |
---|
[1094] | 131 | |
---|
| 132 | Button btnExpandAll = new Button(shell, SWT.NONE); |
---|
| 133 | btnExpandAll.addSelectionListener(new SelectionAdapter() { |
---|
| 134 | @Override |
---|
| 135 | public void widgetSelected(SelectionEvent e) { |
---|
[1146] | 136 | expandAll(instanceTree, true); |
---|
[1094] | 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) { |
---|
[1146] | 145 | expandAll(instanceTree, false); |
---|
[1094] | 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 | /** |
---|
[1275] | 158 | * convenience method for creating the display of the instances |
---|
[1094] | 159 | */ |
---|
[1146] | 160 | private void buildInstanceTree() { |
---|
| 161 | List<IUserSession> sessions = taskModel.getUserSessions(); |
---|
[1094] | 162 | |
---|
[1146] | 163 | TreeItem root = new TreeItem(instanceTree, SWT.NULL); |
---|
| 164 | root.setText(sessions.size() + " sessions"); |
---|
[1153] | 165 | root.setData(taskModel); |
---|
[1146] | 166 | |
---|
| 167 | for (IUserSession session : sessions) { |
---|
| 168 | buildInstanceTree(root, session); |
---|
| 169 | } |
---|
[1094] | 170 | } |
---|
| 171 | |
---|
| 172 | /** |
---|
[1275] | 173 | * convenience method for creating the display of the instances |
---|
[1094] | 174 | */ |
---|
[1146] | 175 | private void buildInstanceTree(TreeItem currentParent, ITaskInstanceList taskInstanceList) { |
---|
| 176 | TreeItem child = new TreeItem(currentParent, SWT.NULL); |
---|
| 177 | child.setText(taskInstanceList.toString()); |
---|
| 178 | child.setData(taskInstanceList); |
---|
| 179 | |
---|
| 180 | for (ITaskInstanceList childTask : taskInstanceList) { |
---|
| 181 | buildInstanceTree(child, childTask); |
---|
| 182 | } |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | /** |
---|
[1275] | 186 | * convenience method for creating the display of the task model |
---|
[1146] | 187 | */ |
---|
[1153] | 188 | private void buildModelTree(ITaskModel taskModel) { |
---|
| 189 | modelTree.removeAll(); |
---|
| 190 | |
---|
| 191 | // load the correct children on expansion |
---|
| 192 | if (modelTree.getListeners(SWT.Expand).length == 0) { |
---|
| 193 | modelTree.addListener(SWT.Expand, new Listener() { |
---|
| 194 | public void handleEvent(final Event event) { |
---|
| 195 | ensureChildren((TreeItem) event.item); |
---|
| 196 | ((TreeItem) event.item).setExpanded(true); |
---|
| 197 | } |
---|
| 198 | }); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | Collection<ITask> tasks = taskModel.getTasks(); |
---|
| 202 | |
---|
| 203 | TreeItem root = new TreeItem(modelTree, SWT.NULL); |
---|
[1262] | 204 | root.setText(tasks.size() + " tasks in model (showing only root tasks)"); |
---|
[1153] | 205 | root.setData(taskModel); |
---|
| 206 | |
---|
[1262] | 207 | tasks = createSortedTaskList(tasks); |
---|
[1153] | 208 | |
---|
| 209 | for (ITask task : tasks) { |
---|
| 210 | createTreeItemFor(task, root); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | root.setExpanded(true); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | /** |
---|
[1275] | 217 | * convenience method for creating the display of the task model |
---|
[1153] | 218 | */ |
---|
[1146] | 219 | private void buildModelTree(ITaskInstance taskInstance) { |
---|
| 220 | modelTree.removeAll(); |
---|
| 221 | TreeItem root = new TreeItem(modelTree, SWT.NULL); |
---|
| 222 | root.setText("model of instance " + taskInstance); |
---|
| 223 | root.setData(taskInstance); |
---|
| 224 | |
---|
[1153] | 225 | createTreeItemFor(taskInstance.getTask(), root); |
---|
| 226 | |
---|
| 227 | expandAll(root, true, 20); |
---|
[1146] | 228 | } |
---|
| 229 | |
---|
| 230 | /** |
---|
[1275] | 231 | * reduces the list of tasks in a model so that it only contains root nodes and that it is |
---|
| 232 | * sorted starting with the most complex task first |
---|
[1146] | 233 | */ |
---|
[1262] | 234 | private List<ITask> createSortedTaskList(Collection<ITask> tasks) { |
---|
| 235 | Set<ITask> resultSet = new HashSet<ITask>(tasks); |
---|
[1146] | 236 | |
---|
[1153] | 237 | for (ITask task : tasks) { |
---|
[1262] | 238 | removeChildren(task, resultSet); |
---|
[1153] | 239 | } |
---|
| 240 | |
---|
[1262] | 241 | List<ITask> result = new LinkedList<ITask>(resultSet); |
---|
| 242 | |
---|
| 243 | Collections.sort(result, new Comparator<ITask>() { |
---|
| 244 | @Override |
---|
| 245 | public int compare(ITask task1, ITask task2) { |
---|
[1275] | 246 | return depth(task2) - depth(task1); |
---|
[1262] | 247 | } |
---|
| 248 | |
---|
| 249 | private int depth(ITask task) { |
---|
| 250 | int maxChildDepth = 0; |
---|
| 251 | if (task instanceof IStructuringTemporalRelationship) { |
---|
| 252 | for (ITask child : ((IStructuringTemporalRelationship) task).getChildren()) { |
---|
| 253 | maxChildDepth = Math.max(maxChildDepth, depth(child)); |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | else if (task instanceof IMarkingTemporalRelationship) { |
---|
| 257 | maxChildDepth = Math.max |
---|
| 258 | (maxChildDepth, depth(((IMarkingTemporalRelationship) task).getMarkedTask())); |
---|
| 259 | } |
---|
| 260 | return maxChildDepth + 1; |
---|
| 261 | } |
---|
| 262 | }); |
---|
| 263 | |
---|
[1153] | 264 | return result; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | /** |
---|
[1275] | 268 | * convenience method for removed all non root tasks of the provided list |
---|
[1153] | 269 | */ |
---|
| 270 | private void removeChildren(ITask task, Set<ITask> result) { |
---|
[1146] | 271 | if (task instanceof IStructuringTemporalRelationship) { |
---|
[1153] | 272 | for (ITask child : ((IStructuringTemporalRelationship) task).getChildren()) { |
---|
| 273 | result.remove(child); |
---|
[1094] | 274 | } |
---|
| 275 | } |
---|
[1146] | 276 | else if (task instanceof IMarkingTemporalRelationship) { |
---|
[1153] | 277 | result.remove(((IMarkingTemporalRelationship) task).getMarkedTask()); |
---|
[1146] | 278 | } |
---|
[1094] | 279 | } |
---|
| 280 | |
---|
| 281 | /** |
---|
[1275] | 282 | * expands all nodes in the tree |
---|
[1094] | 283 | */ |
---|
| 284 | private void expandAll(Tree tree, boolean expanded) { |
---|
| 285 | for (TreeItem item : tree.getItems()) { |
---|
[1153] | 286 | expandAll(item, expanded, Integer.MAX_VALUE); |
---|
[1094] | 287 | } |
---|
| 288 | } |
---|
| 289 | |
---|
| 290 | /** |
---|
[1275] | 291 | * expands all nodes in the tree |
---|
[1094] | 292 | */ |
---|
[1153] | 293 | private void expandAll(TreeItem item, boolean expanded, int maxChildrenToExpand) { |
---|
| 294 | if (expanded) { |
---|
| 295 | ensureChildren(item); |
---|
[1094] | 296 | } |
---|
[1153] | 297 | |
---|
| 298 | if (item.getItems().length < maxChildrenToExpand) { |
---|
| 299 | item.setExpanded(expanded); |
---|
| 300 | |
---|
| 301 | for (TreeItem childItem : item.getItems()) { |
---|
| 302 | expandAll(childItem, expanded, maxChildrenToExpand); |
---|
| 303 | } |
---|
| 304 | } |
---|
[1094] | 305 | } |
---|
| 306 | |
---|
[1153] | 307 | /** |
---|
[1275] | 308 | * ensures, that the children of a specific node are loaded |
---|
[1153] | 309 | */ |
---|
| 310 | private void ensureChildren(TreeItem parent) { |
---|
| 311 | if (parent.getData() instanceof ITask) { |
---|
| 312 | TreeItem[] items = parent.getItems(); |
---|
| 313 | if ((items == null) || (items.length == 0) || (items[0].getData() == null)) { |
---|
[1185] | 314 | if (items != null) { |
---|
| 315 | for (int i = 0; i < items.length; i++) { |
---|
| 316 | items[i].dispose(); |
---|
| 317 | } |
---|
[1153] | 318 | } |
---|
| 319 | |
---|
| 320 | ITask task = (ITask) parent.getData(); |
---|
| 321 | |
---|
| 322 | if (task instanceof IStructuringTemporalRelationship) { |
---|
| 323 | for (ITask subTask : ((IStructuringTemporalRelationship) task).getChildren()) { |
---|
| 324 | createTreeItemFor(subTask, parent); |
---|
| 325 | } |
---|
| 326 | } |
---|
| 327 | else if (task instanceof IMarkingTemporalRelationship) { |
---|
| 328 | createTreeItemFor |
---|
| 329 | (((IMarkingTemporalRelationship) task).getMarkedTask(), parent); |
---|
| 330 | } |
---|
| 331 | } |
---|
| 332 | } |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | /** |
---|
[1275] | 336 | * convenience method to create a tree item for a task |
---|
[1153] | 337 | */ |
---|
| 338 | private void createTreeItemFor(ITask task, TreeItem parent) { |
---|
| 339 | TreeItem item = new TreeItem(parent, SWT.NULL); |
---|
| 340 | item.setText(task.toString()); |
---|
| 341 | item.setData(task); |
---|
| 342 | |
---|
| 343 | // simulate a child |
---|
| 344 | if ((task instanceof IStructuringTemporalRelationship) || |
---|
| 345 | (task instanceof IMarkingTemporalRelationship)) |
---|
| 346 | { |
---|
| 347 | new TreeItem(item, SWT.NULL); |
---|
| 348 | } |
---|
| 349 | } |
---|
| 350 | |
---|
[1094] | 351 | } |
---|