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 org.eclipse.swt.SWT; |
---|
18 | import org.eclipse.swt.events.SelectionAdapter; |
---|
19 | import org.eclipse.swt.events.SelectionEvent; |
---|
20 | import org.eclipse.swt.layout.GridData; |
---|
21 | import org.eclipse.swt.layout.GridLayout; |
---|
22 | import org.eclipse.swt.widgets.Button; |
---|
23 | import org.eclipse.swt.widgets.Dialog; |
---|
24 | import org.eclipse.swt.widgets.Display; |
---|
25 | import org.eclipse.swt.widgets.Shell; |
---|
26 | import org.eclipse.swt.widgets.Tree; |
---|
27 | import org.eclipse.swt.widgets.TreeItem; |
---|
28 | |
---|
29 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTree; |
---|
30 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; |
---|
31 | |
---|
32 | import org.eclipse.swt.widgets.Label; |
---|
33 | |
---|
34 | /** |
---|
35 | * <p> |
---|
36 | * TODO comment |
---|
37 | * </p> |
---|
38 | * |
---|
39 | * @author Patrick Harms |
---|
40 | */ |
---|
41 | public class ShowTaskTreeDialog extends Dialog { |
---|
42 | |
---|
43 | /** */ |
---|
44 | protected Shell shell; |
---|
45 | |
---|
46 | /** */ |
---|
47 | private Tree tree; |
---|
48 | |
---|
49 | /** */ |
---|
50 | protected ITaskTree taskTree; |
---|
51 | |
---|
52 | /** |
---|
53 | * |
---|
54 | */ |
---|
55 | public ShowTaskTreeDialog(Shell parent, int style, ITaskTree taskTree, String taskTreeName) { |
---|
56 | super(parent, style); |
---|
57 | setText("Task Tree " + taskTreeName); |
---|
58 | this.taskTree = taskTree; |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * |
---|
63 | */ |
---|
64 | public void open() { |
---|
65 | createContents(); |
---|
66 | shell.open(); |
---|
67 | shell.layout(); |
---|
68 | Display display = getParent().getDisplay(); |
---|
69 | while (!shell.isDisposed()) { |
---|
70 | if (!display.readAndDispatch()) { |
---|
71 | display.sleep(); |
---|
72 | } |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * |
---|
78 | */ |
---|
79 | private void createContents() { |
---|
80 | shell = new Shell(getParent(), SWT.SHELL_TRIM | SWT.BORDER | SWT.APPLICATION_MODAL); |
---|
81 | shell.setSize(450, 300); |
---|
82 | shell.setText(getText()); |
---|
83 | |
---|
84 | shell.setLayout(new GridLayout(4, false)); |
---|
85 | |
---|
86 | tree = new Tree(shell, SWT.BORDER | SWT.MULTI); |
---|
87 | tree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 4, 1)); |
---|
88 | |
---|
89 | buildTree(); |
---|
90 | |
---|
91 | Button btnExpandAll = new Button(shell, SWT.NONE); |
---|
92 | btnExpandAll.addSelectionListener(new SelectionAdapter() { |
---|
93 | @Override |
---|
94 | public void widgetSelected(SelectionEvent e) { |
---|
95 | expandAll(tree, true); |
---|
96 | } |
---|
97 | }); |
---|
98 | btnExpandAll.setText("Expand all"); |
---|
99 | |
---|
100 | Button btnCollapseAll = new Button(shell, SWT.NONE); |
---|
101 | btnCollapseAll.addSelectionListener(new SelectionAdapter() { |
---|
102 | @Override |
---|
103 | public void widgetSelected(SelectionEvent e) { |
---|
104 | expandAll(tree, false); |
---|
105 | } |
---|
106 | }); |
---|
107 | btnCollapseAll.setText("Collapse all"); |
---|
108 | |
---|
109 | //new Label(shell, SWT.NONE); |
---|
110 | new Label(shell, SWT.NONE); |
---|
111 | new Label(shell, SWT.NONE); |
---|
112 | new Label(shell, SWT.NONE); |
---|
113 | |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * |
---|
118 | */ |
---|
119 | private void buildTree() { |
---|
120 | ITaskTreeNode root = taskTree.getRoot(); |
---|
121 | |
---|
122 | TreeItem child = new TreeItem(tree, SWT.NULL); |
---|
123 | child.setText(root.getName() + " (" + root.getDescription() + ")"); |
---|
124 | child.setData(root); |
---|
125 | buildGuiTree(child, root); |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * |
---|
130 | */ |
---|
131 | private void buildGuiTree(TreeItem currentParent, ITaskTreeNode node) { |
---|
132 | if (node.getChildren() != null) { |
---|
133 | |
---|
134 | for (ITaskTreeNode childTask : node.getChildren()) { |
---|
135 | TreeItem child = new TreeItem(currentParent, SWT.NULL); |
---|
136 | child.setText(childTask.getName() + " (" + childTask.getDescription() + ")"); |
---|
137 | child.setData(childTask); |
---|
138 | buildGuiTree(child, childTask); |
---|
139 | } |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * |
---|
145 | */ |
---|
146 | private void expandAll(Tree tree, boolean expanded) { |
---|
147 | for (TreeItem item : tree.getItems()) { |
---|
148 | expandAll(item, expanded); |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | /** |
---|
153 | * |
---|
154 | */ |
---|
155 | private void expandAll(TreeItem item, boolean expanded) { |
---|
156 | item.setExpanded(expanded); |
---|
157 | for (TreeItem childItem : item.getItems()) { |
---|
158 | expandAll(childItem, expanded); |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | } |
---|