PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/robotframework/swing/tree/TreeOperator.java

https://github.com/robotframework/SwingLibrary
Java | 267 lines | 199 code | 53 blank | 15 comment | 2 complexity | 6931a8eab141b1a9059d8c89ddc4c4a9 MD5 | raw file
  1. /*
  2. * Copyright 2008-2011 Nokia Siemens Networks Oyj
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.robotframework.swing.tree;
  17. import java.awt.Component;
  18. import java.awt.Point;
  19. import java.util.ArrayList;
  20. import java.util.Arrays;
  21. import java.util.Collection;
  22. import java.util.List;
  23. import javax.swing.JPopupMenu;
  24. import javax.swing.JTree;
  25. import javax.swing.tree.TreeModel;
  26. import javax.swing.tree.TreeNode;
  27. import javax.swing.tree.TreePath;
  28. import org.apache.commons.collections.CollectionUtils;
  29. import org.apache.commons.collections.Transformer;
  30. import org.netbeans.jemmy.ComponentChooser;
  31. import org.netbeans.jemmy.Waiter;
  32. import org.netbeans.jemmy.operators.ContainerOperator;
  33. import org.netbeans.jemmy.operators.JPopupMenuOperator;
  34. import org.netbeans.jemmy.operators.JTreeOperator;
  35. import org.robotframework.swing.common.TimeoutCopier;
  36. import org.robotframework.swing.common.TimeoutName;
  37. import org.robotframework.swing.operator.ComponentWrapper;
  38. import org.robotframework.swing.popup.PopupCaller;
  39. public class TreeOperator implements ComponentWrapper {
  40. protected PopupCaller popupCaller = new PopupCaller();
  41. protected TreePathFactory treePathFactory = new TreePathFactory(this);
  42. protected JTreeOperator jTreeOperator;
  43. public TreeOperator(ContainerOperator containerOperator, ComponentChooser componentChooser) {
  44. jTreeOperator = new JTreeOperator(containerOperator, componentChooser);
  45. }
  46. public TreeOperator(ContainerOperator containerOperator, int index) {
  47. jTreeOperator = new JTreeOperator(containerOperator, index);
  48. }
  49. public TreeOperator(JTreeOperator treeOperator) {
  50. this.jTreeOperator = treeOperator;
  51. }
  52. public JPopupMenu callPopupOnRow(int row) {
  53. jTreeOperator.selectRow(row);
  54. jTreeOperator.scrollToRow(row);
  55. Point pointToClick = jTreeOperator.getPointToClick(row);
  56. return popupCaller.callPopupOnComponent(jTreeOperator, pointToClick);
  57. }
  58. public TreePath findPath(final String treePath) {
  59. try {
  60. return (TreePath) treePathWaiterFor(treePath).waitAction(null);
  61. } catch (InterruptedException e) {
  62. throw new RuntimeException(e);
  63. }
  64. }
  65. private Waiter treePathWaiterFor(final String treePath) {
  66. Waiter waiter = new Waiter(new TreePathWaitable(jTreeOperator, treePath));
  67. waiter.setTimeouts(new TimeoutCopier(jTreeOperator,
  68. TimeoutName.J_TREE_OPERATOR_WAIT_NEXT_NODE_TIMEOUT).getTimeouts());
  69. return waiter;
  70. }
  71. public void expand(String nodeIdentifier) {
  72. expand(createTreePath(nodeIdentifier));
  73. }
  74. public void expand(TreePath path) {
  75. jTreeOperator.expandPath(path);
  76. }
  77. public void collapse(String nodeIdentifier) {
  78. collapse(createTreePath(nodeIdentifier));
  79. }
  80. public void collapse(TreePath nodeIdentifier) {
  81. jTreeOperator.collapsePath(nodeIdentifier);
  82. }
  83. public void addSelection(String nodeIdentifier) {
  84. jTreeOperator.addSelectionPath(createTreePath(nodeIdentifier));
  85. }
  86. public void addSelectionPath(TreePath nodePath) {
  87. jTreeOperator.addSelectionPath(nodePath);
  88. }
  89. public TreePath getDuplicatedNodeInstance(String nodeIdentifier, Integer nodeInstance) {
  90. TreeNode parentNode = (TreeNode) createTreePath(nodeIdentifier).getParentPath().getLastPathComponent();
  91. Object[] nodeList = jTreeOperator.getChildren(parentNode);
  92. Object firstNode = createTreePath(nodeIdentifier).getLastPathComponent();
  93. List<Object> listOfNodesWithSameName = new ArrayList<>();
  94. for (Object node : nodeList) {
  95. if ((firstNode.toString()).equals(node.toString())) {
  96. listOfNodesWithSameName.add(node);
  97. }
  98. }
  99. Object desiredNode = listOfNodesWithSameName.get(nodeInstance);
  100. return jTreeOperator.getChildPath(createTreePath(nodeIdentifier).getParentPath(), parentNode.getIndex((TreeNode) desiredNode));
  101. }
  102. public void removeSelection(String nodeIdentifier) {
  103. jTreeOperator.removeSelectionPath(createTreePath(nodeIdentifier));
  104. }
  105. public void removeSelection(TreePath nodePath) {
  106. jTreeOperator.removeSelectionPath(nodePath);
  107. }
  108. public boolean isExpanded(String nodeIdentifier) {
  109. return isExpanded(createTreePath(nodeIdentifier));
  110. }
  111. public boolean isExpanded(TreePath nodeIdentifier) {
  112. return jTreeOperator.isExpanded(nodeIdentifier);
  113. }
  114. public boolean isCollapsed(String nodeIdentifier) {
  115. return isCollapsed(createTreePath(nodeIdentifier));
  116. }
  117. public boolean isCollapsed(TreePath nodeIdentifier) {
  118. return jTreeOperator.isCollapsed(nodeIdentifier);
  119. }
  120. public boolean isLeaf(String nodeIdentifier) {
  121. return isLeaf(createTreePath(nodeIdentifier));
  122. }
  123. public boolean isLeaf(TreePath nodeIdentifier) {
  124. TreeNode lastPathComponent = (TreeNode) nodeIdentifier.getLastPathComponent();
  125. return lastPathComponent.isLeaf();
  126. }
  127. public boolean isPathSelected(String nodeIdentifier) {
  128. return jTreeOperator.isPathSelected(createTreePath(nodeIdentifier));
  129. }
  130. public boolean isPathSelected(TreePath nodePath) {
  131. return jTreeOperator.isPathSelected(nodePath);
  132. }
  133. public boolean isVisible(String nodeIdentifier) {
  134. TreePath treePath = createTreePath(nodeIdentifier);
  135. return jTreeOperator.isVisible(treePath);
  136. }
  137. public boolean isPathVisible(TreePath nodePath) {
  138. return jTreeOperator.isVisible(nodePath);
  139. }
  140. public void clickOnNode(String nodeIdentifier, int clickCount) {
  141. jTreeOperator.clickOnPath(createTreePath(nodeIdentifier), clickCount);
  142. }
  143. public void clickOnNode(TreePath nodeIdentifier, int clickCount) {
  144. jTreeOperator.clickOnPath(nodeIdentifier, clickCount);
  145. }
  146. public JPopupMenuOperator createPopupOperator(String nodeIdentifier) {
  147. return popupFactory().createOperator(nodeIdentifier);
  148. }
  149. public JPopupMenuOperator createPopupOperatorOnSelectedNodes() {
  150. return popupFactory().createOperatorBySelection();
  151. }
  152. public String getTreeNodeLabel(int index) {
  153. TreePath pathForRow = jTreeOperator.getPathForRow(index);
  154. return pathForRow.getLastPathComponent().toString();
  155. }
  156. public int getTreeNodeIndex(String nodePath) {
  157. return jTreeOperator.getRowForPath(findPath(nodePath));
  158. }
  159. public Component getSource() {
  160. return jTreeOperator.getSource();
  161. }
  162. public TreeModel getModel() {
  163. return jTreeOperator.getModel();
  164. }
  165. public TreePath getPathForRow(int i) {
  166. return jTreeOperator.getPathForRow(i);
  167. }
  168. public int getRowCount() {
  169. return jTreeOperator.getRowCount();
  170. }
  171. public void clearSelection() {
  172. jTreeOperator.clearSelection();
  173. }
  174. public boolean isRootVisible() {
  175. return jTreeOperator.isRootVisible();
  176. }
  177. public JPopupMenu callPopupOnPath(TreePath treePath) {
  178. return jTreeOperator.callPopupOnPath(treePath);
  179. }
  180. public JPopupMenu callPopupOnPaths(TreePath[] treePaths) {
  181. return jTreeOperator.callPopupOnPaths(treePaths);
  182. }
  183. public TreePath[] getSelectionPaths() {
  184. return jTreeOperator.getSelectionPaths();
  185. }
  186. public void operateOnAllNodes(TreePathAction action) {
  187. createIterator().operateOnAllNodes(action);
  188. }
  189. @SuppressWarnings("unchecked")
  190. public Collection<String> getTreeNodeChildNames(String nodeIdentifier) {
  191. final TreePath treePath = createTreePath(nodeIdentifier);
  192. return CollectionUtils.collect(getChildPaths(treePath), new Transformer() {
  193. public Object transform(Object input) {
  194. Object node = ((TreePath) input).getLastPathComponent();
  195. return new NodeTextExtractor((JTree) getSource()).getText(node, treePath);
  196. }
  197. });
  198. }
  199. private List<TreePath> getChildPaths(TreePath treePath) {
  200. return Arrays.asList(jTreeOperator.getChildPaths(treePath));
  201. }
  202. protected TreeIterator createIterator() {
  203. return new TreeIterator(jTreeOperator);
  204. }
  205. protected TreePopupMenuOperatorFactory popupFactory() {
  206. return new TreePopupMenuOperatorFactory(this);
  207. }
  208. protected TreePath createTreePath(String nodeIdentifier) {
  209. return treePathFactory.createTreePath(nodeIdentifier);
  210. }
  211. public JTreeOperator getTreeOperator() {
  212. return jTreeOperator;
  213. }
  214. }