1 | |
---|
2 | package de.ugoe.cs.quest.ui.swt; |
---|
3 | |
---|
4 | import java.util.List; |
---|
5 | |
---|
6 | import org.eclipse.swt.SWT; |
---|
7 | import org.eclipse.swt.events.SelectionAdapter; |
---|
8 | import org.eclipse.swt.events.SelectionEvent; |
---|
9 | import org.eclipse.swt.layout.GridData; |
---|
10 | import org.eclipse.swt.layout.GridLayout; |
---|
11 | import org.eclipse.swt.widgets.Button; |
---|
12 | import org.eclipse.swt.widgets.Dialog; |
---|
13 | import org.eclipse.swt.widgets.Display; |
---|
14 | import org.eclipse.swt.widgets.Shell; |
---|
15 | import org.eclipse.swt.widgets.Tree; |
---|
16 | import org.eclipse.swt.widgets.TreeItem; |
---|
17 | |
---|
18 | import de.ugoe.cs.quest.eventcore.guimodel.GUIModel; |
---|
19 | import de.ugoe.cs.quest.eventcore.guimodel.IGUIElement; |
---|
20 | import org.eclipse.swt.widgets.Label; |
---|
21 | |
---|
22 | /** |
---|
23 | * <p> |
---|
24 | * TODO comment |
---|
25 | * </p> |
---|
26 | * |
---|
27 | * @version $Revision: $ $Date: Aug 28, 2012$ |
---|
28 | * @author 2012, last modified by $Author: sherbold$ |
---|
29 | */ |
---|
30 | public class ShowGuiModelDialog extends Dialog { |
---|
31 | |
---|
32 | protected Shell shell; |
---|
33 | private Tree guiTree; |
---|
34 | |
---|
35 | protected GUIModel model; |
---|
36 | |
---|
37 | /** |
---|
38 | * Create the dialog. |
---|
39 | * |
---|
40 | * @param parent |
---|
41 | * @param style |
---|
42 | */ |
---|
43 | public ShowGuiModelDialog(Shell parent, int style, GUIModel model, String modelName) { |
---|
44 | super(parent, style); |
---|
45 | setText("GUI Model " + modelName); |
---|
46 | this.model = model; |
---|
47 | } |
---|
48 | |
---|
49 | /** |
---|
50 | * Open the dialog. |
---|
51 | * |
---|
52 | * @return the result |
---|
53 | */ |
---|
54 | public void open() { |
---|
55 | createContents(); |
---|
56 | shell.open(); |
---|
57 | shell.layout(); |
---|
58 | Display display = getParent().getDisplay(); |
---|
59 | while (!shell.isDisposed()) { |
---|
60 | if (!display.readAndDispatch()) { |
---|
61 | display.sleep(); |
---|
62 | } |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | * Create contents of the dialog. |
---|
68 | */ |
---|
69 | private void createContents() { |
---|
70 | shell = new Shell(getParent(), SWT.SHELL_TRIM | SWT.BORDER | SWT.APPLICATION_MODAL); |
---|
71 | shell.setSize(450, 300); |
---|
72 | shell.setText(getText()); |
---|
73 | |
---|
74 | shell.setLayout(new GridLayout(2, false)); |
---|
75 | |
---|
76 | guiTree = new Tree(shell, SWT.BORDER); |
---|
77 | guiTree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1)); |
---|
78 | |
---|
79 | buildGuiTree(); |
---|
80 | |
---|
81 | Button btnExpandAll = new Button(shell, SWT.NONE); |
---|
82 | btnExpandAll.addSelectionListener(new SelectionAdapter() { |
---|
83 | @Override |
---|
84 | public void widgetSelected(SelectionEvent e) { |
---|
85 | expandAll(guiTree, true); |
---|
86 | } |
---|
87 | }); |
---|
88 | btnExpandAll.setText("Expand all"); |
---|
89 | |
---|
90 | Button btnCollapseAll = new Button(shell, SWT.NONE); |
---|
91 | btnCollapseAll.addSelectionListener(new SelectionAdapter() { |
---|
92 | @Override |
---|
93 | public void widgetSelected(SelectionEvent e) { |
---|
94 | expandAll(guiTree, false); |
---|
95 | } |
---|
96 | }); |
---|
97 | btnCollapseAll.setText("Collapse all"); |
---|
98 | new Label(shell, SWT.NONE); |
---|
99 | new Label(shell, SWT.NONE); |
---|
100 | |
---|
101 | } |
---|
102 | |
---|
103 | private void buildGuiTree() { |
---|
104 | for (IGUIElement element : model.getRootElements()) { |
---|
105 | TreeItem child = new TreeItem(guiTree, SWT.NULL); |
---|
106 | child.setText(element.toString()); |
---|
107 | buildGuiTree(child, model.getChildren(element)); |
---|
108 | } |
---|
109 | } |
---|
110 | |
---|
111 | private void buildGuiTree(TreeItem currentParent, List<IGUIElement> elements) { |
---|
112 | for (IGUIElement element : elements) { |
---|
113 | TreeItem child = new TreeItem(currentParent, SWT.NULL); |
---|
114 | child.setText(element.toString()); |
---|
115 | buildGuiTree(child, model.getChildren(element)); |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | private void expandAll(Tree tree, boolean expanded) { |
---|
120 | for (TreeItem item : tree.getItems()) { |
---|
121 | expandAll(item, expanded); |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | private void expandAll(TreeItem item, boolean expanded) { |
---|
126 | item.setExpanded(expanded); |
---|
127 | for (TreeItem childItem : item.getItems()) { |
---|
128 | expandAll(childItem, expanded); |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|
132 | } |
---|