Changeset 846
Legend:
- Unmodified
- Added
- Removed
-
trunk/quest-core-events/src/main/java/de/ugoe/cs/quest/eventcore/guimodel/GUIModel.java
r831 r846 227 227 mergeSubTree(root); 228 228 } 229 230 /** 231 * <p> 232 * Merges the tree nodes of two GUI elements. The GUI elements need to have the same parent. 233 * </p> 234 * 235 * @param guiElement1 236 * the first merge GUI element 237 * @param guiElement2 238 * the second merge GUI element 239 * @throws IllegalArgumentException 240 * thrown if the two GUI elements do not have the same parent 241 */ 242 public void mergeGUIElements(IGUIElement guiElement1, IGUIElement guiElement2) 243 throws IllegalArgumentException 244 { 245 // check if both nodes have the same parent 246 IGUIElement parentElement = guiElement1.getParent(); 247 if (parentElement != null && !parentElement.equals(guiElement2.getParent())) { 248 throw new IllegalArgumentException("can only merge nodes with the same parent"); 249 } 250 251 // get the TreeNode of the parent of the GUI elements 252 TreeNode parent = findNode(parentElement); 253 254 // get the TreeNodes for both GUI elements 255 TreeNode node1 = findNode(guiElement1); 256 TreeNode node2 = findNode(guiElement2); 257 258 if (node1 == null || node2 == null) { 259 throw new IllegalArgumentException( 260 "Error while merging nodes: one element is not part of the GUI model!"); 261 } 262 263 TreeNode replacement = mergeTreeNodes(node1, node2); 264 265 if (parent != null) { 266 // remove node1 and node2 from the parent's children and add the replacement instead 267 // assumes that there are no duplicates of node1 and node2 268 if (parent.children != null) { 269 parent.children.set(parent.children.indexOf(node1), replacement); 270 parent.children.remove(node2); 271 } 272 } 273 274 } 229 275 230 276 /** … … 428 474 429 475 out.println(); 476 } 477 478 /** 479 * <p> 480 * Retrieves the TreeNode associated with a GUI element. Returns null if no such TreeNode is 481 * found. 482 * </p> 483 * 484 * @param element 485 * the GUI element 486 * @return associated TreeNode; null if no such node exists 487 */ 488 private TreeNode findNode(IGUIElement element) { 489 if (element == null) { 490 return null; 491 } 492 493 TreeNode result = null; 494 for (TreeNode node : allNodes) { 495 if (node.guiElement.equals(element)) { 496 if (result == null) { 497 result = node; 498 } 499 else { 500 Console 501 .traceln(Level.SEVERE, 502 "Multiple nodes in the internal GUI model match the same GUI element. " 503 + "This should not be the case and the GUI model is probably invalid."); 504 } 505 } 506 } 507 return result; 430 508 } 431 509 -
trunk/quest-ui-swt/src/main/java/de/ugoe/cs/quest/ui/swt/ShowGuiModelDialog.java
r777 r846 12 12 import org.eclipse.swt.widgets.Dialog; 13 13 import org.eclipse.swt.widgets.Display; 14 import org.eclipse.swt.widgets.MessageBox; 14 15 import org.eclipse.swt.widgets.Shell; 15 16 import org.eclipse.swt.widgets.Tree; … … 18 19 import de.ugoe.cs.quest.eventcore.guimodel.GUIModel; 19 20 import de.ugoe.cs.quest.eventcore.guimodel.IGUIElement; 21 import de.ugoe.cs.util.console.Console; 22 20 23 import org.eclipse.swt.widgets.Label; 21 24 … … 72 75 shell.setText(getText()); 73 76 74 shell.setLayout(new GridLayout( 2, false));77 shell.setLayout(new GridLayout(3, false)); 75 78 76 guiTree = new Tree(shell, SWT.BORDER );77 guiTree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));79 guiTree = new Tree(shell, SWT.BORDER | SWT.MULTI); 80 guiTree.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 3, 1)); 78 81 79 82 buildGuiTree(); … … 96 99 }); 97 100 btnCollapseAll.setText("Collapse all"); 101 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 }); 109 btnMerge.setText("Merge"); 110 111 //new Label(shell, SWT.NONE); 112 new Label(shell, SWT.NONE); 98 113 new Label(shell, SWT.NONE); 99 114 new Label(shell, SWT.NONE); … … 105 120 TreeItem child = new TreeItem(guiTree, SWT.NULL); 106 121 child.setText(element.toString()); 122 child.setData(element); 107 123 buildGuiTree(child, model.getChildren(element)); 108 124 } … … 113 129 TreeItem child = new TreeItem(currentParent, SWT.NULL); 114 130 child.setText(element.toString()); 131 child.setData(element); 115 132 buildGuiTree(child, model.getChildren(element)); 116 133 } … … 129 146 } 130 147 } 148 149 private void mergeSelectedNode(Tree tree) { 150 TreeItem[] selectedNodes = tree.getSelection(); 151 if( selectedNodes.length<2 ) { 152 MessageBox messageBox = new MessageBox(shell, SWT.ERROR); 153 messageBox.setMessage("Must select at least two nodes to merge!"); 154 messageBox.setText("Error"); 155 messageBox.open(); 156 return; 157 } 158 159 TreeItem firstParent = selectedNodes[0].getParentItem(); 160 for( int i=1 ; i<selectedNodes.length ; i++ ) { 161 if( firstParent!=selectedNodes[i].getParentItem() ) { 162 MessageBox messageBox = new MessageBox(shell, SWT.ERROR); 163 messageBox.setMessage("All selected nodes must have the same parent!"); 164 messageBox.setText("Error"); 165 messageBox.open(); 166 return; 167 } 168 } 169 170 try { 171 // try to merge the elements 172 IGUIElement firstElement = (IGUIElement) selectedNodes[0].getData(); 173 for( int i=1 ; i<selectedNodes.length ; i++ ) { 174 model.mergeGUIElements(firstElement, (IGUIElement) selectedNodes[i].getData()); 175 } 176 } catch( IllegalArgumentException e) { 177 Console.logException(e); 178 } 179 180 // update visualization of the model 181 firstParent.removeAll(); 182 buildGuiTree(firstParent, model.getChildren((IGUIElement) firstParent.getData())); 183 firstParent.setExpanded(true); 184 } 131 185 132 186 }
Note: See TracChangeset
for help on using the changeset viewer.