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