[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; |
---|
| 18 | import java.util.HashSet; |
---|
[1146] | 19 | import java.util.List; |
---|
[1153] | 20 | import java.util.Set; |
---|
[1146] | 21 | |
---|
[1094] | 22 | import org.eclipse.swt.SWT; |
---|
[1146] | 23 | import org.eclipse.swt.custom.SashForm; |
---|
[1094] | 24 | import org.eclipse.swt.events.SelectionAdapter; |
---|
| 25 | import org.eclipse.swt.events.SelectionEvent; |
---|
| 26 | import org.eclipse.swt.layout.GridData; |
---|
| 27 | import org.eclipse.swt.layout.GridLayout; |
---|
| 28 | import org.eclipse.swt.widgets.Button; |
---|
| 29 | import org.eclipse.swt.widgets.Dialog; |
---|
| 30 | import org.eclipse.swt.widgets.Display; |
---|
[1153] | 31 | import org.eclipse.swt.widgets.Event; |
---|
| 32 | import org.eclipse.swt.widgets.Listener; |
---|
[1094] | 33 | import org.eclipse.swt.widgets.Shell; |
---|
| 34 | import org.eclipse.swt.widgets.Tree; |
---|
| 35 | import org.eclipse.swt.widgets.TreeItem; |
---|
| 36 | |
---|
[1146] | 37 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IMarkingTemporalRelationship; |
---|
| 38 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IStructuringTemporalRelationship; |
---|
| 39 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
| 40 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
| 41 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; |
---|
| 42 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskModel; |
---|
| 43 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
[1094] | 44 | |
---|
| 45 | import org.eclipse.swt.widgets.Label; |
---|
| 46 | |
---|
| 47 | /** |
---|
| 48 | * <p> |
---|
| 49 | * TODO comment |
---|
| 50 | * </p> |
---|
| 51 | * |
---|
| 52 | * @author Patrick Harms |
---|
| 53 | */ |
---|
| 54 | public class ShowTaskTreeDialog extends Dialog { |
---|
| 55 | |
---|
| 56 | /** */ |
---|
| 57 | protected Shell shell; |
---|
| 58 | |
---|
| 59 | /** */ |
---|
[1146] | 60 | private Tree instanceTree; |
---|
| 61 | |
---|
| 62 | /** */ |
---|
| 63 | private Tree modelTree; |
---|
[1094] | 64 | |
---|
| 65 | /** */ |
---|
[1146] | 66 | protected ITaskModel taskModel; |
---|
[1094] | 67 | |
---|
| 68 | /** |
---|
| 69 | * |
---|
| 70 | */ |
---|
[1146] | 71 | public ShowTaskTreeDialog(Shell parent, int style, ITaskModel taskModel, String taskTreeName) { |
---|
[1094] | 72 | super(parent, style); |
---|
[1146] | 73 | setText("Task Model " + taskTreeName); |
---|
| 74 | this.taskModel = taskModel; |
---|
[1094] | 75 | } |
---|
| 76 | |
---|
| 77 | /** |
---|
| 78 | * |
---|
| 79 | */ |
---|
| 80 | public void open() { |
---|
| 81 | createContents(); |
---|
| 82 | shell.open(); |
---|
| 83 | shell.layout(); |
---|
| 84 | Display display = getParent().getDisplay(); |
---|
| 85 | while (!shell.isDisposed()) { |
---|
| 86 | if (!display.readAndDispatch()) { |
---|
| 87 | display.sleep(); |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | * |
---|
| 94 | */ |
---|
| 95 | private void createContents() { |
---|
| 96 | shell = new Shell(getParent(), SWT.SHELL_TRIM | SWT.BORDER | SWT.APPLICATION_MODAL); |
---|
[1153] | 97 | shell.setSize(750, 800); |
---|
[1094] | 98 | shell.setText(getText()); |
---|
| 99 | |
---|
| 100 | shell.setLayout(new GridLayout(4, false)); |
---|
[1146] | 101 | |
---|
[1153] | 102 | SashForm sashForm = new SashForm(shell, SWT.HORIZONTAL); |
---|
[1146] | 103 | sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 4, 1)); |
---|
| 104 | |
---|
| 105 | instanceTree = new Tree(sashForm, SWT.BORDER | SWT.MULTI); |
---|
| 106 | instanceTree.addSelectionListener(new SelectionAdapter() { |
---|
| 107 | @Override |
---|
| 108 | public void widgetSelected(SelectionEvent e) { |
---|
| 109 | modelTree.removeAll(); |
---|
| 110 | TreeItem[] selectedItems = instanceTree.getSelection(); |
---|
[1153] | 111 | if (selectedItems.length == 1) { |
---|
| 112 | if (selectedItems[0].getData() instanceof ITaskInstance) { |
---|
| 113 | buildModelTree((ITaskInstance) selectedItems[0].getData()); |
---|
| 114 | } |
---|
| 115 | else if (selectedItems[0].getData() instanceof ITaskModel) { |
---|
| 116 | buildModelTree((ITaskModel) selectedItems[0].getData()); |
---|
| 117 | } |
---|
[1146] | 118 | } |
---|
| 119 | } |
---|
| 120 | }); |
---|
[1094] | 121 | |
---|
[1146] | 122 | buildInstanceTree(); |
---|
[1094] | 123 | |
---|
[1146] | 124 | modelTree = new Tree(sashForm, SWT.BORDER | SWT.MULTI); |
---|
[1153] | 125 | buildModelTree(taskModel); |
---|
[1094] | 126 | |
---|
| 127 | Button btnExpandAll = new Button(shell, SWT.NONE); |
---|
| 128 | btnExpandAll.addSelectionListener(new SelectionAdapter() { |
---|
| 129 | @Override |
---|
| 130 | public void widgetSelected(SelectionEvent e) { |
---|
[1146] | 131 | expandAll(instanceTree, true); |
---|
[1094] | 132 | } |
---|
| 133 | }); |
---|
| 134 | btnExpandAll.setText("Expand all"); |
---|
| 135 | |
---|
| 136 | Button btnCollapseAll = new Button(shell, SWT.NONE); |
---|
| 137 | btnCollapseAll.addSelectionListener(new SelectionAdapter() { |
---|
| 138 | @Override |
---|
| 139 | public void widgetSelected(SelectionEvent e) { |
---|
[1146] | 140 | expandAll(instanceTree, false); |
---|
[1094] | 141 | } |
---|
| 142 | }); |
---|
| 143 | btnCollapseAll.setText("Collapse all"); |
---|
| 144 | |
---|
| 145 | //new Label(shell, SWT.NONE); |
---|
| 146 | new Label(shell, SWT.NONE); |
---|
| 147 | new Label(shell, SWT.NONE); |
---|
| 148 | new Label(shell, SWT.NONE); |
---|
| 149 | |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | /** |
---|
| 153 | * |
---|
| 154 | */ |
---|
[1146] | 155 | private void buildInstanceTree() { |
---|
| 156 | List<IUserSession> sessions = taskModel.getUserSessions(); |
---|
[1094] | 157 | |
---|
[1146] | 158 | TreeItem root = new TreeItem(instanceTree, SWT.NULL); |
---|
| 159 | root.setText(sessions.size() + " sessions"); |
---|
[1153] | 160 | root.setData(taskModel); |
---|
[1146] | 161 | |
---|
| 162 | for (IUserSession session : sessions) { |
---|
| 163 | buildInstanceTree(root, session); |
---|
| 164 | } |
---|
[1094] | 165 | } |
---|
| 166 | |
---|
| 167 | /** |
---|
| 168 | * |
---|
| 169 | */ |
---|
[1146] | 170 | private void buildInstanceTree(TreeItem currentParent, ITaskInstanceList taskInstanceList) { |
---|
| 171 | TreeItem child = new TreeItem(currentParent, SWT.NULL); |
---|
| 172 | child.setText(taskInstanceList.toString()); |
---|
| 173 | child.setData(taskInstanceList); |
---|
| 174 | |
---|
| 175 | for (ITaskInstanceList childTask : taskInstanceList) { |
---|
| 176 | buildInstanceTree(child, childTask); |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | /** |
---|
| 181 | * |
---|
| 182 | */ |
---|
[1153] | 183 | private void buildModelTree(ITaskModel taskModel) { |
---|
| 184 | modelTree.removeAll(); |
---|
| 185 | |
---|
| 186 | // load the correct children on expansion |
---|
| 187 | if (modelTree.getListeners(SWT.Expand).length == 0) { |
---|
| 188 | modelTree.addListener(SWT.Expand, new Listener() { |
---|
| 189 | public void handleEvent(final Event event) { |
---|
| 190 | ensureChildren((TreeItem) event.item); |
---|
| 191 | ((TreeItem) event.item).setExpanded(true); |
---|
| 192 | } |
---|
| 193 | }); |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | Collection<ITask> tasks = taskModel.getTasks(); |
---|
| 197 | |
---|
| 198 | TreeItem root = new TreeItem(modelTree, SWT.NULL); |
---|
| 199 | root.setText(tasks.size() + " tasks in model"); |
---|
| 200 | root.setData(taskModel); |
---|
| 201 | |
---|
| 202 | tasks = determineRootTasks(tasks); |
---|
| 203 | |
---|
| 204 | for (ITask task : tasks) { |
---|
| 205 | createTreeItemFor(task, root); |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | root.setExpanded(true); |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | /** |
---|
| 212 | * |
---|
| 213 | */ |
---|
[1146] | 214 | private void buildModelTree(ITaskInstance taskInstance) { |
---|
| 215 | modelTree.removeAll(); |
---|
| 216 | TreeItem root = new TreeItem(modelTree, SWT.NULL); |
---|
| 217 | root.setText("model of instance " + taskInstance); |
---|
| 218 | root.setData(taskInstance); |
---|
| 219 | |
---|
[1153] | 220 | createTreeItemFor(taskInstance.getTask(), root); |
---|
| 221 | |
---|
| 222 | expandAll(root, true, 20); |
---|
[1146] | 223 | } |
---|
| 224 | |
---|
| 225 | /** |
---|
[1153] | 226 | * |
---|
[1146] | 227 | */ |
---|
[1153] | 228 | private Collection<ITask> determineRootTasks(Collection<ITask> tasks) { |
---|
| 229 | Set<ITask> result = new HashSet<ITask>(tasks); |
---|
[1146] | 230 | |
---|
[1153] | 231 | for (ITask task : tasks) { |
---|
| 232 | removeChildren(task, result); |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | return result; |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | /** |
---|
| 239 | * |
---|
| 240 | */ |
---|
| 241 | private void removeChildren(ITask task, Set<ITask> result) { |
---|
[1146] | 242 | if (task instanceof IStructuringTemporalRelationship) { |
---|
[1153] | 243 | for (ITask child : ((IStructuringTemporalRelationship) task).getChildren()) { |
---|
| 244 | result.remove(child); |
---|
[1094] | 245 | } |
---|
| 246 | } |
---|
[1146] | 247 | else if (task instanceof IMarkingTemporalRelationship) { |
---|
[1153] | 248 | result.remove(((IMarkingTemporalRelationship) task).getMarkedTask()); |
---|
[1146] | 249 | } |
---|
[1094] | 250 | } |
---|
| 251 | |
---|
| 252 | /** |
---|
| 253 | * |
---|
| 254 | */ |
---|
| 255 | private void expandAll(Tree tree, boolean expanded) { |
---|
| 256 | for (TreeItem item : tree.getItems()) { |
---|
[1153] | 257 | expandAll(item, expanded, Integer.MAX_VALUE); |
---|
[1094] | 258 | } |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | /** |
---|
| 262 | * |
---|
| 263 | */ |
---|
[1153] | 264 | private void expandAll(TreeItem item, boolean expanded, int maxChildrenToExpand) { |
---|
| 265 | if (expanded) { |
---|
| 266 | ensureChildren(item); |
---|
[1094] | 267 | } |
---|
[1153] | 268 | |
---|
| 269 | if (item.getItems().length < maxChildrenToExpand) { |
---|
| 270 | item.setExpanded(expanded); |
---|
| 271 | |
---|
| 272 | for (TreeItem childItem : item.getItems()) { |
---|
| 273 | expandAll(childItem, expanded, maxChildrenToExpand); |
---|
| 274 | } |
---|
| 275 | } |
---|
[1094] | 276 | } |
---|
| 277 | |
---|
[1153] | 278 | /** |
---|
| 279 | * |
---|
| 280 | */ |
---|
| 281 | private void ensureChildren(TreeItem parent) { |
---|
| 282 | if (parent.getData() instanceof ITask) { |
---|
| 283 | TreeItem[] items = parent.getItems(); |
---|
| 284 | if ((items == null) || (items.length == 0) || (items[0].getData() == null)) { |
---|
[1185] | 285 | if (items != null) { |
---|
| 286 | for (int i = 0; i < items.length; i++) { |
---|
| 287 | items[i].dispose(); |
---|
| 288 | } |
---|
[1153] | 289 | } |
---|
| 290 | |
---|
| 291 | ITask task = (ITask) parent.getData(); |
---|
| 292 | |
---|
| 293 | if (task instanceof IStructuringTemporalRelationship) { |
---|
| 294 | for (ITask subTask : ((IStructuringTemporalRelationship) task).getChildren()) { |
---|
| 295 | createTreeItemFor(subTask, parent); |
---|
| 296 | } |
---|
| 297 | } |
---|
| 298 | else if (task instanceof IMarkingTemporalRelationship) { |
---|
| 299 | createTreeItemFor |
---|
| 300 | (((IMarkingTemporalRelationship) task).getMarkedTask(), parent); |
---|
| 301 | } |
---|
| 302 | } |
---|
| 303 | } |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | /** |
---|
| 307 | * |
---|
| 308 | */ |
---|
| 309 | private void createTreeItemFor(ITask task, TreeItem parent) { |
---|
| 310 | TreeItem item = new TreeItem(parent, SWT.NULL); |
---|
| 311 | item.setText(task.toString()); |
---|
| 312 | item.setData(task); |
---|
| 313 | |
---|
| 314 | // simulate a child |
---|
| 315 | if ((task instanceof IStructuringTemporalRelationship) || |
---|
| 316 | (task instanceof IMarkingTemporalRelationship)) |
---|
| 317 | { |
---|
| 318 | new TreeItem(item, SWT.NULL); |
---|
| 319 | } |
---|
| 320 | } |
---|
| 321 | |
---|
[1094] | 322 | } |
---|