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.List; |
---|
18 | |
---|
19 | import org.eclipse.swt.SWT; |
---|
20 | import org.eclipse.swt.custom.SashForm; |
---|
21 | import org.eclipse.swt.events.SelectionAdapter; |
---|
22 | import org.eclipse.swt.events.SelectionEvent; |
---|
23 | import org.eclipse.swt.layout.GridData; |
---|
24 | import org.eclipse.swt.layout.GridLayout; |
---|
25 | import org.eclipse.swt.widgets.Button; |
---|
26 | import org.eclipse.swt.widgets.Dialog; |
---|
27 | import org.eclipse.swt.widgets.Display; |
---|
28 | import org.eclipse.swt.widgets.Shell; |
---|
29 | import org.eclipse.swt.widgets.Tree; |
---|
30 | import org.eclipse.swt.widgets.TreeItem; |
---|
31 | |
---|
32 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IMarkingTemporalRelationship; |
---|
33 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IStructuringTemporalRelationship; |
---|
34 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
35 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
36 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; |
---|
37 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskModel; |
---|
38 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
39 | |
---|
40 | import org.eclipse.swt.widgets.Label; |
---|
41 | |
---|
42 | /** |
---|
43 | * <p> |
---|
44 | * TODO comment |
---|
45 | * </p> |
---|
46 | * |
---|
47 | * @author Patrick Harms |
---|
48 | */ |
---|
49 | public class ShowTaskTreeDialog extends Dialog { |
---|
50 | |
---|
51 | /** */ |
---|
52 | protected Shell shell; |
---|
53 | |
---|
54 | /** */ |
---|
55 | private Tree instanceTree; |
---|
56 | |
---|
57 | /** */ |
---|
58 | private Tree modelTree; |
---|
59 | |
---|
60 | /** */ |
---|
61 | protected ITaskModel taskModel; |
---|
62 | |
---|
63 | /** |
---|
64 | * |
---|
65 | */ |
---|
66 | public ShowTaskTreeDialog(Shell parent, int style, ITaskModel taskModel, String taskTreeName) { |
---|
67 | super(parent, style); |
---|
68 | setText("Task Model " + taskTreeName); |
---|
69 | this.taskModel = taskModel; |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * |
---|
74 | */ |
---|
75 | public void open() { |
---|
76 | createContents(); |
---|
77 | shell.open(); |
---|
78 | shell.layout(); |
---|
79 | Display display = getParent().getDisplay(); |
---|
80 | while (!shell.isDisposed()) { |
---|
81 | if (!display.readAndDispatch()) { |
---|
82 | display.sleep(); |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * |
---|
89 | */ |
---|
90 | private void createContents() { |
---|
91 | shell = new Shell(getParent(), SWT.SHELL_TRIM | SWT.BORDER | SWT.APPLICATION_MODAL); |
---|
92 | shell.setSize(450, 300); |
---|
93 | shell.setText(getText()); |
---|
94 | |
---|
95 | shell.setLayout(new GridLayout(4, false)); |
---|
96 | |
---|
97 | SashForm sashForm = new SashForm(shell, SWT.VERTICAL); |
---|
98 | sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 4, 1)); |
---|
99 | |
---|
100 | instanceTree = new Tree(sashForm, SWT.BORDER | SWT.MULTI); |
---|
101 | instanceTree.addSelectionListener(new SelectionAdapter() { |
---|
102 | @Override |
---|
103 | public void widgetSelected(SelectionEvent e) { |
---|
104 | modelTree.removeAll(); |
---|
105 | TreeItem[] selectedItems = instanceTree.getSelection(); |
---|
106 | if ((selectedItems.length == 1) && |
---|
107 | (selectedItems[0].getData() instanceof ITaskInstance)) |
---|
108 | { |
---|
109 | buildModelTree((ITaskInstance) selectedItems[0].getData()); |
---|
110 | } |
---|
111 | } |
---|
112 | }); |
---|
113 | |
---|
114 | buildInstanceTree(); |
---|
115 | |
---|
116 | modelTree = new Tree(sashForm, SWT.BORDER | SWT.MULTI); |
---|
117 | |
---|
118 | Button btnExpandAll = new Button(shell, SWT.NONE); |
---|
119 | btnExpandAll.addSelectionListener(new SelectionAdapter() { |
---|
120 | @Override |
---|
121 | public void widgetSelected(SelectionEvent e) { |
---|
122 | expandAll(instanceTree, true); |
---|
123 | } |
---|
124 | }); |
---|
125 | btnExpandAll.setText("Expand all"); |
---|
126 | |
---|
127 | Button btnCollapseAll = new Button(shell, SWT.NONE); |
---|
128 | btnCollapseAll.addSelectionListener(new SelectionAdapter() { |
---|
129 | @Override |
---|
130 | public void widgetSelected(SelectionEvent e) { |
---|
131 | expandAll(instanceTree, false); |
---|
132 | } |
---|
133 | }); |
---|
134 | btnCollapseAll.setText("Collapse all"); |
---|
135 | |
---|
136 | //new Label(shell, SWT.NONE); |
---|
137 | new Label(shell, SWT.NONE); |
---|
138 | new Label(shell, SWT.NONE); |
---|
139 | new Label(shell, SWT.NONE); |
---|
140 | |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * |
---|
145 | */ |
---|
146 | private void buildInstanceTree() { |
---|
147 | List<IUserSession> sessions = taskModel.getUserSessions(); |
---|
148 | |
---|
149 | TreeItem root = new TreeItem(instanceTree, SWT.NULL); |
---|
150 | root.setText(sessions.size() + " sessions"); |
---|
151 | root.setData(sessions); |
---|
152 | |
---|
153 | for (IUserSession session : sessions) { |
---|
154 | buildInstanceTree(root, session); |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * |
---|
160 | */ |
---|
161 | private void buildInstanceTree(TreeItem currentParent, ITaskInstanceList taskInstanceList) { |
---|
162 | TreeItem child = new TreeItem(currentParent, SWT.NULL); |
---|
163 | child.setText(taskInstanceList.toString()); |
---|
164 | child.setData(taskInstanceList); |
---|
165 | |
---|
166 | for (ITaskInstanceList childTask : taskInstanceList) { |
---|
167 | buildInstanceTree(child, childTask); |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | /** |
---|
172 | * |
---|
173 | */ |
---|
174 | private void buildModelTree(ITaskInstance taskInstance) { |
---|
175 | modelTree.removeAll(); |
---|
176 | TreeItem root = new TreeItem(modelTree, SWT.NULL); |
---|
177 | root.setText("model of instance " + taskInstance); |
---|
178 | root.setData(taskInstance); |
---|
179 | |
---|
180 | buildModelTree(taskInstance.getTask(), root); |
---|
181 | expandAll(root, true); |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | * |
---|
186 | */ |
---|
187 | private void buildModelTree(ITask task, TreeItem parent) { |
---|
188 | TreeItem node = new TreeItem(parent, SWT.NULL); |
---|
189 | node.setText(task.toString()); |
---|
190 | node.setData(task); |
---|
191 | |
---|
192 | if (task instanceof IStructuringTemporalRelationship) { |
---|
193 | for (ITask subTask : ((IStructuringTemporalRelationship) task).getChildren()) { |
---|
194 | buildModelTree(subTask, node); |
---|
195 | } |
---|
196 | } |
---|
197 | else if (task instanceof IMarkingTemporalRelationship) { |
---|
198 | buildModelTree(((IMarkingTemporalRelationship) task).getMarkedTask(), node); |
---|
199 | } |
---|
200 | } |
---|
201 | |
---|
202 | /** |
---|
203 | * |
---|
204 | */ |
---|
205 | private void expandAll(Tree tree, boolean expanded) { |
---|
206 | for (TreeItem item : tree.getItems()) { |
---|
207 | expandAll(item, expanded); |
---|
208 | } |
---|
209 | } |
---|
210 | |
---|
211 | /** |
---|
212 | * |
---|
213 | */ |
---|
214 | private void expandAll(TreeItem item, boolean expanded) { |
---|
215 | item.setExpanded(expanded); |
---|
216 | for (TreeItem childItem : item.getItems()) { |
---|
217 | expandAll(childItem, expanded); |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | } |
---|