[927] | 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. |
---|
[658] | 14 | |
---|
[922] | 15 | package de.ugoe.cs.autoquest.ui.swt; |
---|
[658] | 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; |
---|
[846] | 25 | import org.eclipse.swt.widgets.MessageBox; |
---|
[658] | 26 | import org.eclipse.swt.widgets.Shell; |
---|
| 27 | import org.eclipse.swt.widgets.Tree; |
---|
| 28 | import org.eclipse.swt.widgets.TreeItem; |
---|
| 29 | |
---|
[922] | 30 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIModel; |
---|
| 31 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement; |
---|
[846] | 32 | import de.ugoe.cs.util.console.Console; |
---|
| 33 | |
---|
[658] | 34 | import org.eclipse.swt.widgets.Label; |
---|
| 35 | |
---|
| 36 | public class ShowGuiModelDialog extends Dialog { |
---|
| 37 | |
---|
| 38 | protected Shell shell; |
---|
| 39 | private Tree guiTree; |
---|
| 40 | |
---|
| 41 | protected GUIModel model; |
---|
| 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 | |
---|
[777] | 49 | public void open() { |
---|
[658] | 50 | createContents(); |
---|
| 51 | shell.open(); |
---|
| 52 | shell.layout(); |
---|
| 53 | Display display = getParent().getDisplay(); |
---|
| 54 | while (!shell.isDisposed()) { |
---|
| 55 | if (!display.readAndDispatch()) { |
---|
| 56 | display.sleep(); |
---|
| 57 | } |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | private void createContents() { |
---|
| 62 | shell = new Shell(getParent(), SWT.SHELL_TRIM | SWT.BORDER | SWT.APPLICATION_MODAL); |
---|
| 63 | shell.setSize(450, 300); |
---|
| 64 | shell.setText(getText()); |
---|
| 65 | |
---|
[847] | 66 | shell.setLayout(new GridLayout(4, false)); |
---|
[658] | 67 | |
---|
[846] | 68 | guiTree = new Tree(shell, SWT.BORDER | SWT.MULTI); |
---|
[847] | 69 | guiTree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 4, 1)); |
---|
[658] | 70 | |
---|
| 71 | buildGuiTree(); |
---|
| 72 | |
---|
| 73 | Button btnExpandAll = new Button(shell, SWT.NONE); |
---|
| 74 | btnExpandAll.addSelectionListener(new SelectionAdapter() { |
---|
| 75 | @Override |
---|
| 76 | public void widgetSelected(SelectionEvent e) { |
---|
| 77 | expandAll(guiTree, true); |
---|
| 78 | } |
---|
| 79 | }); |
---|
| 80 | btnExpandAll.setText("Expand all"); |
---|
| 81 | |
---|
| 82 | Button btnCollapseAll = new Button(shell, SWT.NONE); |
---|
| 83 | btnCollapseAll.addSelectionListener(new SelectionAdapter() { |
---|
| 84 | @Override |
---|
| 85 | public void widgetSelected(SelectionEvent e) { |
---|
| 86 | expandAll(guiTree, false); |
---|
| 87 | } |
---|
| 88 | }); |
---|
| 89 | btnCollapseAll.setText("Collapse all"); |
---|
[846] | 90 | |
---|
[847] | 91 | Button btnCondense = new Button(shell, SWT.NONE); |
---|
| 92 | btnCondense.addSelectionListener(new SelectionAdapter() { |
---|
| 93 | @Override |
---|
| 94 | public void widgetSelected(SelectionEvent e) { |
---|
| 95 | model.condenseModel(); |
---|
| 96 | guiTree.removeAll(); |
---|
| 97 | buildGuiTree(); |
---|
| 98 | } |
---|
| 99 | }); |
---|
| 100 | btnCondense.setText("Condense"); |
---|
| 101 | |
---|
[846] | 102 | Button btnMerge = new Button(shell, SWT.NONE); |
---|
| 103 | btnMerge.addSelectionListener(new SelectionAdapter() { |
---|
| 104 | @Override |
---|
| 105 | public void widgetSelected(SelectionEvent e) { |
---|
| 106 | mergeSelectedNode(guiTree); |
---|
| 107 | } |
---|
| 108 | }); |
---|
[847] | 109 | btnMerge.setText("Merge nodes"); |
---|
[846] | 110 | |
---|
| 111 | //new Label(shell, SWT.NONE); |
---|
[658] | 112 | new Label(shell, SWT.NONE); |
---|
| 113 | new Label(shell, SWT.NONE); |
---|
[846] | 114 | new Label(shell, SWT.NONE); |
---|
[658] | 115 | |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | private void buildGuiTree() { |
---|
[1086] | 119 | GUIModel.Traverser traverser = model.getTraverser(); |
---|
| 120 | |
---|
| 121 | IGUIElement root = traverser.firstChild(); |
---|
| 122 | |
---|
| 123 | while (root != null) { |
---|
[658] | 124 | TreeItem child = new TreeItem(guiTree, SWT.NULL); |
---|
[1086] | 125 | child.setText(root.toString()); |
---|
| 126 | child.setData(root); |
---|
| 127 | buildGuiTree(child, traverser); |
---|
| 128 | root = traverser.nextSibling(); |
---|
[658] | 129 | } |
---|
| 130 | } |
---|
| 131 | |
---|
[1086] | 132 | private void buildGuiTree(TreeItem currentParent, GUIModel.Traverser traverser) { |
---|
| 133 | if (traverser.hasFirstChild()) { |
---|
| 134 | IGUIElement childElement = traverser.firstChild(); |
---|
| 135 | |
---|
| 136 | while (childElement != null) { |
---|
| 137 | TreeItem child = new TreeItem(currentParent, SWT.NULL); |
---|
| 138 | child.setText(childElement.toString()); |
---|
| 139 | child.setData(childElement); |
---|
| 140 | buildGuiTree(child, traverser); |
---|
| 141 | childElement = traverser.nextSibling(); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | traverser.parent(); |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
[658] | 148 | private void expandAll(Tree tree, boolean expanded) { |
---|
| 149 | for (TreeItem item : tree.getItems()) { |
---|
| 150 | expandAll(item, expanded); |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | private void expandAll(TreeItem item, boolean expanded) { |
---|
| 155 | item.setExpanded(expanded); |
---|
| 156 | for (TreeItem childItem : item.getItems()) { |
---|
| 157 | expandAll(childItem, expanded); |
---|
| 158 | } |
---|
| 159 | } |
---|
[846] | 160 | |
---|
| 161 | private void mergeSelectedNode(Tree tree) { |
---|
| 162 | TreeItem[] selectedNodes = tree.getSelection(); |
---|
| 163 | if( selectedNodes.length<2 ) { |
---|
| 164 | MessageBox messageBox = new MessageBox(shell, SWT.ERROR); |
---|
| 165 | messageBox.setMessage("Must select at least two nodes to merge!"); |
---|
| 166 | messageBox.setText("Error"); |
---|
| 167 | messageBox.open(); |
---|
| 168 | return; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | TreeItem firstParent = selectedNodes[0].getParentItem(); |
---|
| 172 | for( int i=1 ; i<selectedNodes.length ; i++ ) { |
---|
| 173 | if( firstParent!=selectedNodes[i].getParentItem() ) { |
---|
| 174 | MessageBox messageBox = new MessageBox(shell, SWT.ERROR); |
---|
| 175 | messageBox.setMessage("All selected nodes must have the same parent!"); |
---|
| 176 | messageBox.setText("Error"); |
---|
| 177 | messageBox.open(); |
---|
| 178 | return; |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | try { |
---|
| 183 | // try to merge the elements |
---|
| 184 | IGUIElement firstElement = (IGUIElement) selectedNodes[0].getData(); |
---|
| 185 | for( int i=1 ; i<selectedNodes.length ; i++ ) { |
---|
| 186 | model.mergeGUIElements(firstElement, (IGUIElement) selectedNodes[i].getData()); |
---|
| 187 | } |
---|
| 188 | } catch( IllegalArgumentException e) { |
---|
| 189 | Console.logException(e); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | // update visualization of the model |
---|
| 193 | firstParent.removeAll(); |
---|
[1088] | 194 | buildGuiTree(firstParent, model.getTraverser((IGUIElement) firstParent.getData())); |
---|
[846] | 195 | firstParent.setExpanded(true); |
---|
| 196 | } |
---|
[658] | 197 | |
---|
| 198 | } |
---|