source: trunk/autoquest-ui-swt/src/main/java/de/ugoe/cs/autoquest/ui/swt/ShowGuiModelDialog.java @ 927

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
  • Property svn:mime-type set to text/plain
File size: 6.4 KB
Line 
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
15package de.ugoe.cs.autoquest.ui.swt;
16
17import java.util.List;
18
19import org.eclipse.swt.SWT;
20import org.eclipse.swt.events.SelectionAdapter;
21import org.eclipse.swt.events.SelectionEvent;
22import org.eclipse.swt.layout.GridData;
23import org.eclipse.swt.layout.GridLayout;
24import org.eclipse.swt.widgets.Button;
25import org.eclipse.swt.widgets.Dialog;
26import org.eclipse.swt.widgets.Display;
27import org.eclipse.swt.widgets.MessageBox;
28import org.eclipse.swt.widgets.Shell;
29import org.eclipse.swt.widgets.Tree;
30import org.eclipse.swt.widgets.TreeItem;
31
32import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel;
33import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
34import de.ugoe.cs.util.console.Console;
35
36import org.eclipse.swt.widgets.Label;
37
38public class ShowGuiModelDialog extends Dialog {
39
40    protected Shell shell;
41    private Tree guiTree;
42
43    protected GUIModel model;
44
45    public ShowGuiModelDialog(Shell parent, int style, GUIModel model, String modelName) {
46        super(parent, style);
47        setText("GUI Model " + modelName);
48        this.model = model;
49    }
50
51    public void open() {
52        createContents();
53        shell.open();
54        shell.layout();
55        Display display = getParent().getDisplay();
56        while (!shell.isDisposed()) {
57            if (!display.readAndDispatch()) {
58                display.sleep();
59            }
60        }
61    }
62
63    private void createContents() {
64        shell = new Shell(getParent(), SWT.SHELL_TRIM | SWT.BORDER | SWT.APPLICATION_MODAL);
65        shell.setSize(450, 300);
66        shell.setText(getText());
67
68        shell.setLayout(new GridLayout(4, false));
69
70        guiTree = new Tree(shell, SWT.BORDER | SWT.MULTI);
71        guiTree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 4, 1));
72
73        buildGuiTree();
74
75        Button btnExpandAll = new Button(shell, SWT.NONE);
76        btnExpandAll.addSelectionListener(new SelectionAdapter() {
77            @Override
78            public void widgetSelected(SelectionEvent e) {
79                expandAll(guiTree, true);
80            }
81        });
82        btnExpandAll.setText("Expand all");
83
84        Button btnCollapseAll = new Button(shell, SWT.NONE);
85        btnCollapseAll.addSelectionListener(new SelectionAdapter() {
86            @Override
87            public void widgetSelected(SelectionEvent e) {
88                expandAll(guiTree, false);
89            }
90        });
91        btnCollapseAll.setText("Collapse all");
92       
93        Button btnCondense = new Button(shell, SWT.NONE);
94        btnCondense.addSelectionListener(new SelectionAdapter() {
95            @Override
96            public void widgetSelected(SelectionEvent e) {
97                model.condenseModel();
98                guiTree.removeAll();
99                buildGuiTree();
100            }
101        });
102        btnCondense.setText("Condense");
103       
104        Button btnMerge = new Button(shell, SWT.NONE);
105        btnMerge.addSelectionListener(new SelectionAdapter() {
106            @Override
107            public void widgetSelected(SelectionEvent e) {
108                mergeSelectedNode(guiTree);
109            }
110        });
111        btnMerge.setText("Merge nodes");
112       
113        //new Label(shell, SWT.NONE);
114        new Label(shell, SWT.NONE);
115        new Label(shell, SWT.NONE);
116        new Label(shell, SWT.NONE);
117
118    }
119
120    private void buildGuiTree() {
121        for (IGUIElement element : model.getRootElements()) {
122            TreeItem child = new TreeItem(guiTree, SWT.NULL);
123            child.setText(element.toString());
124            child.setData(element);
125            buildGuiTree(child, model.getChildren(element));
126        }
127    }
128
129    private void buildGuiTree(TreeItem currentParent, List<IGUIElement> elements) {
130        for (IGUIElement element : elements) {
131            TreeItem child = new TreeItem(currentParent, SWT.NULL);
132            child.setText(element.toString());
133            child.setData(element);
134            buildGuiTree(child, model.getChildren(element));
135        }
136    }
137
138    private void expandAll(Tree tree, boolean expanded) {
139        for (TreeItem item : tree.getItems()) {
140            expandAll(item, expanded);
141        }
142    }
143
144    private void expandAll(TreeItem item, boolean expanded) {
145        item.setExpanded(expanded);
146        for (TreeItem childItem : item.getItems()) {
147            expandAll(childItem, expanded);
148        }
149    }
150   
151    private void mergeSelectedNode(Tree tree) {
152        TreeItem[] selectedNodes = tree.getSelection();
153        if( selectedNodes.length<2 ) {
154            MessageBox messageBox = new MessageBox(shell, SWT.ERROR);
155            messageBox.setMessage("Must select at least two nodes to merge!");
156            messageBox.setText("Error");
157            messageBox.open();
158            return;
159        }
160       
161        TreeItem firstParent = selectedNodes[0].getParentItem();
162        for( int i=1 ; i<selectedNodes.length ; i++ ) {
163            if( firstParent!=selectedNodes[i].getParentItem() ) {
164                MessageBox messageBox = new MessageBox(shell, SWT.ERROR);
165                messageBox.setMessage("All selected nodes must have the same parent!");
166                messageBox.setText("Error");
167                messageBox.open();
168                return;
169            }
170        }
171       
172        try {
173            // try to merge the elements
174            IGUIElement firstElement = (IGUIElement) selectedNodes[0].getData();
175            for( int i=1 ; i<selectedNodes.length ; i++ ) {
176                model.mergeGUIElements(firstElement, (IGUIElement) selectedNodes[i].getData());
177            }
178        } catch( IllegalArgumentException e) {
179            Console.logException(e);
180        }
181       
182        // update visualization of the model
183        firstParent.removeAll();
184        buildGuiTree(firstParent, model.getChildren((IGUIElement) firstParent.getData()));
185        firstParent.setExpanded(true);
186    }
187
188}
Note: See TracBrowser for help on using the repository browser.