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