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